blob: b236ea8f8cff32e90948928508e95ef79f0188fe [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 Peskine07510f52022-11-11 16:37:16 +01001455/* Construct and attempt to import a large unstructured key. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001456void import_large_key( int type_arg, int byte_size_arg,
1457 int expected_status_arg )
1458{
1459 psa_key_type_t type = type_arg;
1460 size_t byte_size = byte_size_arg;
1461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1462 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001463 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001464 psa_status_t status;
1465 uint8_t *buffer = NULL;
1466 size_t buffer_size = byte_size + 1;
1467 size_t n;
1468
Steven Cooreman69967ce2021-01-18 18:01:08 +01001469 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001470 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001471 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001472 memset( buffer, 'K', byte_size );
1473
1474 PSA_ASSERT( psa_crypto_init( ) );
1475
1476 /* Try importing the key */
1477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1478 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001479 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001480 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001481 TEST_EQUAL( status, expected_status );
1482
1483 if( status == PSA_SUCCESS )
1484 {
Ronald Cron5425a212020-08-04 14:58:35 +02001485 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001486 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1487 TEST_EQUAL( psa_get_key_bits( &attributes ),
1488 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001489 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001490 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001491 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001492 for( n = 0; n < byte_size; n++ )
1493 TEST_EQUAL( buffer[n], 'K' );
1494 for( n = byte_size; n < buffer_size; n++ )
1495 TEST_EQUAL( buffer[n], 0 );
1496 }
1497
1498exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001499 /*
1500 * Key attributes may have been returned by psa_get_key_attributes()
1501 * thus reset them as required.
1502 */
1503 psa_reset_key_attributes( &attributes );
1504
Ronald Cron5425a212020-08-04 14:58:35 +02001505 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001506 PSA_DONE( );
1507 mbedtls_free( buffer );
1508}
1509/* END_CASE */
1510
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001511/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001512/* Import an RSA key with a valid structure (but not valid numbers
1513 * inside, beyond having sensible size and parity). This is expected to
1514 * fail for large keys. */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001515void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1516{
Ronald Cron5425a212020-08-04 14:58:35 +02001517 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001518 size_t bits = bits_arg;
1519 psa_status_t expected_status = expected_status_arg;
1520 psa_status_t status;
1521 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001522 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001523 size_t buffer_size = /* Slight overapproximations */
1524 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001525 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001526 unsigned char *p;
1527 int ret;
1528 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001530
Gilles Peskine8817f612018-12-18 00:18:46 +01001531 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001532 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001533
1534 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1535 bits, keypair ) ) >= 0 );
1536 length = ret;
1537
1538 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001539 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001540 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001541 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001542
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001543 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001544 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001545
1546exit:
1547 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001548 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001549}
1550/* END_CASE */
1551
1552/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001553void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001554 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001555 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301556 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001557 int expected_bits,
1558 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001559 int expected_export_status_arg,
Gilles Peskine07510f52022-11-11 16:37:16 +01001560 /*whether reexport must give the original input exactly*/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561 int canonical_input )
1562{
Ronald Cron5425a212020-08-04 14:58:35 +02001563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001565 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001566 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301568 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001569 unsigned char *exported = NULL;
1570 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001571 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001572 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001573 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001575 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001576
Moran Pekercb088e72018-07-17 17:36:59 +03001577 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001578 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001579 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001580 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001581 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001582
Archana4d7ae1d2021-07-07 02:50:22 +05301583 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001584 psa_set_key_usage_flags( &attributes, usage_arg );
1585 psa_set_key_algorithm( &attributes, alg );
1586 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001587
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001588 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001589 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590
1591 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001592 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001593 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1594 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001595 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001596
1597 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001598 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001599 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001600
1601 /* The exported length must be set by psa_export_key() to a value between 0
1602 * and export_size. On errors, the exported length must be 0. */
1603 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1604 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001605 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001606
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001607 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001608 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001610 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001611 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001612 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001613 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001614
Gilles Peskineea38a922021-02-13 00:05:16 +01001615 /* Run sanity checks on the exported key. For non-canonical inputs,
1616 * this validates the canonical representations. For canonical inputs,
1617 * this doesn't directly validate the implementation, but it still helps
1618 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301619 if( !psa_key_lifetime_is_external( lifetime ) )
1620 {
1621 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1622 goto exit;
1623 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001624
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001626 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627 else
1628 {
Ronald Cron5425a212020-08-04 14:58:35 +02001629 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001630 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001631 &key2 ) );
1632 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001633 reexported,
1634 export_size,
1635 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001636 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301637 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001638 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001639 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001640 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301641 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301642 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001643 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001644
1645destroy:
1646 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001647 PSA_ASSERT( psa_destroy_key( key ) );
1648 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001649
1650exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001651 /*
1652 * Key attributes may have been returned by psa_get_key_attributes()
1653 * thus reset them as required.
1654 */
1655 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301656 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001657 mbedtls_free( exported );
1658 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001659 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660}
1661/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001662
Moran Pekerf709f4a2018-06-06 17:26:04 +03001663/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001664void import_export_public_key( data_t *data,
Gilles Peskine07510f52022-11-11 16:37:16 +01001665 int type_arg, // key pair or public key
Gilles Peskine2d277862018-06-18 15:41:12 +02001666 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301667 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001668 int export_size_delta,
1669 int expected_export_status_arg,
1670 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001671{
Ronald Cron5425a212020-08-04 14:58:35 +02001672 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001673 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001674 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001675 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001676 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301677 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001678 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001679 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001680 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001682
Gilles Peskine8817f612018-12-18 00:18:46 +01001683 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001684
Archana4d7ae1d2021-07-07 02:50:22 +05301685 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001686 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1687 psa_set_key_algorithm( &attributes, alg );
1688 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001689
1690 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001691 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001692
Gilles Peskine49c25912018-10-29 15:15:31 +01001693 /* Export the public key */
1694 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001695 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001696 exported, export_size,
1697 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001698 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001699 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001700 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001701 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001702 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001703 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001704 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001705 TEST_LE_U( expected_public_key->len,
1706 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1707 TEST_LE_U( expected_public_key->len,
1708 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1709 TEST_LE_U( expected_public_key->len,
1710 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001711 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1712 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001713 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001714exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001715 /*
1716 * Key attributes may have been returned by psa_get_key_attributes()
1717 * thus reset them as required.
1718 */
1719 psa_reset_key_attributes( &attributes );
1720
itayzafrir3e02b3b2018-06-12 17:06:52 +03001721 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001722 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001723 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001724}
1725/* END_CASE */
1726
Gilles Peskine20035e32018-02-03 22:44:14 +01001727/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001728void import_and_exercise_key( data_t *data,
1729 int type_arg,
1730 int bits_arg,
1731 int alg_arg )
1732{
Ronald Cron5425a212020-08-04 14:58:35 +02001733 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001734 psa_key_type_t type = type_arg;
1735 size_t bits = bits_arg;
1736 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001737 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001739 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001740
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001742
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 psa_set_key_usage_flags( &attributes, usage );
1744 psa_set_key_algorithm( &attributes, alg );
1745 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001746
1747 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001748 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001749
1750 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001751 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001752 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1753 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001754
1755 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001756 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001757 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001758
Ronald Cron5425a212020-08-04 14:58:35 +02001759 PSA_ASSERT( psa_destroy_key( key ) );
1760 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001761
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001762exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001763 /*
1764 * Key attributes may have been returned by psa_get_key_attributes()
1765 * thus reset them as required.
1766 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001767 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001768
1769 psa_reset_key_attributes( &attributes );
1770 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001771 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001772}
1773/* END_CASE */
1774
1775/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001776void effective_key_attributes( int type_arg, int expected_type_arg,
1777 int bits_arg, int expected_bits_arg,
1778 int usage_arg, int expected_usage_arg,
1779 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001780{
Ronald Cron5425a212020-08-04 14:58:35 +02001781 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001782 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001783 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001784 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001785 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001786 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001787 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001788 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001789 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001791
Gilles Peskine8817f612018-12-18 00:18:46 +01001792 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001793
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001794 psa_set_key_usage_flags( &attributes, usage );
1795 psa_set_key_algorithm( &attributes, alg );
1796 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001797 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001798
Ronald Cron5425a212020-08-04 14:58:35 +02001799 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001800 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001801
Ronald Cron5425a212020-08-04 14:58:35 +02001802 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001803 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1804 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1805 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1806 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001807
1808exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001809 /*
1810 * Key attributes may have been returned by psa_get_key_attributes()
1811 * thus reset them as required.
1812 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001813 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001814
1815 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001816 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001817}
1818/* END_CASE */
1819
1820/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001821void check_key_policy( int type_arg, int bits_arg,
1822 int usage_arg, int alg_arg )
1823{
1824 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001825 usage_arg,
1826 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001827 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001828 goto exit;
1829}
1830/* END_CASE */
1831
1832/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001833void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001834{
1835 /* Test each valid way of initializing the object, except for `= {0}`, as
1836 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1837 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001838 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001839 psa_key_attributes_t func = psa_key_attributes_init( );
1840 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1841 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001842
1843 memset( &zero, 0, sizeof( zero ) );
1844
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001845 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1846 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1847 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001848
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001849 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1850 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1851 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1852
1853 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1854 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1855 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1856
1857 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1858 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1859 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1860
1861 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1862 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1863 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001864}
1865/* END_CASE */
1866
1867/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001868void mac_key_policy( int policy_usage_arg,
1869 int policy_alg_arg,
1870 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001871 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001872 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001873 int expected_status_sign_arg,
1874 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001875{
Ronald Cron5425a212020-08-04 14:58:35 +02001876 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001878 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001879 psa_key_type_t key_type = key_type_arg;
1880 psa_algorithm_t policy_alg = policy_alg_arg;
1881 psa_algorithm_t exercise_alg = exercise_alg_arg;
1882 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001883 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001884 psa_status_t expected_status_sign = expected_status_sign_arg;
1885 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001887
Gilles Peskine8817f612018-12-18 00:18:46 +01001888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001889
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001890 psa_set_key_usage_flags( &attributes, policy_usage );
1891 psa_set_key_algorithm( &attributes, policy_alg );
1892 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001893
Gilles Peskine049c7532019-05-15 20:22:09 +02001894 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001895 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001896
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001897 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1898 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001899
Ronald Cron5425a212020-08-04 14:58:35 +02001900 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001901 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001902
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001903 /* Calculate the MAC, one-shot case. */
1904 uint8_t input[128] = {0};
1905 size_t mac_len;
1906 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1907 input, 128,
1908 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1909 expected_status_sign );
1910
Neil Armstrong3af9b972022-02-07 12:20:21 +01001911 /* Calculate the MAC, multi-part case. */
1912 PSA_ASSERT( psa_mac_abort( &operation ) );
1913 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1914 if( status == PSA_SUCCESS )
1915 {
1916 status = psa_mac_update( &operation, input, 128 );
1917 if( status == PSA_SUCCESS )
1918 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1919 &mac_len ),
1920 expected_status_sign );
1921 else
1922 TEST_EQUAL( status, expected_status_sign );
1923 }
1924 else
1925 {
1926 TEST_EQUAL( status, expected_status_sign );
1927 }
1928 PSA_ASSERT( psa_mac_abort( &operation ) );
1929
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001930 /* Verify correct MAC, one-shot case. */
1931 status = psa_mac_verify( key, exercise_alg, input, 128,
1932 mac, mac_len );
1933
1934 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1935 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001937 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001938
Neil Armstrong3af9b972022-02-07 12:20:21 +01001939 /* Verify correct MAC, multi-part case. */
1940 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1941 if( status == PSA_SUCCESS )
1942 {
1943 status = psa_mac_update( &operation, input, 128 );
1944 if( status == PSA_SUCCESS )
1945 {
1946 status = psa_mac_verify_finish( &operation, mac, mac_len );
1947 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1948 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1949 else
1950 TEST_EQUAL( status, expected_status_verify );
1951 }
1952 else
1953 {
1954 TEST_EQUAL( status, expected_status_verify );
1955 }
1956 }
1957 else
1958 {
1959 TEST_EQUAL( status, expected_status_verify );
1960 }
1961
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001963
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001964 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001965 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001966 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967
1968exit:
1969 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001970 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001971 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001972}
1973/* END_CASE */
1974
1975/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001976void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977 int policy_alg,
1978 int key_type,
1979 data_t *key_data,
1980 int exercise_alg )
1981{
Ronald Cron5425a212020-08-04 14:58:35 +02001982 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001984 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001985 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001986 size_t output_buffer_size = 0;
1987 size_t input_buffer_size = 0;
1988 size_t output_length = 0;
1989 uint8_t *output = NULL;
1990 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001991 psa_status_t status;
1992
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001993 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1994 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1995 input_buffer_size );
1996
1997 ASSERT_ALLOC( input, input_buffer_size );
1998 ASSERT_ALLOC( output, output_buffer_size );
1999
Gilles Peskine8817f612018-12-18 00:18:46 +01002000 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002001
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002002 psa_set_key_usage_flags( &attributes, policy_usage );
2003 psa_set_key_algorithm( &attributes, policy_alg );
2004 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005
Gilles Peskine049c7532019-05-15 20:22:09 +02002006 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002007 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002008
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002009 /* Check if no key usage flag implication is done */
2010 TEST_EQUAL( policy_usage,
2011 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002012
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002013 /* Encrypt check, one-shot */
2014 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
2015 output, output_buffer_size,
2016 &output_length);
2017 if( policy_alg == exercise_alg &&
2018 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2019 PSA_ASSERT( status );
2020 else
2021 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2022
2023 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002024 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025 if( policy_alg == exercise_alg &&
2026 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002027 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002029 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 psa_cipher_abort( &operation );
2031
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002032 /* Decrypt check, one-shot */
2033 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
2034 input, input_buffer_size,
2035 &output_length);
2036 if( policy_alg == exercise_alg &&
2037 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
2038 PSA_ASSERT( status );
2039 else
2040 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2041
2042 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002043 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044 if( policy_alg == exercise_alg &&
2045 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002046 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002048 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002049
2050exit:
2051 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002052 mbedtls_free( input );
2053 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002054 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002055 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056}
2057/* END_CASE */
2058
2059/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002060void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061 int policy_alg,
2062 int key_type,
2063 data_t *key_data,
2064 int nonce_length_arg,
2065 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002066 int exercise_alg,
2067 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068{
Ronald Cron5425a212020-08-04 14:58:35 +02002069 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002071 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002072 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002074 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075 unsigned char nonce[16] = {0};
2076 size_t nonce_length = nonce_length_arg;
2077 unsigned char tag[16];
2078 size_t tag_length = tag_length_arg;
2079 size_t output_length;
2080
Gilles Peskine7be11a72022-04-14 00:12:57 +02002081 TEST_LE_U( nonce_length, sizeof( nonce ) );
2082 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083
Gilles Peskine8817f612018-12-18 00:18:46 +01002084 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002086 psa_set_key_usage_flags( &attributes, policy_usage );
2087 psa_set_key_algorithm( &attributes, policy_alg );
2088 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002089
Gilles Peskine049c7532019-05-15 20:22:09 +02002090 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002091 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002092
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002093 /* Check if no key usage implication is done */
2094 TEST_EQUAL( policy_usage,
2095 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002096
Neil Armstrong752d8112022-02-07 14:51:11 +01002097 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002098 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099 nonce, nonce_length,
2100 NULL, 0,
2101 NULL, 0,
2102 tag, tag_length,
2103 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002104 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2105 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002106 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002107 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108
Neil Armstrong752d8112022-02-07 14:51:11 +01002109 /* Encrypt check, multi-part */
2110 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2111 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2112 TEST_EQUAL( status, expected_status );
2113 else
2114 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2115
2116 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002117 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002118 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119 nonce, nonce_length,
2120 NULL, 0,
2121 tag, tag_length,
2122 NULL, 0,
2123 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002124 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2125 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2126 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002127 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002128 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002129 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130
Neil Armstrong752d8112022-02-07 14:51:11 +01002131 /* Decrypt check, multi-part */
2132 PSA_ASSERT( psa_aead_abort( &operation ) );
2133 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2134 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2135 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2136 else
2137 TEST_EQUAL( status, expected_status );
2138
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002140 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002141 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002142 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143}
2144/* END_CASE */
2145
2146/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002147void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148 int policy_alg,
2149 int key_type,
2150 data_t *key_data,
2151 int exercise_alg )
2152{
Ronald Cron5425a212020-08-04 14:58:35 +02002153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002155 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156 psa_status_t status;
2157 size_t key_bits;
2158 size_t buffer_length;
2159 unsigned char *buffer = NULL;
2160 size_t output_length;
2161
Gilles Peskine8817f612018-12-18 00:18:46 +01002162 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002163
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002164 psa_set_key_usage_flags( &attributes, policy_usage );
2165 psa_set_key_algorithm( &attributes, policy_alg );
2166 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167
Gilles Peskine049c7532019-05-15 20:22:09 +02002168 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002169 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002170
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002171 /* Check if no key usage implication is done */
2172 TEST_EQUAL( policy_usage,
2173 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002174
Ronald Cron5425a212020-08-04 14:58:35 +02002175 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002176 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002177 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2178 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002179 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180
Ronald Cron5425a212020-08-04 14:58:35 +02002181 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002182 NULL, 0,
2183 NULL, 0,
2184 buffer, buffer_length,
2185 &output_length );
2186 if( policy_alg == exercise_alg &&
2187 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002188 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002189 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002190 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002192 if( buffer_length != 0 )
2193 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002194 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195 buffer, buffer_length,
2196 NULL, 0,
2197 buffer, buffer_length,
2198 &output_length );
2199 if( policy_alg == exercise_alg &&
2200 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002201 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002202 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002203 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204
2205exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002206 /*
2207 * Key attributes may have been returned by psa_get_key_attributes()
2208 * thus reset them as required.
2209 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002210 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002211
2212 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002213 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002214 mbedtls_free( buffer );
2215}
2216/* END_CASE */
2217
2218/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002219void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002220 int policy_alg,
2221 int key_type,
2222 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002223 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002224 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002225 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002226{
Ronald Cron5425a212020-08-04 14:58:35 +02002227 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002228 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002229 psa_key_usage_t policy_usage = policy_usage_arg;
2230 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002231 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002232 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2233 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2234 * compatible with the policy and `payload_length_arg` is supposed to be
2235 * a valid input length to sign. If `payload_length_arg <= 0`,
2236 * `exercise_alg` is supposed to be forbidden by the policy. */
2237 int compatible_alg = payload_length_arg > 0;
2238 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002239 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002240 size_t signature_length;
2241
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002242 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002243 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002244 TEST_EQUAL( expected_usage,
2245 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002246
Gilles Peskine8817f612018-12-18 00:18:46 +01002247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002249 psa_set_key_usage_flags( &attributes, policy_usage );
2250 psa_set_key_algorithm( &attributes, policy_alg );
2251 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002252
Gilles Peskine049c7532019-05-15 20:22:09 +02002253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002254 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002255
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002256 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2257
Ronald Cron5425a212020-08-04 14:58:35 +02002258 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002259 payload, payload_length,
2260 signature, sizeof( signature ),
2261 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002262 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002263 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002264 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002265 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002266
2267 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002268 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002269 payload, payload_length,
2270 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002271 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002272 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002273 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002274 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002275
Gilles Peskinef7b41372021-09-22 16:15:05 +02002276 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002277 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002278 {
2279 status = psa_sign_message( key, exercise_alg,
2280 payload, payload_length,
2281 signature, sizeof( signature ),
2282 &signature_length );
2283 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2284 PSA_ASSERT( status );
2285 else
2286 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2287
2288 memset( signature, 0, sizeof( signature ) );
2289 status = psa_verify_message( key, exercise_alg,
2290 payload, payload_length,
2291 signature, sizeof( signature ) );
2292 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2293 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2294 else
2295 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2296 }
2297
Gilles Peskined5b33222018-06-18 22:20:03 +02002298exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002299 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002300 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002301}
2302/* END_CASE */
2303
Janos Follathba3fab92019-06-11 14:50:16 +01002304/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002305void derive_key_policy( int policy_usage,
2306 int policy_alg,
2307 int key_type,
2308 data_t *key_data,
2309 int exercise_alg )
2310{
Ronald Cron5425a212020-08-04 14:58:35 +02002311 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002313 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002314 psa_status_t status;
2315
Gilles Peskine8817f612018-12-18 00:18:46 +01002316 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002317
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002318 psa_set_key_usage_flags( &attributes, policy_usage );
2319 psa_set_key_algorithm( &attributes, policy_alg );
2320 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002321
Gilles Peskine049c7532019-05-15 20:22:09 +02002322 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002323 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002324
Janos Follathba3fab92019-06-11 14:50:16 +01002325 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2326
2327 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2328 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002329 {
Janos Follathba3fab92019-06-11 14:50:16 +01002330 PSA_ASSERT( psa_key_derivation_input_bytes(
2331 &operation,
2332 PSA_KEY_DERIVATION_INPUT_SEED,
2333 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002334 }
Janos Follathba3fab92019-06-11 14:50:16 +01002335
2336 status = psa_key_derivation_input_key( &operation,
2337 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002338 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002339
Gilles Peskineea0fb492018-07-12 17:17:20 +02002340 if( policy_alg == exercise_alg &&
2341 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002342 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002343 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002344 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002345
2346exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002347 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002348 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002349 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002350}
2351/* END_CASE */
2352
2353/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002354void agreement_key_policy( int policy_usage,
2355 int policy_alg,
2356 int key_type_arg,
2357 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002358 int exercise_alg,
2359 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002360{
Ronald Cron5425a212020-08-04 14:58:35 +02002361 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002363 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002364 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002365 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002366 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002367
Gilles Peskine8817f612018-12-18 00:18:46 +01002368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002369
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002370 psa_set_key_usage_flags( &attributes, policy_usage );
2371 psa_set_key_algorithm( &attributes, policy_alg );
2372 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002373
Gilles Peskine049c7532019-05-15 20:22:09 +02002374 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002375 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002376
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002377 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002378 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002379
Steven Cooremance48e852020-10-05 16:02:45 +02002380 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002381
2382exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002383 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002385 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002386}
2387/* END_CASE */
2388
2389/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002390void key_policy_alg2( int key_type_arg, data_t *key_data,
2391 int usage_arg, int alg_arg, int alg2_arg )
2392{
Ronald Cron5425a212020-08-04 14:58:35 +02002393 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002394 psa_key_type_t key_type = key_type_arg;
2395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2396 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2397 psa_key_usage_t usage = usage_arg;
2398 psa_algorithm_t alg = alg_arg;
2399 psa_algorithm_t alg2 = alg2_arg;
2400
2401 PSA_ASSERT( psa_crypto_init( ) );
2402
2403 psa_set_key_usage_flags( &attributes, usage );
2404 psa_set_key_algorithm( &attributes, alg );
2405 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2406 psa_set_key_type( &attributes, key_type );
2407 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002408 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002409
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002410 /* Update the usage flags to obtain implicit usage flags */
2411 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002412 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002413 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2414 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2415 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2416
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002417 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002418 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002419 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002420 goto exit;
2421
2422exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002423 /*
2424 * Key attributes may have been returned by psa_get_key_attributes()
2425 * thus reset them as required.
2426 */
2427 psa_reset_key_attributes( &got_attributes );
2428
Ronald Cron5425a212020-08-04 14:58:35 +02002429 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002430 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002431}
2432/* END_CASE */
2433
2434/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002435void raw_agreement_key_policy( int policy_usage,
2436 int policy_alg,
2437 int key_type_arg,
2438 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002439 int exercise_alg,
2440 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002441{
Ronald Cron5425a212020-08-04 14:58:35 +02002442 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002444 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002445 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002446 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002447 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002448
2449 PSA_ASSERT( psa_crypto_init( ) );
2450
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002451 psa_set_key_usage_flags( &attributes, policy_usage );
2452 psa_set_key_algorithm( &attributes, policy_alg );
2453 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002454
Gilles Peskine049c7532019-05-15 20:22:09 +02002455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002456 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002457
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002458 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002459
Steven Cooremance48e852020-10-05 16:02:45 +02002460 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002461
2462exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002463 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002464 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002465 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002466}
2467/* END_CASE */
2468
2469/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002470void copy_success( int source_usage_arg,
2471 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302472 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002473 int type_arg, data_t *material,
2474 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002475 int target_usage_arg,
2476 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302477 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002478 int expected_usage_arg,
2479 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002480{
Gilles Peskineca25db92019-04-19 11:43:08 +02002481 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2482 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002483 psa_key_usage_t expected_usage = expected_usage_arg;
2484 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002485 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302486 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2487 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002488 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2489 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002490 uint8_t *export_buffer = NULL;
2491
Gilles Peskine57ab7212019-01-28 13:03:09 +01002492 PSA_ASSERT( psa_crypto_init( ) );
2493
Gilles Peskineca25db92019-04-19 11:43:08 +02002494 /* Prepare the source key. */
2495 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2496 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002497 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002498 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302499 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002500 PSA_ASSERT( psa_import_key( &source_attributes,
2501 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002502 &source_key ) );
2503 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002504
Gilles Peskineca25db92019-04-19 11:43:08 +02002505 /* Prepare the target attributes. */
2506 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002507 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002508 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002509 }
Archana8a180362021-07-05 02:18:48 +05302510 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002511
Gilles Peskineca25db92019-04-19 11:43:08 +02002512 if( target_usage_arg != -1 )
2513 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2514 if( target_alg_arg != -1 )
2515 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002516 if( target_alg2_arg != -1 )
2517 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002518
Archana8a180362021-07-05 02:18:48 +05302519
Gilles Peskine57ab7212019-01-28 13:03:09 +01002520 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002521 PSA_ASSERT( psa_copy_key( source_key,
2522 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002523
2524 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002525 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002526
2527 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002528 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002529 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2530 psa_get_key_type( &target_attributes ) );
2531 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2532 psa_get_key_bits( &target_attributes ) );
2533 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2534 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002535 TEST_EQUAL( expected_alg2,
2536 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002537 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2538 {
2539 size_t length;
2540 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002541 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002542 material->len, &length ) );
2543 ASSERT_COMPARE( material->x, material->len,
2544 export_buffer, length );
2545 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002546
Archana8a180362021-07-05 02:18:48 +05302547 if( !psa_key_lifetime_is_external( target_lifetime ) )
2548 {
2549 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2550 goto exit;
2551 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2552 goto exit;
2553 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002554
Ronald Cron5425a212020-08-04 14:58:35 +02002555 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002556
2557exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002558 /*
2559 * Source and target key attributes may have been returned by
2560 * psa_get_key_attributes() thus reset them as required.
2561 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002562 psa_reset_key_attributes( &source_attributes );
2563 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002564
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002565 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002566 mbedtls_free( export_buffer );
2567}
2568/* END_CASE */
2569
2570/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002571void copy_fail( int source_usage_arg,
2572 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302573 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002574 int type_arg, data_t *material,
2575 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002576 int target_usage_arg,
2577 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002578 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002579 int expected_status_arg )
2580{
2581 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2582 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002583 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2584 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002585 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002586
2587 PSA_ASSERT( psa_crypto_init( ) );
2588
2589 /* Prepare the source key. */
2590 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2591 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002592 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002593 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302594 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002595 PSA_ASSERT( psa_import_key( &source_attributes,
2596 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002597 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002598
2599 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002600 psa_set_key_id( &target_attributes, key_id );
2601 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002602 psa_set_key_type( &target_attributes, target_type_arg );
2603 psa_set_key_bits( &target_attributes, target_bits_arg );
2604 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2605 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002606 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002607
2608 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002609 TEST_EQUAL( psa_copy_key( source_key,
2610 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002611 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002612
Ronald Cron5425a212020-08-04 14:58:35 +02002613 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002614
Gilles Peskine4a644642019-05-03 17:14:08 +02002615exit:
2616 psa_reset_key_attributes( &source_attributes );
2617 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002618 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002619}
2620/* END_CASE */
2621
2622/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002623void hash_operation_init( )
2624{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002625 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002626 /* Test each valid way of initializing the object, except for `= {0}`, as
2627 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2628 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002629 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002630 psa_hash_operation_t func = psa_hash_operation_init( );
2631 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2632 psa_hash_operation_t zero;
2633
2634 memset( &zero, 0, sizeof( zero ) );
2635
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002636 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002637 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2638 PSA_ERROR_BAD_STATE );
2639 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2640 PSA_ERROR_BAD_STATE );
2641 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2642 PSA_ERROR_BAD_STATE );
2643
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002644 /* A default hash operation should be abortable without error. */
2645 PSA_ASSERT( psa_hash_abort( &func ) );
2646 PSA_ASSERT( psa_hash_abort( &init ) );
2647 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002648}
2649/* END_CASE */
2650
2651/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002652void hash_setup( int alg_arg,
2653 int expected_status_arg )
2654{
2655 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002656 uint8_t *output = NULL;
2657 size_t output_size = 0;
2658 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002659 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002660 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002661 psa_status_t status;
2662
Gilles Peskine8817f612018-12-18 00:18:46 +01002663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002664
Neil Armstrongedb20862022-02-07 15:47:44 +01002665 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002666 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002667 ASSERT_ALLOC( output, output_size );
2668
2669 status = psa_hash_compute( alg, NULL, 0,
2670 output, output_size, &output_length );
2671 TEST_EQUAL( status, expected_status );
2672
2673 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002674 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002675 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002676
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002677 /* Whether setup succeeded or failed, abort must succeed. */
2678 PSA_ASSERT( psa_hash_abort( &operation ) );
2679
2680 /* If setup failed, reproduce the failure, so as to
2681 * test the resulting state of the operation object. */
2682 if( status != PSA_SUCCESS )
2683 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2684
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002685 /* Now the operation object should be reusable. */
2686#if defined(KNOWN_SUPPORTED_HASH_ALG)
2687 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2688 PSA_ASSERT( psa_hash_abort( &operation ) );
2689#endif
2690
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002691exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002692 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002693 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002694}
2695/* END_CASE */
2696
2697/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002698void hash_compute_fail( int alg_arg, data_t *input,
2699 int output_size_arg, int expected_status_arg )
2700{
2701 psa_algorithm_t alg = alg_arg;
2702 uint8_t *output = NULL;
2703 size_t output_size = output_size_arg;
2704 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002705 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002706 psa_status_t expected_status = expected_status_arg;
2707 psa_status_t status;
2708
2709 ASSERT_ALLOC( output, output_size );
2710
2711 PSA_ASSERT( psa_crypto_init( ) );
2712
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002713 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002714 status = psa_hash_compute( alg, input->x, input->len,
2715 output, output_size, &output_length );
2716 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002717 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002718
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002719 /* Hash Compute, multi-part */
2720 status = psa_hash_setup( &operation, alg );
2721 if( status == PSA_SUCCESS )
2722 {
2723 status = psa_hash_update( &operation, input->x, input->len );
2724 if( status == PSA_SUCCESS )
2725 {
2726 status = psa_hash_finish( &operation, output, output_size,
2727 &output_length );
2728 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002729 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002730 else
2731 TEST_EQUAL( status, expected_status );
2732 }
2733 else
2734 {
2735 TEST_EQUAL( status, expected_status );
2736 }
2737 }
2738 else
2739 {
2740 TEST_EQUAL( status, expected_status );
2741 }
2742
Gilles Peskine0a749c82019-11-28 19:33:58 +01002743exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002744 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002745 mbedtls_free( output );
2746 PSA_DONE( );
2747}
2748/* END_CASE */
2749
2750/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002751void hash_compare_fail( int alg_arg, data_t *input,
2752 data_t *reference_hash,
2753 int expected_status_arg )
2754{
2755 psa_algorithm_t alg = alg_arg;
2756 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002757 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002758 psa_status_t status;
2759
2760 PSA_ASSERT( psa_crypto_init( ) );
2761
Neil Armstrong55a1be12022-02-07 11:23:20 +01002762 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002763 status = psa_hash_compare( alg, input->x, input->len,
2764 reference_hash->x, reference_hash->len );
2765 TEST_EQUAL( status, expected_status );
2766
Neil Armstrong55a1be12022-02-07 11:23:20 +01002767 /* Hash Compare, multi-part */
2768 status = psa_hash_setup( &operation, alg );
2769 if( status == PSA_SUCCESS )
2770 {
2771 status = psa_hash_update( &operation, input->x, input->len );
2772 if( status == PSA_SUCCESS )
2773 {
2774 status = psa_hash_verify( &operation, reference_hash->x,
2775 reference_hash->len );
2776 TEST_EQUAL( status, expected_status );
2777 }
2778 else
2779 {
2780 TEST_EQUAL( status, expected_status );
2781 }
2782 }
2783 else
2784 {
2785 TEST_EQUAL( status, expected_status );
2786 }
2787
Gilles Peskine88e08462020-01-28 20:43:00 +01002788exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002789 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002790 PSA_DONE( );
2791}
2792/* END_CASE */
2793
2794/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002795void hash_compute_compare( int alg_arg, data_t *input,
2796 data_t *expected_output )
2797{
2798 psa_algorithm_t alg = alg_arg;
2799 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2800 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002801 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002802 size_t i;
2803
2804 PSA_ASSERT( psa_crypto_init( ) );
2805
Neil Armstrongca30a002022-02-07 11:40:23 +01002806 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002807 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002808 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002809 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002810 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002811 ASSERT_COMPARE( output, output_length,
2812 expected_output->x, expected_output->len );
2813
Neil Armstrongca30a002022-02-07 11:40:23 +01002814 /* Compute with tight buffer, multi-part */
2815 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2816 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2817 PSA_ASSERT( psa_hash_finish( &operation, output,
2818 PSA_HASH_LENGTH( alg ),
2819 &output_length ) );
2820 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2821 ASSERT_COMPARE( output, output_length,
2822 expected_output->x, expected_output->len );
2823
2824 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002825 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2826 output, sizeof( output ),
2827 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002828 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002829 ASSERT_COMPARE( output, output_length,
2830 expected_output->x, expected_output->len );
2831
Neil Armstrongca30a002022-02-07 11:40:23 +01002832 /* Compute with larger buffer, multi-part */
2833 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2834 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2835 PSA_ASSERT( psa_hash_finish( &operation, output,
2836 sizeof( output ), &output_length ) );
2837 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2838 ASSERT_COMPARE( output, output_length,
2839 expected_output->x, expected_output->len );
2840
2841 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002842 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2843 output, output_length ) );
2844
Neil Armstrongca30a002022-02-07 11:40:23 +01002845 /* Compare with correct hash, multi-part */
2846 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2847 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2848 PSA_ASSERT( psa_hash_verify( &operation, output,
2849 output_length ) );
2850
2851 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002852 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2853 output, output_length + 1 ),
2854 PSA_ERROR_INVALID_SIGNATURE );
2855
Neil Armstrongca30a002022-02-07 11:40:23 +01002856 /* Compare with trailing garbage, multi-part */
2857 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2858 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2859 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2860 PSA_ERROR_INVALID_SIGNATURE );
2861
2862 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002863 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2864 output, output_length - 1 ),
2865 PSA_ERROR_INVALID_SIGNATURE );
2866
Neil Armstrongca30a002022-02-07 11:40:23 +01002867 /* Compare with truncated hash, multi-part */
2868 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2869 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2870 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2871 PSA_ERROR_INVALID_SIGNATURE );
2872
Gilles Peskine0a749c82019-11-28 19:33:58 +01002873 /* Compare with corrupted value */
2874 for( i = 0; i < output_length; i++ )
2875 {
Chris Jones9634bb12021-01-20 15:56:42 +00002876 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002877 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002878
2879 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002880 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2881 output, output_length ),
2882 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002883
2884 /* Multi-Part */
2885 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2886 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2887 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2888 PSA_ERROR_INVALID_SIGNATURE );
2889
Gilles Peskine0a749c82019-11-28 19:33:58 +01002890 output[i] ^= 1;
2891 }
2892
2893exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002894 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002895 PSA_DONE( );
2896}
2897/* END_CASE */
2898
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002899/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002900void hash_bad_order( )
2901{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002902 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002903 unsigned char input[] = "";
2904 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002905 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002906 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2907 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2908 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002909 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002910 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002911 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002912
Gilles Peskine8817f612018-12-18 00:18:46 +01002913 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002914
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002915 /* Call setup twice in a row. */
2916 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002917 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002918 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2919 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002920 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002921 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002922 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002923
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002924 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002925 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002926 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002927 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002928
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002929 /* Check that update calls abort on error. */
2930 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002931 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002932 ASSERT_OPERATION_IS_ACTIVE( operation );
2933 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2934 PSA_ERROR_BAD_STATE );
2935 ASSERT_OPERATION_IS_INACTIVE( operation );
2936 PSA_ASSERT( psa_hash_abort( &operation ) );
2937 ASSERT_OPERATION_IS_INACTIVE( operation );
2938
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002939 /* Call update after finish. */
2940 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2941 PSA_ASSERT( psa_hash_finish( &operation,
2942 hash, sizeof( hash ), &hash_len ) );
2943 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002944 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002945 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002946
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002947 /* Call verify without calling setup beforehand. */
2948 TEST_EQUAL( psa_hash_verify( &operation,
2949 valid_hash, sizeof( valid_hash ) ),
2950 PSA_ERROR_BAD_STATE );
2951 PSA_ASSERT( psa_hash_abort( &operation ) );
2952
2953 /* Call verify after finish. */
2954 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2955 PSA_ASSERT( psa_hash_finish( &operation,
2956 hash, sizeof( hash ), &hash_len ) );
2957 TEST_EQUAL( psa_hash_verify( &operation,
2958 valid_hash, sizeof( valid_hash ) ),
2959 PSA_ERROR_BAD_STATE );
2960 PSA_ASSERT( psa_hash_abort( &operation ) );
2961
2962 /* Call verify twice in a row. */
2963 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002964 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002965 PSA_ASSERT( psa_hash_verify( &operation,
2966 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002967 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002968 TEST_EQUAL( psa_hash_verify( &operation,
2969 valid_hash, sizeof( valid_hash ) ),
2970 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002971 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002972 PSA_ASSERT( psa_hash_abort( &operation ) );
2973
2974 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002975 TEST_EQUAL( psa_hash_finish( &operation,
2976 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002977 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002978 PSA_ASSERT( psa_hash_abort( &operation ) );
2979
2980 /* Call finish twice in a row. */
2981 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2982 PSA_ASSERT( psa_hash_finish( &operation,
2983 hash, sizeof( hash ), &hash_len ) );
2984 TEST_EQUAL( psa_hash_finish( &operation,
2985 hash, sizeof( hash ), &hash_len ),
2986 PSA_ERROR_BAD_STATE );
2987 PSA_ASSERT( psa_hash_abort( &operation ) );
2988
2989 /* Call finish after calling verify. */
2990 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2991 PSA_ASSERT( psa_hash_verify( &operation,
2992 valid_hash, sizeof( valid_hash ) ) );
2993 TEST_EQUAL( psa_hash_finish( &operation,
2994 hash, sizeof( hash ), &hash_len ),
2995 PSA_ERROR_BAD_STATE );
2996 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002997
2998exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002999 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02003000}
3001/* END_CASE */
3002
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003003/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02003004void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03003005{
3006 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003007 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3008 * appended to it */
3009 unsigned char hash[] = {
3010 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3011 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
3012 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003013 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003014 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003015
Gilles Peskine8817f612018-12-18 00:18:46 +01003016 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03003017
itayzafrir27e69452018-11-01 14:26:34 +02003018 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003020 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003021 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003022 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003023 ASSERT_OPERATION_IS_INACTIVE( operation );
3024 PSA_ASSERT( psa_hash_abort( &operation ) );
3025 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03003026
itayzafrir27e69452018-11-01 14:26:34 +02003027 /* psa_hash_verify with a non-matching hash */
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 + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003030 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03003031
itayzafrir27e69452018-11-01 14:26:34 +02003032 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003034 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003035 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03003036
itayzafrirec93d302018-10-18 18:01:10 +03003037exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003038 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03003039}
3040/* END_CASE */
3041
Ronald Cronee414c72021-03-18 18:50:08 +01003042/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003043void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03003044{
3045 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003046 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003047 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003048 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003049 size_t hash_len;
3050
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003052
itayzafrir58028322018-10-25 10:22:01 +03003053 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003054 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003055 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003056 hash, expected_size - 1, &hash_len ),
3057 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003058
3059exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003060 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003061}
3062/* END_CASE */
3063
Ronald Cronee414c72021-03-18 18:50:08 +01003064/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003065void hash_clone_source_state( )
3066{
3067 psa_algorithm_t alg = PSA_ALG_SHA_256;
3068 unsigned char hash[PSA_HASH_MAX_SIZE];
3069 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3070 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3071 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3072 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3073 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3074 size_t hash_len;
3075
3076 PSA_ASSERT( psa_crypto_init( ) );
3077 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3078
3079 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3080 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3081 PSA_ASSERT( psa_hash_finish( &op_finished,
3082 hash, sizeof( hash ), &hash_len ) );
3083 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3084 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3085
3086 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3087 PSA_ERROR_BAD_STATE );
3088
3089 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3090 PSA_ASSERT( psa_hash_finish( &op_init,
3091 hash, sizeof( hash ), &hash_len ) );
3092 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3093 PSA_ASSERT( psa_hash_finish( &op_finished,
3094 hash, sizeof( hash ), &hash_len ) );
3095 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3096 PSA_ASSERT( psa_hash_finish( &op_aborted,
3097 hash, sizeof( hash ), &hash_len ) );
3098
3099exit:
3100 psa_hash_abort( &op_source );
3101 psa_hash_abort( &op_init );
3102 psa_hash_abort( &op_setup );
3103 psa_hash_abort( &op_finished );
3104 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003105 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003106}
3107/* END_CASE */
3108
Ronald Cronee414c72021-03-18 18:50:08 +01003109/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003110void hash_clone_target_state( )
3111{
3112 psa_algorithm_t alg = PSA_ALG_SHA_256;
3113 unsigned char hash[PSA_HASH_MAX_SIZE];
3114 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3115 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3116 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3117 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3118 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3119 size_t hash_len;
3120
3121 PSA_ASSERT( psa_crypto_init( ) );
3122
3123 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3124 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3125 PSA_ASSERT( psa_hash_finish( &op_finished,
3126 hash, sizeof( hash ), &hash_len ) );
3127 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3128 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3129
3130 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3131 PSA_ASSERT( psa_hash_finish( &op_target,
3132 hash, sizeof( hash ), &hash_len ) );
3133
3134 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3135 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3136 PSA_ERROR_BAD_STATE );
3137 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3138 PSA_ERROR_BAD_STATE );
3139
3140exit:
3141 psa_hash_abort( &op_target );
3142 psa_hash_abort( &op_init );
3143 psa_hash_abort( &op_setup );
3144 psa_hash_abort( &op_finished );
3145 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003146 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003147}
3148/* END_CASE */
3149
itayzafrir58028322018-10-25 10:22:01 +03003150/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003151void mac_operation_init( )
3152{
Jaeden Amero252ef282019-02-15 14:05:35 +00003153 const uint8_t input[1] = { 0 };
3154
Jaeden Amero769ce272019-01-04 11:48:03 +00003155 /* Test each valid way of initializing the object, except for `= {0}`, as
3156 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3157 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003158 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003159 psa_mac_operation_t func = psa_mac_operation_init( );
3160 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3161 psa_mac_operation_t zero;
3162
3163 memset( &zero, 0, sizeof( zero ) );
3164
Jaeden Amero252ef282019-02-15 14:05:35 +00003165 /* A freshly-initialized MAC operation should not be usable. */
3166 TEST_EQUAL( psa_mac_update( &func,
3167 input, sizeof( input ) ),
3168 PSA_ERROR_BAD_STATE );
3169 TEST_EQUAL( psa_mac_update( &init,
3170 input, sizeof( input ) ),
3171 PSA_ERROR_BAD_STATE );
3172 TEST_EQUAL( psa_mac_update( &zero,
3173 input, sizeof( input ) ),
3174 PSA_ERROR_BAD_STATE );
3175
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003176 /* A default MAC operation should be abortable without error. */
3177 PSA_ASSERT( psa_mac_abort( &func ) );
3178 PSA_ASSERT( psa_mac_abort( &init ) );
3179 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003180}
3181/* END_CASE */
3182
3183/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003184void mac_setup( int key_type_arg,
3185 data_t *key,
3186 int alg_arg,
3187 int expected_status_arg )
3188{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003189 psa_key_type_t key_type = key_type_arg;
3190 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003191 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003192 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003193 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3194#if defined(KNOWN_SUPPORTED_MAC_ALG)
3195 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3196#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003197
Gilles Peskine8817f612018-12-18 00:18:46 +01003198 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003199
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003200 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3201 &operation, &status ) )
3202 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003203 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003204
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003205 /* The operation object should be reusable. */
3206#if defined(KNOWN_SUPPORTED_MAC_ALG)
3207 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3208 smoke_test_key_data,
3209 sizeof( smoke_test_key_data ),
3210 KNOWN_SUPPORTED_MAC_ALG,
3211 &operation, &status ) )
3212 goto exit;
3213 TEST_EQUAL( status, PSA_SUCCESS );
3214#endif
3215
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003216exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003217 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003218}
3219/* END_CASE */
3220
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003221/* 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 +00003222void mac_bad_order( )
3223{
Ronald Cron5425a212020-08-04 14:58:35 +02003224 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003225 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3226 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003227 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003228 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3229 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3230 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003232 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3233 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3234 size_t sign_mac_length = 0;
3235 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3236 const uint8_t verify_mac[] = {
3237 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3238 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3239 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3240
3241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003242 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003243 psa_set_key_algorithm( &attributes, alg );
3244 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003245
Ronald Cron5425a212020-08-04 14:58:35 +02003246 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3247 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003248
Jaeden Amero252ef282019-02-15 14:05:35 +00003249 /* Call update without calling setup beforehand. */
3250 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3251 PSA_ERROR_BAD_STATE );
3252 PSA_ASSERT( psa_mac_abort( &operation ) );
3253
3254 /* Call sign finish without calling setup beforehand. */
3255 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3256 &sign_mac_length),
3257 PSA_ERROR_BAD_STATE );
3258 PSA_ASSERT( psa_mac_abort( &operation ) );
3259
3260 /* Call verify finish without calling setup beforehand. */
3261 TEST_EQUAL( psa_mac_verify_finish( &operation,
3262 verify_mac, sizeof( verify_mac ) ),
3263 PSA_ERROR_BAD_STATE );
3264 PSA_ASSERT( psa_mac_abort( &operation ) );
3265
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003266 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003267 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003268 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003269 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003270 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003271 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003272 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003273 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003274
Jaeden Amero252ef282019-02-15 14:05:35 +00003275 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003276 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003277 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3278 PSA_ASSERT( psa_mac_sign_finish( &operation,
3279 sign_mac, sizeof( sign_mac ),
3280 &sign_mac_length ) );
3281 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3282 PSA_ERROR_BAD_STATE );
3283 PSA_ASSERT( psa_mac_abort( &operation ) );
3284
3285 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003286 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003287 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3288 PSA_ASSERT( psa_mac_verify_finish( &operation,
3289 verify_mac, sizeof( verify_mac ) ) );
3290 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3291 PSA_ERROR_BAD_STATE );
3292 PSA_ASSERT( psa_mac_abort( &operation ) );
3293
3294 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003295 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003296 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3297 PSA_ASSERT( psa_mac_sign_finish( &operation,
3298 sign_mac, sizeof( sign_mac ),
3299 &sign_mac_length ) );
3300 TEST_EQUAL( psa_mac_sign_finish( &operation,
3301 sign_mac, sizeof( sign_mac ),
3302 &sign_mac_length ),
3303 PSA_ERROR_BAD_STATE );
3304 PSA_ASSERT( psa_mac_abort( &operation ) );
3305
3306 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003307 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003308 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3309 PSA_ASSERT( psa_mac_verify_finish( &operation,
3310 verify_mac, sizeof( verify_mac ) ) );
3311 TEST_EQUAL( psa_mac_verify_finish( &operation,
3312 verify_mac, sizeof( verify_mac ) ),
3313 PSA_ERROR_BAD_STATE );
3314 PSA_ASSERT( psa_mac_abort( &operation ) );
3315
3316 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003317 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003318 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003319 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003320 TEST_EQUAL( psa_mac_verify_finish( &operation,
3321 verify_mac, sizeof( verify_mac ) ),
3322 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003323 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003324 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003325 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003326
3327 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003328 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003329 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003330 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003331 TEST_EQUAL( psa_mac_sign_finish( &operation,
3332 sign_mac, sizeof( sign_mac ),
3333 &sign_mac_length ),
3334 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003335 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003336 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003337 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003338
Ronald Cron5425a212020-08-04 14:58:35 +02003339 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003340
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003341exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003342 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003343}
3344/* END_CASE */
3345
3346/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003347void mac_sign_verify_multi( int key_type_arg,
3348 data_t *key_data,
3349 int alg_arg,
3350 data_t *input,
3351 int is_verify,
3352 data_t *expected_mac )
3353{
3354 size_t data_part_len = 0;
3355
3356 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3357 {
3358 /* Split data into length(data_part_len) parts. */
3359 mbedtls_test_set_step( 2000 + data_part_len );
3360
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003361 if( mac_multipart_internal_func( key_type_arg, key_data,
3362 alg_arg,
3363 input, data_part_len,
3364 expected_mac,
3365 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003366 break;
3367
3368 /* length(0) part, length(data_part_len) part, length(0) part... */
3369 mbedtls_test_set_step( 3000 + data_part_len );
3370
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003371 if( mac_multipart_internal_func( key_type_arg, key_data,
3372 alg_arg,
3373 input, data_part_len,
3374 expected_mac,
3375 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003376 break;
3377 }
3378
3379 /* Goto is required to silence warnings about unused labels, as we
3380 * don't actually do any test assertions in this function. */
3381 goto exit;
3382}
3383/* END_CASE */
3384
3385/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003386void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003387 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003388 int alg_arg,
3389 data_t *input,
3390 data_t *expected_mac )
3391{
Ronald Cron5425a212020-08-04 14:58:35 +02003392 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003393 psa_key_type_t key_type = key_type_arg;
3394 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003395 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003397 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003398 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003399 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003400 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003401 const size_t output_sizes_to_test[] = {
3402 0,
3403 1,
3404 expected_mac->len - 1,
3405 expected_mac->len,
3406 expected_mac->len + 1,
3407 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003408
Gilles Peskine7be11a72022-04-14 00:12:57 +02003409 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003410 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003411 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003412
Gilles Peskine8817f612018-12-18 00:18:46 +01003413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003414
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003416 psa_set_key_algorithm( &attributes, alg );
3417 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003418
Ronald Cron5425a212020-08-04 14:58:35 +02003419 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3420 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003421
Gilles Peskine8b356b52020-08-25 23:44:59 +02003422 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3423 {
3424 const size_t output_size = output_sizes_to_test[i];
3425 psa_status_t expected_status =
3426 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3427 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003428
Chris Jones9634bb12021-01-20 15:56:42 +00003429 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003430 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003431
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003432 /* Calculate the MAC, one-shot case. */
3433 TEST_EQUAL( psa_mac_compute( key, alg,
3434 input->x, input->len,
3435 actual_mac, output_size, &mac_length ),
3436 expected_status );
3437 if( expected_status == PSA_SUCCESS )
3438 {
3439 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3440 actual_mac, mac_length );
3441 }
3442
3443 if( output_size > 0 )
3444 memset( actual_mac, 0, output_size );
3445
3446 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003447 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003448 PSA_ASSERT( psa_mac_update( &operation,
3449 input->x, input->len ) );
3450 TEST_EQUAL( psa_mac_sign_finish( &operation,
3451 actual_mac, output_size,
3452 &mac_length ),
3453 expected_status );
3454 PSA_ASSERT( psa_mac_abort( &operation ) );
3455
3456 if( expected_status == PSA_SUCCESS )
3457 {
3458 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3459 actual_mac, mac_length );
3460 }
3461 mbedtls_free( actual_mac );
3462 actual_mac = NULL;
3463 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003464
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003465exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003466 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003467 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003468 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003469 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003470}
3471/* END_CASE */
3472
3473/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003474void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003475 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003476 int alg_arg,
3477 data_t *input,
3478 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003479{
Ronald Cron5425a212020-08-04 14:58:35 +02003480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003481 psa_key_type_t key_type = key_type_arg;
3482 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003483 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003485 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003486
Gilles Peskine7be11a72022-04-14 00:12:57 +02003487 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003488
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003490
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003491 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003492 psa_set_key_algorithm( &attributes, alg );
3493 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003494
Ronald Cron5425a212020-08-04 14:58:35 +02003495 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3496 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003497
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003498 /* Verify correct MAC, one-shot case. */
3499 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3500 expected_mac->x, expected_mac->len ) );
3501
3502 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003503 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003504 PSA_ASSERT( psa_mac_update( &operation,
3505 input->x, input->len ) );
3506 PSA_ASSERT( psa_mac_verify_finish( &operation,
3507 expected_mac->x,
3508 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003509
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003510 /* Test a MAC that's too short, one-shot case. */
3511 TEST_EQUAL( psa_mac_verify( key, alg,
3512 input->x, input->len,
3513 expected_mac->x,
3514 expected_mac->len - 1 ),
3515 PSA_ERROR_INVALID_SIGNATURE );
3516
3517 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003518 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003519 PSA_ASSERT( psa_mac_update( &operation,
3520 input->x, input->len ) );
3521 TEST_EQUAL( psa_mac_verify_finish( &operation,
3522 expected_mac->x,
3523 expected_mac->len - 1 ),
3524 PSA_ERROR_INVALID_SIGNATURE );
3525
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003526 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003527 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3528 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003529 TEST_EQUAL( psa_mac_verify( key, alg,
3530 input->x, input->len,
3531 perturbed_mac, expected_mac->len + 1 ),
3532 PSA_ERROR_INVALID_SIGNATURE );
3533
3534 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003535 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003536 PSA_ASSERT( psa_mac_update( &operation,
3537 input->x, input->len ) );
3538 TEST_EQUAL( psa_mac_verify_finish( &operation,
3539 perturbed_mac,
3540 expected_mac->len + 1 ),
3541 PSA_ERROR_INVALID_SIGNATURE );
3542
3543 /* Test changing one byte. */
3544 for( size_t i = 0; i < expected_mac->len; i++ )
3545 {
Chris Jones9634bb12021-01-20 15:56:42 +00003546 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003547 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003548
3549 TEST_EQUAL( psa_mac_verify( key, alg,
3550 input->x, input->len,
3551 perturbed_mac, expected_mac->len ),
3552 PSA_ERROR_INVALID_SIGNATURE );
3553
Ronald Cron5425a212020-08-04 14:58:35 +02003554 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003555 PSA_ASSERT( psa_mac_update( &operation,
3556 input->x, input->len ) );
3557 TEST_EQUAL( psa_mac_verify_finish( &operation,
3558 perturbed_mac,
3559 expected_mac->len ),
3560 PSA_ERROR_INVALID_SIGNATURE );
3561 perturbed_mac[i] ^= 1;
3562 }
3563
Gilles Peskine8c9def32018-02-08 10:02:12 +01003564exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003565 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003566 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003567 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003568 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003569}
3570/* END_CASE */
3571
3572/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003573void cipher_operation_init( )
3574{
Jaeden Ameroab439972019-02-15 14:12:05 +00003575 const uint8_t input[1] = { 0 };
3576 unsigned char output[1] = { 0 };
3577 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003578 /* Test each valid way of initializing the object, except for `= {0}`, as
3579 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3580 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003581 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003582 psa_cipher_operation_t func = psa_cipher_operation_init( );
3583 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3584 psa_cipher_operation_t zero;
3585
3586 memset( &zero, 0, sizeof( zero ) );
3587
Jaeden Ameroab439972019-02-15 14:12:05 +00003588 /* A freshly-initialized cipher operation should not be usable. */
3589 TEST_EQUAL( psa_cipher_update( &func,
3590 input, sizeof( input ),
3591 output, sizeof( output ),
3592 &output_length ),
3593 PSA_ERROR_BAD_STATE );
3594 TEST_EQUAL( psa_cipher_update( &init,
3595 input, sizeof( input ),
3596 output, sizeof( output ),
3597 &output_length ),
3598 PSA_ERROR_BAD_STATE );
3599 TEST_EQUAL( psa_cipher_update( &zero,
3600 input, sizeof( input ),
3601 output, sizeof( output ),
3602 &output_length ),
3603 PSA_ERROR_BAD_STATE );
3604
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003605 /* A default cipher operation should be abortable without error. */
3606 PSA_ASSERT( psa_cipher_abort( &func ) );
3607 PSA_ASSERT( psa_cipher_abort( &init ) );
3608 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003609}
3610/* END_CASE */
3611
3612/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003613void cipher_setup( int key_type_arg,
3614 data_t *key,
3615 int alg_arg,
3616 int expected_status_arg )
3617{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003618 psa_key_type_t key_type = key_type_arg;
3619 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003620 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003621 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003622 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003623#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003624 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3625#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003626
Gilles Peskine8817f612018-12-18 00:18:46 +01003627 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003628
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003629 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3630 &operation, &status ) )
3631 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003632 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003633
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003634 /* The operation object should be reusable. */
3635#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3636 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3637 smoke_test_key_data,
3638 sizeof( smoke_test_key_data ),
3639 KNOWN_SUPPORTED_CIPHER_ALG,
3640 &operation, &status ) )
3641 goto exit;
3642 TEST_EQUAL( status, PSA_SUCCESS );
3643#endif
3644
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003645exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003646 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003647 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003648}
3649/* END_CASE */
3650
Ronald Cronee414c72021-03-18 18:50:08 +01003651/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003652void cipher_bad_order( )
3653{
Ronald Cron5425a212020-08-04 14:58:35 +02003654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003655 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3656 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003658 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003659 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003660 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003661 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3662 0xaa, 0xaa, 0xaa, 0xaa };
3663 const uint8_t text[] = {
3664 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3665 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003666 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003667 size_t length = 0;
3668
3669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3671 psa_set_key_algorithm( &attributes, alg );
3672 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003673 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3674 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003675
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003676 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003677 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003678 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003679 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003680 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003681 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003682 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003683 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003684
3685 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003686 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003687 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003688 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003689 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003690 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003691 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003692 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003693
Jaeden Ameroab439972019-02-15 14:12:05 +00003694 /* Generate an IV without calling setup beforehand. */
3695 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3696 buffer, sizeof( buffer ),
3697 &length ),
3698 PSA_ERROR_BAD_STATE );
3699 PSA_ASSERT( psa_cipher_abort( &operation ) );
3700
3701 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003702 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003703 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3704 buffer, sizeof( buffer ),
3705 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003706 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003707 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3708 buffer, sizeof( buffer ),
3709 &length ),
3710 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003711 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003712 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003713 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003714
3715 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003716 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003717 PSA_ASSERT( psa_cipher_set_iv( &operation,
3718 iv, sizeof( iv ) ) );
3719 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3720 buffer, sizeof( buffer ),
3721 &length ),
3722 PSA_ERROR_BAD_STATE );
3723 PSA_ASSERT( psa_cipher_abort( &operation ) );
3724
3725 /* Set an IV without calling setup beforehand. */
3726 TEST_EQUAL( psa_cipher_set_iv( &operation,
3727 iv, sizeof( iv ) ),
3728 PSA_ERROR_BAD_STATE );
3729 PSA_ASSERT( psa_cipher_abort( &operation ) );
3730
3731 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003732 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003733 PSA_ASSERT( psa_cipher_set_iv( &operation,
3734 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003735 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003736 TEST_EQUAL( psa_cipher_set_iv( &operation,
3737 iv, sizeof( iv ) ),
3738 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003739 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003740 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003741 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003742
3743 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003744 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003745 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3746 buffer, sizeof( buffer ),
3747 &length ) );
3748 TEST_EQUAL( psa_cipher_set_iv( &operation,
3749 iv, sizeof( iv ) ),
3750 PSA_ERROR_BAD_STATE );
3751 PSA_ASSERT( psa_cipher_abort( &operation ) );
3752
3753 /* Call update without calling setup beforehand. */
3754 TEST_EQUAL( psa_cipher_update( &operation,
3755 text, sizeof( text ),
3756 buffer, sizeof( buffer ),
3757 &length ),
3758 PSA_ERROR_BAD_STATE );
3759 PSA_ASSERT( psa_cipher_abort( &operation ) );
3760
3761 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003762 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003763 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003764 TEST_EQUAL( psa_cipher_update( &operation,
3765 text, sizeof( text ),
3766 buffer, sizeof( buffer ),
3767 &length ),
3768 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003769 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003770 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003771 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003772
3773 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003774 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003775 PSA_ASSERT( psa_cipher_set_iv( &operation,
3776 iv, sizeof( iv ) ) );
3777 PSA_ASSERT( psa_cipher_finish( &operation,
3778 buffer, sizeof( buffer ), &length ) );
3779 TEST_EQUAL( psa_cipher_update( &operation,
3780 text, sizeof( text ),
3781 buffer, sizeof( buffer ),
3782 &length ),
3783 PSA_ERROR_BAD_STATE );
3784 PSA_ASSERT( psa_cipher_abort( &operation ) );
3785
3786 /* Call finish without calling setup beforehand. */
3787 TEST_EQUAL( psa_cipher_finish( &operation,
3788 buffer, sizeof( buffer ), &length ),
3789 PSA_ERROR_BAD_STATE );
3790 PSA_ASSERT( psa_cipher_abort( &operation ) );
3791
3792 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003793 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003794 /* Not calling update means we are encrypting an empty buffer, which is OK
3795 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003796 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003797 TEST_EQUAL( psa_cipher_finish( &operation,
3798 buffer, sizeof( buffer ), &length ),
3799 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003800 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003801 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003802 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003803
3804 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003805 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003806 PSA_ASSERT( psa_cipher_set_iv( &operation,
3807 iv, sizeof( iv ) ) );
3808 PSA_ASSERT( psa_cipher_finish( &operation,
3809 buffer, sizeof( buffer ), &length ) );
3810 TEST_EQUAL( psa_cipher_finish( &operation,
3811 buffer, sizeof( buffer ), &length ),
3812 PSA_ERROR_BAD_STATE );
3813 PSA_ASSERT( psa_cipher_abort( &operation ) );
3814
Ronald Cron5425a212020-08-04 14:58:35 +02003815 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003816
Jaeden Ameroab439972019-02-15 14:12:05 +00003817exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003818 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003819 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003820}
3821/* END_CASE */
3822
3823/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003824void cipher_encrypt_fail( int alg_arg,
3825 int key_type_arg,
3826 data_t *key_data,
3827 data_t *input,
3828 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003829{
Ronald Cron5425a212020-08-04 14:58:35 +02003830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003831 psa_status_t status;
3832 psa_key_type_t key_type = key_type_arg;
3833 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003834 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003835 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3836 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3837 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003838 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003839 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003840 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003841 size_t function_output_length;
3842 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3844
3845 if ( PSA_ERROR_BAD_STATE != expected_status )
3846 {
3847 PSA_ASSERT( psa_crypto_init( ) );
3848
3849 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3850 psa_set_key_algorithm( &attributes, alg );
3851 psa_set_key_type( &attributes, key_type );
3852
3853 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3854 input->len );
3855 ASSERT_ALLOC( output, output_buffer_size );
3856
3857 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3858 &key ) );
3859 }
3860
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003861 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003862 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3863 output_buffer_size, &output_length );
3864
3865 TEST_EQUAL( status, expected_status );
3866
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003867 /* Encrypt, multi-part */
3868 status = psa_cipher_encrypt_setup( &operation, key, alg );
3869 if( status == PSA_SUCCESS )
3870 {
3871 if( alg != PSA_ALG_ECB_NO_PADDING )
3872 {
3873 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3874 iv, iv_size,
3875 &iv_length ) );
3876 }
3877
3878 status = psa_cipher_update( &operation, input->x, input->len,
3879 output, output_buffer_size,
3880 &function_output_length );
3881 if( status == PSA_SUCCESS )
3882 {
3883 output_length += function_output_length;
3884
3885 status = psa_cipher_finish( &operation, output + output_length,
3886 output_buffer_size - output_length,
3887 &function_output_length );
3888
3889 TEST_EQUAL( status, expected_status );
3890 }
3891 else
3892 {
3893 TEST_EQUAL( status, expected_status );
3894 }
3895 }
3896 else
3897 {
3898 TEST_EQUAL( status, expected_status );
3899 }
3900
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003901exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003902 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903 mbedtls_free( output );
3904 psa_destroy_key( key );
3905 PSA_DONE( );
3906}
3907/* END_CASE */
3908
3909/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003910void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3911 data_t *input, int iv_length,
3912 int expected_result )
3913{
3914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3915 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3917 size_t output_buffer_size = 0;
3918 unsigned char *output = NULL;
3919
3920 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3921 ASSERT_ALLOC( output, output_buffer_size );
3922
3923 PSA_ASSERT( psa_crypto_init( ) );
3924
3925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3926 psa_set_key_algorithm( &attributes, alg );
3927 psa_set_key_type( &attributes, key_type );
3928
3929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3930 &key ) );
3931 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3932 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3933 iv_length ) );
3934
3935exit:
3936 psa_cipher_abort( &operation );
3937 mbedtls_free( output );
3938 psa_destroy_key( key );
3939 PSA_DONE( );
3940}
3941/* END_CASE */
3942
3943/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003944void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3945 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003946{
3947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3948 psa_key_type_t key_type = key_type_arg;
3949 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003950 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3951 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003952 unsigned char *output = NULL;
3953 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003954 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3956
3957 PSA_ASSERT( psa_crypto_init( ) );
3958
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003959 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003960 TEST_LE_U( ciphertext->len,
3961 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3962 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003963 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003964 TEST_LE_U( plaintext->len,
3965 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3966 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3967 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003968
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003969
3970 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003971 psa_set_key_usage_flags( &attributes,
3972 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003973 psa_set_key_algorithm( &attributes, alg );
3974 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3976 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003977 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3978 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003979 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003980
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003981 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003982 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3983 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3984 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003985 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3986 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003987 PSA_ERROR_BAD_STATE );
3988
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003989 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003990 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3991 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003992 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003993 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003994 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3995 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003996 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003997 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003998
Gilles Peskine286c3142022-04-20 17:09:38 +02003999 /* Multipart encryption */
4000 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4001 output_length = 0;
4002 length = ~0;
4003 PSA_ASSERT( psa_cipher_update( &operation,
4004 plaintext->x, plaintext->len,
4005 output, output_buffer_size,
4006 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004007 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02004008 output_length += length;
4009 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine42649d92022-11-23 14:15:57 +01004010 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine286c3142022-04-20 17:09:38 +02004011 output_buffer_size - output_length,
4012 &length ) );
4013 output_length += length;
4014 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004015 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004016
Gilles Peskine286c3142022-04-20 17:09:38 +02004017 /* Multipart encryption */
4018 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
4019 output_length = 0;
4020 length = ~0;
4021 PSA_ASSERT( psa_cipher_update( &operation,
4022 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004023 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02004024 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004025 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02004026 output_length += length;
4027 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine42649d92022-11-23 14:15:57 +01004028 mbedtls_buffer_offset( output, output_length ),
Gilles Peskine286c3142022-04-20 17:09:38 +02004029 output_buffer_size - output_length,
4030 &length ) );
4031 output_length += length;
4032 ASSERT_COMPARE( plaintext->x, plaintext->len,
4033 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004034
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004035 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004036 output_length = ~0;
4037 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
4038 output, output_buffer_size,
4039 &output_length ) );
4040 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
4041 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004042
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004043 /* One-shot decryption */
4044 output_length = ~0;
4045 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4046 output, output_buffer_size,
4047 &output_length ) );
4048 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004049 output, output_length );
4050
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004051exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004052 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004053 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004054 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004055 psa_destroy_key( key );
4056 PSA_DONE( );
4057}
4058/* END_CASE */
4059
4060/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004061void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4062{
4063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4064 psa_algorithm_t alg = alg_arg;
4065 psa_key_type_t key_type = key_type_arg;
4066 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4067 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4068 psa_status_t status;
4069
4070 PSA_ASSERT( psa_crypto_init( ) );
4071
4072 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4073 psa_set_key_algorithm( &attributes, alg );
4074 psa_set_key_type( &attributes, key_type );
4075
4076 /* Usage of either of these two size macros would cause divide by zero
4077 * with incorrect key types previously. Input length should be irrelevant
4078 * here. */
4079 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4080 0 );
4081 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4082
4083
4084 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4085 &key ) );
4086
4087 /* Should fail due to invalid alg type (to support invalid key type).
4088 * Encrypt or decrypt will end up in the same place. */
4089 status = psa_cipher_encrypt_setup( &operation, key, alg );
4090
4091 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4092
4093exit:
4094 psa_cipher_abort( &operation );
4095 psa_destroy_key( key );
4096 PSA_DONE( );
4097}
4098/* END_CASE */
4099
4100/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004101void cipher_encrypt_validation( int alg_arg,
4102 int key_type_arg,
4103 data_t *key_data,
4104 data_t *input )
4105{
4106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4107 psa_key_type_t key_type = key_type_arg;
4108 psa_algorithm_t alg = alg_arg;
4109 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4110 unsigned char *output1 = NULL;
4111 size_t output1_buffer_size = 0;
4112 size_t output1_length = 0;
4113 unsigned char *output2 = NULL;
4114 size_t output2_buffer_size = 0;
4115 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004116 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004117 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004119
Gilles Peskine8817f612018-12-18 00:18:46 +01004120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004122 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4123 psa_set_key_algorithm( &attributes, alg );
4124 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004125
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004126 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4127 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4128 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4129 ASSERT_ALLOC( output1, output1_buffer_size );
4130 ASSERT_ALLOC( output2, output2_buffer_size );
4131
Ronald Cron5425a212020-08-04 14:58:35 +02004132 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4133 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004134
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004135 /* The one-shot cipher encryption uses generated iv so validating
4136 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004137 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4138 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004139 TEST_LE_U( output1_length,
4140 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4141 TEST_LE_U( output1_length,
4142 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004143
4144 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4145 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004146
Gilles Peskine8817f612018-12-18 00:18:46 +01004147 PSA_ASSERT( psa_cipher_update( &operation,
4148 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004149 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004150 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004151 TEST_LE_U( function_output_length,
4152 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4153 TEST_LE_U( function_output_length,
4154 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004155 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004156
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004157 PSA_ASSERT( psa_cipher_finish( &operation,
4158 output2 + output2_length,
4159 output2_buffer_size - output2_length,
4160 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004161 TEST_LE_U( function_output_length,
4162 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4163 TEST_LE_U( function_output_length,
4164 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004165 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004166
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004167 PSA_ASSERT( psa_cipher_abort( &operation ) );
4168 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4169 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004170
Gilles Peskine50e586b2018-06-08 14:28:46 +02004171exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004172 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004173 mbedtls_free( output1 );
4174 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004175 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004176 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004177}
4178/* END_CASE */
4179
4180/* BEGIN_CASE */
4181void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004182 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004183 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004184 int first_part_size_arg,
4185 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004186 data_t *expected_output,
4187 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004188{
Ronald Cron5425a212020-08-04 14:58:35 +02004189 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004190 psa_key_type_t key_type = key_type_arg;
4191 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004192 psa_status_t status;
4193 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004194 size_t first_part_size = first_part_size_arg;
4195 size_t output1_length = output1_length_arg;
4196 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004197 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004198 size_t output_buffer_size = 0;
4199 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004200 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004201 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004202 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004203
Gilles Peskine8817f612018-12-18 00:18:46 +01004204 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004205
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004206 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4207 psa_set_key_algorithm( &attributes, alg );
4208 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004209
Ronald Cron5425a212020-08-04 14:58:35 +02004210 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4211 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004212
Ronald Cron5425a212020-08-04 14:58:35 +02004213 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004214
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004215 if( iv->len > 0 )
4216 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004217 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004218 }
4219
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004220 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4221 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004222 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004223
Gilles Peskine7be11a72022-04-14 00:12:57 +02004224 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004225 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4226 output, output_buffer_size,
4227 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004228 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004229 TEST_LE_U( function_output_length,
4230 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4231 TEST_LE_U( function_output_length,
4232 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004233 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004234
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004235 if( first_part_size < input->len )
4236 {
4237 PSA_ASSERT( psa_cipher_update( &operation,
4238 input->x + first_part_size,
4239 input->len - first_part_size,
4240 ( output_buffer_size == 0 ? NULL :
4241 output + total_output_length ),
4242 output_buffer_size - total_output_length,
4243 &function_output_length ) );
4244 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004245 TEST_LE_U( function_output_length,
4246 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4247 alg,
4248 input->len - first_part_size ) );
4249 TEST_LE_U( function_output_length,
4250 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004251 total_output_length += function_output_length;
4252 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004253
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004254 status = psa_cipher_finish( &operation,
4255 ( output_buffer_size == 0 ? NULL :
4256 output + total_output_length ),
4257 output_buffer_size - total_output_length,
4258 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004259 TEST_LE_U( function_output_length,
4260 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4261 TEST_LE_U( function_output_length,
4262 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004263 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004264 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004265
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004266 if( expected_status == PSA_SUCCESS )
4267 {
4268 PSA_ASSERT( psa_cipher_abort( &operation ) );
4269
4270 ASSERT_COMPARE( expected_output->x, expected_output->len,
4271 output, total_output_length );
4272 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004273
4274exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004275 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004276 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004277 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004278 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004279}
4280/* END_CASE */
4281
4282/* BEGIN_CASE */
4283void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004284 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004285 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004286 int first_part_size_arg,
4287 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004288 data_t *expected_output,
4289 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004290{
Ronald Cron5425a212020-08-04 14:58:35 +02004291 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004292 psa_key_type_t key_type = key_type_arg;
4293 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004294 psa_status_t status;
4295 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004296 size_t first_part_size = first_part_size_arg;
4297 size_t output1_length = output1_length_arg;
4298 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004299 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004300 size_t output_buffer_size = 0;
4301 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004302 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004303 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004304 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004305
Gilles Peskine8817f612018-12-18 00:18:46 +01004306 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004307
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004308 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4309 psa_set_key_algorithm( &attributes, alg );
4310 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004311
Ronald Cron5425a212020-08-04 14:58:35 +02004312 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4313 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004314
Ronald Cron5425a212020-08-04 14:58:35 +02004315 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316
Steven Cooreman177deba2020-09-07 17:14:14 +02004317 if( iv->len > 0 )
4318 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004319 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004320 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004321
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004322 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4323 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004324 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004325
Gilles Peskine7be11a72022-04-14 00:12:57 +02004326 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004327 PSA_ASSERT( psa_cipher_update( &operation,
4328 input->x, first_part_size,
4329 output, output_buffer_size,
4330 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004331 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004332 TEST_LE_U( function_output_length,
4333 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4334 TEST_LE_U( function_output_length,
4335 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004336 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004337
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004338 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004339 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004340 PSA_ASSERT( psa_cipher_update( &operation,
4341 input->x + first_part_size,
4342 input->len - first_part_size,
4343 ( output_buffer_size == 0 ? NULL :
4344 output + total_output_length ),
4345 output_buffer_size - total_output_length,
4346 &function_output_length ) );
4347 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004348 TEST_LE_U( function_output_length,
4349 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4350 alg,
4351 input->len - first_part_size ) );
4352 TEST_LE_U( function_output_length,
4353 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004354 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004355 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004356
Gilles Peskine50e586b2018-06-08 14:28:46 +02004357 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004358 ( output_buffer_size == 0 ? NULL :
4359 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004360 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004361 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004362 TEST_LE_U( function_output_length,
4363 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4364 TEST_LE_U( function_output_length,
4365 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004366 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004367 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004368
4369 if( expected_status == PSA_SUCCESS )
4370 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004371 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004372
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004373 ASSERT_COMPARE( expected_output->x, expected_output->len,
4374 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004375 }
4376
Gilles Peskine50e586b2018-06-08 14:28:46 +02004377exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004378 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004379 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004380 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004381 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004382}
4383/* END_CASE */
4384
Gilles Peskine50e586b2018-06-08 14:28:46 +02004385/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004386void cipher_decrypt_fail( int alg_arg,
4387 int key_type_arg,
4388 data_t *key_data,
4389 data_t *iv,
4390 data_t *input_arg,
4391 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004392{
4393 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4394 psa_status_t status;
4395 psa_key_type_t key_type = key_type_arg;
4396 psa_algorithm_t alg = alg_arg;
4397 psa_status_t expected_status = expected_status_arg;
4398 unsigned char *input = NULL;
4399 size_t input_buffer_size = 0;
4400 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004401 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004402 size_t output_buffer_size = 0;
4403 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004404 size_t function_output_length;
4405 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4407
4408 if ( PSA_ERROR_BAD_STATE != expected_status )
4409 {
4410 PSA_ASSERT( psa_crypto_init( ) );
4411
4412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4413 psa_set_key_algorithm( &attributes, alg );
4414 psa_set_key_type( &attributes, key_type );
4415
4416 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4417 &key ) );
4418 }
4419
4420 /* Allocate input buffer and copy the iv and the plaintext */
4421 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4422 if ( input_buffer_size > 0 )
4423 {
4424 ASSERT_ALLOC( input, input_buffer_size );
4425 memcpy( input, iv->x, iv->len );
4426 memcpy( input + iv->len, input_arg->x, input_arg->len );
4427 }
4428
4429 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4430 ASSERT_ALLOC( output, output_buffer_size );
4431
Neil Armstrong66a479f2022-02-07 15:41:19 +01004432 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004433 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4434 output_buffer_size, &output_length );
4435 TEST_EQUAL( status, expected_status );
4436
Neil Armstrong66a479f2022-02-07 15:41:19 +01004437 /* Decrypt, multi-part */
4438 status = psa_cipher_decrypt_setup( &operation, key, alg );
4439 if( status == PSA_SUCCESS )
4440 {
4441 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4442 input_arg->len ) +
4443 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4444 ASSERT_ALLOC( output_multi, output_buffer_size );
4445
4446 if( iv->len > 0 )
4447 {
4448 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4449
4450 if( status != PSA_SUCCESS )
4451 TEST_EQUAL( status, expected_status );
4452 }
4453
4454 if( status == PSA_SUCCESS )
4455 {
4456 status = psa_cipher_update( &operation,
4457 input_arg->x, input_arg->len,
4458 output_multi, output_buffer_size,
4459 &function_output_length );
4460 if( status == PSA_SUCCESS )
4461 {
4462 output_length = function_output_length;
4463
4464 status = psa_cipher_finish( &operation,
4465 output_multi + output_length,
4466 output_buffer_size - output_length,
4467 &function_output_length );
4468
4469 TEST_EQUAL( status, expected_status );
4470 }
4471 else
4472 {
4473 TEST_EQUAL( status, expected_status );
4474 }
4475 }
4476 else
4477 {
4478 TEST_EQUAL( status, expected_status );
4479 }
4480 }
4481 else
4482 {
4483 TEST_EQUAL( status, expected_status );
4484 }
4485
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004486exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004487 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004488 mbedtls_free( input );
4489 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004490 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004491 psa_destroy_key( key );
4492 PSA_DONE( );
4493}
4494/* END_CASE */
4495
4496/* BEGIN_CASE */
4497void cipher_decrypt( int alg_arg,
4498 int key_type_arg,
4499 data_t *key_data,
4500 data_t *iv,
4501 data_t *input_arg,
4502 data_t *expected_output )
4503{
4504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4505 psa_key_type_t key_type = key_type_arg;
4506 psa_algorithm_t alg = alg_arg;
4507 unsigned char *input = NULL;
4508 size_t input_buffer_size = 0;
4509 unsigned char *output = NULL;
4510 size_t output_buffer_size = 0;
4511 size_t output_length = 0;
4512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4513
4514 PSA_ASSERT( psa_crypto_init( ) );
4515
4516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4517 psa_set_key_algorithm( &attributes, alg );
4518 psa_set_key_type( &attributes, key_type );
4519
4520 /* Allocate input buffer and copy the iv and the plaintext */
4521 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4522 if ( input_buffer_size > 0 )
4523 {
4524 ASSERT_ALLOC( input, input_buffer_size );
4525 memcpy( input, iv->x, iv->len );
4526 memcpy( input + iv->len, input_arg->x, input_arg->len );
4527 }
4528
4529 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4530 ASSERT_ALLOC( output, output_buffer_size );
4531
4532 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4533 &key ) );
4534
4535 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4536 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004537 TEST_LE_U( output_length,
4538 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4539 TEST_LE_U( output_length,
4540 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004541
4542 ASSERT_COMPARE( expected_output->x, expected_output->len,
4543 output, output_length );
4544exit:
4545 mbedtls_free( input );
4546 mbedtls_free( output );
4547 psa_destroy_key( key );
4548 PSA_DONE( );
4549}
4550/* END_CASE */
4551
4552/* BEGIN_CASE */
4553void cipher_verify_output( int alg_arg,
4554 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004555 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004556 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004557{
Ronald Cron5425a212020-08-04 14:58:35 +02004558 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004559 psa_key_type_t key_type = key_type_arg;
4560 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004561 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004562 size_t output1_size = 0;
4563 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004564 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004565 size_t output2_size = 0;
4566 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004568
Gilles Peskine8817f612018-12-18 00:18:46 +01004569 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004570
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4572 psa_set_key_algorithm( &attributes, alg );
4573 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004574
Ronald Cron5425a212020-08-04 14:58:35 +02004575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4576 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004577 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004578 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004579
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004580 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4581 output1, output1_size,
4582 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004583 TEST_LE_U( output1_length,
4584 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4585 TEST_LE_U( output1_length,
4586 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004587
4588 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004589 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004590
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004591 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4592 output2, output2_size,
4593 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004594 TEST_LE_U( output2_length,
4595 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4596 TEST_LE_U( output2_length,
4597 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004598
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004599 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004600
4601exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004602 mbedtls_free( output1 );
4603 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004604 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004605 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004606}
4607/* END_CASE */
4608
4609/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004610void cipher_verify_output_multipart( int alg_arg,
4611 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004612 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004613 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004614 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004615{
Ronald Cron5425a212020-08-04 14:58:35 +02004616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004617 psa_key_type_t key_type = key_type_arg;
4618 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004619 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004620 unsigned char iv[16] = {0};
4621 size_t iv_size = 16;
4622 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004623 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004624 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004625 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004626 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004627 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004628 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004629 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004630 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4631 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004632 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004633
Gilles Peskine8817f612018-12-18 00:18:46 +01004634 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004635
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004636 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4637 psa_set_key_algorithm( &attributes, alg );
4638 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004639
Ronald Cron5425a212020-08-04 14:58:35 +02004640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4641 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004642
Ronald Cron5425a212020-08-04 14:58:35 +02004643 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4644 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004645
Steven Cooreman177deba2020-09-07 17:14:14 +02004646 if( alg != PSA_ALG_ECB_NO_PADDING )
4647 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004648 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4649 iv, iv_size,
4650 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004651 }
4652
gabor-mezei-armceface22021-01-21 12:26:17 +01004653 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004654 TEST_LE_U( output1_buffer_size,
4655 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004656 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004657
Gilles Peskine7be11a72022-04-14 00:12:57 +02004658 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004659
Gilles Peskine8817f612018-12-18 00:18:46 +01004660 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4661 output1, output1_buffer_size,
4662 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004663 TEST_LE_U( function_output_length,
4664 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4665 TEST_LE_U( function_output_length,
4666 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004667 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004668
Gilles Peskine8817f612018-12-18 00:18:46 +01004669 PSA_ASSERT( psa_cipher_update( &operation1,
4670 input->x + first_part_size,
4671 input->len - first_part_size,
4672 output1, output1_buffer_size,
4673 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004674 TEST_LE_U( function_output_length,
4675 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4676 alg,
4677 input->len - first_part_size ) );
4678 TEST_LE_U( function_output_length,
4679 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004680 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004681
Gilles Peskine8817f612018-12-18 00:18:46 +01004682 PSA_ASSERT( psa_cipher_finish( &operation1,
4683 output1 + output1_length,
4684 output1_buffer_size - output1_length,
4685 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004686 TEST_LE_U( function_output_length,
4687 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4688 TEST_LE_U( function_output_length,
4689 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004690 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004691
Gilles Peskine8817f612018-12-18 00:18:46 +01004692 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004693
Gilles Peskine048b7f02018-06-08 14:20:49 +02004694 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004695 TEST_LE_U( output2_buffer_size,
4696 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4697 TEST_LE_U( output2_buffer_size,
4698 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004699 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004700
Steven Cooreman177deba2020-09-07 17:14:14 +02004701 if( iv_length > 0 )
4702 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004703 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4704 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004705 }
Moran Pekerded84402018-06-06 16:36:50 +03004706
Gilles Peskine8817f612018-12-18 00:18:46 +01004707 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4708 output2, output2_buffer_size,
4709 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004710 TEST_LE_U( function_output_length,
4711 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4712 TEST_LE_U( function_output_length,
4713 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004714 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004715
Gilles Peskine8817f612018-12-18 00:18:46 +01004716 PSA_ASSERT( psa_cipher_update( &operation2,
4717 output1 + first_part_size,
4718 output1_length - first_part_size,
4719 output2, output2_buffer_size,
4720 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004721 TEST_LE_U( function_output_length,
4722 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4723 alg,
4724 output1_length - first_part_size ) );
4725 TEST_LE_U( function_output_length,
4726 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004727 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004728
Gilles Peskine8817f612018-12-18 00:18:46 +01004729 PSA_ASSERT( psa_cipher_finish( &operation2,
4730 output2 + output2_length,
4731 output2_buffer_size - output2_length,
4732 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004733 TEST_LE_U( function_output_length,
4734 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4735 TEST_LE_U( function_output_length,
4736 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004737 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004738
Gilles Peskine8817f612018-12-18 00:18:46 +01004739 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004740
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004741 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004742
4743exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004744 psa_cipher_abort( &operation1 );
4745 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004746 mbedtls_free( output1 );
4747 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004749 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004750}
4751/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004752
Gilles Peskine20035e32018-02-03 22:44:14 +01004753/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004754void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004755 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004756 data_t *nonce,
4757 data_t *additional_data,
4758 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004759 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004760{
Ronald Cron5425a212020-08-04 14:58:35 +02004761 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004762 psa_key_type_t key_type = key_type_arg;
4763 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004764 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004765 unsigned char *output_data = NULL;
4766 size_t output_size = 0;
4767 size_t output_length = 0;
4768 unsigned char *output_data2 = NULL;
4769 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004770 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004771 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004772 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004773
Gilles Peskine8817f612018-12-18 00:18:46 +01004774 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004776 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4777 psa_set_key_algorithm( &attributes, alg );
4778 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004779
Gilles Peskine049c7532019-05-15 20:22:09 +02004780 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004781 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004782 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4783 key_bits = psa_get_key_bits( &attributes );
4784
4785 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4786 alg );
4787 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4788 * should be exact. */
4789 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4790 expected_result != PSA_ERROR_NOT_SUPPORTED )
4791 {
4792 TEST_EQUAL( output_size,
4793 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004794 TEST_LE_U( output_size,
4795 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004796 }
4797 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004798
Steven Cooremanf49478b2021-02-15 15:19:25 +01004799 status = psa_aead_encrypt( key, alg,
4800 nonce->x, nonce->len,
4801 additional_data->x,
4802 additional_data->len,
4803 input_data->x, input_data->len,
4804 output_data, output_size,
4805 &output_length );
4806
4807 /* If the operation is not supported, just skip and not fail in case the
4808 * encryption involves a common limitation of cryptography hardwares and
4809 * an alternative implementation. */
4810 if( status == PSA_ERROR_NOT_SUPPORTED )
4811 {
4812 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4813 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4814 }
4815
4816 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817
4818 if( PSA_SUCCESS == expected_result )
4819 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004820 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821
Gilles Peskine003a4a92019-05-14 16:09:40 +02004822 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4823 * should be exact. */
4824 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004825 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004826
Gilles Peskine7be11a72022-04-14 00:12:57 +02004827 TEST_LE_U( input_data->len,
4828 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004829
Ronald Cron5425a212020-08-04 14:58:35 +02004830 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004831 nonce->x, nonce->len,
4832 additional_data->x,
4833 additional_data->len,
4834 output_data, output_length,
4835 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004836 &output_length2 ),
4837 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004838
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004839 ASSERT_COMPARE( input_data->x, input_data->len,
4840 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004841 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004842
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004844 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004845 mbedtls_free( output_data );
4846 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004847 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848}
4849/* END_CASE */
4850
4851/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004852void aead_encrypt( int key_type_arg, data_t *key_data,
4853 int alg_arg,
4854 data_t *nonce,
4855 data_t *additional_data,
4856 data_t *input_data,
4857 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004858{
Ronald Cron5425a212020-08-04 14:58:35 +02004859 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004860 psa_key_type_t key_type = key_type_arg;
4861 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004862 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863 unsigned char *output_data = NULL;
4864 size_t output_size = 0;
4865 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004866 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004867 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004868
Gilles Peskine8817f612018-12-18 00:18:46 +01004869 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004871 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4872 psa_set_key_algorithm( &attributes, alg );
4873 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004874
Gilles Peskine049c7532019-05-15 20:22:09 +02004875 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004876 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004877 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4878 key_bits = psa_get_key_bits( &attributes );
4879
4880 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4881 alg );
4882 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4883 * should be exact. */
4884 TEST_EQUAL( output_size,
4885 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004886 TEST_LE_U( output_size,
4887 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004888 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004889
Steven Cooremand588ea12021-01-11 19:36:04 +01004890 status = psa_aead_encrypt( key, alg,
4891 nonce->x, nonce->len,
4892 additional_data->x, additional_data->len,
4893 input_data->x, input_data->len,
4894 output_data, output_size,
4895 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004896
Ronald Cron28a45ed2021-02-09 20:35:42 +01004897 /* If the operation is not supported, just skip and not fail in case the
4898 * encryption involves a common limitation of cryptography hardwares and
4899 * an alternative implementation. */
4900 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004901 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004902 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4903 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004904 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004905
4906 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004907 ASSERT_COMPARE( expected_result->x, expected_result->len,
4908 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004909
Gilles Peskinea1cac842018-06-11 19:33:02 +02004910exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004911 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004912 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004913 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004914}
4915/* END_CASE */
4916
4917/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004918void aead_decrypt( int key_type_arg, data_t *key_data,
4919 int alg_arg,
4920 data_t *nonce,
4921 data_t *additional_data,
4922 data_t *input_data,
4923 data_t *expected_data,
4924 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004925{
Ronald Cron5425a212020-08-04 14:58:35 +02004926 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004927 psa_key_type_t key_type = key_type_arg;
4928 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004929 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004930 unsigned char *output_data = NULL;
4931 size_t output_size = 0;
4932 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004934 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004935 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004936
Gilles Peskine8817f612018-12-18 00:18:46 +01004937 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004938
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004939 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4940 psa_set_key_algorithm( &attributes, alg );
4941 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004942
Gilles Peskine049c7532019-05-15 20:22:09 +02004943 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004944 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004945 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4946 key_bits = psa_get_key_bits( &attributes );
4947
4948 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4949 alg );
4950 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4951 expected_result != PSA_ERROR_NOT_SUPPORTED )
4952 {
4953 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4954 * should be exact. */
4955 TEST_EQUAL( output_size,
4956 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004957 TEST_LE_U( output_size,
4958 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004959 }
4960 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004961
Steven Cooremand588ea12021-01-11 19:36:04 +01004962 status = psa_aead_decrypt( key, alg,
4963 nonce->x, nonce->len,
4964 additional_data->x,
4965 additional_data->len,
4966 input_data->x, input_data->len,
4967 output_data, output_size,
4968 &output_length );
4969
Ronald Cron28a45ed2021-02-09 20:35:42 +01004970 /* If the operation is not supported, just skip and not fail in case the
4971 * decryption involves a common limitation of cryptography hardwares and
4972 * an alternative implementation. */
4973 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004974 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004975 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4976 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004977 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004978
4979 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004980
Gilles Peskine2d277862018-06-18 15:41:12 +02004981 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004982 ASSERT_COMPARE( expected_data->x, expected_data->len,
4983 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004984
Gilles Peskinea1cac842018-06-11 19:33:02 +02004985exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004986 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004987 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004988 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004989}
4990/* END_CASE */
4991
4992/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004993void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4994 int alg_arg,
4995 data_t *nonce,
4996 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004997 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004998 int do_set_lengths,
4999 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005000{
Paul Elliottd3f82412021-06-16 16:52:21 +01005001 size_t ad_part_len = 0;
5002 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005003 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005004
Paul Elliott32f46ba2021-09-23 18:24:36 +01005005 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005006 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005007 mbedtls_test_set_step( ad_part_len );
5008
5009 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005010 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005011 if( ad_part_len & 0x01 )
5012 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5013 else
5014 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005015 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005016
5017 /* Split ad into length(ad_part_len) parts. */
5018 if( !aead_multipart_internal_func( key_type_arg, key_data,
5019 alg_arg, nonce,
5020 additional_data,
5021 ad_part_len,
5022 input_data, -1,
5023 set_lengths_method,
5024 expected_output,
5025 1, 0 ) )
5026 break;
5027
5028 /* length(0) part, length(ad_part_len) part, length(0) part... */
5029 mbedtls_test_set_step( 1000 + ad_part_len );
5030
5031 if( !aead_multipart_internal_func( key_type_arg, key_data,
5032 alg_arg, nonce,
5033 additional_data,
5034 ad_part_len,
5035 input_data, -1,
5036 set_lengths_method,
5037 expected_output,
5038 1, 1 ) )
5039 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005040 }
Paul Elliottd3f82412021-06-16 16:52:21 +01005041
Paul Elliott32f46ba2021-09-23 18:24:36 +01005042 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005043 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005044 /* Split data into length(data_part_len) parts. */
5045 mbedtls_test_set_step( 2000 + data_part_len );
5046
5047 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005048 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005049 if( data_part_len & 0x01 )
5050 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5051 else
5052 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005053 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005054
Paul Elliott32f46ba2021-09-23 18:24:36 +01005055 if( !aead_multipart_internal_func( key_type_arg, key_data,
5056 alg_arg, nonce,
5057 additional_data, -1,
5058 input_data, data_part_len,
5059 set_lengths_method,
5060 expected_output,
5061 1, 0 ) )
5062 break;
5063
5064 /* length(0) part, length(data_part_len) part, length(0) part... */
5065 mbedtls_test_set_step( 3000 + data_part_len );
5066
5067 if( !aead_multipart_internal_func( key_type_arg, key_data,
5068 alg_arg, nonce,
5069 additional_data, -1,
5070 input_data, data_part_len,
5071 set_lengths_method,
5072 expected_output,
5073 1, 1 ) )
5074 break;
5075 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005076
Paul Elliott8fc45162021-06-23 16:06:01 +01005077 /* Goto is required to silence warnings about unused labels, as we
5078 * don't actually do any test assertions in this function. */
5079 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005080}
5081/* END_CASE */
5082
5083/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005084void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5085 int alg_arg,
5086 data_t *nonce,
5087 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005088 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005089 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005090 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005091{
Paul Elliottd3f82412021-06-16 16:52:21 +01005092 size_t ad_part_len = 0;
5093 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005094 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005095
Paul Elliott32f46ba2021-09-23 18:24:36 +01005096 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005097 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005098 /* Split ad into length(ad_part_len) parts. */
5099 mbedtls_test_set_step( ad_part_len );
5100
5101 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005102 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005103 if( ad_part_len & 0x01 )
5104 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5105 else
5106 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005107 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005108
5109 if( !aead_multipart_internal_func( key_type_arg, key_data,
5110 alg_arg, nonce,
5111 additional_data,
5112 ad_part_len,
5113 input_data, -1,
5114 set_lengths_method,
5115 expected_output,
5116 0, 0 ) )
5117 break;
5118
5119 /* length(0) part, length(ad_part_len) part, length(0) part... */
5120 mbedtls_test_set_step( 1000 + ad_part_len );
5121
5122 if( !aead_multipart_internal_func( key_type_arg, key_data,
5123 alg_arg, nonce,
5124 additional_data,
5125 ad_part_len,
5126 input_data, -1,
5127 set_lengths_method,
5128 expected_output,
5129 0, 1 ) )
5130 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005131 }
5132
Paul Elliott32f46ba2021-09-23 18:24:36 +01005133 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005134 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005135 /* Split data into length(data_part_len) parts. */
5136 mbedtls_test_set_step( 2000 + data_part_len );
5137
5138 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005139 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005140 if( data_part_len & 0x01 )
5141 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5142 else
5143 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005144 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005145
5146 if( !aead_multipart_internal_func( key_type_arg, key_data,
5147 alg_arg, nonce,
5148 additional_data, -1,
5149 input_data, data_part_len,
5150 set_lengths_method,
5151 expected_output,
5152 0, 0 ) )
5153 break;
5154
5155 /* length(0) part, length(data_part_len) part, length(0) part... */
5156 mbedtls_test_set_step( 3000 + data_part_len );
5157
5158 if( !aead_multipart_internal_func( key_type_arg, key_data,
5159 alg_arg, nonce,
5160 additional_data, -1,
5161 input_data, data_part_len,
5162 set_lengths_method,
5163 expected_output,
5164 0, 1 ) )
5165 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005166 }
5167
Paul Elliott8fc45162021-06-23 16:06:01 +01005168 /* Goto is required to silence warnings about unused labels, as we
5169 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005170 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005171}
5172/* END_CASE */
5173
5174/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005175void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5176 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005177 int nonce_length,
5178 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005179 data_t *additional_data,
5180 data_t *input_data,
5181 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005182{
5183
5184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5185 psa_key_type_t key_type = key_type_arg;
5186 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005187 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005188 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5190 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005191 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005192 size_t actual_nonce_length = 0;
5193 size_t expected_nonce_length = expected_nonce_length_arg;
5194 unsigned char *output = NULL;
5195 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005196 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005197 size_t ciphertext_size = 0;
5198 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005199 size_t tag_length = 0;
5200 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005201
5202 PSA_ASSERT( psa_crypto_init( ) );
5203
5204 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5205 psa_set_key_algorithm( & attributes, alg );
5206 psa_set_key_type( & attributes, key_type );
5207
5208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5209 &key ) );
5210
5211 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5212
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005213 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5214
Paul Elliottf1277632021-08-24 18:11:37 +01005215 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005216
Paul Elliottf1277632021-08-24 18:11:37 +01005217 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005218
Gilles Peskine7be11a72022-04-14 00:12:57 +02005219 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005220
Paul Elliottf1277632021-08-24 18:11:37 +01005221 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005222
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005223 status = psa_aead_encrypt_setup( &operation, key, alg );
5224
5225 /* If the operation is not supported, just skip and not fail in case the
5226 * encryption involves a common limitation of cryptography hardwares and
5227 * an alternative implementation. */
5228 if( status == PSA_ERROR_NOT_SUPPORTED )
5229 {
5230 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005231 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005232 }
5233
5234 PSA_ASSERT( status );
5235
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005236 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005237 nonce_length,
5238 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005239
Paul Elliott693bf312021-07-23 17:40:41 +01005240 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005241
Paul Elliottf1277632021-08-24 18:11:37 +01005242 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005243
Paul Elliott88ecbe12021-09-22 17:23:03 +01005244 if( expected_status == PSA_SUCCESS )
5245 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5246 alg ) );
5247
Gilles Peskine7be11a72022-04-14 00:12:57 +02005248 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005249
Paul Elliott693bf312021-07-23 17:40:41 +01005250 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005251 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005252 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005253 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5254 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005255
5256 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5257 additional_data->len ) );
5258
5259 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005260 output, output_size,
5261 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005262
Paul Elliottf1277632021-08-24 18:11:37 +01005263 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5264 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005265 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5266 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005267
5268exit:
5269 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005270 mbedtls_free( output );
5271 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005272 psa_aead_abort( &operation );
5273 PSA_DONE( );
5274}
5275/* END_CASE */
5276
5277/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005278void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5279 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005280 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005281 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005282 data_t *additional_data,
5283 data_t *input_data,
5284 int expected_status_arg )
5285{
5286
5287 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5288 psa_key_type_t key_type = key_type_arg;
5289 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005290 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005291 uint8_t *nonce_buffer = NULL;
5292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5293 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5294 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005295 unsigned char *output = NULL;
5296 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005297 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005298 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005299 size_t ciphertext_size = 0;
5300 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005301 size_t tag_length = 0;
5302 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005303 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005304 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005305
5306 PSA_ASSERT( psa_crypto_init( ) );
5307
5308 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5309 psa_set_key_algorithm( &attributes, alg );
5310 psa_set_key_type( &attributes, key_type );
5311
5312 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5313 &key ) );
5314
5315 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5316
5317 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5318
Paul Elliott6f0e7202021-08-25 12:57:18 +01005319 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005320
Paul Elliott6f0e7202021-08-25 12:57:18 +01005321 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005322
Gilles Peskine7be11a72022-04-14 00:12:57 +02005323 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005324
Paul Elliott6f0e7202021-08-25 12:57:18 +01005325 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005326
Paul Elliott863864a2021-07-23 17:28:31 +01005327 status = psa_aead_encrypt_setup( &operation, key, alg );
5328
5329 /* If the operation is not supported, just skip and not fail in case the
5330 * encryption involves a common limitation of cryptography hardwares and
5331 * an alternative implementation. */
5332 if( status == PSA_ERROR_NOT_SUPPORTED )
5333 {
5334 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005335 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005336 }
5337
5338 PSA_ASSERT( status );
5339
Paul Elliott4023ffd2021-09-10 16:21:22 +01005340 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5341 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005342 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005343 /* Arbitrary size buffer, to test zero length valid buffer. */
5344 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005345 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005346 }
5347 else
5348 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005349 /* If length is zero, then this will return NULL. */
5350 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005351 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005352
Paul Elliott4023ffd2021-09-10 16:21:22 +01005353 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005354 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005355 for( index = 0; index < nonce_length - 1; ++index )
5356 {
5357 nonce_buffer[index] = 'a' + index;
5358 }
Paul Elliott66696b52021-08-16 18:42:41 +01005359 }
Paul Elliott863864a2021-07-23 17:28:31 +01005360 }
5361
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005362 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5363 {
5364 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5365 input_data->len ) );
5366 }
5367
Paul Elliott6f0e7202021-08-25 12:57:18 +01005368 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005369
Paul Elliott693bf312021-07-23 17:40:41 +01005370 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005371
5372 if( expected_status == PSA_SUCCESS )
5373 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005374 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5375 {
5376 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5377 input_data->len ) );
5378 }
5379 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5380 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005381
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005382 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5383 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5384 additional_data->len ),
5385 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005386
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005387 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005388 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005389 &ciphertext_length ),
5390 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005391
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005392 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005393 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005394 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5395 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005396 }
5397
5398exit:
5399 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005400 mbedtls_free( output );
5401 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005402 mbedtls_free( nonce_buffer );
5403 psa_aead_abort( &operation );
5404 PSA_DONE( );
5405}
5406/* END_CASE */
5407
5408/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005409void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5410 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005411 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005412 data_t *nonce,
5413 data_t *additional_data,
5414 data_t *input_data,
5415 int expected_status_arg )
5416{
5417
5418 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5419 psa_key_type_t key_type = key_type_arg;
5420 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005421 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005422 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5423 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5424 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005425 unsigned char *output = NULL;
5426 unsigned char *ciphertext = NULL;
5427 size_t output_size = output_size_arg;
5428 size_t ciphertext_size = 0;
5429 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005430 size_t tag_length = 0;
5431 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5432
5433 PSA_ASSERT( psa_crypto_init( ) );
5434
5435 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5436 psa_set_key_algorithm( &attributes, alg );
5437 psa_set_key_type( &attributes, key_type );
5438
5439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5440 &key ) );
5441
5442 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5443
Paul Elliottc6d11d02021-09-01 12:04:23 +01005444 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005445
Paul Elliottc6d11d02021-09-01 12:04:23 +01005446 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005447
Paul Elliottc6d11d02021-09-01 12:04:23 +01005448 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005449
Paul Elliott43fbda62021-07-23 18:30:59 +01005450 status = psa_aead_encrypt_setup( &operation, key, alg );
5451
5452 /* If the operation is not supported, just skip and not fail in case the
5453 * encryption involves a common limitation of cryptography hardwares and
5454 * an alternative implementation. */
5455 if( status == PSA_ERROR_NOT_SUPPORTED )
5456 {
5457 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5458 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5459 }
5460
5461 PSA_ASSERT( status );
5462
Paul Elliott47b9a142021-10-07 15:04:57 +01005463 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5464 input_data->len ) );
5465
Paul Elliott43fbda62021-07-23 18:30:59 +01005466 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5467
5468 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5469 additional_data->len ) );
5470
5471 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005472 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005473
5474 TEST_EQUAL( status, expected_status );
5475
5476 if( expected_status == PSA_SUCCESS )
5477 {
5478 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005479 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5480 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005481 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5482 }
5483
5484exit:
5485 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005486 mbedtls_free( output );
5487 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005488 psa_aead_abort( &operation );
5489 PSA_DONE( );
5490}
5491/* END_CASE */
5492
Paul Elliott91b021e2021-07-23 18:52:31 +01005493/* BEGIN_CASE */
5494void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5495 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005496 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005497 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005498 data_t *nonce,
5499 data_t *additional_data,
5500 data_t *input_data,
5501 int expected_status_arg )
5502{
5503
5504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5505 psa_key_type_t key_type = key_type_arg;
5506 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005507 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5509 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5510 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005511 unsigned char *ciphertext = NULL;
5512 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005513 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005514 size_t ciphertext_size = 0;
5515 size_t ciphertext_length = 0;
5516 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005517 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005518 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005519
5520 PSA_ASSERT( psa_crypto_init( ) );
5521
5522 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5523 psa_set_key_algorithm( &attributes, alg );
5524 psa_set_key_type( &attributes, key_type );
5525
5526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5527 &key ) );
5528
5529 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5530
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005531 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005532
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005533 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005534
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005535 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005536
Paul Elliott719c1322021-09-13 18:27:22 +01005537 ASSERT_ALLOC( tag_buffer, tag_size );
5538
Paul Elliott91b021e2021-07-23 18:52:31 +01005539 status = psa_aead_encrypt_setup( &operation, key, alg );
5540
5541 /* If the operation is not supported, just skip and not fail in case the
5542 * encryption involves a common limitation of cryptography hardwares and
5543 * an alternative implementation. */
5544 if( status == PSA_ERROR_NOT_SUPPORTED )
5545 {
5546 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5547 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5548 }
5549
5550 PSA_ASSERT( status );
5551
5552 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5553
Paul Elliott76bda482021-10-07 17:07:23 +01005554 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5555 input_data->len ) );
5556
Paul Elliott91b021e2021-07-23 18:52:31 +01005557 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5558 additional_data->len ) );
5559
5560 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005561 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005562
5563 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005564 status = psa_aead_finish( &operation, finish_ciphertext,
5565 finish_ciphertext_size,
5566 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005567 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005568
5569 TEST_EQUAL( status, expected_status );
5570
5571exit:
5572 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005573 mbedtls_free( ciphertext );
5574 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005575 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005576 psa_aead_abort( &operation );
5577 PSA_DONE( );
5578}
5579/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005580
5581/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005582void aead_multipart_verify( int key_type_arg, data_t *key_data,
5583 int alg_arg,
5584 data_t *nonce,
5585 data_t *additional_data,
5586 data_t *input_data,
5587 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005588 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005589 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005590 int expected_status_arg )
5591{
5592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5593 psa_key_type_t key_type = key_type_arg;
5594 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005595 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005596 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5597 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5598 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005599 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005600 unsigned char *plaintext = NULL;
5601 unsigned char *finish_plaintext = NULL;
5602 size_t plaintext_size = 0;
5603 size_t plaintext_length = 0;
5604 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005605 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005606 unsigned char *tag_buffer = NULL;
5607 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005608
5609 PSA_ASSERT( psa_crypto_init( ) );
5610
5611 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5612 psa_set_key_algorithm( &attributes, alg );
5613 psa_set_key_type( &attributes, key_type );
5614
5615 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5616 &key ) );
5617
5618 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5619
5620 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5621 input_data->len );
5622
5623 ASSERT_ALLOC( plaintext, plaintext_size );
5624
5625 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5626
5627 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5628
Paul Elliott9961a662021-09-17 19:19:02 +01005629 status = psa_aead_decrypt_setup( &operation, key, alg );
5630
5631 /* If the operation is not supported, just skip and not fail in case the
5632 * encryption involves a common limitation of cryptography hardwares and
5633 * an alternative implementation. */
5634 if( status == PSA_ERROR_NOT_SUPPORTED )
5635 {
5636 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5637 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5638 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005639 TEST_EQUAL( status, expected_setup_status );
5640
5641 if( status != PSA_SUCCESS )
5642 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005643
5644 PSA_ASSERT( status );
5645
5646 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5647
Paul Elliottfec6f372021-10-06 17:15:02 +01005648 status = psa_aead_set_lengths( &operation, additional_data->len,
5649 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005650 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005651
Paul Elliott9961a662021-09-17 19:19:02 +01005652 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5653 additional_data->len ) );
5654
5655 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5656 input_data->len,
5657 plaintext, plaintext_size,
5658 &plaintext_length ) );
5659
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005660 if( tag_usage == USE_GIVEN_TAG )
5661 {
5662 tag_buffer = tag->x;
5663 tag_size = tag->len;
5664 }
5665
Paul Elliott9961a662021-09-17 19:19:02 +01005666 status = psa_aead_verify( &operation, finish_plaintext,
5667 verify_plaintext_size,
5668 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005669 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005670
5671 TEST_EQUAL( status, expected_status );
5672
5673exit:
5674 psa_destroy_key( key );
5675 mbedtls_free( plaintext );
5676 mbedtls_free( finish_plaintext );
5677 psa_aead_abort( &operation );
5678 PSA_DONE( );
5679}
5680/* END_CASE */
5681
Paul Elliott9961a662021-09-17 19:19:02 +01005682/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005683void aead_multipart_setup( int key_type_arg, data_t *key_data,
5684 int alg_arg, int expected_status_arg )
5685{
5686 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5687 psa_key_type_t key_type = key_type_arg;
5688 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005689 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5691 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5692 psa_status_t expected_status = expected_status_arg;
5693
5694 PSA_ASSERT( psa_crypto_init( ) );
5695
5696 psa_set_key_usage_flags( &attributes,
5697 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5698 psa_set_key_algorithm( &attributes, alg );
5699 psa_set_key_type( &attributes, key_type );
5700
5701 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5702 &key ) );
5703
Paul Elliott5221ef62021-09-19 17:33:03 +01005704 status = psa_aead_encrypt_setup( &operation, key, alg );
5705
5706 TEST_EQUAL( status, expected_status );
5707
5708 psa_aead_abort( &operation );
5709
Paul Elliott5221ef62021-09-19 17:33:03 +01005710 status = psa_aead_decrypt_setup( &operation, key, alg );
5711
5712 TEST_EQUAL(status, expected_status );
5713
5714exit:
5715 psa_destroy_key( key );
5716 psa_aead_abort( &operation );
5717 PSA_DONE( );
5718}
5719/* END_CASE */
5720
5721/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005722void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5723 int alg_arg,
5724 data_t *nonce,
5725 data_t *additional_data,
5726 data_t *input_data )
5727{
5728 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5729 psa_key_type_t key_type = key_type_arg;
5730 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005731 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732 unsigned char *output_data = NULL;
5733 unsigned char *final_data = NULL;
5734 size_t output_size = 0;
5735 size_t finish_output_size = 0;
5736 size_t output_length = 0;
5737 size_t key_bits = 0;
5738 size_t tag_length = 0;
5739 size_t tag_size = 0;
5740 size_t nonce_length = 0;
5741 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5742 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5743 size_t output_part_length = 0;
5744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5745
5746 PSA_ASSERT( psa_crypto_init( ) );
5747
5748 psa_set_key_usage_flags( & attributes,
5749 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5750 psa_set_key_algorithm( & attributes, alg );
5751 psa_set_key_type( & attributes, key_type );
5752
5753 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5754 &key ) );
5755
5756 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5757 key_bits = psa_get_key_bits( &attributes );
5758
5759 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5760
Gilles Peskine7be11a72022-04-14 00:12:57 +02005761 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005762
5763 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5764
5765 ASSERT_ALLOC( output_data, output_size );
5766
5767 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5768
Gilles Peskine7be11a72022-04-14 00:12:57 +02005769 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005770
5771 ASSERT_ALLOC( final_data, finish_output_size );
5772
5773 /* Test all operations error without calling setup first. */
5774
Paul Elliottc23a9a02021-06-21 18:32:46 +01005775 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5776 PSA_ERROR_BAD_STATE );
5777
5778 psa_aead_abort( &operation );
5779
Paul Elliottc23a9a02021-06-21 18:32:46 +01005780 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5781 PSA_AEAD_NONCE_MAX_SIZE,
5782 &nonce_length ),
5783 PSA_ERROR_BAD_STATE );
5784
5785 psa_aead_abort( &operation );
5786
Paul Elliott481be342021-07-16 17:38:47 +01005787 /* ------------------------------------------------------- */
5788
Paul Elliottc23a9a02021-06-21 18:32:46 +01005789 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5790 input_data->len ),
5791 PSA_ERROR_BAD_STATE );
5792
5793 psa_aead_abort( &operation );
5794
Paul Elliott481be342021-07-16 17:38:47 +01005795 /* ------------------------------------------------------- */
5796
Paul Elliottc23a9a02021-06-21 18:32:46 +01005797 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5798 additional_data->len ),
5799 PSA_ERROR_BAD_STATE );
5800
5801 psa_aead_abort( &operation );
5802
Paul Elliott481be342021-07-16 17:38:47 +01005803 /* ------------------------------------------------------- */
5804
Paul Elliottc23a9a02021-06-21 18:32:46 +01005805 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5806 input_data->len, output_data,
5807 output_size, &output_length ),
5808 PSA_ERROR_BAD_STATE );
5809
5810 psa_aead_abort( &operation );
5811
Paul Elliott481be342021-07-16 17:38:47 +01005812 /* ------------------------------------------------------- */
5813
Paul Elliottc23a9a02021-06-21 18:32:46 +01005814 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5815 finish_output_size,
5816 &output_part_length,
5817 tag_buffer, tag_length,
5818 &tag_size ),
5819 PSA_ERROR_BAD_STATE );
5820
5821 psa_aead_abort( &operation );
5822
Paul Elliott481be342021-07-16 17:38:47 +01005823 /* ------------------------------------------------------- */
5824
Paul Elliottc23a9a02021-06-21 18:32:46 +01005825 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5826 finish_output_size,
5827 &output_part_length,
5828 tag_buffer,
5829 tag_length ),
5830 PSA_ERROR_BAD_STATE );
5831
5832 psa_aead_abort( &operation );
5833
5834 /* Test for double setups. */
5835
Paul Elliottc23a9a02021-06-21 18:32:46 +01005836 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5837
5838 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5839 PSA_ERROR_BAD_STATE );
5840
5841 psa_aead_abort( &operation );
5842
Paul Elliott481be342021-07-16 17:38:47 +01005843 /* ------------------------------------------------------- */
5844
Paul Elliottc23a9a02021-06-21 18:32:46 +01005845 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5846
5847 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5848 PSA_ERROR_BAD_STATE );
5849
5850 psa_aead_abort( &operation );
5851
Paul Elliott374a2be2021-07-16 17:53:40 +01005852 /* ------------------------------------------------------- */
5853
Paul Elliott374a2be2021-07-16 17:53:40 +01005854 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5855
5856 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5857 PSA_ERROR_BAD_STATE );
5858
5859 psa_aead_abort( &operation );
5860
5861 /* ------------------------------------------------------- */
5862
Paul Elliott374a2be2021-07-16 17:53:40 +01005863 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5864
5865 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5866 PSA_ERROR_BAD_STATE );
5867
5868 psa_aead_abort( &operation );
5869
Paul Elliottc23a9a02021-06-21 18:32:46 +01005870 /* Test for not setting a nonce. */
5871
Paul Elliottc23a9a02021-06-21 18:32:46 +01005872 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5873
5874 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5875 additional_data->len ),
5876 PSA_ERROR_BAD_STATE );
5877
5878 psa_aead_abort( &operation );
5879
Paul Elliott7f628422021-09-01 12:08:29 +01005880 /* ------------------------------------------------------- */
5881
Paul Elliott7f628422021-09-01 12:08:29 +01005882 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5883
5884 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5885 input_data->len, output_data,
5886 output_size, &output_length ),
5887 PSA_ERROR_BAD_STATE );
5888
5889 psa_aead_abort( &operation );
5890
Paul Elliottbdc2c682021-09-21 18:37:10 +01005891 /* ------------------------------------------------------- */
5892
Paul Elliottbdc2c682021-09-21 18:37:10 +01005893 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5894
5895 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5896 finish_output_size,
5897 &output_part_length,
5898 tag_buffer, tag_length,
5899 &tag_size ),
5900 PSA_ERROR_BAD_STATE );
5901
5902 psa_aead_abort( &operation );
5903
5904 /* ------------------------------------------------------- */
5905
Paul Elliottbdc2c682021-09-21 18:37:10 +01005906 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5907
5908 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5909 finish_output_size,
5910 &output_part_length,
5911 tag_buffer,
5912 tag_length ),
5913 PSA_ERROR_BAD_STATE );
5914
5915 psa_aead_abort( &operation );
5916
Paul Elliottc23a9a02021-06-21 18:32:46 +01005917 /* Test for double setting nonce. */
5918
Paul Elliottc23a9a02021-06-21 18:32:46 +01005919 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5920
5921 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5922
5923 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5924 PSA_ERROR_BAD_STATE );
5925
5926 psa_aead_abort( &operation );
5927
Paul Elliott374a2be2021-07-16 17:53:40 +01005928 /* Test for double generating nonce. */
5929
Paul Elliott374a2be2021-07-16 17:53:40 +01005930 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5931
5932 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5933 PSA_AEAD_NONCE_MAX_SIZE,
5934 &nonce_length ) );
5935
5936 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5937 PSA_AEAD_NONCE_MAX_SIZE,
5938 &nonce_length ),
5939 PSA_ERROR_BAD_STATE );
5940
5941
5942 psa_aead_abort( &operation );
5943
5944 /* Test for generate nonce then set and vice versa */
5945
Paul Elliott374a2be2021-07-16 17:53:40 +01005946 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5947
5948 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5949 PSA_AEAD_NONCE_MAX_SIZE,
5950 &nonce_length ) );
5951
5952 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5953 PSA_ERROR_BAD_STATE );
5954
5955 psa_aead_abort( &operation );
5956
Andrzej Kurekad837522021-12-15 15:28:49 +01005957 /* Test for generating nonce after calling set lengths */
5958
5959 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5960
5961 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5962 input_data->len ) );
5963
5964 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5965 PSA_AEAD_NONCE_MAX_SIZE,
5966 &nonce_length ) );
5967
5968 psa_aead_abort( &operation );
5969
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005970 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005971
5972 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5973
5974 if( operation.alg == PSA_ALG_CCM )
5975 {
5976 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5977 input_data->len ),
5978 PSA_ERROR_INVALID_ARGUMENT );
5979 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5980 PSA_AEAD_NONCE_MAX_SIZE,
5981 &nonce_length ),
5982 PSA_ERROR_BAD_STATE );
5983 }
5984 else
5985 {
5986 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5987 input_data->len ) );
5988 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5989 PSA_AEAD_NONCE_MAX_SIZE,
5990 &nonce_length ) );
5991 }
5992
5993 psa_aead_abort( &operation );
5994
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005995 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005996#if SIZE_MAX > UINT32_MAX
5997 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5998
5999 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6000 {
6001 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6002 input_data->len ),
6003 PSA_ERROR_INVALID_ARGUMENT );
6004 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6005 PSA_AEAD_NONCE_MAX_SIZE,
6006 &nonce_length ),
6007 PSA_ERROR_BAD_STATE );
6008 }
6009 else
6010 {
6011 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6012 input_data->len ) );
6013 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6014 PSA_AEAD_NONCE_MAX_SIZE,
6015 &nonce_length ) );
6016 }
6017
6018 psa_aead_abort( &operation );
6019#endif
6020
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006021 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006022
6023 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6024
6025 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6026 PSA_AEAD_NONCE_MAX_SIZE,
6027 &nonce_length ) );
6028
6029 if( operation.alg == PSA_ALG_CCM )
6030 {
6031 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6032 input_data->len ),
6033 PSA_ERROR_INVALID_ARGUMENT );
6034 }
6035 else
6036 {
6037 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6038 input_data->len ) );
6039 }
6040
6041 psa_aead_abort( &operation );
6042
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006043 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006044 /* Test for setting nonce after calling set lengths */
6045
6046 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6047
6048 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6049 input_data->len ) );
6050
6051 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6052
6053 psa_aead_abort( &operation );
6054
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006055 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006056
6057 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6058
6059 if( operation.alg == PSA_ALG_CCM )
6060 {
6061 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6062 input_data->len ),
6063 PSA_ERROR_INVALID_ARGUMENT );
6064 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6065 PSA_ERROR_BAD_STATE );
6066 }
6067 else
6068 {
6069 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6070 input_data->len ) );
6071 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6072 }
6073
6074 psa_aead_abort( &operation );
6075
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006076 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006077#if SIZE_MAX > UINT32_MAX
6078 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6079
6080 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6081 {
6082 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6083 input_data->len ),
6084 PSA_ERROR_INVALID_ARGUMENT );
6085 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6086 PSA_ERROR_BAD_STATE );
6087 }
6088 else
6089 {
6090 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6091 input_data->len ) );
6092 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6093 }
6094
6095 psa_aead_abort( &operation );
6096#endif
6097
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006098 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006099
6100 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6101
6102 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6103
6104 if( operation.alg == PSA_ALG_CCM )
6105 {
6106 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6107 input_data->len ),
6108 PSA_ERROR_INVALID_ARGUMENT );
6109 }
6110 else
6111 {
6112 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6113 input_data->len ) );
6114 }
6115
6116 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006117
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006118 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006119#if SIZE_MAX > UINT32_MAX
6120 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6121
6122 if( operation.alg == PSA_ALG_GCM )
6123 {
6124 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6125 SIZE_MAX ),
6126 PSA_ERROR_INVALID_ARGUMENT );
6127 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6128 PSA_ERROR_BAD_STATE );
6129 }
6130 else if ( operation.alg != PSA_ALG_CCM )
6131 {
6132 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6133 SIZE_MAX ) );
6134 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6135 }
6136
6137 psa_aead_abort( &operation );
6138
Tom Cosgrove1797b052022-12-04 17:19:59 +00006139 /* Test for calling set lengths with a plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006140 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6141
6142 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6143
6144 if( operation.alg == PSA_ALG_GCM )
6145 {
6146 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6147 SIZE_MAX ),
6148 PSA_ERROR_INVALID_ARGUMENT );
6149 }
6150 else if ( operation.alg != PSA_ALG_CCM )
6151 {
6152 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6153 SIZE_MAX ) );
6154 }
6155
6156 psa_aead_abort( &operation );
6157#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006158
6159 /* ------------------------------------------------------- */
6160
Paul Elliott374a2be2021-07-16 17:53:40 +01006161 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6162
6163 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6164
6165 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6166 PSA_AEAD_NONCE_MAX_SIZE,
6167 &nonce_length ),
6168 PSA_ERROR_BAD_STATE );
6169
6170 psa_aead_abort( &operation );
6171
Paul Elliott7220cae2021-06-22 17:25:57 +01006172 /* Test for generating nonce in decrypt setup. */
6173
Paul Elliott7220cae2021-06-22 17:25:57 +01006174 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6175
6176 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6177 PSA_AEAD_NONCE_MAX_SIZE,
6178 &nonce_length ),
6179 PSA_ERROR_BAD_STATE );
6180
6181 psa_aead_abort( &operation );
6182
Paul Elliottc23a9a02021-06-21 18:32:46 +01006183 /* Test for setting lengths twice. */
6184
Paul Elliottc23a9a02021-06-21 18:32:46 +01006185 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6186
6187 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6188
6189 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6190 input_data->len ) );
6191
6192 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6193 input_data->len ),
6194 PSA_ERROR_BAD_STATE );
6195
6196 psa_aead_abort( &operation );
6197
Andrzej Kurekad837522021-12-15 15:28:49 +01006198 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006199
Paul Elliottc23a9a02021-06-21 18:32:46 +01006200 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6201
6202 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6203
Andrzej Kurekad837522021-12-15 15:28:49 +01006204 if( operation.alg == PSA_ALG_CCM )
6205 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006206
Andrzej Kurekad837522021-12-15 15:28:49 +01006207 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6208 additional_data->len ),
6209 PSA_ERROR_BAD_STATE );
6210 }
6211 else
6212 {
6213 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6214 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006215
Andrzej Kurekad837522021-12-15 15:28:49 +01006216 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6217 input_data->len ),
6218 PSA_ERROR_BAD_STATE );
6219 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006220 psa_aead_abort( &operation );
6221
6222 /* ------------------------------------------------------- */
6223
Paul Elliottf94bd992021-09-19 18:15:59 +01006224 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6225
6226 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6227
Andrzej Kurekad837522021-12-15 15:28:49 +01006228 if( operation.alg == PSA_ALG_CCM )
6229 {
6230 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6231 input_data->len, output_data,
6232 output_size, &output_length ),
6233 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006234
Andrzej Kurekad837522021-12-15 15:28:49 +01006235 }
6236 else
6237 {
6238 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6239 input_data->len, output_data,
6240 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006241
Andrzej Kurekad837522021-12-15 15:28:49 +01006242 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6243 input_data->len ),
6244 PSA_ERROR_BAD_STATE );
6245 }
6246 psa_aead_abort( &operation );
6247
6248 /* ------------------------------------------------------- */
6249
6250 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6251
6252 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6253
6254 if( operation.alg == PSA_ALG_CCM )
6255 {
6256 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6257 finish_output_size,
6258 &output_part_length,
6259 tag_buffer, tag_length,
6260 &tag_size ) );
6261 }
6262 else
6263 {
6264 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6265 finish_output_size,
6266 &output_part_length,
6267 tag_buffer, tag_length,
6268 &tag_size ) );
6269
6270 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6271 input_data->len ),
6272 PSA_ERROR_BAD_STATE );
6273 }
6274 psa_aead_abort( &operation );
6275
6276 /* Test for setting lengths after generating nonce + already starting data. */
6277
6278 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6279
6280 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6281 PSA_AEAD_NONCE_MAX_SIZE,
6282 &nonce_length ) );
6283 if( operation.alg == PSA_ALG_CCM )
6284 {
6285
6286 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6287 additional_data->len ),
6288 PSA_ERROR_BAD_STATE );
6289 }
6290 else
6291 {
6292 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6293 additional_data->len ) );
6294
6295 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6296 input_data->len ),
6297 PSA_ERROR_BAD_STATE );
6298 }
6299 psa_aead_abort( &operation );
6300
6301 /* ------------------------------------------------------- */
6302
6303 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6304
6305 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6306 PSA_AEAD_NONCE_MAX_SIZE,
6307 &nonce_length ) );
6308 if( operation.alg == PSA_ALG_CCM )
6309 {
6310 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6311 input_data->len, output_data,
6312 output_size, &output_length ),
6313 PSA_ERROR_BAD_STATE );
6314
6315 }
6316 else
6317 {
6318 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6319 input_data->len, output_data,
6320 output_size, &output_length ) );
6321
6322 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6323 input_data->len ),
6324 PSA_ERROR_BAD_STATE );
6325 }
6326 psa_aead_abort( &operation );
6327
6328 /* ------------------------------------------------------- */
6329
6330 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6331
6332 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6333 PSA_AEAD_NONCE_MAX_SIZE,
6334 &nonce_length ) );
6335 if( operation.alg == PSA_ALG_CCM )
6336 {
6337 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6338 finish_output_size,
6339 &output_part_length,
6340 tag_buffer, tag_length,
6341 &tag_size ) );
6342 }
6343 else
6344 {
6345 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6346 finish_output_size,
6347 &output_part_length,
6348 tag_buffer, tag_length,
6349 &tag_size ) );
6350
6351 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6352 input_data->len ),
6353 PSA_ERROR_BAD_STATE );
6354 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006355 psa_aead_abort( &operation );
6356
Paul Elliott243080c2021-07-21 19:01:17 +01006357 /* Test for not sending any additional data or data after setting non zero
6358 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006359
Paul Elliottc23a9a02021-06-21 18:32:46 +01006360 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6361
6362 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6363
6364 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6365 input_data->len ) );
6366
6367 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6368 finish_output_size,
6369 &output_part_length,
6370 tag_buffer, tag_length,
6371 &tag_size ),
6372 PSA_ERROR_INVALID_ARGUMENT );
6373
6374 psa_aead_abort( &operation );
6375
Paul Elliott243080c2021-07-21 19:01:17 +01006376 /* Test for not sending any additional data or data after setting non-zero
6377 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006378
Paul Elliottc23a9a02021-06-21 18:32:46 +01006379 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6380
6381 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6382
6383 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6384 input_data->len ) );
6385
6386 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6387 finish_output_size,
6388 &output_part_length,
6389 tag_buffer,
6390 tag_length ),
6391 PSA_ERROR_INVALID_ARGUMENT );
6392
6393 psa_aead_abort( &operation );
6394
Paul Elliott243080c2021-07-21 19:01:17 +01006395 /* Test for not sending any additional data after setting a non-zero length
6396 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006397
Paul Elliottc23a9a02021-06-21 18:32:46 +01006398 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6399
6400 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6401
6402 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6403 input_data->len ) );
6404
6405 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6406 input_data->len, output_data,
6407 output_size, &output_length ),
6408 PSA_ERROR_INVALID_ARGUMENT );
6409
6410 psa_aead_abort( &operation );
6411
Paul Elliottf94bd992021-09-19 18:15:59 +01006412 /* Test for not sending any data after setting a non-zero length for it.*/
6413
Paul Elliottf94bd992021-09-19 18:15:59 +01006414 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6415
6416 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6417
6418 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6419 input_data->len ) );
6420
6421 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6422 additional_data->len ) );
6423
6424 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6425 finish_output_size,
6426 &output_part_length,
6427 tag_buffer, tag_length,
6428 &tag_size ),
6429 PSA_ERROR_INVALID_ARGUMENT );
6430
6431 psa_aead_abort( &operation );
6432
Paul Elliottb0450fe2021-09-01 15:06:26 +01006433 /* Test for sending too much additional data after setting lengths. */
6434
Paul Elliottb0450fe2021-09-01 15:06:26 +01006435 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6436
6437 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6438
6439 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6440
6441
6442 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6443 additional_data->len ),
6444 PSA_ERROR_INVALID_ARGUMENT );
6445
6446 psa_aead_abort( &operation );
6447
Paul Elliotta2a09b02021-09-22 14:56:40 +01006448 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006449
6450 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6451
6452 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6453
6454 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6455 input_data->len ) );
6456
6457 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6458 additional_data->len ) );
6459
6460 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6461 1 ),
6462 PSA_ERROR_INVALID_ARGUMENT );
6463
6464 psa_aead_abort( &operation );
6465
Paul Elliottb0450fe2021-09-01 15:06:26 +01006466 /* Test for sending too much data after setting lengths. */
6467
Paul Elliottb0450fe2021-09-01 15:06:26 +01006468 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6469
6470 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6471
6472 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6473
6474 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6475 input_data->len, output_data,
6476 output_size, &output_length ),
6477 PSA_ERROR_INVALID_ARGUMENT );
6478
6479 psa_aead_abort( &operation );
6480
Paul Elliotta2a09b02021-09-22 14:56:40 +01006481 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006482
6483 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6484
6485 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6486
6487 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6488 input_data->len ) );
6489
6490 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6491 additional_data->len ) );
6492
6493 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6494 input_data->len, output_data,
6495 output_size, &output_length ) );
6496
6497 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6498 1, output_data,
6499 output_size, &output_length ),
6500 PSA_ERROR_INVALID_ARGUMENT );
6501
6502 psa_aead_abort( &operation );
6503
Paul Elliottc23a9a02021-06-21 18:32:46 +01006504 /* Test sending additional data after data. */
6505
Paul Elliottc23a9a02021-06-21 18:32:46 +01006506 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6507
6508 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6509
Andrzej Kurekad837522021-12-15 15:28:49 +01006510 if( operation.alg != PSA_ALG_CCM )
6511 {
6512 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6513 input_data->len, output_data,
6514 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006515
Andrzej Kurekad837522021-12-15 15:28:49 +01006516 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6517 additional_data->len ),
6518 PSA_ERROR_BAD_STATE );
6519 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006520 psa_aead_abort( &operation );
6521
Paul Elliott534d0b42021-06-22 19:15:20 +01006522 /* Test calling finish on decryption. */
6523
Paul Elliott534d0b42021-06-22 19:15:20 +01006524 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6525
6526 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6527
6528 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6529 finish_output_size,
6530 &output_part_length,
6531 tag_buffer, tag_length,
6532 &tag_size ),
6533 PSA_ERROR_BAD_STATE );
6534
6535 psa_aead_abort( &operation );
6536
6537 /* Test calling verify on encryption. */
6538
Paul Elliott534d0b42021-06-22 19:15:20 +01006539 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6540
6541 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6542
6543 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6544 finish_output_size,
6545 &output_part_length,
6546 tag_buffer,
6547 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006548 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006549
6550 psa_aead_abort( &operation );
6551
6552
Paul Elliottc23a9a02021-06-21 18:32:46 +01006553exit:
6554 psa_destroy_key( key );
6555 psa_aead_abort( &operation );
6556 mbedtls_free( output_data );
6557 mbedtls_free( final_data );
6558 PSA_DONE( );
6559}
6560/* END_CASE */
6561
6562/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006563void signature_size( int type_arg,
6564 int bits,
6565 int alg_arg,
6566 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006567{
6568 psa_key_type_t type = type_arg;
6569 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006570 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006571
Gilles Peskinefe11b722018-12-18 00:24:04 +01006572 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006573
Gilles Peskinee59236f2018-01-27 23:32:46 +01006574exit:
6575 ;
6576}
6577/* END_CASE */
6578
6579/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006580void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6581 int alg_arg, data_t *input_data,
6582 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006583{
Ronald Cron5425a212020-08-04 14:58:35 +02006584 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006585 psa_key_type_t key_type = key_type_arg;
6586 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006587 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006588 unsigned char *signature = NULL;
6589 size_t signature_size;
6590 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006592
Gilles Peskine8817f612018-12-18 00:18:46 +01006593 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006594
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006595 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006596 psa_set_key_algorithm( &attributes, alg );
6597 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006598
Gilles Peskine049c7532019-05-15 20:22:09 +02006599 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006600 &key ) );
6601 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006602 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006603
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006604 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006605 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006606 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006607 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006608 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006609 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006610 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006611
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006612 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006613 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006614 input_data->x, input_data->len,
6615 signature, signature_size,
6616 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006617 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006618 ASSERT_COMPARE( output_data->x, output_data->len,
6619 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006620
6621exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006622 /*
6623 * Key attributes may have been returned by psa_get_key_attributes()
6624 * thus reset them as required.
6625 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006626 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006627
Ronald Cron5425a212020-08-04 14:58:35 +02006628 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006629 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006630 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006631}
6632/* END_CASE */
6633
6634/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006635void sign_hash_fail( int key_type_arg, data_t *key_data,
6636 int alg_arg, data_t *input_data,
6637 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006638{
Ronald Cron5425a212020-08-04 14:58:35 +02006639 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006640 psa_key_type_t key_type = key_type_arg;
6641 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006642 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006643 psa_status_t actual_status;
6644 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006645 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006646 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006647 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006648
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006649 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006650
Gilles Peskine8817f612018-12-18 00:18:46 +01006651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006652
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006653 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006654 psa_set_key_algorithm( &attributes, alg );
6655 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006656
Gilles Peskine049c7532019-05-15 20:22:09 +02006657 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006658 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006659
Ronald Cron5425a212020-08-04 14:58:35 +02006660 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006661 input_data->x, input_data->len,
6662 signature, signature_size,
6663 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006664 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006665 /* The value of *signature_length is unspecified on error, but
6666 * whatever it is, it should be less than signature_size, so that
6667 * if the caller tries to read *signature_length bytes without
6668 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006669 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006670
6671exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006672 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006673 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006674 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006675 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006676}
6677/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006678
6679/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006680void sign_verify_hash( int key_type_arg, data_t *key_data,
6681 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006682{
Ronald Cron5425a212020-08-04 14:58:35 +02006683 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006684 psa_key_type_t key_type = key_type_arg;
6685 psa_algorithm_t alg = alg_arg;
6686 size_t key_bits;
6687 unsigned char *signature = NULL;
6688 size_t signature_size;
6689 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006691
Gilles Peskine8817f612018-12-18 00:18:46 +01006692 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006693
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006694 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006695 psa_set_key_algorithm( &attributes, alg );
6696 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006697
Gilles Peskine049c7532019-05-15 20:22:09 +02006698 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006699 &key ) );
6700 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006701 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006702
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006703 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006704 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006705 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006706 key_bits, alg );
6707 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006708 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006709 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006710
6711 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006712 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006713 input_data->x, input_data->len,
6714 signature, signature_size,
6715 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006716 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006717 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006718 TEST_ASSERT( signature_length > 0 );
6719
6720 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006721 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006722 input_data->x, input_data->len,
6723 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006724
6725 if( input_data->len != 0 )
6726 {
6727 /* Flip a bit in the input and verify that the signature is now
6728 * detected as invalid. Flip a bit at the beginning, not at the end,
6729 * because ECDSA may ignore the last few bits of the input. */
6730 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006731 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006732 input_data->x, input_data->len,
6733 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006734 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006735 }
6736
6737exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006738 /*
6739 * Key attributes may have been returned by psa_get_key_attributes()
6740 * thus reset them as required.
6741 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006742 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006743
Ronald Cron5425a212020-08-04 14:58:35 +02006744 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006745 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006746 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006747}
6748/* END_CASE */
6749
6750/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006751void verify_hash( int key_type_arg, data_t *key_data,
6752 int alg_arg, data_t *hash_data,
6753 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006754{
Ronald Cron5425a212020-08-04 14:58:35 +02006755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006756 psa_key_type_t key_type = key_type_arg;
6757 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006759
Gilles Peskine7be11a72022-04-14 00:12:57 +02006760 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006761
Gilles Peskine8817f612018-12-18 00:18:46 +01006762 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006763
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006764 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006765 psa_set_key_algorithm( &attributes, alg );
6766 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006767
Gilles Peskine049c7532019-05-15 20:22:09 +02006768 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006769 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006770
Ronald Cron5425a212020-08-04 14:58:35 +02006771 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006772 hash_data->x, hash_data->len,
6773 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006774
itayzafrir5c753392018-05-08 11:18:38 +03006775exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006776 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006777 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006778 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006779}
6780/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006781
6782/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006783void verify_hash_fail( int key_type_arg, data_t *key_data,
6784 int alg_arg, data_t *hash_data,
6785 data_t *signature_data,
6786 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006787{
Ronald Cron5425a212020-08-04 14:58:35 +02006788 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006789 psa_key_type_t key_type = key_type_arg;
6790 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006791 psa_status_t actual_status;
6792 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006794
Gilles Peskine8817f612018-12-18 00:18:46 +01006795 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006796
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006797 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006798 psa_set_key_algorithm( &attributes, alg );
6799 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006800
Gilles Peskine049c7532019-05-15 20:22:09 +02006801 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006802 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006803
Ronald Cron5425a212020-08-04 14:58:35 +02006804 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006805 hash_data->x, hash_data->len,
6806 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006807 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006808
6809exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006810 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006811 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006812 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006813}
6814/* END_CASE */
6815
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006816/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006817void sign_message_deterministic( int key_type_arg,
6818 data_t *key_data,
6819 int alg_arg,
6820 data_t *input_data,
6821 data_t *output_data )
6822{
6823 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6824 psa_key_type_t key_type = key_type_arg;
6825 psa_algorithm_t alg = alg_arg;
6826 size_t key_bits;
6827 unsigned char *signature = NULL;
6828 size_t signature_size;
6829 size_t signature_length = 0xdeadbeef;
6830 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6831
6832 PSA_ASSERT( psa_crypto_init( ) );
6833
6834 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6835 psa_set_key_algorithm( &attributes, alg );
6836 psa_set_key_type( &attributes, key_type );
6837
6838 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6839 &key ) );
6840 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6841 key_bits = psa_get_key_bits( &attributes );
6842
6843 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6844 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006845 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006846 ASSERT_ALLOC( signature, signature_size );
6847
6848 PSA_ASSERT( psa_sign_message( key, alg,
6849 input_data->x, input_data->len,
6850 signature, signature_size,
6851 &signature_length ) );
6852
6853 ASSERT_COMPARE( output_data->x, output_data->len,
6854 signature, signature_length );
6855
6856exit:
6857 psa_reset_key_attributes( &attributes );
6858
6859 psa_destroy_key( key );
6860 mbedtls_free( signature );
6861 PSA_DONE( );
6862
6863}
6864/* END_CASE */
6865
6866/* BEGIN_CASE */
6867void sign_message_fail( int key_type_arg,
6868 data_t *key_data,
6869 int alg_arg,
6870 data_t *input_data,
6871 int signature_size_arg,
6872 int expected_status_arg )
6873{
6874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6875 psa_key_type_t key_type = key_type_arg;
6876 psa_algorithm_t alg = alg_arg;
6877 size_t signature_size = signature_size_arg;
6878 psa_status_t actual_status;
6879 psa_status_t expected_status = expected_status_arg;
6880 unsigned char *signature = NULL;
6881 size_t signature_length = 0xdeadbeef;
6882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6883
6884 ASSERT_ALLOC( signature, signature_size );
6885
6886 PSA_ASSERT( psa_crypto_init( ) );
6887
6888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6889 psa_set_key_algorithm( &attributes, alg );
6890 psa_set_key_type( &attributes, key_type );
6891
6892 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6893 &key ) );
6894
6895 actual_status = psa_sign_message( key, alg,
6896 input_data->x, input_data->len,
6897 signature, signature_size,
6898 &signature_length );
6899 TEST_EQUAL( actual_status, expected_status );
6900 /* The value of *signature_length is unspecified on error, but
6901 * whatever it is, it should be less than signature_size, so that
6902 * if the caller tries to read *signature_length bytes without
6903 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006904 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006905
6906exit:
6907 psa_reset_key_attributes( &attributes );
6908 psa_destroy_key( key );
6909 mbedtls_free( signature );
6910 PSA_DONE( );
6911}
6912/* END_CASE */
6913
6914/* BEGIN_CASE */
6915void sign_verify_message( int key_type_arg,
6916 data_t *key_data,
6917 int alg_arg,
6918 data_t *input_data )
6919{
6920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6921 psa_key_type_t key_type = key_type_arg;
6922 psa_algorithm_t alg = alg_arg;
6923 size_t key_bits;
6924 unsigned char *signature = NULL;
6925 size_t signature_size;
6926 size_t signature_length = 0xdeadbeef;
6927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6928
6929 PSA_ASSERT( psa_crypto_init( ) );
6930
6931 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6932 PSA_KEY_USAGE_VERIFY_MESSAGE );
6933 psa_set_key_algorithm( &attributes, alg );
6934 psa_set_key_type( &attributes, key_type );
6935
6936 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6937 &key ) );
6938 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6939 key_bits = psa_get_key_bits( &attributes );
6940
6941 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6942 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006943 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006944 ASSERT_ALLOC( signature, signature_size );
6945
6946 PSA_ASSERT( psa_sign_message( key, alg,
6947 input_data->x, input_data->len,
6948 signature, signature_size,
6949 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006950 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006951 TEST_ASSERT( signature_length > 0 );
6952
6953 PSA_ASSERT( psa_verify_message( key, alg,
6954 input_data->x, input_data->len,
6955 signature, signature_length ) );
6956
6957 if( input_data->len != 0 )
6958 {
6959 /* Flip a bit in the input and verify that the signature is now
6960 * detected as invalid. Flip a bit at the beginning, not at the end,
6961 * because ECDSA may ignore the last few bits of the input. */
6962 input_data->x[0] ^= 1;
6963 TEST_EQUAL( psa_verify_message( key, alg,
6964 input_data->x, input_data->len,
6965 signature, signature_length ),
6966 PSA_ERROR_INVALID_SIGNATURE );
6967 }
6968
6969exit:
6970 psa_reset_key_attributes( &attributes );
6971
6972 psa_destroy_key( key );
6973 mbedtls_free( signature );
6974 PSA_DONE( );
6975}
6976/* END_CASE */
6977
6978/* BEGIN_CASE */
6979void verify_message( int key_type_arg,
6980 data_t *key_data,
6981 int alg_arg,
6982 data_t *input_data,
6983 data_t *signature_data )
6984{
6985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6986 psa_key_type_t key_type = key_type_arg;
6987 psa_algorithm_t alg = alg_arg;
6988 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6989
Gilles Peskine7be11a72022-04-14 00:12:57 +02006990 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006991
6992 PSA_ASSERT( psa_crypto_init( ) );
6993
6994 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6995 psa_set_key_algorithm( &attributes, alg );
6996 psa_set_key_type( &attributes, key_type );
6997
6998 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6999 &key ) );
7000
7001 PSA_ASSERT( psa_verify_message( key, alg,
7002 input_data->x, input_data->len,
7003 signature_data->x, signature_data->len ) );
7004
7005exit:
7006 psa_reset_key_attributes( &attributes );
7007 psa_destroy_key( key );
7008 PSA_DONE( );
7009}
7010/* END_CASE */
7011
7012/* BEGIN_CASE */
7013void verify_message_fail( int key_type_arg,
7014 data_t *key_data,
7015 int alg_arg,
7016 data_t *hash_data,
7017 data_t *signature_data,
7018 int expected_status_arg )
7019{
7020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7021 psa_key_type_t key_type = key_type_arg;
7022 psa_algorithm_t alg = alg_arg;
7023 psa_status_t actual_status;
7024 psa_status_t expected_status = expected_status_arg;
7025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7026
7027 PSA_ASSERT( psa_crypto_init( ) );
7028
7029 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
7030 psa_set_key_algorithm( &attributes, alg );
7031 psa_set_key_type( &attributes, key_type );
7032
7033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
7034 &key ) );
7035
7036 actual_status = psa_verify_message( key, alg,
7037 hash_data->x, hash_data->len,
7038 signature_data->x,
7039 signature_data->len );
7040 TEST_EQUAL( actual_status, expected_status );
7041
7042exit:
7043 psa_reset_key_attributes( &attributes );
7044 psa_destroy_key( key );
7045 PSA_DONE( );
7046}
7047/* END_CASE */
7048
7049/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007050void asymmetric_encrypt( int key_type_arg,
7051 data_t *key_data,
7052 int alg_arg,
7053 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007054 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007055 int expected_output_length_arg,
7056 int expected_status_arg )
7057{
Ronald Cron5425a212020-08-04 14:58:35 +02007058 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007059 psa_key_type_t key_type = key_type_arg;
7060 psa_algorithm_t alg = alg_arg;
7061 size_t expected_output_length = expected_output_length_arg;
7062 size_t key_bits;
7063 unsigned char *output = NULL;
7064 size_t output_size;
7065 size_t output_length = ~0;
7066 psa_status_t actual_status;
7067 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007068 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007069
Gilles Peskine8817f612018-12-18 00:18:46 +01007070 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007071
Gilles Peskine656896e2018-06-29 19:12:28 +02007072 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007073 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7074 psa_set_key_algorithm( &attributes, alg );
7075 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007076 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007077 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007078
7079 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007080 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007081 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007082
Gilles Peskine656896e2018-06-29 19:12:28 +02007083 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007084 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007085 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007086
7087 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007088 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007089 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007090 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007091 output, output_size,
7092 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007093 TEST_EQUAL( actual_status, expected_status );
7094 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007095
Gilles Peskine68428122018-06-30 18:42:41 +02007096 /* If the label is empty, the test framework puts a non-null pointer
7097 * in label->x. Test that a null pointer works as well. */
7098 if( label->len == 0 )
7099 {
7100 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007101 if( output_size != 0 )
7102 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007103 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007104 input_data->x, input_data->len,
7105 NULL, label->len,
7106 output, output_size,
7107 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007108 TEST_EQUAL( actual_status, expected_status );
7109 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007110 }
7111
Gilles Peskine656896e2018-06-29 19:12:28 +02007112exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007113 /*
7114 * Key attributes may have been returned by psa_get_key_attributes()
7115 * thus reset them as required.
7116 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007117 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007118
Ronald Cron5425a212020-08-04 14:58:35 +02007119 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007120 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007121 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007122}
7123/* END_CASE */
7124
7125/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007126void asymmetric_encrypt_decrypt( int key_type_arg,
7127 data_t *key_data,
7128 int alg_arg,
7129 data_t *input_data,
7130 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007131{
Ronald Cron5425a212020-08-04 14:58:35 +02007132 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007133 psa_key_type_t key_type = key_type_arg;
7134 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007135 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007136 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007137 size_t output_size;
7138 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007139 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007140 size_t output2_size;
7141 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007142 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007143
Gilles Peskine8817f612018-12-18 00:18:46 +01007144 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007145
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7147 psa_set_key_algorithm( &attributes, alg );
7148 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007149
Gilles Peskine049c7532019-05-15 20:22:09 +02007150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007151 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007152
7153 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007154 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007155 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007156
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007157 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007158 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007159 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007160
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007161 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007162 TEST_LE_U( output2_size,
7163 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7164 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007165 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007166
Gilles Peskineeebd7382018-06-08 18:11:54 +02007167 /* We test encryption by checking that encrypt-then-decrypt gives back
7168 * the original plaintext because of the non-optional random
7169 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007170 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007171 input_data->x, input_data->len,
7172 label->x, label->len,
7173 output, output_size,
7174 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007175 /* We don't know what ciphertext length to expect, but check that
7176 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007177 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007178
Ronald Cron5425a212020-08-04 14:58:35 +02007179 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007180 output, output_length,
7181 label->x, label->len,
7182 output2, output2_size,
7183 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007184 ASSERT_COMPARE( input_data->x, input_data->len,
7185 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007186
7187exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007188 /*
7189 * Key attributes may have been returned by psa_get_key_attributes()
7190 * thus reset them as required.
7191 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007192 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007193
Ronald Cron5425a212020-08-04 14:58:35 +02007194 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007195 mbedtls_free( output );
7196 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007197 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007198}
7199/* END_CASE */
7200
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007201/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007202void asymmetric_decrypt( int key_type_arg,
7203 data_t *key_data,
7204 int alg_arg,
7205 data_t *input_data,
7206 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007207 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007208{
Ronald Cron5425a212020-08-04 14:58:35 +02007209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007210 psa_key_type_t key_type = key_type_arg;
7211 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007212 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007213 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007214 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007215 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007217
Gilles Peskine8817f612018-12-18 00:18:46 +01007218 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007219
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7221 psa_set_key_algorithm( &attributes, alg );
7222 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007223
Gilles Peskine049c7532019-05-15 20:22:09 +02007224 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007225 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007226
gabor-mezei-armceface22021-01-21 12:26:17 +01007227 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7228 key_bits = psa_get_key_bits( &attributes );
7229
7230 /* Determine the maximum ciphertext length */
7231 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007232 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007233 ASSERT_ALLOC( output, output_size );
7234
Ronald Cron5425a212020-08-04 14:58:35 +02007235 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007236 input_data->x, input_data->len,
7237 label->x, label->len,
7238 output,
7239 output_size,
7240 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007241 ASSERT_COMPARE( expected_data->x, expected_data->len,
7242 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007243
Gilles Peskine68428122018-06-30 18:42:41 +02007244 /* If the label is empty, the test framework puts a non-null pointer
7245 * in label->x. Test that a null pointer works as well. */
7246 if( label->len == 0 )
7247 {
7248 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007249 if( output_size != 0 )
7250 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007251 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007252 input_data->x, input_data->len,
7253 NULL, label->len,
7254 output,
7255 output_size,
7256 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007257 ASSERT_COMPARE( expected_data->x, expected_data->len,
7258 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007259 }
7260
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007261exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007262 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007263 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007264 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007265 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007266}
7267/* END_CASE */
7268
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007269/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007270void asymmetric_decrypt_fail( int key_type_arg,
7271 data_t *key_data,
7272 int alg_arg,
7273 data_t *input_data,
7274 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007275 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007276 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007277{
Ronald Cron5425a212020-08-04 14:58:35 +02007278 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007279 psa_key_type_t key_type = key_type_arg;
7280 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007281 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007282 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007283 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007284 psa_status_t actual_status;
7285 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007287
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007288 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007289
Gilles Peskine8817f612018-12-18 00:18:46 +01007290 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007291
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007292 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7293 psa_set_key_algorithm( &attributes, alg );
7294 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007295
Gilles Peskine049c7532019-05-15 20:22:09 +02007296 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007297 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007298
Ronald Cron5425a212020-08-04 14:58:35 +02007299 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007300 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007301 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007302 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007303 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007304 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007305 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007306
Gilles Peskine68428122018-06-30 18:42:41 +02007307 /* If the label is empty, the test framework puts a non-null pointer
7308 * in label->x. Test that a null pointer works as well. */
7309 if( label->len == 0 )
7310 {
7311 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007312 if( output_size != 0 )
7313 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007314 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007315 input_data->x, input_data->len,
7316 NULL, label->len,
7317 output, output_size,
7318 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007319 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007320 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007321 }
7322
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007323exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007324 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007325 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007326 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007327 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007328}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007329/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007330
7331/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007332void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007333{
7334 /* Test each valid way of initializing the object, except for `= {0}`, as
7335 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7336 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007337 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007338 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007339 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7340 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7341 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007342
7343 memset( &zero, 0, sizeof( zero ) );
7344
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007345 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007346 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007347 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007348 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007349 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007350 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007351 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007352
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007353 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007354 PSA_ASSERT( psa_key_derivation_abort(&func) );
7355 PSA_ASSERT( psa_key_derivation_abort(&init) );
7356 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007357}
7358/* END_CASE */
7359
Janos Follath16de4a42019-06-13 16:32:24 +01007360/* BEGIN_CASE */
7361void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007362{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007363 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007364 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007365 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007366
Gilles Peskine8817f612018-12-18 00:18:46 +01007367 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007368
Janos Follath16de4a42019-06-13 16:32:24 +01007369 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007370 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007371
7372exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007373 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007374 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007375}
7376/* END_CASE */
7377
Janos Follathaf3c2a02019-06-12 12:34:34 +01007378/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007379void derive_set_capacity( int alg_arg, int capacity_arg,
7380 int expected_status_arg )
7381{
7382 psa_algorithm_t alg = alg_arg;
7383 size_t capacity = capacity_arg;
7384 psa_status_t expected_status = expected_status_arg;
7385 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7386
7387 PSA_ASSERT( psa_crypto_init( ) );
7388
7389 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7390
7391 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7392 expected_status );
7393
7394exit:
7395 psa_key_derivation_abort( &operation );
7396 PSA_DONE( );
7397}
7398/* END_CASE */
7399
7400/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007401void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007402 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007403 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007404 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007405 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007406 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007407 int expected_status_arg3,
7408 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007409{
7410 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007411 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7412 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007413 psa_status_t expected_statuses[] = {expected_status_arg1,
7414 expected_status_arg2,
7415 expected_status_arg3};
7416 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007417 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7418 MBEDTLS_SVC_KEY_ID_INIT,
7419 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007420 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7422 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007423 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007424 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007425 psa_status_t expected_output_status = expected_output_status_arg;
7426 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007427
7428 PSA_ASSERT( psa_crypto_init( ) );
7429
7430 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7431 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007432
7433 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7434
7435 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7436 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007437 mbedtls_test_set_step( i );
7438 if( steps[i] == 0 )
7439 {
7440 /* Skip this step */
7441 }
7442 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007443 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007444 psa_set_key_type( &attributes, key_types[i] );
7445 PSA_ASSERT( psa_import_key( &attributes,
7446 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007447 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007448 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7449 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7450 {
7451 // When taking a private key as secret input, use key agreement
7452 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007453 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7454 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007455 expected_statuses[i] );
7456 }
7457 else
7458 {
7459 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007460 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007461 expected_statuses[i] );
7462 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007463 }
7464 else
7465 {
7466 TEST_EQUAL( psa_key_derivation_input_bytes(
7467 &operation, steps[i],
7468 inputs[i]->x, inputs[i]->len ),
7469 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007470 }
7471 }
7472
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007473 if( output_key_type != PSA_KEY_TYPE_NONE )
7474 {
7475 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007476 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007477 psa_set_key_bits( &attributes, 8 );
7478 actual_output_status =
7479 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007480 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007481 }
7482 else
7483 {
7484 uint8_t buffer[1];
7485 actual_output_status =
7486 psa_key_derivation_output_bytes( &operation,
7487 buffer, sizeof( buffer ) );
7488 }
7489 TEST_EQUAL( actual_output_status, expected_output_status );
7490
Janos Follathaf3c2a02019-06-12 12:34:34 +01007491exit:
7492 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007493 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7494 psa_destroy_key( keys[i] );
7495 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007496 PSA_DONE( );
7497}
7498/* END_CASE */
7499
Janos Follathd958bb72019-07-03 15:02:16 +01007500/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007501void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007502{
Janos Follathd958bb72019-07-03 15:02:16 +01007503 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007504 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007505 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007506 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007507 unsigned char input1[] = "Input 1";
7508 size_t input1_length = sizeof( input1 );
7509 unsigned char input2[] = "Input 2";
7510 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007511 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007512 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007513 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7514 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7515 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007517
Gilles Peskine8817f612018-12-18 00:18:46 +01007518 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007519
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007520 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7521 psa_set_key_algorithm( &attributes, alg );
7522 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007523
Gilles Peskine73676cb2019-05-15 20:15:10 +02007524 PSA_ASSERT( psa_import_key( &attributes,
7525 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007526 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007527
7528 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007529 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7530 input1, input1_length,
7531 input2, input2_length,
7532 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007533 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007535 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007536 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007537 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007538
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007539 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007541 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007542 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007543
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007544exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007545 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007546 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007547 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007548}
7549/* END_CASE */
7550
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007551/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007552void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007553{
7554 uint8_t output_buffer[16];
7555 size_t buffer_size = 16;
7556 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007557 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007558
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007559 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7560 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007561 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007562
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007563 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007564 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007565
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007566 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007567
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007568 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7569 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007570 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007571
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007572 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007573 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007574
7575exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007576 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007577}
7578/* END_CASE */
7579
7580/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007581void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007582 int step1_arg, data_t *input1, int expected_status_arg1,
7583 int step2_arg, data_t *input2, int expected_status_arg2,
7584 int step3_arg, data_t *input3, int expected_status_arg3,
7585 int step4_arg, data_t *input4, int expected_status_arg4,
7586 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007587 int requested_capacity_arg,
7588 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007589 data_t *expected_output2,
7590 int other_key_input_type,
7591 int key_input_type,
7592 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007593{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007594 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007595 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7596 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007597 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7598 MBEDTLS_SVC_KEY_ID_INIT,
7599 MBEDTLS_SVC_KEY_ID_INIT,
7600 MBEDTLS_SVC_KEY_ID_INIT};
7601 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7602 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007603 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007604 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007605 uint8_t *expected_outputs[2] =
7606 {expected_output1->x, expected_output2->x};
7607 size_t output_sizes[2] =
7608 {expected_output1->len, expected_output2->len};
7609 size_t output_buffer_size = 0;
7610 uint8_t *output_buffer = NULL;
7611 size_t expected_capacity;
7612 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007613 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7614 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7615 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7616 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007617 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007618 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007619 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007620
7621 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7622 {
7623 if( output_sizes[i] > output_buffer_size )
7624 output_buffer_size = output_sizes[i];
7625 if( output_sizes[i] == 0 )
7626 expected_outputs[i] = NULL;
7627 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007628 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007630
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007631 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007632 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7633 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7634 requested_capacity ) );
7635 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007636 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007637 switch( steps[i] )
7638 {
7639 case 0:
7640 break;
7641 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007642 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007643 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007644 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007645 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007646 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007647 inputs[i]->x, inputs[i]->len ),
7648 statuses[i] );
7649
7650 if( statuses[i] != PSA_SUCCESS )
7651 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007652 break;
7653 case 1: // input key
7654 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7655 psa_set_key_algorithm( &attributes1, alg );
7656 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7657
7658 PSA_ASSERT( psa_import_key( &attributes1,
7659 inputs[i]->x, inputs[i]->len,
7660 &keys[i] ) );
7661
Przemek Stekiel38647de2022-04-19 13:27:47 +02007662 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007663 {
7664 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007665 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7666 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007667 }
7668
Przemek Stekiel38647de2022-04-19 13:27:47 +02007669 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7670 steps[i],
7671 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007672 break;
7673 default:
7674 TEST_ASSERT( ! "default case not supported" );
7675 break;
7676 }
7677 break;
7678 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007679 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007680 {
7681 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007682 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7683 steps[i],
7684 inputs[i]->x,
7685 inputs[i]->len ),
7686 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007687 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007688 case 1: // input key, type DERIVE
7689 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007690 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7691 psa_set_key_algorithm( &attributes2, alg );
7692 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7693
7694 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007695 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007696 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7697
7698 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007699 inputs[i]->x, inputs[i]->len,
7700 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007701
Przemek Stekiel38647de2022-04-19 13:27:47 +02007702 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7703 steps[i],
7704 keys[i] ),
7705 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007706 break;
7707 case 2: // key agreement
7708 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7709 psa_set_key_algorithm( &attributes3, alg );
7710 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7711
7712 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007713 inputs[i]->x, inputs[i]->len,
7714 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007715
7716 TEST_EQUAL( psa_key_derivation_key_agreement(
7717 &operation,
7718 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7719 keys[i], key_agreement_peer_key->x,
7720 key_agreement_peer_key->len ), statuses[i] );
7721 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007722 default:
7723 TEST_ASSERT( ! "default case not supported" );
7724 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007725 }
7726
Przemek Stekiel38647de2022-04-19 13:27:47 +02007727 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007728 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007729 break;
7730 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007731 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007732 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007733 inputs[i]->x, inputs[i]->len ), statuses[i] );
7734
7735 if( statuses[i] != PSA_SUCCESS )
7736 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007737 break;
7738 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007739 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007740
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007741 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007742 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007743 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007744 expected_capacity = requested_capacity;
7745
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007746 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007747 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007748 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7749
7750 /* For output key derivation secret must be provided using
7751 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007752 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007753 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007754
7755 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7756 psa_set_key_algorithm( &attributes4, alg );
7757 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007758 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007759
7760 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007761 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007762 }
7763 else // output bytes
7764 {
7765 /* Expansion phase. */
7766 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007767 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007768 /* Read some bytes. */
7769 status = psa_key_derivation_output_bytes( &operation,
7770 output_buffer, output_sizes[i] );
7771 if( expected_capacity == 0 && output_sizes[i] == 0 )
7772 {
7773 /* Reading 0 bytes when 0 bytes are available can go either way. */
7774 TEST_ASSERT( status == PSA_SUCCESS ||
7775 status == PSA_ERROR_INSUFFICIENT_DATA );
7776 continue;
7777 }
7778 else if( expected_capacity == 0 ||
7779 output_sizes[i] > expected_capacity )
7780 {
7781 /* Capacity exceeded. */
7782 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7783 expected_capacity = 0;
7784 continue;
7785 }
7786 /* Success. Check the read data. */
7787 PSA_ASSERT( status );
7788 if( output_sizes[i] != 0 )
7789 ASSERT_COMPARE( output_buffer, output_sizes[i],
7790 expected_outputs[i], output_sizes[i] );
7791 /* Check the operation status. */
7792 expected_capacity -= output_sizes[i];
7793 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7794 &current_capacity ) );
7795 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007796 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007797 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007798 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007799
7800exit:
7801 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007802 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007803 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7804 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007805 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007806 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007807}
7808/* END_CASE */
7809
7810/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007811void derive_full( int alg_arg,
7812 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007813 data_t *input1,
7814 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007815 int requested_capacity_arg )
7816{
Ronald Cron5425a212020-08-04 14:58:35 +02007817 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007818 psa_algorithm_t alg = alg_arg;
7819 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007820 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007821 unsigned char output_buffer[16];
7822 size_t expected_capacity = requested_capacity;
7823 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007825
Gilles Peskine8817f612018-12-18 00:18:46 +01007826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007827
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7829 psa_set_key_algorithm( &attributes, alg );
7830 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007831
Gilles Peskine049c7532019-05-15 20:22:09 +02007832 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007833 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007834
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007835 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7836 input1->x, input1->len,
7837 input2->x, input2->len,
7838 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007839 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007840
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007841 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007842 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007843 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007844
7845 /* Expansion phase. */
7846 while( current_capacity > 0 )
7847 {
7848 size_t read_size = sizeof( output_buffer );
7849 if( read_size > current_capacity )
7850 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007851 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007852 output_buffer,
7853 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007854 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007855 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007856 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007857 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007858 }
7859
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007860 /* Check that the operation refuses to go over capacity. */
7861 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007862 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007863
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007864 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007865
7866exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007867 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007868 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007869 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007870}
7871/* END_CASE */
7872
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007873/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007874void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007875 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007876 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007877 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007878 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007879{
7880 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7881 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007882 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007883 uint8_t *output_buffer = NULL;
7884 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007885 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7886 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7887 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007888
7889 ASSERT_ALLOC( output_buffer, expected_output->len );
7890 PSA_ASSERT( psa_crypto_init() );
7891
7892 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007893 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007894 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007895
7896 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007897 step, input->x, input->len ),
7898 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007899
7900 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7901 goto exit;
7902
7903 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7904 expected_output->len );
7905
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007906 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007907 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7908 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7909 expected_output->len );
7910
7911exit:
7912 mbedtls_free( output_buffer );
7913 psa_key_derivation_abort( &operation );
7914 PSA_DONE();
7915}
7916/* END_CASE */
7917
Janos Follathe60c9052019-07-03 13:51:30 +01007918/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007919void derive_key_exercise( int alg_arg,
7920 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007921 data_t *input1,
7922 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007923 int derived_type_arg,
7924 int derived_bits_arg,
7925 int derived_usage_arg,
7926 int derived_alg_arg )
7927{
Ronald Cron5425a212020-08-04 14:58:35 +02007928 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7929 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007930 psa_algorithm_t alg = alg_arg;
7931 psa_key_type_t derived_type = derived_type_arg;
7932 size_t derived_bits = derived_bits_arg;
7933 psa_key_usage_t derived_usage = derived_usage_arg;
7934 psa_algorithm_t derived_alg = derived_alg_arg;
7935 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007936 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007938 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007939
Gilles Peskine8817f612018-12-18 00:18:46 +01007940 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007941
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7943 psa_set_key_algorithm( &attributes, alg );
7944 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007945 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007946 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007947
7948 /* Derive a key. */
Manuel Pégourié-Gonnard9502b562023-01-04 13:16:53 +01007949 if ( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007950 input1->x, input1->len,
7951 input2->x, input2->len,
7952 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007953 goto exit;
7954
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007955 psa_set_key_usage_flags( &attributes, derived_usage );
7956 psa_set_key_algorithm( &attributes, derived_alg );
7957 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007958 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007959 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007960 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007961
7962 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007963 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007964 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7965 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007966
7967 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007968 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007969 goto exit;
7970
7971exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007972 /*
7973 * Key attributes may have been returned by psa_get_key_attributes()
7974 * thus reset them as required.
7975 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007976 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007977
7978 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007979 psa_destroy_key( base_key );
7980 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007981 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007982}
7983/* END_CASE */
7984
Janos Follath42fd8882019-07-03 14:17:09 +01007985/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007986void derive_key_export( int alg_arg,
7987 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007988 data_t *input1,
7989 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007990 int bytes1_arg,
7991 int bytes2_arg )
7992{
Ronald Cron5425a212020-08-04 14:58:35 +02007993 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7994 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007995 psa_algorithm_t alg = alg_arg;
7996 size_t bytes1 = bytes1_arg;
7997 size_t bytes2 = bytes2_arg;
7998 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007999 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008000 uint8_t *output_buffer = NULL;
8001 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008002 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8003 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02008004 size_t length;
8005
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008006 ASSERT_ALLOC( output_buffer, capacity );
8007 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01008008 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008009
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008010 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8011 psa_set_key_algorithm( &base_attributes, alg );
8012 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008013 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008014 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008015
8016 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008017 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8018 input1->x, input1->len,
8019 input2->x, input2->len,
8020 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008021 goto exit;
8022
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008023 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008024 output_buffer,
8025 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008026 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008027
8028 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008029 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8030 input1->x, input1->len,
8031 input2->x, input2->len,
8032 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008033 goto exit;
8034
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008035 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8036 psa_set_key_algorithm( &derived_attributes, 0 );
8037 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008038 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008039 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008040 &derived_key ) );
8041 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008042 export_buffer, bytes1,
8043 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008044 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02008045 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008046 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008047 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008048 &derived_key ) );
8049 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008050 export_buffer + bytes1, bytes2,
8051 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008052 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008053
8054 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008055 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8056 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008057
8058exit:
8059 mbedtls_free( output_buffer );
8060 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008061 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008062 psa_destroy_key( base_key );
8063 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008064 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008065}
8066/* END_CASE */
8067
8068/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008069void derive_key_type( int alg_arg,
8070 data_t *key_data,
8071 data_t *input1,
8072 data_t *input2,
8073 int key_type_arg, int bits_arg,
8074 data_t *expected_export )
8075{
8076 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8077 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8078 const psa_algorithm_t alg = alg_arg;
8079 const psa_key_type_t key_type = key_type_arg;
8080 const size_t bits = bits_arg;
8081 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8082 const size_t export_buffer_size =
8083 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8084 uint8_t *export_buffer = NULL;
8085 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8086 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8087 size_t export_length;
8088
8089 ASSERT_ALLOC( export_buffer, export_buffer_size );
8090 PSA_ASSERT( psa_crypto_init( ) );
8091
8092 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8093 psa_set_key_algorithm( &base_attributes, alg );
8094 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8095 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8096 &base_key ) );
8097
Przemek Stekielc85f0912022-03-08 11:37:54 +01008098 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008099 &operation, base_key, alg,
8100 input1->x, input1->len,
8101 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008102 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008103 goto exit;
8104
8105 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8106 psa_set_key_algorithm( &derived_attributes, 0 );
8107 psa_set_key_type( &derived_attributes, key_type );
8108 psa_set_key_bits( &derived_attributes, bits );
8109 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8110 &derived_key ) );
8111
8112 PSA_ASSERT( psa_export_key( derived_key,
8113 export_buffer, export_buffer_size,
8114 &export_length ) );
8115 ASSERT_COMPARE( export_buffer, export_length,
8116 expected_export->x, expected_export->len );
8117
8118exit:
8119 mbedtls_free( export_buffer );
8120 psa_key_derivation_abort( &operation );
8121 psa_destroy_key( base_key );
8122 psa_destroy_key( derived_key );
8123 PSA_DONE( );
8124}
8125/* END_CASE */
8126
8127/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008128void derive_key( int alg_arg,
8129 data_t *key_data, data_t *input1, data_t *input2,
8130 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008131 int expected_status_arg,
8132 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008133{
Ronald Cron5425a212020-08-04 14:58:35 +02008134 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8135 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008136 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008137 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008138 size_t bits = bits_arg;
8139 psa_status_t expected_status = expected_status_arg;
8140 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8141 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8142 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8143
8144 PSA_ASSERT( psa_crypto_init( ) );
8145
8146 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8147 psa_set_key_algorithm( &base_attributes, alg );
8148 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8149 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008150 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008151
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008152 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8153 input1->x, input1->len,
8154 input2->x, input2->len,
8155 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008156 goto exit;
8157
8158 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8159 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008160 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008161 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008162
8163 psa_status_t status =
8164 psa_key_derivation_output_key( &derived_attributes,
8165 &operation,
8166 &derived_key );
8167 if( is_large_output > 0 )
8168 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8169 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008170
8171exit:
8172 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008173 psa_destroy_key( base_key );
8174 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008175 PSA_DONE( );
8176}
8177/* END_CASE */
8178
8179/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008180void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008181 int our_key_type_arg, int our_key_alg_arg,
8182 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008183 int expected_status_arg )
8184{
Ronald Cron5425a212020-08-04 14:58:35 +02008185 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008186 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008187 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008188 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008189 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008190 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008191 psa_status_t expected_status = expected_status_arg;
8192 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008193
Gilles Peskine8817f612018-12-18 00:18:46 +01008194 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008195
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008196 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008197 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008198 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008199 PSA_ASSERT( psa_import_key( &attributes,
8200 our_key_data->x, our_key_data->len,
8201 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008202
Gilles Peskine77f40d82019-04-11 21:27:06 +02008203 /* The tests currently include inputs that should fail at either step.
8204 * Test cases that fail at the setup step should be changed to call
8205 * key_derivation_setup instead, and this function should be renamed
8206 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008207 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008208 if( status == PSA_SUCCESS )
8209 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008210 TEST_EQUAL( psa_key_derivation_key_agreement(
8211 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8212 our_key,
8213 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008214 expected_status );
8215 }
8216 else
8217 {
8218 TEST_ASSERT( status == expected_status );
8219 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008220
8221exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008222 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008223 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008224 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008225}
8226/* END_CASE */
8227
8228/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008229void raw_key_agreement( int alg_arg,
8230 int our_key_type_arg, data_t *our_key_data,
8231 data_t *peer_key_data,
8232 data_t *expected_output )
8233{
Ronald Cron5425a212020-08-04 14:58:35 +02008234 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008235 psa_algorithm_t alg = alg_arg;
8236 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008237 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008238 unsigned char *output = NULL;
8239 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008240 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008241
Gilles Peskinef0cba732019-04-11 22:12:38 +02008242 PSA_ASSERT( psa_crypto_init( ) );
8243
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008244 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8245 psa_set_key_algorithm( &attributes, alg );
8246 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008247 PSA_ASSERT( psa_import_key( &attributes,
8248 our_key_data->x, our_key_data->len,
8249 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008250
gabor-mezei-armceface22021-01-21 12:26:17 +01008251 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8252 key_bits = psa_get_key_bits( &attributes );
8253
Gilles Peskine992bee82022-04-13 23:25:52 +02008254 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008255 TEST_LE_U( expected_output->len,
8256 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8257 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8258 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008259
8260 /* Good case with exact output size */
8261 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008262 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8263 peer_key_data->x, peer_key_data->len,
8264 output, expected_output->len,
8265 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008266 ASSERT_COMPARE( output, output_length,
8267 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008268 mbedtls_free( output );
8269 output = NULL;
8270 output_length = ~0;
8271
8272 /* Larger buffer */
8273 ASSERT_ALLOC( output, expected_output->len + 1 );
8274 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8275 peer_key_data->x, peer_key_data->len,
8276 output, expected_output->len + 1,
8277 &output_length ) );
8278 ASSERT_COMPARE( output, output_length,
8279 expected_output->x, expected_output->len );
8280 mbedtls_free( output );
8281 output = NULL;
8282 output_length = ~0;
8283
8284 /* Buffer too small */
8285 ASSERT_ALLOC( output, expected_output->len - 1 );
8286 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8287 peer_key_data->x, peer_key_data->len,
8288 output, expected_output->len - 1,
8289 &output_length ),
8290 PSA_ERROR_BUFFER_TOO_SMALL );
8291 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008292 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008293 mbedtls_free( output );
8294 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008295
8296exit:
8297 mbedtls_free( output );
8298 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008299 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008300}
8301/* END_CASE */
8302
8303/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008304void key_agreement_capacity( int alg_arg,
8305 int our_key_type_arg, data_t *our_key_data,
8306 data_t *peer_key_data,
8307 int expected_capacity_arg )
8308{
Ronald Cron5425a212020-08-04 14:58:35 +02008309 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008310 psa_algorithm_t alg = alg_arg;
8311 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008312 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008314 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008315 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008316
Gilles Peskine8817f612018-12-18 00:18:46 +01008317 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008318
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008319 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8320 psa_set_key_algorithm( &attributes, alg );
8321 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008322 PSA_ASSERT( psa_import_key( &attributes,
8323 our_key_data->x, our_key_data->len,
8324 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008325
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008326 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008327 PSA_ASSERT( psa_key_derivation_key_agreement(
8328 &operation,
8329 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8330 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008331 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8332 {
8333 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008334 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008335 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008336 NULL, 0 ) );
8337 }
Gilles Peskine59685592018-09-18 12:11:34 +02008338
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008339 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008340 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008341 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008342 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008343
Gilles Peskinebf491972018-10-25 22:36:12 +02008344 /* Test the actual capacity by reading the output. */
8345 while( actual_capacity > sizeof( output ) )
8346 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008347 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008348 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008349 actual_capacity -= sizeof( output );
8350 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008351 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008352 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008353 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008354 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008355
Gilles Peskine59685592018-09-18 12:11:34 +02008356exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008357 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008358 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008359 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008360}
8361/* END_CASE */
8362
8363/* BEGIN_CASE */
8364void key_agreement_output( int alg_arg,
8365 int our_key_type_arg, data_t *our_key_data,
8366 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008367 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008368{
Ronald Cron5425a212020-08-04 14:58:35 +02008369 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008370 psa_algorithm_t alg = alg_arg;
8371 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008374 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008375
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008376 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8377 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008378
Gilles Peskine8817f612018-12-18 00:18:46 +01008379 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008380
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008381 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8382 psa_set_key_algorithm( &attributes, alg );
8383 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008384 PSA_ASSERT( psa_import_key( &attributes,
8385 our_key_data->x, our_key_data->len,
8386 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008387
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008388 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008389 PSA_ASSERT( psa_key_derivation_key_agreement(
8390 &operation,
8391 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8392 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008393 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8394 {
8395 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008396 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008397 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008398 NULL, 0 ) );
8399 }
Gilles Peskine59685592018-09-18 12:11:34 +02008400
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008401 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008402 actual_output,
8403 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008404 ASSERT_COMPARE( actual_output, expected_output1->len,
8405 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008406 if( expected_output2->len != 0 )
8407 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008408 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008409 actual_output,
8410 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008411 ASSERT_COMPARE( actual_output, expected_output2->len,
8412 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008413 }
Gilles Peskine59685592018-09-18 12:11:34 +02008414
8415exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008416 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008417 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008418 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008419 mbedtls_free( actual_output );
8420}
8421/* END_CASE */
8422
8423/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008424void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008425{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008426 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008427 unsigned char *output = NULL;
8428 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008429 size_t i;
8430 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008431
Simon Butcher49f8e312020-03-03 15:51:50 +00008432 TEST_ASSERT( bytes_arg >= 0 );
8433
Gilles Peskine91892022021-02-08 19:50:26 +01008434 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008435 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008436
Gilles Peskine8817f612018-12-18 00:18:46 +01008437 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008438
Gilles Peskinea50d7392018-06-21 10:22:13 +02008439 /* Run several times, to ensure that every output byte will be
8440 * nonzero at least once with overwhelming probability
8441 * (2^(-8*number_of_runs)). */
8442 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008443 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008444 if( bytes != 0 )
8445 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008446 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008447
Gilles Peskinea50d7392018-06-21 10:22:13 +02008448 for( i = 0; i < bytes; i++ )
8449 {
8450 if( output[i] != 0 )
8451 ++changed[i];
8452 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008453 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008454
8455 /* Check that every byte was changed to nonzero at least once. This
8456 * validates that psa_generate_random is overwriting every byte of
8457 * the output buffer. */
8458 for( i = 0; i < bytes; i++ )
8459 {
8460 TEST_ASSERT( changed[i] != 0 );
8461 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008462
8463exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008464 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008465 mbedtls_free( output );
8466 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008467}
8468/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008469
8470/* BEGIN_CASE */
8471void generate_key( int type_arg,
8472 int bits_arg,
8473 int usage_arg,
8474 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008475 int expected_status_arg,
8476 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008477{
Ronald Cron5425a212020-08-04 14:58:35 +02008478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008479 psa_key_type_t type = type_arg;
8480 psa_key_usage_t usage = usage_arg;
8481 size_t bits = bits_arg;
8482 psa_algorithm_t alg = alg_arg;
8483 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008485 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008486
Gilles Peskine8817f612018-12-18 00:18:46 +01008487 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008488
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008489 psa_set_key_usage_flags( &attributes, usage );
8490 psa_set_key_algorithm( &attributes, alg );
8491 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008492 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008493
8494 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008495 psa_status_t status = psa_generate_key( &attributes, &key );
8496
8497 if( is_large_key > 0 )
8498 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8499 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008500 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008501 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008502
8503 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008504 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008505 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8506 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008507
Gilles Peskine818ca122018-06-20 18:16:48 +02008508 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008509 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008510 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008511
8512exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008513 /*
8514 * Key attributes may have been returned by psa_get_key_attributes()
8515 * thus reset them as required.
8516 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008517 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008518
Ronald Cron5425a212020-08-04 14:58:35 +02008519 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008520 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008521}
8522/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008523
Ronald Cronee414c72021-03-18 18:50:08 +01008524/* 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 +02008525void generate_key_rsa( int bits_arg,
8526 data_t *e_arg,
8527 int expected_status_arg )
8528{
Ronald Cron5425a212020-08-04 14:58:35 +02008529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008530 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008531 size_t bits = bits_arg;
8532 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8533 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8534 psa_status_t expected_status = expected_status_arg;
8535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8536 uint8_t *exported = NULL;
8537 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008538 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008539 size_t exported_length = SIZE_MAX;
8540 uint8_t *e_read_buffer = NULL;
8541 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008542 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008543 size_t e_read_length = SIZE_MAX;
8544
8545 if( e_arg->len == 0 ||
8546 ( e_arg->len == 3 &&
8547 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8548 {
8549 is_default_public_exponent = 1;
8550 e_read_size = 0;
8551 }
8552 ASSERT_ALLOC( e_read_buffer, e_read_size );
8553 ASSERT_ALLOC( exported, exported_size );
8554
8555 PSA_ASSERT( psa_crypto_init( ) );
8556
8557 psa_set_key_usage_flags( &attributes, usage );
8558 psa_set_key_algorithm( &attributes, alg );
8559 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8560 e_arg->x, e_arg->len ) );
8561 psa_set_key_bits( &attributes, bits );
8562
8563 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008564 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008565 if( expected_status != PSA_SUCCESS )
8566 goto exit;
8567
8568 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008569 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008570 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8571 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8572 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8573 e_read_buffer, e_read_size,
8574 &e_read_length ) );
8575 if( is_default_public_exponent )
8576 TEST_EQUAL( e_read_length, 0 );
8577 else
8578 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8579
8580 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008581 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008582 goto exit;
8583
8584 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008585 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008586 exported, exported_size,
8587 &exported_length ) );
8588 {
8589 uint8_t *p = exported;
8590 uint8_t *end = exported + exported_length;
8591 size_t len;
8592 /* RSAPublicKey ::= SEQUENCE {
8593 * modulus INTEGER, -- n
8594 * publicExponent INTEGER } -- e
8595 */
8596 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008597 MBEDTLS_ASN1_SEQUENCE |
8598 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008599 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008600 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8601 MBEDTLS_ASN1_INTEGER ) );
8602 if( len >= 1 && p[0] == 0 )
8603 {
8604 ++p;
8605 --len;
8606 }
8607 if( e_arg->len == 0 )
8608 {
8609 TEST_EQUAL( len, 3 );
8610 TEST_EQUAL( p[0], 1 );
8611 TEST_EQUAL( p[1], 0 );
8612 TEST_EQUAL( p[2], 1 );
8613 }
8614 else
8615 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8616 }
8617
8618exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008619 /*
8620 * Key attributes may have been returned by psa_get_key_attributes() or
8621 * set by psa_set_key_domain_parameters() thus reset them as required.
8622 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008623 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008624
Ronald Cron5425a212020-08-04 14:58:35 +02008625 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008626 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008627 mbedtls_free( e_read_buffer );
8628 mbedtls_free( exported );
8629}
8630/* END_CASE */
8631
Darryl Greend49a4992018-06-18 17:27:26 +01008632/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008633void persistent_key_load_key_from_storage( data_t *data,
8634 int type_arg, int bits_arg,
8635 int usage_flags_arg, int alg_arg,
8636 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008637{
Ronald Cron71016a92020-08-28 19:01:50 +02008638 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8641 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008642 psa_key_type_t type = type_arg;
8643 size_t bits = bits_arg;
8644 psa_key_usage_t usage_flags = usage_flags_arg;
8645 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008646 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008647 unsigned char *first_export = NULL;
8648 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008649 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008650 size_t first_exported_length;
8651 size_t second_exported_length;
8652
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008653 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8654 {
8655 ASSERT_ALLOC( first_export, export_size );
8656 ASSERT_ALLOC( second_export, export_size );
8657 }
Darryl Greend49a4992018-06-18 17:27:26 +01008658
Gilles Peskine8817f612018-12-18 00:18:46 +01008659 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008660
Gilles Peskinec87af662019-05-15 16:12:22 +02008661 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008662 psa_set_key_usage_flags( &attributes, usage_flags );
8663 psa_set_key_algorithm( &attributes, alg );
8664 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008665 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008666
Darryl Green0c6575a2018-11-07 16:05:30 +00008667 switch( generation_method )
8668 {
8669 case IMPORT_KEY:
8670 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008671 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008672 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008673 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008674
Darryl Green0c6575a2018-11-07 16:05:30 +00008675 case GENERATE_KEY:
8676 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008677 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008678 break;
8679
8680 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008681#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008682 {
8683 /* Create base key */
8684 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8685 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8686 psa_set_key_usage_flags( &base_attributes,
8687 PSA_KEY_USAGE_DERIVE );
8688 psa_set_key_algorithm( &base_attributes, derive_alg );
8689 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008690 PSA_ASSERT( psa_import_key( &base_attributes,
8691 data->x, data->len,
8692 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008693 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008694 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008695 PSA_ASSERT( psa_key_derivation_input_key(
8696 &operation,
8697 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008698 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008699 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008700 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008701 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8702 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008703 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008704 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008705 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008706 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008707 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008708#else
8709 TEST_ASSUME( ! "KDF not supported in this configuration" );
8710#endif
8711 break;
8712
8713 default:
8714 TEST_ASSERT( ! "generation_method not implemented in test" );
8715 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008716 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008717 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008718
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008719 /* Export the key if permitted by the key policy. */
8720 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8721 {
Ronald Cron5425a212020-08-04 14:58:35 +02008722 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008723 first_export, export_size,
8724 &first_exported_length ) );
8725 if( generation_method == IMPORT_KEY )
8726 ASSERT_COMPARE( data->x, data->len,
8727 first_export, first_exported_length );
8728 }
Darryl Greend49a4992018-06-18 17:27:26 +01008729
8730 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008731 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008732 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008733 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008734
Darryl Greend49a4992018-06-18 17:27:26 +01008735 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008737 TEST_ASSERT( mbedtls_svc_key_id_equal(
8738 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008739 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8740 PSA_KEY_LIFETIME_PERSISTENT );
8741 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8742 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008743 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008744 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008745 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008746
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008747 /* Export the key again if permitted by the key policy. */
8748 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008749 {
Ronald Cron5425a212020-08-04 14:58:35 +02008750 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008751 second_export, export_size,
8752 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008753 ASSERT_COMPARE( first_export, first_exported_length,
8754 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008755 }
8756
8757 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008758 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008759 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008760
8761exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008762 /*
8763 * Key attributes may have been returned by psa_get_key_attributes()
8764 * thus reset them as required.
8765 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008766 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008767
Darryl Greend49a4992018-06-18 17:27:26 +01008768 mbedtls_free( first_export );
8769 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008770 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008771 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008772 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008773 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008774}
8775/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008776
Neil Armstronga557cb82022-06-10 08:58:32 +02008777/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008778void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8779 int primitive_arg, int hash_arg, int role_arg,
Valerio Setti1070aed2022-11-11 19:37:31 +01008780 int test_input, data_t *pw_data,
8781 int inj_err_type_arg,
8782 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008783{
8784 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8785 psa_pake_operation_t operation = psa_pake_operation_init();
8786 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008787 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008788 psa_key_type_t key_type_pw = key_type_pw_arg;
8789 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008790 psa_algorithm_t hash_alg = hash_arg;
8791 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008792 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01008794 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
8795 psa_status_t expected_error = expected_error_arg;
8796 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008797 unsigned char *output_buffer = NULL;
8798 size_t output_len = 0;
8799
8800 PSA_INIT( );
8801
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008802 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8803 PSA_PAKE_STEP_KEY_SHARE);
8804 ASSERT_ALLOC( output_buffer, buf_size );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008805
8806 if( pw_data->len > 0 )
8807 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008808 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008809 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008810 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008811 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8812 &key ) );
8813 }
8814
8815 psa_pake_cs_set_algorithm( &cipher_suite, alg );
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008816 psa_pake_cs_set_primitive( &cipher_suite, primitive );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008817 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8818
Neil Armstrong645cccd2022-06-08 17:36:23 +02008819 PSA_ASSERT( psa_pake_abort( &operation ) );
8820
Valerio Setti1070aed2022-11-11 19:37:31 +01008821 if ( inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS )
8822 {
8823 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8824 expected_error );
8825 PSA_ASSERT( psa_pake_abort( &operation ) );
8826 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8827 expected_error );
8828 PSA_ASSERT( psa_pake_abort( &operation ) );
8829 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8830 expected_error );
8831 PSA_ASSERT( psa_pake_abort( &operation ) );
8832 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8833 expected_error );
8834 PSA_ASSERT( psa_pake_abort( &operation ) );
8835 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8836 NULL, 0, NULL ),
8837 expected_error );
8838 PSA_ASSERT( psa_pake_abort( &operation ) );
8839 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
8840 expected_error );
8841 PSA_ASSERT( psa_pake_abort( &operation ) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008842 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008843 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008844
Valerio Setti1070aed2022-11-11 19:37:31 +01008845 status = psa_pake_setup( &operation, &cipher_suite );
8846 if (status != PSA_SUCCESS)
8847 {
8848 TEST_EQUAL( status, expected_error );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008849 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008850 }
8851
8852 if( inj_err_type == INJECT_ERR_DUPLICATE_SETUP )
8853 {
8854 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8855 expected_error );
8856 goto exit;
8857 }
8858
8859 status = psa_pake_set_role( &operation, role);
8860 if ( status != PSA_SUCCESS )
8861 {
8862 TEST_EQUAL( status, expected_error );
8863 goto exit;
8864 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008865
8866 if( pw_data->len > 0 )
8867 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008868 status = psa_pake_set_password_key( &operation, key );
8869 if ( status != PSA_SUCCESS )
8870 {
8871 TEST_EQUAL( status, expected_error );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008872 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008873 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008874 }
8875
Valerio Setti1070aed2022-11-11 19:37:31 +01008876 if ( inj_err_type == INJECT_ERR_INVALID_USER )
8877 {
8878 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8879 PSA_ERROR_INVALID_ARGUMENT );
8880 goto exit;
8881 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008882
Valerio Setti1070aed2022-11-11 19:37:31 +01008883 if ( inj_err_type == INJECT_ERR_INVALID_PEER )
8884 {
8885 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8886 PSA_ERROR_INVALID_ARGUMENT );
8887 goto exit;
8888 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008889
Valerio Setti1070aed2022-11-11 19:37:31 +01008890 if ( inj_err_type == INJECT_ERR_SET_USER )
8891 {
8892 const uint8_t unsupported_id[] = "abcd";
8893 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8894 PSA_ERROR_NOT_SUPPORTED );
8895 goto exit;
8896 }
8897
8898 if ( inj_err_type == INJECT_ERR_SET_PEER )
8899 {
8900 const uint8_t unsupported_id[] = "abcd";
8901 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8902 PSA_ERROR_NOT_SUPPORTED );
8903 goto exit;
8904 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008905
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008906 const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive,
8907 PSA_PAKE_STEP_KEY_SHARE );
8908 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive,
8909 PSA_PAKE_STEP_ZK_PUBLIC );
8910 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive,
8911 PSA_PAKE_STEP_ZK_PROOF );
8912
Valerio Setti1070aed2022-11-11 19:37:31 +01008913 if ( test_input )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008914 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008915 if ( inj_err_type == INJECT_EMPTY_IO_BUFFER )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008916 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008917 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0 ),
8918 PSA_ERROR_INVALID_ARGUMENT );
8919 goto exit;
8920 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008921
Valerio Setti1070aed2022-11-11 19:37:31 +01008922 if ( inj_err_type == INJECT_UNKNOWN_STEP )
8923 {
8924 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8925 output_buffer, size_zk_proof ),
8926 PSA_ERROR_INVALID_ARGUMENT );
8927 goto exit;
8928 }
8929
8930 if ( inj_err_type == INJECT_INVALID_FIRST_STEP )
8931 {
8932 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8933 output_buffer, size_zk_proof ),
8934 PSA_ERROR_BAD_STATE );
8935 goto exit;
8936 }
8937
8938 status = psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8939 output_buffer, size_key_share );
8940 if ( status != PSA_SUCCESS )
8941 {
8942 TEST_EQUAL( status, expected_error);
8943 goto exit;
8944 }
8945
8946 if ( inj_err_type == INJECT_WRONG_BUFFER_SIZE )
8947 {
8948 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8949 output_buffer, size_zk_public + 1 ),
8950 PSA_ERROR_INVALID_ARGUMENT );
8951 goto exit;
8952 }
8953
8954 if ( inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE )
8955 {
8956 // Just trigger any kind of error. We don't care about the result here
8957 psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8958 output_buffer, size_zk_public + 1 );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008959 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008960 output_buffer, size_zk_public ),
Valerio Setti1070aed2022-11-11 19:37:31 +01008961 PSA_ERROR_BAD_STATE );
8962 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008963 }
Valerio Setti1070aed2022-11-11 19:37:31 +01008964 } else {
8965 if ( inj_err_type == INJECT_EMPTY_IO_BUFFER )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008966 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008967 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8968 NULL, 0, NULL ),
8969 PSA_ERROR_INVALID_ARGUMENT );
8970 goto exit;
8971 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008972
Valerio Setti1070aed2022-11-11 19:37:31 +01008973 if ( inj_err_type == INJECT_UNKNOWN_STEP )
8974 {
8975 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8976 output_buffer, buf_size, &output_len ),
8977 PSA_ERROR_INVALID_ARGUMENT );
8978 goto exit;
8979 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008980
Valerio Setti1070aed2022-11-11 19:37:31 +01008981 if ( inj_err_type == INJECT_INVALID_FIRST_STEP )
8982 {
8983 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8984 output_buffer, buf_size, &output_len ),
8985 PSA_ERROR_BAD_STATE );
8986 goto exit;
8987 }
8988
8989 status = psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8990 output_buffer, buf_size, &output_len );
8991 if ( status != PSA_SUCCESS )
8992 {
8993 TEST_EQUAL( status, expected_error);
8994 goto exit;
8995 }
8996
8997 TEST_ASSERT( output_len > 0 );
8998
8999 if ( inj_err_type == INJECT_WRONG_BUFFER_SIZE )
9000 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009001 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Valerio Setti1070aed2022-11-11 19:37:31 +01009002 output_buffer, size_zk_public - 1, &output_len ),
9003 PSA_ERROR_BUFFER_TOO_SMALL );
9004 goto exit;
9005 }
9006
9007 if ( inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE )
9008 {
9009 // Just trigger any kind of error. We don't care about the result here
9010 psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
9011 output_buffer, size_zk_public - 1, &output_len );
9012 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
9013 output_buffer, buf_size, &output_len ),
9014 PSA_ERROR_BAD_STATE );
9015 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009016 }
9017 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009018
9019exit:
9020 PSA_ASSERT( psa_destroy_key( key ) );
9021 PSA_ASSERT( psa_pake_abort( &operation ) );
9022 mbedtls_free( output_buffer );
9023 PSA_DONE( );
9024}
9025/* END_CASE */
9026
Neil Armstronga557cb82022-06-10 08:58:32 +02009027/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009028void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
9029 int client_input_first, int inject_error,
9030 data_t *pw_data )
9031{
9032 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9033 psa_pake_operation_t server = psa_pake_operation_init();
9034 psa_pake_operation_t client = psa_pake_operation_init();
9035 psa_algorithm_t alg = alg_arg;
9036 psa_algorithm_t hash_alg = hash_arg;
9037 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9038 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9039
9040 PSA_INIT( );
9041
9042 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9043 psa_set_key_algorithm( &attributes, alg );
9044 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9045 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9046 &key ) );
9047
9048 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9049 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9050 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9051
9052
9053 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9054 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9055
9056 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9057 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9058
9059 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9060 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9061
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009062 ecjpake_do_round( alg, primitive_arg, &server, &client,
9063 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009064
9065 if( inject_error == 1 || inject_error == 2 )
9066 goto exit;
9067
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009068 ecjpake_do_round( alg, primitive_arg, &server, &client,
9069 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009070
9071exit:
9072 psa_destroy_key( key );
9073 psa_pake_abort( &server );
9074 psa_pake_abort( &client );
9075 PSA_DONE( );
9076}
9077/* END_CASE */
9078
9079/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009080void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02009081 int derive_alg_arg, data_t *pw_data,
Valerio Setti1070aed2022-11-11 19:37:31 +01009082 int client_input_first, int inj_err_type_arg )
Neil Armstrongd597bc72022-05-25 11:28:39 +02009083{
9084 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9085 psa_pake_operation_t server = psa_pake_operation_init();
9086 psa_pake_operation_t client = psa_pake_operation_init();
9087 psa_algorithm_t alg = alg_arg;
9088 psa_algorithm_t hash_alg = hash_arg;
9089 psa_algorithm_t derive_alg = derive_alg_arg;
9090 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9091 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9092 psa_key_derivation_operation_t server_derive =
9093 PSA_KEY_DERIVATION_OPERATION_INIT;
9094 psa_key_derivation_operation_t client_derive =
9095 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009096 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009097
9098 PSA_INIT( );
9099
Neil Armstrongd597bc72022-05-25 11:28:39 +02009100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9101 psa_set_key_algorithm( &attributes, alg );
9102 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9103 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9104 &key ) );
9105
9106 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9107 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9108 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9109
Neil Armstrong1e855602022-06-15 11:32:11 +02009110 /* Get shared key */
9111 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
9112 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
9113
9114 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
9115 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
9116 {
9117 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
9118 PSA_KEY_DERIVATION_INPUT_SEED,
9119 (const uint8_t*) "", 0) );
9120 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
9121 PSA_KEY_DERIVATION_INPUT_SEED,
9122 (const uint8_t*) "", 0) );
9123 }
9124
Neil Armstrongd597bc72022-05-25 11:28:39 +02009125 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9126 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9127
9128 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9129 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9130
9131 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9132 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9133
Valerio Setti1070aed2022-11-11 19:37:31 +01009134 if( inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1 )
9135 {
9136 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009137 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009138 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009139 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009140 goto exit;
9141 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009142
Neil Armstrongf983caf2022-06-15 15:27:48 +02009143 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009144 ecjpake_do_round( alg, primitive_arg, &server, &client,
9145 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009146
Valerio Setti1070aed2022-11-11 19:37:31 +01009147 if ( inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2 )
9148 {
9149 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009150 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009151 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009152 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009153 goto exit;
9154 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009155
Neil Armstrongf983caf2022-06-15 15:27:48 +02009156 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009157 ecjpake_do_round( alg, primitive_arg, &server, &client,
9158 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009159
Neil Armstrongd597bc72022-05-25 11:28:39 +02009160 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9161 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9162
9163exit:
9164 psa_key_derivation_abort( &server_derive );
9165 psa_key_derivation_abort( &client_derive );
9166 psa_destroy_key( key );
9167 psa_pake_abort( &server );
9168 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009169 PSA_DONE( );
9170}
9171/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009172
9173/* BEGIN_CASE */
9174void ecjpake_size_macros( )
9175{
9176 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9177 const size_t bits = 256;
9178 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9179 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9180 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9181 PSA_ECC_FAMILY_SECP_R1 );
9182
9183 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9184 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9185 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9186 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9187 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9188 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9189 /* The output for ZK_PROOF is the same bitsize as the curve */
9190 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9191 PSA_BITS_TO_BYTES( bits ) );
9192
9193 /* Input sizes are the same as output sizes */
9194 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9195 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9196 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9197 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9198 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9199 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9200
9201 /* These inequalities will always hold even when other PAKEs are added */
9202 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9203 PSA_PAKE_OUTPUT_MAX_SIZE );
9204 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9205 PSA_PAKE_OUTPUT_MAX_SIZE );
9206 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9207 PSA_PAKE_OUTPUT_MAX_SIZE );
9208 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9209 PSA_PAKE_INPUT_MAX_SIZE );
9210 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9211 PSA_PAKE_INPUT_MAX_SIZE );
9212 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9213 PSA_PAKE_INPUT_MAX_SIZE );
9214}
9215/* END_CASE */