blob: f84a0cc3f58f952c36936041e304657d848f6951 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020024#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010025
Gilles Peskine4023c012021-05-27 13:21:20 +020026/* If this comes up, it's a bug in the test code or in the test data. */
27#define UNUSED 0xdeadbeef
28
Dave Rodgman647791d2021-06-23 12:49:59 +010029/* Assert that an operation is (not) active.
30 * This serves as a proxy for checking if the operation is aborted. */
31#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
32#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010033
Przemek Stekiel7c795482022-11-15 22:26:12 +010034#if defined(PSA_WANT_ALG_JPAKE)
35void ecjpake_operation_setup( psa_pake_operation_t *operation,
36 psa_pake_cipher_suite_t *cipher_suite,
37 psa_pake_role_t role,
38 mbedtls_svc_key_id_t key,
39 size_t key_available )
40{
41 *operation = psa_pake_operation_init();
42
43 TEST_EQUAL( psa_pake_setup( operation, cipher_suite ),
44 PSA_SUCCESS );
45
46 TEST_EQUAL( psa_pake_set_role( operation, role),
47 PSA_SUCCESS );
48
49 if( key_available )
50 TEST_EQUAL( psa_pake_set_password_key( operation, key ),
51 PSA_SUCCESS );
52exit:
53 return;
54}
55#endif
56
Jaeden Amerof24c7f82018-06-27 17:20:43 +010057/** An invalid export length that will never be set by psa_export_key(). */
58static const size_t INVALID_EXPORT_LENGTH = ~0U;
59
Gilles Peskinea7aa4422018-08-14 15:17:54 +020060/** Test if a buffer contains a constant byte value.
61 *
62 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020063 *
64 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020065 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020066 * \param size Size of the buffer in bytes.
67 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020068 * \return 1 if the buffer is all-bits-zero.
69 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020071static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020072{
73 size_t i;
74 for( i = 0; i < size; i++ )
75 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020076 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020077 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020078 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020079 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020080}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010081#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020082/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
83static int asn1_write_10x( unsigned char **p,
84 unsigned char *start,
85 size_t bits,
86 unsigned char x )
87{
88 int ret;
89 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020090 if( bits == 0 )
91 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
92 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020093 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030094 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020095 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
96 *p -= len;
97 ( *p )[len-1] = x;
98 if( bits % 8 == 0 )
99 ( *p )[1] |= 1;
100 else
101 ( *p )[0] |= 1 << ( bits % 8 );
102 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
103 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
104 MBEDTLS_ASN1_INTEGER ) );
105 return( len );
106}
107
108static int construct_fake_rsa_key( unsigned char *buffer,
109 size_t buffer_size,
110 unsigned char **p,
111 size_t bits,
112 int keypair )
113{
114 size_t half_bits = ( bits + 1 ) / 2;
115 int ret;
116 int len = 0;
117 /* Construct something that looks like a DER encoding of
118 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
119 * RSAPrivateKey ::= SEQUENCE {
120 * version Version,
121 * modulus INTEGER, -- n
122 * publicExponent INTEGER, -- e
123 * privateExponent INTEGER, -- d
124 * prime1 INTEGER, -- p
125 * prime2 INTEGER, -- q
126 * exponent1 INTEGER, -- d mod (p-1)
127 * exponent2 INTEGER, -- d mod (q-1)
128 * coefficient INTEGER, -- (inverse of q) mod p
129 * otherPrimeInfos OtherPrimeInfos OPTIONAL
130 * }
131 * Or, for a public key, the same structure with only
132 * version, modulus and publicExponent.
133 */
134 *p = buffer + buffer_size;
135 if( keypair )
136 {
137 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
138 asn1_write_10x( p, buffer, half_bits, 1 ) );
139 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
140 asn1_write_10x( p, buffer, half_bits, 1 ) );
141 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
142 asn1_write_10x( p, buffer, half_bits, 1 ) );
143 MBEDTLS_ASN1_CHK_ADD( len, /* q */
144 asn1_write_10x( p, buffer, half_bits, 1 ) );
145 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
146 asn1_write_10x( p, buffer, half_bits, 3 ) );
147 MBEDTLS_ASN1_CHK_ADD( len, /* d */
148 asn1_write_10x( p, buffer, bits, 1 ) );
149 }
150 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
151 asn1_write_10x( p, buffer, 17, 1 ) );
152 MBEDTLS_ASN1_CHK_ADD( len, /* n */
153 asn1_write_10x( p, buffer, bits, 1 ) );
154 if( keypair )
155 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
156 mbedtls_asn1_write_int( p, buffer, 0 ) );
157 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
158 {
159 const unsigned char tag =
160 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
161 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
162 }
163 return( len );
164}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100165#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200166
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100167int exercise_mac_setup( psa_key_type_t key_type,
168 const unsigned char *key_bytes,
169 size_t key_length,
170 psa_algorithm_t alg,
171 psa_mac_operation_t *operation,
172 psa_status_t *status )
173{
Ronald Cron5425a212020-08-04 14:58:35 +0200174 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200175 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100176
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200178 psa_set_key_algorithm( &attributes, alg );
179 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200180 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100181
Ronald Cron5425a212020-08-04 14:58:35 +0200182 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100183 /* Whether setup succeeded or failed, abort must succeed. */
184 PSA_ASSERT( psa_mac_abort( operation ) );
185 /* If setup failed, reproduce the failure, so that the caller can
186 * test the resulting state of the operation object. */
187 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 {
Ronald Cron5425a212020-08-04 14:58:35 +0200189 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 }
191
Ronald Cron5425a212020-08-04 14:58:35 +0200192 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 return( 1 );
194
195exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200196 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 return( 0 );
198}
199
200int exercise_cipher_setup( psa_key_type_t key_type,
201 const unsigned char *key_bytes,
202 size_t key_length,
203 psa_algorithm_t alg,
204 psa_cipher_operation_t *operation,
205 psa_status_t *status )
206{
Ronald Cron5425a212020-08-04 14:58:35 +0200207 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100209
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200210 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
211 psa_set_key_algorithm( &attributes, alg );
212 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200213 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100214
Ronald Cron5425a212020-08-04 14:58:35 +0200215 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100216 /* Whether setup succeeded or failed, abort must succeed. */
217 PSA_ASSERT( psa_cipher_abort( operation ) );
218 /* If setup failed, reproduce the failure, so that the caller can
219 * test the resulting state of the operation object. */
220 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100221 {
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100223 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100224 }
225
Ronald Cron5425a212020-08-04 14:58:35 +0200226 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 return( 1 );
228
229exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200230 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100231 return( 0 );
232}
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200235{
236 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200237 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200238 uint8_t buffer[1];
239 size_t length;
240 int ok = 0;
241
Ronald Cronecfb2372020-07-23 17:13:42 +0200242 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200243 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
244 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
245 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200246 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000247 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200248 TEST_EQUAL(
249 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
250 TEST_EQUAL(
251 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200252 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200253 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
254 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
255 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
256 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
257
Ronald Cron5425a212020-08-04 14:58:35 +0200258 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000259 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200260 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200261 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000262 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200263
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200264 ok = 1;
265
266exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100267 /*
268 * Key attributes may have been returned by psa_get_key_attributes()
269 * thus reset them as required.
270 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200271 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100272
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200273 return( ok );
274}
275
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200276/* Assert that a key isn't reported as having a slot number. */
277#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
278#define ASSERT_NO_SLOT_NUMBER( attributes ) \
279 do \
280 { \
281 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
282 TEST_EQUAL( psa_get_key_slot_number( \
283 attributes, \
284 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
285 PSA_ERROR_INVALID_ARGUMENT ); \
286 } \
287 while( 0 )
288#else /* MBEDTLS_PSA_CRYPTO_SE_C */
289#define ASSERT_NO_SLOT_NUMBER( attributes ) \
290 ( (void) 0 )
291#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
292
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100293/* An overapproximation of the amount of storage needed for a key of the
294 * given type and with the given content. The API doesn't make it easy
295 * to find a good value for the size. The current implementation doesn't
296 * care about the value anyway. */
297#define KEY_BITS_FROM_DATA( type, data ) \
298 ( data )->len
299
Darryl Green0c6575a2018-11-07 16:05:30 +0000300typedef enum {
301 IMPORT_KEY = 0,
302 GENERATE_KEY = 1,
303 DERIVE_KEY = 2
304} generate_method;
305
Paul Elliott33746aa2021-09-15 16:40:40 +0100306typedef enum
307{
308 DO_NOT_SET_LENGTHS = 0,
309 SET_LENGTHS_BEFORE_NONCE = 1,
310 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100311} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100312
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100313typedef enum
314{
315 USE_NULL_TAG = 0,
316 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100317} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100318
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319/*!
320 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100321 * \param key_type_arg Type of key passed in
322 * \param key_data The encryption / decryption key data
323 * \param alg_arg The type of algorithm used
324 * \param nonce Nonce data
325 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100326 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 * feed additional data in to be encrypted /
328 * decrypted. If -1, no chunking.
329 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100330 * \param data_part_len_arg If not -1, the length of chunks to feed
331 * the data in to be encrypted / decrypted. If
332 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100333 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100334 * expected here, this controls whether or not
335 * to set lengths, and in what order with
336 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100337 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100339 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100340 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100342 */
343static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
344 int alg_arg,
345 data_t *nonce,
346 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100347 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100350 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100352 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100353 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100354{
355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
356 psa_key_type_t key_type = key_type_arg;
357 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100358 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100359 unsigned char *output_data = NULL;
360 unsigned char *part_data = NULL;
361 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100362 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100363 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100364 size_t output_size = 0;
365 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 size_t output_length = 0;
367 size_t key_bits = 0;
368 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100369 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100370 size_t part_length = 0;
371 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100372 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100373 size_t ad_part_len = 0;
374 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100375 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
377 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
378
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100380 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100381
Paul Elliottd3f82412021-06-16 16:52:21 +0100382 PSA_ASSERT( psa_crypto_init( ) );
383
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100384 if( is_encrypt )
385 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
386 else
387 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
388
Paul Elliottd3f82412021-06-16 16:52:21 +0100389 psa_set_key_algorithm( &attributes, alg );
390 psa_set_key_type( &attributes, key_type );
391
392 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
393 &key ) );
394
395 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
396 key_bits = psa_get_key_bits( &attributes );
397
398 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
399
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 if( is_encrypt )
401 {
402 /* Tag gets written at end of buffer. */
403 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
404 ( input_data->len +
405 tag_length ) );
406 data_true_size = input_data->len;
407 }
408 else
409 {
410 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
411 ( input_data->len -
412 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100414 /* Do not want to attempt to decrypt tag. */
415 data_true_size = input_data->len - tag_length;
416 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100417
418 ASSERT_ALLOC( output_data, output_size );
419
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100420 if( is_encrypt )
421 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100422 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200423 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100424 }
425 else
426 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100427 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200428 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100429 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100430
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100431 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100432
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100433 if( is_encrypt )
434 status = psa_aead_encrypt_setup( &operation, key, alg );
435 else
436 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100437
438 /* If the operation is not supported, just skip and not fail in case the
439 * encryption involves a common limitation of cryptography hardwares and
440 * an alternative implementation. */
441 if( status == PSA_ERROR_NOT_SUPPORTED )
442 {
443 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
444 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
445 }
446
447 PSA_ASSERT( status );
448
Paul Elliott33746aa2021-09-15 16:40:40 +0100449 if( set_lengths_method == DO_NOT_SET_LENGTHS )
450 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
451 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100452 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100453 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
454 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100455 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
456 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100457 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100458 {
459 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
460
Paul Elliott33746aa2021-09-15 16:40:40 +0100461 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
462 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100463 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100464
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100465 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100466 {
467 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100468 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100469
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100470 for( part_offset = 0, part_count = 0;
471 part_offset < additional_data->len;
472 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100473 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100474 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 {
Paul Elliott329d5382021-07-22 17:10:45 +0100476 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100478 else if( additional_data->len - part_offset < ad_part_len )
479 {
480 part_length = additional_data->len - part_offset;
481 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100482 else
483 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100484 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100485 }
486
487 PSA_ASSERT( psa_aead_update_ad( &operation,
488 additional_data->x + part_offset,
489 part_length ) );
490
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 }
492 }
493 else
494 {
495 /* Pass additional data in one go. */
496 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
497 additional_data->len ) );
498 }
499
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100500 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100501 {
502 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100503 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100504 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100505 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100506
507 ASSERT_ALLOC( part_data, part_data_size );
508
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100509 for( part_offset = 0, part_count = 0;
510 part_offset < data_true_size;
511 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100512 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100513 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 {
Paul Elliott329d5382021-07-22 17:10:45 +0100515 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100516 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100517 else if( ( data_true_size - part_offset ) < data_part_len )
518 {
519 part_length = ( data_true_size - part_offset );
520 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100521 else
522 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100523 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100524 }
525
526 PSA_ASSERT( psa_aead_update( &operation,
527 ( input_data->x + part_offset ),
528 part_length, part_data,
529 part_data_size,
530 &output_part_length ) );
531
532 if( output_data && output_part_length )
533 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100534 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100535 output_part_length );
536 }
537
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 output_length += output_part_length;
539 }
540 }
541 else
542 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100543 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100544 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100545 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100546 output_size, &output_length ) );
547 }
548
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100549 if( is_encrypt )
550 PSA_ASSERT( psa_aead_finish( &operation, final_data,
551 final_output_size,
552 &output_part_length,
553 tag_buffer, tag_length,
554 &tag_size ) );
555 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100556 {
Paul Elliott9961a662021-09-17 19:19:02 +0100557 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100558 final_output_size,
559 &output_part_length,
560 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100561 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100562 }
563
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564 if( output_data && output_part_length )
565 memcpy( ( output_data + output_length ), final_data,
566 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100567
568 output_length += output_part_length;
569
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100570
571 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
572 * should be exact.*/
573 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100574 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 TEST_EQUAL( tag_length, tag_size );
576
577 if( output_data && tag_length )
578 memcpy( ( output_data + output_length ), tag_buffer,
579 tag_length );
580
581 output_length += tag_length;
582
583 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200584 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
585 input_data->len ) );
586 TEST_LE_U( output_length,
587 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588 }
589 else
590 {
591 TEST_EQUAL( output_length,
592 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
593 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200594 TEST_LE_U( output_length,
595 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100596 }
597
Paul Elliottd3f82412021-06-16 16:52:21 +0100598
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100599 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100600 output_data, output_length );
601
Paul Elliottd3f82412021-06-16 16:52:21 +0100602
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100603 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100604
605exit:
606 psa_destroy_key( key );
607 psa_aead_abort( &operation );
608 mbedtls_free( output_data );
609 mbedtls_free( part_data );
610 mbedtls_free( final_data );
611 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100612
613 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100614}
615
Neil Armstrong4766f992022-02-28 16:23:59 +0100616/*!
617 * \brief Internal Function for MAC multipart tests.
618 * \param key_type_arg Type of key passed in
619 * \param key_data The encryption / decryption key data
620 * \param alg_arg The type of algorithm used
621 * \param input_data Data to encrypt / decrypt
622 * \param data_part_len_arg If not -1, the length of chunks to feed
623 * the data in to be encrypted / decrypted. If
624 * -1, no chunking
625 * \param expected_output Expected output
626 * \param is_verify If non-zero this is an verify operation.
627 * \param do_zero_parts If non-zero, interleave zero length chunks
628 * with normal length chunks.
629 * \return int Zero on failure, non-zero on success.
630 */
631static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
632 int alg_arg,
633 data_t *input_data,
634 int data_part_len_arg,
635 data_t *expected_output,
636 int is_verify,
637 int do_zero_parts )
638{
639 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
640 psa_key_type_t key_type = key_type_arg;
641 psa_algorithm_t alg = alg_arg;
642 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
643 unsigned char mac[PSA_MAC_MAX_SIZE];
644 size_t part_offset = 0;
645 size_t part_length = 0;
646 size_t data_part_len = 0;
647 size_t mac_len = 0;
648 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
649 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
650
651 int test_ok = 0;
652 size_t part_count = 0;
653
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100654 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100655
656 if( is_verify )
657 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
658 else
659 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
660
661 psa_set_key_algorithm( &attributes, alg );
662 psa_set_key_type( &attributes, key_type );
663
664 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
665 &key ) );
666
667 if( is_verify )
668 status = psa_mac_verify_setup( &operation, key, alg );
669 else
670 status = psa_mac_sign_setup( &operation, key, alg );
671
672 PSA_ASSERT( status );
673
674 if( data_part_len_arg != -1 )
675 {
676 /* Pass data in parts */
677 data_part_len = ( size_t ) data_part_len_arg;
678
679 for( part_offset = 0, part_count = 0;
680 part_offset < input_data->len;
681 part_offset += part_length, part_count++ )
682 {
683 if( do_zero_parts && ( part_count & 0x01 ) )
684 {
685 part_length = 0;
686 }
687 else if( ( input_data->len - part_offset ) < data_part_len )
688 {
689 part_length = ( input_data->len - part_offset );
690 }
691 else
692 {
693 part_length = data_part_len;
694 }
695
696 PSA_ASSERT( psa_mac_update( &operation,
697 ( input_data->x + part_offset ),
698 part_length ) );
699 }
700 }
701 else
702 {
703 /* Pass all data in one go. */
704 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
705 input_data->len ) );
706 }
707
708 if( is_verify )
709 {
710 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
711 expected_output->len ) );
712 }
713 else
714 {
715 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
716 PSA_MAC_MAX_SIZE, &mac_len ) );
717
718 ASSERT_COMPARE( expected_output->x, expected_output->len,
719 mac, mac_len );
720 }
721
722 test_ok = 1;
723
724exit:
725 psa_destroy_key( key );
726 psa_mac_abort( &operation );
727 PSA_DONE( );
728
729 return( test_ok );
730}
731
Neil Armstrong75673ab2022-06-15 17:39:01 +0200732#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200733static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
734 psa_pake_operation_t *server,
735 psa_pake_operation_t *client,
736 int client_input_first,
737 int round, int inject_error )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200738{
739 unsigned char *buffer0 = NULL, *buffer1 = NULL;
740 size_t buffer_length = (
741 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
742 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
743 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200744 /* The output should be exactly this size according to the spec */
745 const size_t expected_size_key_share =
746 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
747 /* The output should be exactly this size according to the spec */
748 const size_t expected_size_zk_public =
749 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
750 /* The output can be smaller: the spec allows stripping leading zeroes */
751 const size_t max_expected_size_zk_proof =
752 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200753 size_t buffer0_off = 0;
754 size_t buffer1_off = 0;
755 size_t s_g1_len, s_g2_len, s_a_len;
756 size_t s_g1_off, s_g2_off, s_a_off;
757 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
758 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
759 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
760 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
761 size_t c_g1_len, c_g2_len, c_a_len;
762 size_t c_g1_off, c_g2_off, c_a_off;
763 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
764 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
765 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
766 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
767 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200768 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200769
770 ASSERT_ALLOC( buffer0, buffer_length );
771 ASSERT_ALLOC( buffer1, buffer_length );
772
773 switch( round )
774 {
775 case 1:
776 /* Server first round Output */
777 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
778 buffer0 + buffer0_off,
779 512 - buffer0_off, &s_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200780 TEST_EQUAL( s_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200781 s_g1_off = buffer0_off;
782 buffer0_off += s_g1_len;
783 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
784 buffer0 + buffer0_off,
785 512 - buffer0_off, &s_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200786 TEST_EQUAL( s_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200787 s_x1_pk_off = buffer0_off;
788 buffer0_off += s_x1_pk_len;
789 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
790 buffer0 + buffer0_off,
791 512 - buffer0_off, &s_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200792 TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200793 s_x1_pr_off = buffer0_off;
794 buffer0_off += s_x1_pr_len;
795 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
796 buffer0 + buffer0_off,
797 512 - buffer0_off, &s_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200798 TEST_EQUAL( s_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200799 s_g2_off = buffer0_off;
800 buffer0_off += s_g2_len;
801 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
802 buffer0 + buffer0_off,
803 512 - buffer0_off, &s_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200804 TEST_EQUAL( s_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200805 s_x2_pk_off = buffer0_off;
806 buffer0_off += s_x2_pk_len;
807 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
808 buffer0 + buffer0_off,
809 512 - buffer0_off, &s_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200810 TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200811 s_x2_pr_off = buffer0_off;
812 buffer0_off += s_x2_pr_len;
813
814 if( inject_error == 1 )
815 {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500816 buffer0[s_x1_pr_off + 8] ^= 1;
817 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200818 expected_status = PSA_ERROR_DATA_INVALID;
819 }
820
Neil Armstrong51009d72022-09-05 17:59:54 +0200821 /*
822 * When injecting errors in inputs, the implementation is
823 * free to detect it right away of with a delay.
824 * This permits delaying the error until the end of the input
825 * sequence, if no error appears then, this will be treated
826 * as an error.
827 */
828
Neil Armstrongf983caf2022-06-15 15:27:48 +0200829 if( client_input_first == 1 )
830 {
831 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200832 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
833 buffer0 + s_g1_off, s_g1_len );
834 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200835 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200836 TEST_EQUAL( status, expected_status );
837 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200838 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200839 else
840 {
841 TEST_EQUAL( status, PSA_SUCCESS );
842 }
843
844 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
845 buffer0 + s_x1_pk_off,
846 s_x1_pk_len );
847 if( inject_error == 1 && status != PSA_SUCCESS )
848 {
849 TEST_EQUAL( status, expected_status );
850 break;
851 }
852 else
853 {
854 TEST_EQUAL( status, PSA_SUCCESS );
855 }
856
857 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
858 buffer0 + s_x1_pr_off,
859 s_x1_pr_len );
860 if( inject_error == 1 && status != PSA_SUCCESS )
861 {
862 TEST_EQUAL( status, expected_status );
863 break;
864 }
865 else
866 {
867 TEST_EQUAL( status, PSA_SUCCESS );
868 }
869
870 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
871 buffer0 + s_g2_off,
872 s_g2_len );
873 if( inject_error == 1 && status != PSA_SUCCESS )
874 {
875 TEST_EQUAL( status, expected_status );
876 break;
877 }
878 else
879 {
880 TEST_EQUAL( status, PSA_SUCCESS );
881 }
882
883 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
884 buffer0 + s_x2_pk_off,
885 s_x2_pk_len );
886 if( inject_error == 1 && status != PSA_SUCCESS )
887 {
888 TEST_EQUAL( status, expected_status );
889 break;
890 }
891 else
892 {
893 TEST_EQUAL( status, PSA_SUCCESS );
894 }
895
896 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
897 buffer0 + s_x2_pr_off,
898 s_x2_pr_len );
899 if( inject_error == 1 && status != PSA_SUCCESS )
900 {
901 TEST_EQUAL( status, expected_status );
902 break;
903 }
904 else
905 {
906 TEST_EQUAL( status, PSA_SUCCESS );
907 }
908
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200909 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200911 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200912 }
913
914 /* Client first round Output */
915 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
916 buffer1 + buffer1_off,
917 512 - buffer1_off, &c_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200918 TEST_EQUAL( c_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200919 c_g1_off = buffer1_off;
920 buffer1_off += c_g1_len;
921 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
922 buffer1 + buffer1_off,
923 512 - buffer1_off, &c_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200924 TEST_EQUAL( c_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200925 c_x1_pk_off = buffer1_off;
926 buffer1_off += c_x1_pk_len;
927 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
928 buffer1 + buffer1_off,
929 512 - buffer1_off, &c_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200930 TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200931 c_x1_pr_off = buffer1_off;
932 buffer1_off += c_x1_pr_len;
933 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
934 buffer1 + buffer1_off,
935 512 - buffer1_off, &c_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200936 TEST_EQUAL( c_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200937 c_g2_off = buffer1_off;
938 buffer1_off += c_g2_len;
939 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
940 buffer1 + buffer1_off,
941 512 - buffer1_off, &c_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200942 TEST_EQUAL( c_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200943 c_x2_pk_off = buffer1_off;
944 buffer1_off += c_x2_pk_len;
945 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
946 buffer1 + buffer1_off,
947 512 - buffer1_off, &c_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200948 TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200949 c_x2_pr_off = buffer1_off;
950 buffer1_off += c_x2_pr_len;
951
952 if( client_input_first == 0 )
953 {
954 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200955 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
956 buffer0 + s_g1_off, s_g1_len );
957 if( inject_error == 1 && status != PSA_SUCCESS )
958 {
959 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200960 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200961 }
962 else
963 {
964 TEST_EQUAL( status, PSA_SUCCESS );
965 }
966
967 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
968 buffer0 + s_x1_pk_off,
969 s_x1_pk_len );
970 if( inject_error == 1 && status != PSA_SUCCESS )
971 {
972 TEST_EQUAL( status, expected_status );
973 break;
974 }
975 else
976 {
977 TEST_EQUAL( status, PSA_SUCCESS );
978 }
979
980 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
981 buffer0 + s_x1_pr_off,
982 s_x1_pr_len );
983 if( inject_error == 1 && status != PSA_SUCCESS )
984 {
985 TEST_EQUAL( status, expected_status );
986 break;
987 }
988 else
989 {
990 TEST_EQUAL( status, PSA_SUCCESS );
991 }
992
993 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
994 buffer0 + s_g2_off,
995 s_g2_len );
996 if( inject_error == 1 && status != PSA_SUCCESS )
997 {
998 TEST_EQUAL( status, expected_status );
999 break;
1000 }
1001 else
1002 {
1003 TEST_EQUAL( status, PSA_SUCCESS );
1004 }
1005
1006 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1007 buffer0 + s_x2_pk_off,
1008 s_x2_pk_len );
1009 if( inject_error == 1 && status != PSA_SUCCESS )
1010 {
1011 TEST_EQUAL( status, expected_status );
1012 break;
1013 }
1014 else
1015 {
1016 TEST_EQUAL( status, PSA_SUCCESS );
1017 }
1018
1019 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1020 buffer0 + s_x2_pr_off,
1021 s_x2_pr_len );
1022 if( inject_error == 1 && status != PSA_SUCCESS )
1023 {
1024 TEST_EQUAL( status, expected_status );
1025 break;
1026 }
1027 else
1028 {
1029 TEST_EQUAL( status, PSA_SUCCESS );
1030 }
1031
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001032 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001033 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001034 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001035 }
1036
1037 if( inject_error == 2 )
1038 {
Andrzej Kurekc0182042022-11-08 08:12:56 -05001039 buffer1[c_x1_pr_off + 12] ^= 1;
1040 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001041 expected_status = PSA_ERROR_DATA_INVALID;
1042 }
1043
1044 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001045 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1046 buffer1 + c_g1_off, c_g1_len );
1047 if( inject_error == 2 && status != PSA_SUCCESS )
1048 {
1049 TEST_EQUAL( status, expected_status );
1050 break;
1051 }
1052 else
1053 {
1054 TEST_EQUAL( status, PSA_SUCCESS );
1055 }
1056
1057 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1058 buffer1 + c_x1_pk_off, c_x1_pk_len );
1059 if( inject_error == 2 && status != PSA_SUCCESS )
1060 {
1061 TEST_EQUAL( status, expected_status );
1062 break;
1063 }
1064 else
1065 {
1066 TEST_EQUAL( status, PSA_SUCCESS );
1067 }
1068
1069 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1070 buffer1 + c_x1_pr_off, c_x1_pr_len );
1071 if( inject_error == 2 && status != PSA_SUCCESS )
1072 {
1073 TEST_EQUAL( status, expected_status );
1074 break;
1075 }
1076 else
1077 {
1078 TEST_EQUAL( status, PSA_SUCCESS );
1079 }
1080
1081 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1082 buffer1 + c_g2_off, c_g2_len );
1083 if( inject_error == 2 && status != PSA_SUCCESS )
1084 {
1085 TEST_EQUAL( status, expected_status );
1086 break;
1087 }
1088 else
1089 {
1090 TEST_EQUAL( status, PSA_SUCCESS );
1091 }
1092
1093 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1094 buffer1 + c_x2_pk_off, c_x2_pk_len );
1095 if( inject_error == 2 && status != PSA_SUCCESS )
1096 {
1097 TEST_EQUAL( status, expected_status );
1098 break;
1099 }
1100 else
1101 {
1102 TEST_EQUAL( status, PSA_SUCCESS );
1103 }
1104
1105 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1106 buffer1 + c_x2_pr_off, c_x2_pr_len );
1107 if( inject_error == 2 && status != PSA_SUCCESS )
1108 {
1109 TEST_EQUAL( status, expected_status );
1110 break;
1111 }
1112 else
1113 {
1114 TEST_EQUAL( status, PSA_SUCCESS );
1115 }
1116
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001117 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001118 if( inject_error == 2 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001119 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120
1121 break;
1122
1123 case 2:
1124 /* Server second round Output */
1125 buffer0_off = 0;
1126
1127 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1128 buffer0 + buffer0_off,
1129 512 - buffer0_off, &s_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001130 TEST_EQUAL( s_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001131 s_a_off = buffer0_off;
1132 buffer0_off += s_a_len;
1133 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1134 buffer0 + buffer0_off,
1135 512 - buffer0_off, &s_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001136 TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001137 s_x2s_pk_off = buffer0_off;
1138 buffer0_off += s_x2s_pk_len;
1139 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1140 buffer0 + buffer0_off,
1141 512 - buffer0_off, &s_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001142 TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001143 s_x2s_pr_off = buffer0_off;
1144 buffer0_off += s_x2s_pr_len;
1145
1146 if( inject_error == 3 )
1147 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001148 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001149 expected_status = PSA_ERROR_DATA_INVALID;
1150 }
1151
1152 if( client_input_first == 1 )
1153 {
1154 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001155 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1156 buffer0 + s_a_off, s_a_len );
1157 if( inject_error == 3 && status != PSA_SUCCESS )
1158 {
1159 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001160 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001161 }
1162 else
1163 {
1164 TEST_EQUAL( status, PSA_SUCCESS );
1165 }
1166
1167 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1168 buffer0 + s_x2s_pk_off,
1169 s_x2s_pk_len );
1170 if( inject_error == 3 && status != PSA_SUCCESS )
1171 {
1172 TEST_EQUAL( status, expected_status );
1173 break;
1174 }
1175 else
1176 {
1177 TEST_EQUAL( status, PSA_SUCCESS );
1178 }
1179
1180 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1181 buffer0 + s_x2s_pr_off,
1182 s_x2s_pr_len );
1183 if( inject_error == 3 && status != PSA_SUCCESS )
1184 {
1185 TEST_EQUAL( status, expected_status );
1186 break;
1187 }
1188 else
1189 {
1190 TEST_EQUAL( status, PSA_SUCCESS );
1191 }
1192
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001193 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001194 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001195 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001196 }
1197
1198 /* Client second round Output */
1199 buffer1_off = 0;
1200
1201 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1202 buffer1 + buffer1_off,
1203 512 - buffer1_off, &c_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001204 TEST_EQUAL( c_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205 c_a_off = buffer1_off;
1206 buffer1_off += c_a_len;
1207 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1208 buffer1 + buffer1_off,
1209 512 - buffer1_off, &c_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001210 TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001211 c_x2s_pk_off = buffer1_off;
1212 buffer1_off += c_x2s_pk_len;
1213 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1214 buffer1 + buffer1_off,
1215 512 - buffer1_off, &c_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001216 TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001217 c_x2s_pr_off = buffer1_off;
1218 buffer1_off += c_x2s_pr_len;
1219
1220 if( client_input_first == 0 )
1221 {
1222 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001223 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1224 buffer0 + s_a_off, s_a_len );
1225 if( inject_error == 3 && status != PSA_SUCCESS )
1226 {
1227 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001228 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001229 }
1230 else
1231 {
1232 TEST_EQUAL( status, PSA_SUCCESS );
1233 }
1234
1235 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1236 buffer0 + s_x2s_pk_off,
1237 s_x2s_pk_len );
1238 if( inject_error == 3 && status != PSA_SUCCESS )
1239 {
1240 TEST_EQUAL( status, expected_status );
1241 break;
1242 }
1243 else
1244 {
1245 TEST_EQUAL( status, PSA_SUCCESS );
1246 }
1247
1248 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1249 buffer0 + s_x2s_pr_off,
1250 s_x2s_pr_len );
1251 if( inject_error == 3 && status != PSA_SUCCESS )
1252 {
1253 TEST_EQUAL( status, expected_status );
1254 break;
1255 }
1256 else
1257 {
1258 TEST_EQUAL( status, PSA_SUCCESS );
1259 }
1260
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001261 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001262 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001263 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001264 }
1265
1266 if( inject_error == 4 )
1267 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001268 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001269 expected_status = PSA_ERROR_DATA_INVALID;
1270 }
1271
1272 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001273 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1274 buffer1 + c_a_off, c_a_len );
1275 if( inject_error == 4 && status != PSA_SUCCESS )
1276 {
1277 TEST_EQUAL( status, expected_status );
1278 break;
1279 }
1280 else
1281 {
1282 TEST_EQUAL( status, PSA_SUCCESS );
1283 }
1284
1285 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1286 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1287 if( inject_error == 4 && status != PSA_SUCCESS )
1288 {
1289 TEST_EQUAL( status, expected_status );
1290 break;
1291 }
1292 else
1293 {
1294 TEST_EQUAL( status, PSA_SUCCESS );
1295 }
1296
1297 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1298 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1299 if( inject_error == 4 && status != PSA_SUCCESS )
1300 {
1301 TEST_EQUAL( status, expected_status );
1302 break;
1303 }
1304 else
1305 {
1306 TEST_EQUAL( status, PSA_SUCCESS );
1307 }
1308
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001309 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001310 if( inject_error == 4 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001311 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001312
1313 break;
1314
1315 }
1316
Neil Armstrongf983caf2022-06-15 15:27:48 +02001317exit:
1318 mbedtls_free( buffer0 );
1319 mbedtls_free( buffer1 );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001320}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001321#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001322
Gilles Peskinee59236f2018-01-27 23:32:46 +01001323/* END_HEADER */
1324
1325/* BEGIN_DEPENDENCIES
1326 * depends_on:MBEDTLS_PSA_CRYPTO_C
1327 * END_DEPENDENCIES
1328 */
1329
1330/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001331void static_checks( )
1332{
1333 size_t max_truncated_mac_size =
1334 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1335
1336 /* Check that the length for a truncated MAC always fits in the algorithm
1337 * encoding. The shifted mask is the maximum truncated value. The
1338 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001339 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001340}
1341/* END_CASE */
1342
1343/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001344void import_with_policy( int type_arg,
1345 int usage_arg, int alg_arg,
1346 int expected_status_arg )
1347{
1348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1349 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001350 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001351 psa_key_type_t type = type_arg;
1352 psa_key_usage_t usage = usage_arg;
1353 psa_algorithm_t alg = alg_arg;
1354 psa_status_t expected_status = expected_status_arg;
1355 const uint8_t key_material[16] = {0};
1356 psa_status_t status;
1357
1358 PSA_ASSERT( psa_crypto_init( ) );
1359
1360 psa_set_key_type( &attributes, type );
1361 psa_set_key_usage_flags( &attributes, usage );
1362 psa_set_key_algorithm( &attributes, alg );
1363
1364 status = psa_import_key( &attributes,
1365 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001366 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001367 TEST_EQUAL( status, expected_status );
1368 if( status != PSA_SUCCESS )
1369 goto exit;
1370
Ronald Cron5425a212020-08-04 14:58:35 +02001371 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001372 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001373 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001374 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001375 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001376 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001377
Ronald Cron5425a212020-08-04 14:58:35 +02001378 PSA_ASSERT( psa_destroy_key( key ) );
1379 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001380
1381exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001382 /*
1383 * Key attributes may have been returned by psa_get_key_attributes()
1384 * thus reset them as required.
1385 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001386 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001387
1388 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001389 PSA_DONE( );
1390}
1391/* END_CASE */
1392
1393/* BEGIN_CASE */
1394void import_with_data( data_t *data, int type_arg,
1395 int attr_bits_arg,
1396 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001397{
1398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1399 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001401 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001402 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001403 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001404 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001405
Gilles Peskine8817f612018-12-18 00:18:46 +01001406 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001407
Gilles Peskine4747d192019-04-17 15:05:45 +02001408 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001409 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001410
Ronald Cron5425a212020-08-04 14:58:35 +02001411 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001412 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001413 if( status != PSA_SUCCESS )
1414 goto exit;
1415
Ronald Cron5425a212020-08-04 14:58:35 +02001416 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001417 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001418 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001419 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001420 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001421
Ronald Cron5425a212020-08-04 14:58:35 +02001422 PSA_ASSERT( psa_destroy_key( key ) );
1423 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424
1425exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001426 /*
1427 * Key attributes may have been returned by psa_get_key_attributes()
1428 * thus reset them as required.
1429 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001430 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001431
1432 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001433 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001434}
1435/* END_CASE */
1436
1437/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001438void import_large_key( int type_arg, int byte_size_arg,
1439 int expected_status_arg )
1440{
1441 psa_key_type_t type = type_arg;
1442 size_t byte_size = byte_size_arg;
1443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1444 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001446 psa_status_t status;
1447 uint8_t *buffer = NULL;
1448 size_t buffer_size = byte_size + 1;
1449 size_t n;
1450
Steven Cooreman69967ce2021-01-18 18:01:08 +01001451 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001452 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001453 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001454 memset( buffer, 'K', byte_size );
1455
1456 PSA_ASSERT( psa_crypto_init( ) );
1457
1458 /* Try importing the key */
1459 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1460 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001461 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001462 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001463 TEST_EQUAL( status, expected_status );
1464
1465 if( status == PSA_SUCCESS )
1466 {
Ronald Cron5425a212020-08-04 14:58:35 +02001467 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001468 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1469 TEST_EQUAL( psa_get_key_bits( &attributes ),
1470 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001471 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001472 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001473 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001474 for( n = 0; n < byte_size; n++ )
1475 TEST_EQUAL( buffer[n], 'K' );
1476 for( n = byte_size; n < buffer_size; n++ )
1477 TEST_EQUAL( buffer[n], 0 );
1478 }
1479
1480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001481 /*
1482 * Key attributes may have been returned by psa_get_key_attributes()
1483 * thus reset them as required.
1484 */
1485 psa_reset_key_attributes( &attributes );
1486
Ronald Cron5425a212020-08-04 14:58:35 +02001487 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001488 PSA_DONE( );
1489 mbedtls_free( buffer );
1490}
1491/* END_CASE */
1492
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001493/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001494void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1495{
Ronald Cron5425a212020-08-04 14:58:35 +02001496 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001497 size_t bits = bits_arg;
1498 psa_status_t expected_status = expected_status_arg;
1499 psa_status_t status;
1500 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001501 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001502 size_t buffer_size = /* Slight overapproximations */
1503 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001504 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001505 unsigned char *p;
1506 int ret;
1507 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001509
Gilles Peskine8817f612018-12-18 00:18:46 +01001510 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001511 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001512
1513 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1514 bits, keypair ) ) >= 0 );
1515 length = ret;
1516
1517 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001518 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001519 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001520 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001521
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001522 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001523 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001524
1525exit:
1526 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001527 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001532void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001533 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001534 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301535 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536 int expected_bits,
1537 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001538 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001539 int canonical_input )
1540{
Ronald Cron5425a212020-08-04 14:58:35 +02001541 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001543 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001544 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301546 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547 unsigned char *exported = NULL;
1548 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001549 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001550 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001551 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001552 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001553 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001554
Moran Pekercb088e72018-07-17 17:36:59 +03001555 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001556 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001557 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001558 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001559 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001560
Archana4d7ae1d2021-07-07 02:50:22 +05301561 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001562 psa_set_key_usage_flags( &attributes, usage_arg );
1563 psa_set_key_algorithm( &attributes, alg );
1564 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001565
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001567 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568
1569 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001570 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001571 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1572 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001573 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574
1575 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001576 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001577 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001578
1579 /* The exported length must be set by psa_export_key() to a value between 0
1580 * and export_size. On errors, the exported length must be 0. */
1581 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1582 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001583 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001584
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001585 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001586 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001588 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001589 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001591 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592
Gilles Peskineea38a922021-02-13 00:05:16 +01001593 /* Run sanity checks on the exported key. For non-canonical inputs,
1594 * this validates the canonical representations. For canonical inputs,
1595 * this doesn't directly validate the implementation, but it still helps
1596 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301597 if( !psa_key_lifetime_is_external( lifetime ) )
1598 {
1599 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1600 goto exit;
1601 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001602
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001604 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001605 else
1606 {
Ronald Cron5425a212020-08-04 14:58:35 +02001607 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001608 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001609 &key2 ) );
1610 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 reexported,
1612 export_size,
1613 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001614 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301615 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001616 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001618 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301619 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301620 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001621 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001622
1623destroy:
1624 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001625 PSA_ASSERT( psa_destroy_key( key ) );
1626 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001627
1628exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001629 /*
1630 * Key attributes may have been returned by psa_get_key_attributes()
1631 * thus reset them as required.
1632 */
1633 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301634 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001635 mbedtls_free( exported );
1636 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001637 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001638}
1639/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001640
Moran Pekerf709f4a2018-06-06 17:26:04 +03001641/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001642void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001643 int type_arg,
1644 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301645 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001646 int export_size_delta,
1647 int expected_export_status_arg,
1648 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001649{
Ronald Cron5425a212020-08-04 14:58:35 +02001650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001651 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001652 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001653 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001654 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301655 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001656 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001657 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001658 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001660
Gilles Peskine8817f612018-12-18 00:18:46 +01001661 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001662
Archana4d7ae1d2021-07-07 02:50:22 +05301663 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001664 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1665 psa_set_key_algorithm( &attributes, alg );
1666 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001667
1668 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001669 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001670
Gilles Peskine49c25912018-10-29 15:15:31 +01001671 /* Export the public key */
1672 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001673 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001674 exported, export_size,
1675 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001676 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001677 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001678 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001679 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001680 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001681 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001682 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001683 TEST_LE_U( expected_public_key->len,
1684 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1685 TEST_LE_U( expected_public_key->len,
1686 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1687 TEST_LE_U( expected_public_key->len,
1688 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001689 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1690 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001691 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001692exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001693 /*
1694 * Key attributes may have been returned by psa_get_key_attributes()
1695 * thus reset them as required.
1696 */
1697 psa_reset_key_attributes( &attributes );
1698
itayzafrir3e02b3b2018-06-12 17:06:52 +03001699 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001701 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001702}
1703/* END_CASE */
1704
Gilles Peskine20035e32018-02-03 22:44:14 +01001705/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001706void import_and_exercise_key( data_t *data,
1707 int type_arg,
1708 int bits_arg,
1709 int alg_arg )
1710{
Ronald Cron5425a212020-08-04 14:58:35 +02001711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712 psa_key_type_t type = type_arg;
1713 size_t bits = bits_arg;
1714 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001715 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001717 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001718
Gilles Peskine8817f612018-12-18 00:18:46 +01001719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001720
Gilles Peskine4747d192019-04-17 15:05:45 +02001721 psa_set_key_usage_flags( &attributes, usage );
1722 psa_set_key_algorithm( &attributes, alg );
1723 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001724
1725 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001726 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001727
1728 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001729 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001730 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1731 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001732
1733 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001734 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001735 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001736
Ronald Cron5425a212020-08-04 14:58:35 +02001737 PSA_ASSERT( psa_destroy_key( key ) );
1738 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001739
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001740exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001741 /*
1742 * Key attributes may have been returned by psa_get_key_attributes()
1743 * thus reset them as required.
1744 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001745 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001746
1747 psa_reset_key_attributes( &attributes );
1748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001749 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001750}
1751/* END_CASE */
1752
1753/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001754void effective_key_attributes( int type_arg, int expected_type_arg,
1755 int bits_arg, int expected_bits_arg,
1756 int usage_arg, int expected_usage_arg,
1757 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001758{
Ronald Cron5425a212020-08-04 14:58:35 +02001759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001760 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001761 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001762 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001763 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001764 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001765 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001766 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001767 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001768 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001769
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001771
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001772 psa_set_key_usage_flags( &attributes, usage );
1773 psa_set_key_algorithm( &attributes, alg );
1774 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001775 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001776
Ronald Cron5425a212020-08-04 14:58:35 +02001777 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001778 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001779
Ronald Cron5425a212020-08-04 14:58:35 +02001780 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001781 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1782 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1783 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1784 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001785
1786exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001787 /*
1788 * Key attributes may have been returned by psa_get_key_attributes()
1789 * thus reset them as required.
1790 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001791 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001792
1793 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001794 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001795}
1796/* END_CASE */
1797
1798/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001799void check_key_policy( int type_arg, int bits_arg,
1800 int usage_arg, int alg_arg )
1801{
1802 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001803 usage_arg,
1804 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001805 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001806 goto exit;
1807}
1808/* END_CASE */
1809
1810/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001811void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001812{
1813 /* Test each valid way of initializing the object, except for `= {0}`, as
1814 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1815 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001816 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001817 psa_key_attributes_t func = psa_key_attributes_init( );
1818 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1819 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001820
1821 memset( &zero, 0, sizeof( zero ) );
1822
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001823 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1824 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1825 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001826
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001827 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1828 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1829 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1830
1831 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1832 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1833 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1834
1835 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1836 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1837 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1838
1839 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1840 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1841 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001842}
1843/* END_CASE */
1844
1845/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001846void mac_key_policy( int policy_usage_arg,
1847 int policy_alg_arg,
1848 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001850 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001851 int expected_status_sign_arg,
1852 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001853{
Ronald Cron5425a212020-08-04 14:58:35 +02001854 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001856 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001857 psa_key_type_t key_type = key_type_arg;
1858 psa_algorithm_t policy_alg = policy_alg_arg;
1859 psa_algorithm_t exercise_alg = exercise_alg_arg;
1860 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001861 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001862 psa_status_t expected_status_sign = expected_status_sign_arg;
1863 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001864 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001865
Gilles Peskine8817f612018-12-18 00:18:46 +01001866 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001867
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001868 psa_set_key_usage_flags( &attributes, policy_usage );
1869 psa_set_key_algorithm( &attributes, policy_alg );
1870 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001871
Gilles Peskine049c7532019-05-15 20:22:09 +02001872 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001873 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001874
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001875 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1876 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001877
Ronald Cron5425a212020-08-04 14:58:35 +02001878 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001879 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001880
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001881 /* Calculate the MAC, one-shot case. */
1882 uint8_t input[128] = {0};
1883 size_t mac_len;
1884 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1885 input, 128,
1886 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1887 expected_status_sign );
1888
Neil Armstrong3af9b972022-02-07 12:20:21 +01001889 /* Calculate the MAC, multi-part case. */
1890 PSA_ASSERT( psa_mac_abort( &operation ) );
1891 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1892 if( status == PSA_SUCCESS )
1893 {
1894 status = psa_mac_update( &operation, input, 128 );
1895 if( status == PSA_SUCCESS )
1896 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1897 &mac_len ),
1898 expected_status_sign );
1899 else
1900 TEST_EQUAL( status, expected_status_sign );
1901 }
1902 else
1903 {
1904 TEST_EQUAL( status, expected_status_sign );
1905 }
1906 PSA_ASSERT( psa_mac_abort( &operation ) );
1907
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001908 /* Verify correct MAC, one-shot case. */
1909 status = psa_mac_verify( key, exercise_alg, input, 128,
1910 mac, mac_len );
1911
1912 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1913 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001914 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001915 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001916
Neil Armstrong3af9b972022-02-07 12:20:21 +01001917 /* Verify correct MAC, multi-part case. */
1918 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1919 if( status == PSA_SUCCESS )
1920 {
1921 status = psa_mac_update( &operation, input, 128 );
1922 if( status == PSA_SUCCESS )
1923 {
1924 status = psa_mac_verify_finish( &operation, mac, mac_len );
1925 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1926 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1927 else
1928 TEST_EQUAL( status, expected_status_verify );
1929 }
1930 else
1931 {
1932 TEST_EQUAL( status, expected_status_verify );
1933 }
1934 }
1935 else
1936 {
1937 TEST_EQUAL( status, expected_status_verify );
1938 }
1939
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001940 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001941
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001942 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001943 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001944 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001945
1946exit:
1947 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001948 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001949 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001950}
1951/* END_CASE */
1952
1953/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001954void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955 int policy_alg,
1956 int key_type,
1957 data_t *key_data,
1958 int exercise_alg )
1959{
Ronald Cron5425a212020-08-04 14:58:35 +02001960 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001962 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001963 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001964 size_t output_buffer_size = 0;
1965 size_t input_buffer_size = 0;
1966 size_t output_length = 0;
1967 uint8_t *output = NULL;
1968 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001969 psa_status_t status;
1970
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001971 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1972 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1973 input_buffer_size );
1974
1975 ASSERT_ALLOC( input, input_buffer_size );
1976 ASSERT_ALLOC( output, output_buffer_size );
1977
Gilles Peskine8817f612018-12-18 00:18:46 +01001978 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001979
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001980 psa_set_key_usage_flags( &attributes, policy_usage );
1981 psa_set_key_algorithm( &attributes, policy_alg );
1982 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983
Gilles Peskine049c7532019-05-15 20:22:09 +02001984 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001985 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001987 /* Check if no key usage flag implication is done */
1988 TEST_EQUAL( policy_usage,
1989 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001990
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001991 /* Encrypt check, one-shot */
1992 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1993 output, output_buffer_size,
1994 &output_length);
1995 if( policy_alg == exercise_alg &&
1996 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1997 PSA_ASSERT( status );
1998 else
1999 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2000
2001 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002002 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003 if( policy_alg == exercise_alg &&
2004 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002005 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002007 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002008 psa_cipher_abort( &operation );
2009
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002010 /* Decrypt check, one-shot */
2011 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
2012 input, input_buffer_size,
2013 &output_length);
2014 if( policy_alg == exercise_alg &&
2015 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
2016 PSA_ASSERT( status );
2017 else
2018 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2019
2020 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002021 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022 if( policy_alg == exercise_alg &&
2023 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002024 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002026 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027
2028exit:
2029 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002030 mbedtls_free( input );
2031 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002032 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002033 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034}
2035/* END_CASE */
2036
2037/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002038void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039 int policy_alg,
2040 int key_type,
2041 data_t *key_data,
2042 int nonce_length_arg,
2043 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002044 int exercise_alg,
2045 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046{
Ronald Cron5425a212020-08-04 14:58:35 +02002047 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002048 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002049 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002050 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002051 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002052 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053 unsigned char nonce[16] = {0};
2054 size_t nonce_length = nonce_length_arg;
2055 unsigned char tag[16];
2056 size_t tag_length = tag_length_arg;
2057 size_t output_length;
2058
Gilles Peskine7be11a72022-04-14 00:12:57 +02002059 TEST_LE_U( nonce_length, sizeof( nonce ) );
2060 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061
Gilles Peskine8817f612018-12-18 00:18:46 +01002062 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002064 psa_set_key_usage_flags( &attributes, policy_usage );
2065 psa_set_key_algorithm( &attributes, policy_alg );
2066 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067
Gilles Peskine049c7532019-05-15 20:22:09 +02002068 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002069 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002071 /* Check if no key usage implication is done */
2072 TEST_EQUAL( policy_usage,
2073 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002074
Neil Armstrong752d8112022-02-07 14:51:11 +01002075 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002076 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077 nonce, nonce_length,
2078 NULL, 0,
2079 NULL, 0,
2080 tag, tag_length,
2081 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002082 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2083 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002085 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086
Neil Armstrong752d8112022-02-07 14:51:11 +01002087 /* Encrypt check, multi-part */
2088 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2089 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2090 TEST_EQUAL( status, expected_status );
2091 else
2092 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2093
2094 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002096 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097 nonce, nonce_length,
2098 NULL, 0,
2099 tag, tag_length,
2100 NULL, 0,
2101 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002102 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2103 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2104 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002105 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002106 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002107 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108
Neil Armstrong752d8112022-02-07 14:51:11 +01002109 /* Decrypt check, multi-part */
2110 PSA_ASSERT( psa_aead_abort( &operation ) );
2111 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2112 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2113 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2114 else
2115 TEST_EQUAL( status, expected_status );
2116
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002117exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002118 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002119 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002120 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121}
2122/* END_CASE */
2123
2124/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002125void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002126 int policy_alg,
2127 int key_type,
2128 data_t *key_data,
2129 int exercise_alg )
2130{
Ronald Cron5425a212020-08-04 14:58:35 +02002131 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002132 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002133 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002134 psa_status_t status;
2135 size_t key_bits;
2136 size_t buffer_length;
2137 unsigned char *buffer = NULL;
2138 size_t output_length;
2139
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002142 psa_set_key_usage_flags( &attributes, policy_usage );
2143 psa_set_key_algorithm( &attributes, policy_alg );
2144 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145
Gilles Peskine049c7532019-05-15 20:22:09 +02002146 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002147 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002149 /* Check if no key usage implication is done */
2150 TEST_EQUAL( policy_usage,
2151 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002152
Ronald Cron5425a212020-08-04 14:58:35 +02002153 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002154 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002155 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2156 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002157 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158
Ronald Cron5425a212020-08-04 14:58:35 +02002159 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002160 NULL, 0,
2161 NULL, 0,
2162 buffer, buffer_length,
2163 &output_length );
2164 if( policy_alg == exercise_alg &&
2165 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002166 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002168 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002169
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002170 if( buffer_length != 0 )
2171 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002172 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002173 buffer, buffer_length,
2174 NULL, 0,
2175 buffer, buffer_length,
2176 &output_length );
2177 if( policy_alg == exercise_alg &&
2178 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002179 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002181 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002182
2183exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002184 /*
2185 * Key attributes may have been returned by psa_get_key_attributes()
2186 * thus reset them as required.
2187 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002188 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002189
2190 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002191 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002192 mbedtls_free( buffer );
2193}
2194/* END_CASE */
2195
2196/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002197void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002198 int policy_alg,
2199 int key_type,
2200 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002201 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002202 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002203 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204{
Ronald Cron5425a212020-08-04 14:58:35 +02002205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002207 psa_key_usage_t policy_usage = policy_usage_arg;
2208 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002209 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002210 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2211 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2212 * compatible with the policy and `payload_length_arg` is supposed to be
2213 * a valid input length to sign. If `payload_length_arg <= 0`,
2214 * `exercise_alg` is supposed to be forbidden by the policy. */
2215 int compatible_alg = payload_length_arg > 0;
2216 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002217 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002218 size_t signature_length;
2219
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002220 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002221 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002222 TEST_EQUAL( expected_usage,
2223 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002224
Gilles Peskine8817f612018-12-18 00:18:46 +01002225 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002226
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002227 psa_set_key_usage_flags( &attributes, policy_usage );
2228 psa_set_key_algorithm( &attributes, policy_alg );
2229 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002230
Gilles Peskine049c7532019-05-15 20:22:09 +02002231 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002232 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002233
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002234 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2235
Ronald Cron5425a212020-08-04 14:58:35 +02002236 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002237 payload, payload_length,
2238 signature, sizeof( signature ),
2239 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002240 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002241 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002242 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002243 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002244
2245 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002246 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002247 payload, payload_length,
2248 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002249 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002250 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002251 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002252 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002253
Gilles Peskinef7b41372021-09-22 16:15:05 +02002254 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002255 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002256 {
2257 status = psa_sign_message( key, exercise_alg,
2258 payload, payload_length,
2259 signature, sizeof( signature ),
2260 &signature_length );
2261 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2262 PSA_ASSERT( status );
2263 else
2264 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2265
2266 memset( signature, 0, sizeof( signature ) );
2267 status = psa_verify_message( key, exercise_alg,
2268 payload, payload_length,
2269 signature, sizeof( signature ) );
2270 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2271 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2272 else
2273 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2274 }
2275
Gilles Peskined5b33222018-06-18 22:20:03 +02002276exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002277 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002278 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002279}
2280/* END_CASE */
2281
Janos Follathba3fab92019-06-11 14:50:16 +01002282/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002283void derive_key_policy( int policy_usage,
2284 int policy_alg,
2285 int key_type,
2286 data_t *key_data,
2287 int exercise_alg )
2288{
Ronald Cron5425a212020-08-04 14:58:35 +02002289 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002291 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002292 psa_status_t status;
2293
Gilles Peskine8817f612018-12-18 00:18:46 +01002294 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002295
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002296 psa_set_key_usage_flags( &attributes, policy_usage );
2297 psa_set_key_algorithm( &attributes, policy_alg );
2298 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002299
Gilles Peskine049c7532019-05-15 20:22:09 +02002300 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002301 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002302
Janos Follathba3fab92019-06-11 14:50:16 +01002303 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2304
2305 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2306 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002307 {
Janos Follathba3fab92019-06-11 14:50:16 +01002308 PSA_ASSERT( psa_key_derivation_input_bytes(
2309 &operation,
2310 PSA_KEY_DERIVATION_INPUT_SEED,
2311 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002312 }
Janos Follathba3fab92019-06-11 14:50:16 +01002313
2314 status = psa_key_derivation_input_key( &operation,
2315 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002316 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002317
Gilles Peskineea0fb492018-07-12 17:17:20 +02002318 if( policy_alg == exercise_alg &&
2319 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002320 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002321 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002322 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002323
2324exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002325 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002326 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002327 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002328}
2329/* END_CASE */
2330
2331/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002332void agreement_key_policy( int policy_usage,
2333 int policy_alg,
2334 int key_type_arg,
2335 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002336 int exercise_alg,
2337 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002338{
Ronald Cron5425a212020-08-04 14:58:35 +02002339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002340 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002342 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002343 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002344 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002345
Gilles Peskine8817f612018-12-18 00:18:46 +01002346 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002347
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002348 psa_set_key_usage_flags( &attributes, policy_usage );
2349 psa_set_key_algorithm( &attributes, policy_alg );
2350 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002351
Gilles Peskine049c7532019-05-15 20:22:09 +02002352 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002353 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002354
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002355 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002356 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002357
Steven Cooremance48e852020-10-05 16:02:45 +02002358 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002359
2360exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002361 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002362 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002363 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002364}
2365/* END_CASE */
2366
2367/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002368void key_policy_alg2( int key_type_arg, data_t *key_data,
2369 int usage_arg, int alg_arg, int alg2_arg )
2370{
Ronald Cron5425a212020-08-04 14:58:35 +02002371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002372 psa_key_type_t key_type = key_type_arg;
2373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2374 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2375 psa_key_usage_t usage = usage_arg;
2376 psa_algorithm_t alg = alg_arg;
2377 psa_algorithm_t alg2 = alg2_arg;
2378
2379 PSA_ASSERT( psa_crypto_init( ) );
2380
2381 psa_set_key_usage_flags( &attributes, usage );
2382 psa_set_key_algorithm( &attributes, alg );
2383 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2384 psa_set_key_type( &attributes, key_type );
2385 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002386 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002387
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002388 /* Update the usage flags to obtain implicit usage flags */
2389 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002390 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002391 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2392 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2393 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2394
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002395 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002396 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002397 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002398 goto exit;
2399
2400exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002401 /*
2402 * Key attributes may have been returned by psa_get_key_attributes()
2403 * thus reset them as required.
2404 */
2405 psa_reset_key_attributes( &got_attributes );
2406
Ronald Cron5425a212020-08-04 14:58:35 +02002407 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002408 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002409}
2410/* END_CASE */
2411
2412/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002413void raw_agreement_key_policy( int policy_usage,
2414 int policy_alg,
2415 int key_type_arg,
2416 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002417 int exercise_alg,
2418 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002419{
Ronald Cron5425a212020-08-04 14:58:35 +02002420 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002422 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002423 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002424 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002425 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002426
2427 PSA_ASSERT( psa_crypto_init( ) );
2428
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002429 psa_set_key_usage_flags( &attributes, policy_usage );
2430 psa_set_key_algorithm( &attributes, policy_alg );
2431 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002432
Gilles Peskine049c7532019-05-15 20:22:09 +02002433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002434 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002435
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002436 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002437
Steven Cooremance48e852020-10-05 16:02:45 +02002438 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002439
2440exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002441 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002442 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002443 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002444}
2445/* END_CASE */
2446
2447/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002448void copy_success( int source_usage_arg,
2449 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302450 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002451 int type_arg, data_t *material,
2452 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002453 int target_usage_arg,
2454 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302455 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002456 int expected_usage_arg,
2457 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002458{
Gilles Peskineca25db92019-04-19 11:43:08 +02002459 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2460 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002461 psa_key_usage_t expected_usage = expected_usage_arg;
2462 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002463 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302464 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2465 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002466 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2467 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468 uint8_t *export_buffer = NULL;
2469
Gilles Peskine57ab7212019-01-28 13:03:09 +01002470 PSA_ASSERT( psa_crypto_init( ) );
2471
Gilles Peskineca25db92019-04-19 11:43:08 +02002472 /* Prepare the source key. */
2473 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2474 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002475 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002476 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302477 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002478 PSA_ASSERT( psa_import_key( &source_attributes,
2479 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002480 &source_key ) );
2481 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002482
Gilles Peskineca25db92019-04-19 11:43:08 +02002483 /* Prepare the target attributes. */
2484 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002485 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002486 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002487 }
Archana8a180362021-07-05 02:18:48 +05302488 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002489
Gilles Peskineca25db92019-04-19 11:43:08 +02002490 if( target_usage_arg != -1 )
2491 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2492 if( target_alg_arg != -1 )
2493 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002494 if( target_alg2_arg != -1 )
2495 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002496
Archana8a180362021-07-05 02:18:48 +05302497
Gilles Peskine57ab7212019-01-28 13:03:09 +01002498 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002499 PSA_ASSERT( psa_copy_key( source_key,
2500 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002501
2502 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002503 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002504
2505 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002506 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002507 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2508 psa_get_key_type( &target_attributes ) );
2509 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2510 psa_get_key_bits( &target_attributes ) );
2511 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2512 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002513 TEST_EQUAL( expected_alg2,
2514 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002515 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2516 {
2517 size_t length;
2518 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002519 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002520 material->len, &length ) );
2521 ASSERT_COMPARE( material->x, material->len,
2522 export_buffer, length );
2523 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002524
Archana8a180362021-07-05 02:18:48 +05302525 if( !psa_key_lifetime_is_external( target_lifetime ) )
2526 {
2527 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2528 goto exit;
2529 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2530 goto exit;
2531 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002532
Ronald Cron5425a212020-08-04 14:58:35 +02002533 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002534
2535exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002536 /*
2537 * Source and target key attributes may have been returned by
2538 * psa_get_key_attributes() thus reset them as required.
2539 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002540 psa_reset_key_attributes( &source_attributes );
2541 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002542
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002543 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002544 mbedtls_free( export_buffer );
2545}
2546/* END_CASE */
2547
2548/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002549void copy_fail( int source_usage_arg,
2550 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302551 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002552 int type_arg, data_t *material,
2553 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002554 int target_usage_arg,
2555 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002556 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002557 int expected_status_arg )
2558{
2559 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2560 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002561 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2562 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002563 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002564
2565 PSA_ASSERT( psa_crypto_init( ) );
2566
2567 /* Prepare the source key. */
2568 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2569 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002570 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002571 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302572 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002573 PSA_ASSERT( psa_import_key( &source_attributes,
2574 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002575 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002576
2577 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002578 psa_set_key_id( &target_attributes, key_id );
2579 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002580 psa_set_key_type( &target_attributes, target_type_arg );
2581 psa_set_key_bits( &target_attributes, target_bits_arg );
2582 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2583 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002584 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002585
2586 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002587 TEST_EQUAL( psa_copy_key( source_key,
2588 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002589 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002590
Ronald Cron5425a212020-08-04 14:58:35 +02002591 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002592
Gilles Peskine4a644642019-05-03 17:14:08 +02002593exit:
2594 psa_reset_key_attributes( &source_attributes );
2595 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002596 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002597}
2598/* END_CASE */
2599
2600/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002601void hash_operation_init( )
2602{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002603 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002604 /* Test each valid way of initializing the object, except for `= {0}`, as
2605 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2606 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002607 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002608 psa_hash_operation_t func = psa_hash_operation_init( );
2609 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2610 psa_hash_operation_t zero;
2611
2612 memset( &zero, 0, sizeof( zero ) );
2613
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002614 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002615 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2616 PSA_ERROR_BAD_STATE );
2617 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2618 PSA_ERROR_BAD_STATE );
2619 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2620 PSA_ERROR_BAD_STATE );
2621
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002622 /* A default hash operation should be abortable without error. */
2623 PSA_ASSERT( psa_hash_abort( &func ) );
2624 PSA_ASSERT( psa_hash_abort( &init ) );
2625 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002626}
2627/* END_CASE */
2628
2629/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630void hash_setup( int alg_arg,
2631 int expected_status_arg )
2632{
2633 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002634 uint8_t *output = NULL;
2635 size_t output_size = 0;
2636 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002637 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002638 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002639 psa_status_t status;
2640
Gilles Peskine8817f612018-12-18 00:18:46 +01002641 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002642
Neil Armstrongedb20862022-02-07 15:47:44 +01002643 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002644 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002645 ASSERT_ALLOC( output, output_size );
2646
2647 status = psa_hash_compute( alg, NULL, 0,
2648 output, output_size, &output_length );
2649 TEST_EQUAL( status, expected_status );
2650
2651 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002652 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002653 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002655 /* Whether setup succeeded or failed, abort must succeed. */
2656 PSA_ASSERT( psa_hash_abort( &operation ) );
2657
2658 /* If setup failed, reproduce the failure, so as to
2659 * test the resulting state of the operation object. */
2660 if( status != PSA_SUCCESS )
2661 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2662
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002663 /* Now the operation object should be reusable. */
2664#if defined(KNOWN_SUPPORTED_HASH_ALG)
2665 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2666 PSA_ASSERT( psa_hash_abort( &operation ) );
2667#endif
2668
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002669exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002670 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002671 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002672}
2673/* END_CASE */
2674
2675/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002676void hash_compute_fail( int alg_arg, data_t *input,
2677 int output_size_arg, int expected_status_arg )
2678{
2679 psa_algorithm_t alg = alg_arg;
2680 uint8_t *output = NULL;
2681 size_t output_size = output_size_arg;
2682 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002683 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002684 psa_status_t expected_status = expected_status_arg;
2685 psa_status_t status;
2686
2687 ASSERT_ALLOC( output, output_size );
2688
2689 PSA_ASSERT( psa_crypto_init( ) );
2690
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002691 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002692 status = psa_hash_compute( alg, input->x, input->len,
2693 output, output_size, &output_length );
2694 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002695 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002696
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002697 /* Hash Compute, multi-part */
2698 status = psa_hash_setup( &operation, alg );
2699 if( status == PSA_SUCCESS )
2700 {
2701 status = psa_hash_update( &operation, input->x, input->len );
2702 if( status == PSA_SUCCESS )
2703 {
2704 status = psa_hash_finish( &operation, output, output_size,
2705 &output_length );
2706 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002707 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002708 else
2709 TEST_EQUAL( status, expected_status );
2710 }
2711 else
2712 {
2713 TEST_EQUAL( status, expected_status );
2714 }
2715 }
2716 else
2717 {
2718 TEST_EQUAL( status, expected_status );
2719 }
2720
Gilles Peskine0a749c82019-11-28 19:33:58 +01002721exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002722 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002723 mbedtls_free( output );
2724 PSA_DONE( );
2725}
2726/* END_CASE */
2727
2728/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002729void hash_compare_fail( int alg_arg, data_t *input,
2730 data_t *reference_hash,
2731 int expected_status_arg )
2732{
2733 psa_algorithm_t alg = alg_arg;
2734 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002735 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002736 psa_status_t status;
2737
2738 PSA_ASSERT( psa_crypto_init( ) );
2739
Neil Armstrong55a1be12022-02-07 11:23:20 +01002740 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002741 status = psa_hash_compare( alg, input->x, input->len,
2742 reference_hash->x, reference_hash->len );
2743 TEST_EQUAL( status, expected_status );
2744
Neil Armstrong55a1be12022-02-07 11:23:20 +01002745 /* Hash Compare, multi-part */
2746 status = psa_hash_setup( &operation, alg );
2747 if( status == PSA_SUCCESS )
2748 {
2749 status = psa_hash_update( &operation, input->x, input->len );
2750 if( status == PSA_SUCCESS )
2751 {
2752 status = psa_hash_verify( &operation, reference_hash->x,
2753 reference_hash->len );
2754 TEST_EQUAL( status, expected_status );
2755 }
2756 else
2757 {
2758 TEST_EQUAL( status, expected_status );
2759 }
2760 }
2761 else
2762 {
2763 TEST_EQUAL( status, expected_status );
2764 }
2765
Gilles Peskine88e08462020-01-28 20:43:00 +01002766exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002767 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002768 PSA_DONE( );
2769}
2770/* END_CASE */
2771
2772/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002773void hash_compute_compare( int alg_arg, data_t *input,
2774 data_t *expected_output )
2775{
2776 psa_algorithm_t alg = alg_arg;
2777 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2778 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002779 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002780 size_t i;
2781
2782 PSA_ASSERT( psa_crypto_init( ) );
2783
Neil Armstrongca30a002022-02-07 11:40:23 +01002784 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002785 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002786 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002787 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002788 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002789 ASSERT_COMPARE( output, output_length,
2790 expected_output->x, expected_output->len );
2791
Neil Armstrongca30a002022-02-07 11:40:23 +01002792 /* Compute with tight buffer, multi-part */
2793 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2794 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2795 PSA_ASSERT( psa_hash_finish( &operation, output,
2796 PSA_HASH_LENGTH( alg ),
2797 &output_length ) );
2798 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2799 ASSERT_COMPARE( output, output_length,
2800 expected_output->x, expected_output->len );
2801
2802 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002803 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2804 output, sizeof( output ),
2805 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002806 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002807 ASSERT_COMPARE( output, output_length,
2808 expected_output->x, expected_output->len );
2809
Neil Armstrongca30a002022-02-07 11:40:23 +01002810 /* Compute with larger buffer, multi-part */
2811 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2812 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2813 PSA_ASSERT( psa_hash_finish( &operation, output,
2814 sizeof( output ), &output_length ) );
2815 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2816 ASSERT_COMPARE( output, output_length,
2817 expected_output->x, expected_output->len );
2818
2819 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002820 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2821 output, output_length ) );
2822
Neil Armstrongca30a002022-02-07 11:40:23 +01002823 /* Compare with correct hash, multi-part */
2824 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2825 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2826 PSA_ASSERT( psa_hash_verify( &operation, output,
2827 output_length ) );
2828
2829 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002830 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2831 output, output_length + 1 ),
2832 PSA_ERROR_INVALID_SIGNATURE );
2833
Neil Armstrongca30a002022-02-07 11:40:23 +01002834 /* Compare with trailing garbage, multi-part */
2835 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2836 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2837 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2838 PSA_ERROR_INVALID_SIGNATURE );
2839
2840 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002841 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2842 output, output_length - 1 ),
2843 PSA_ERROR_INVALID_SIGNATURE );
2844
Neil Armstrongca30a002022-02-07 11:40:23 +01002845 /* Compare with truncated hash, multi-part */
2846 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2847 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2848 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2849 PSA_ERROR_INVALID_SIGNATURE );
2850
Gilles Peskine0a749c82019-11-28 19:33:58 +01002851 /* Compare with corrupted value */
2852 for( i = 0; i < output_length; i++ )
2853 {
Chris Jones9634bb12021-01-20 15:56:42 +00002854 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002855 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002856
2857 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002858 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2859 output, output_length ),
2860 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002861
2862 /* Multi-Part */
2863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2864 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2865 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2866 PSA_ERROR_INVALID_SIGNATURE );
2867
Gilles Peskine0a749c82019-11-28 19:33:58 +01002868 output[i] ^= 1;
2869 }
2870
2871exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002872 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002873 PSA_DONE( );
2874}
2875/* END_CASE */
2876
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002877/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002878void hash_bad_order( )
2879{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002880 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002881 unsigned char input[] = "";
2882 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002883 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002884 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2885 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2886 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002887 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002888 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002889 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002890
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002892
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002893 /* Call setup twice in a row. */
2894 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002895 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002896 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2897 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002898 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002899 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002900 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002901
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002902 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002903 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002904 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002905 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002906
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002907 /* Check that update calls abort on error. */
2908 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002909 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002910 ASSERT_OPERATION_IS_ACTIVE( operation );
2911 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2912 PSA_ERROR_BAD_STATE );
2913 ASSERT_OPERATION_IS_INACTIVE( operation );
2914 PSA_ASSERT( psa_hash_abort( &operation ) );
2915 ASSERT_OPERATION_IS_INACTIVE( operation );
2916
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002917 /* Call update after finish. */
2918 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2919 PSA_ASSERT( psa_hash_finish( &operation,
2920 hash, sizeof( hash ), &hash_len ) );
2921 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002922 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002923 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002924
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002925 /* Call verify without calling setup beforehand. */
2926 TEST_EQUAL( psa_hash_verify( &operation,
2927 valid_hash, sizeof( valid_hash ) ),
2928 PSA_ERROR_BAD_STATE );
2929 PSA_ASSERT( psa_hash_abort( &operation ) );
2930
2931 /* Call verify after finish. */
2932 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2933 PSA_ASSERT( psa_hash_finish( &operation,
2934 hash, sizeof( hash ), &hash_len ) );
2935 TEST_EQUAL( psa_hash_verify( &operation,
2936 valid_hash, sizeof( valid_hash ) ),
2937 PSA_ERROR_BAD_STATE );
2938 PSA_ASSERT( psa_hash_abort( &operation ) );
2939
2940 /* Call verify twice in a row. */
2941 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002942 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002943 PSA_ASSERT( psa_hash_verify( &operation,
2944 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002945 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002946 TEST_EQUAL( psa_hash_verify( &operation,
2947 valid_hash, sizeof( valid_hash ) ),
2948 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002949 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002950 PSA_ASSERT( psa_hash_abort( &operation ) );
2951
2952 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002953 TEST_EQUAL( psa_hash_finish( &operation,
2954 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002955 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002956 PSA_ASSERT( psa_hash_abort( &operation ) );
2957
2958 /* Call finish twice in a row. */
2959 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2960 PSA_ASSERT( psa_hash_finish( &operation,
2961 hash, sizeof( hash ), &hash_len ) );
2962 TEST_EQUAL( psa_hash_finish( &operation,
2963 hash, sizeof( hash ), &hash_len ),
2964 PSA_ERROR_BAD_STATE );
2965 PSA_ASSERT( psa_hash_abort( &operation ) );
2966
2967 /* Call finish after calling verify. */
2968 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2969 PSA_ASSERT( psa_hash_verify( &operation,
2970 valid_hash, sizeof( valid_hash ) ) );
2971 TEST_EQUAL( psa_hash_finish( &operation,
2972 hash, sizeof( hash ), &hash_len ),
2973 PSA_ERROR_BAD_STATE );
2974 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002975
2976exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002977 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002978}
2979/* END_CASE */
2980
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002981/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002982void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002983{
2984 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002985 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2986 * appended to it */
2987 unsigned char hash[] = {
2988 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2989 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2990 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002991 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002992 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002993
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002995
itayzafrir27e69452018-11-01 14:26:34 +02002996 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002997 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002998 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002999 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003000 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003001 ASSERT_OPERATION_IS_INACTIVE( operation );
3002 PSA_ASSERT( psa_hash_abort( &operation ) );
3003 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03003004
itayzafrir27e69452018-11-01 14:26:34 +02003005 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01003006 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003007 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003008 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03003009
itayzafrir27e69452018-11-01 14:26:34 +02003010 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003012 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003013 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03003014
itayzafrirec93d302018-10-18 18:01:10 +03003015exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003016 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03003017}
3018/* END_CASE */
3019
Ronald Cronee414c72021-03-18 18:50:08 +01003020/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003021void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03003022{
3023 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003024 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003025 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003026 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003027 size_t hash_len;
3028
Gilles Peskine8817f612018-12-18 00:18:46 +01003029 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003030
itayzafrir58028322018-10-25 10:22:01 +03003031 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003033 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003034 hash, expected_size - 1, &hash_len ),
3035 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003036
3037exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003038 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003039}
3040/* END_CASE */
3041
Ronald Cronee414c72021-03-18 18:50:08 +01003042/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003043void hash_clone_source_state( )
3044{
3045 psa_algorithm_t alg = PSA_ALG_SHA_256;
3046 unsigned char hash[PSA_HASH_MAX_SIZE];
3047 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3048 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3049 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3050 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3051 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3052 size_t hash_len;
3053
3054 PSA_ASSERT( psa_crypto_init( ) );
3055 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3056
3057 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3058 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3059 PSA_ASSERT( psa_hash_finish( &op_finished,
3060 hash, sizeof( hash ), &hash_len ) );
3061 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3062 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3063
3064 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3065 PSA_ERROR_BAD_STATE );
3066
3067 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3068 PSA_ASSERT( psa_hash_finish( &op_init,
3069 hash, sizeof( hash ), &hash_len ) );
3070 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3071 PSA_ASSERT( psa_hash_finish( &op_finished,
3072 hash, sizeof( hash ), &hash_len ) );
3073 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3074 PSA_ASSERT( psa_hash_finish( &op_aborted,
3075 hash, sizeof( hash ), &hash_len ) );
3076
3077exit:
3078 psa_hash_abort( &op_source );
3079 psa_hash_abort( &op_init );
3080 psa_hash_abort( &op_setup );
3081 psa_hash_abort( &op_finished );
3082 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003083 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003084}
3085/* END_CASE */
3086
Ronald Cronee414c72021-03-18 18:50:08 +01003087/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003088void hash_clone_target_state( )
3089{
3090 psa_algorithm_t alg = PSA_ALG_SHA_256;
3091 unsigned char hash[PSA_HASH_MAX_SIZE];
3092 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3093 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3094 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3095 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3096 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3097 size_t hash_len;
3098
3099 PSA_ASSERT( psa_crypto_init( ) );
3100
3101 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3102 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3103 PSA_ASSERT( psa_hash_finish( &op_finished,
3104 hash, sizeof( hash ), &hash_len ) );
3105 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3106 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3107
3108 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3109 PSA_ASSERT( psa_hash_finish( &op_target,
3110 hash, sizeof( hash ), &hash_len ) );
3111
3112 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3113 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3114 PSA_ERROR_BAD_STATE );
3115 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3116 PSA_ERROR_BAD_STATE );
3117
3118exit:
3119 psa_hash_abort( &op_target );
3120 psa_hash_abort( &op_init );
3121 psa_hash_abort( &op_setup );
3122 psa_hash_abort( &op_finished );
3123 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003124 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003125}
3126/* END_CASE */
3127
itayzafrir58028322018-10-25 10:22:01 +03003128/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003129void mac_operation_init( )
3130{
Jaeden Amero252ef282019-02-15 14:05:35 +00003131 const uint8_t input[1] = { 0 };
3132
Jaeden Amero769ce272019-01-04 11:48:03 +00003133 /* Test each valid way of initializing the object, except for `= {0}`, as
3134 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3135 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003136 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003137 psa_mac_operation_t func = psa_mac_operation_init( );
3138 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3139 psa_mac_operation_t zero;
3140
3141 memset( &zero, 0, sizeof( zero ) );
3142
Jaeden Amero252ef282019-02-15 14:05:35 +00003143 /* A freshly-initialized MAC operation should not be usable. */
3144 TEST_EQUAL( psa_mac_update( &func,
3145 input, sizeof( input ) ),
3146 PSA_ERROR_BAD_STATE );
3147 TEST_EQUAL( psa_mac_update( &init,
3148 input, sizeof( input ) ),
3149 PSA_ERROR_BAD_STATE );
3150 TEST_EQUAL( psa_mac_update( &zero,
3151 input, sizeof( input ) ),
3152 PSA_ERROR_BAD_STATE );
3153
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003154 /* A default MAC operation should be abortable without error. */
3155 PSA_ASSERT( psa_mac_abort( &func ) );
3156 PSA_ASSERT( psa_mac_abort( &init ) );
3157 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003158}
3159/* END_CASE */
3160
3161/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003162void mac_setup( int key_type_arg,
3163 data_t *key,
3164 int alg_arg,
3165 int expected_status_arg )
3166{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003167 psa_key_type_t key_type = key_type_arg;
3168 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003169 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003170 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003171 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3172#if defined(KNOWN_SUPPORTED_MAC_ALG)
3173 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3174#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003177
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003178 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3179 &operation, &status ) )
3180 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003181 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003182
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003183 /* The operation object should be reusable. */
3184#if defined(KNOWN_SUPPORTED_MAC_ALG)
3185 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3186 smoke_test_key_data,
3187 sizeof( smoke_test_key_data ),
3188 KNOWN_SUPPORTED_MAC_ALG,
3189 &operation, &status ) )
3190 goto exit;
3191 TEST_EQUAL( status, PSA_SUCCESS );
3192#endif
3193
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003194exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003195 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003196}
3197/* END_CASE */
3198
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003199/* 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 +00003200void mac_bad_order( )
3201{
Ronald Cron5425a212020-08-04 14:58:35 +02003202 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003203 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3204 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003205 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003206 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3207 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3208 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003210 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3211 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3212 size_t sign_mac_length = 0;
3213 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3214 const uint8_t verify_mac[] = {
3215 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3216 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3217 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3218
3219 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003221 psa_set_key_algorithm( &attributes, alg );
3222 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003223
Ronald Cron5425a212020-08-04 14:58:35 +02003224 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3225 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003226
Jaeden Amero252ef282019-02-15 14:05:35 +00003227 /* Call update without calling setup beforehand. */
3228 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3229 PSA_ERROR_BAD_STATE );
3230 PSA_ASSERT( psa_mac_abort( &operation ) );
3231
3232 /* Call sign finish without calling setup beforehand. */
3233 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3234 &sign_mac_length),
3235 PSA_ERROR_BAD_STATE );
3236 PSA_ASSERT( psa_mac_abort( &operation ) );
3237
3238 /* Call verify finish without calling setup beforehand. */
3239 TEST_EQUAL( psa_mac_verify_finish( &operation,
3240 verify_mac, sizeof( verify_mac ) ),
3241 PSA_ERROR_BAD_STATE );
3242 PSA_ASSERT( psa_mac_abort( &operation ) );
3243
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003244 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003245 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003246 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003247 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003248 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003249 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003250 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003251 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003252
Jaeden Amero252ef282019-02-15 14:05:35 +00003253 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003254 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003255 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3256 PSA_ASSERT( psa_mac_sign_finish( &operation,
3257 sign_mac, sizeof( sign_mac ),
3258 &sign_mac_length ) );
3259 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3260 PSA_ERROR_BAD_STATE );
3261 PSA_ASSERT( psa_mac_abort( &operation ) );
3262
3263 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003264 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003265 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3266 PSA_ASSERT( psa_mac_verify_finish( &operation,
3267 verify_mac, sizeof( verify_mac ) ) );
3268 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3269 PSA_ERROR_BAD_STATE );
3270 PSA_ASSERT( psa_mac_abort( &operation ) );
3271
3272 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003273 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003274 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3275 PSA_ASSERT( psa_mac_sign_finish( &operation,
3276 sign_mac, sizeof( sign_mac ),
3277 &sign_mac_length ) );
3278 TEST_EQUAL( psa_mac_sign_finish( &operation,
3279 sign_mac, sizeof( sign_mac ),
3280 &sign_mac_length ),
3281 PSA_ERROR_BAD_STATE );
3282 PSA_ASSERT( psa_mac_abort( &operation ) );
3283
3284 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003285 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003286 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3287 PSA_ASSERT( psa_mac_verify_finish( &operation,
3288 verify_mac, sizeof( verify_mac ) ) );
3289 TEST_EQUAL( psa_mac_verify_finish( &operation,
3290 verify_mac, sizeof( verify_mac ) ),
3291 PSA_ERROR_BAD_STATE );
3292 PSA_ASSERT( psa_mac_abort( &operation ) );
3293
3294 /* Setup sign but try verify. */
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 ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003297 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003298 TEST_EQUAL( psa_mac_verify_finish( &operation,
3299 verify_mac, sizeof( verify_mac ) ),
3300 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003301 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003302 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003303 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003304
3305 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003306 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003307 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003308 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003309 TEST_EQUAL( psa_mac_sign_finish( &operation,
3310 sign_mac, sizeof( sign_mac ),
3311 &sign_mac_length ),
3312 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003313 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003314 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003315 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316
Ronald Cron5425a212020-08-04 14:58:35 +02003317 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003318
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003319exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003320 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003321}
3322/* END_CASE */
3323
3324/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003325void mac_sign_verify_multi( int key_type_arg,
3326 data_t *key_data,
3327 int alg_arg,
3328 data_t *input,
3329 int is_verify,
3330 data_t *expected_mac )
3331{
3332 size_t data_part_len = 0;
3333
3334 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3335 {
3336 /* Split data into length(data_part_len) parts. */
3337 mbedtls_test_set_step( 2000 + data_part_len );
3338
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003339 if( mac_multipart_internal_func( key_type_arg, key_data,
3340 alg_arg,
3341 input, data_part_len,
3342 expected_mac,
3343 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003344 break;
3345
3346 /* length(0) part, length(data_part_len) part, length(0) part... */
3347 mbedtls_test_set_step( 3000 + data_part_len );
3348
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003349 if( mac_multipart_internal_func( key_type_arg, key_data,
3350 alg_arg,
3351 input, data_part_len,
3352 expected_mac,
3353 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003354 break;
3355 }
3356
3357 /* Goto is required to silence warnings about unused labels, as we
3358 * don't actually do any test assertions in this function. */
3359 goto exit;
3360}
3361/* END_CASE */
3362
3363/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003364void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003365 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003366 int alg_arg,
3367 data_t *input,
3368 data_t *expected_mac )
3369{
Ronald Cron5425a212020-08-04 14:58:35 +02003370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003371 psa_key_type_t key_type = key_type_arg;
3372 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003373 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003375 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003377 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003378 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003379 const size_t output_sizes_to_test[] = {
3380 0,
3381 1,
3382 expected_mac->len - 1,
3383 expected_mac->len,
3384 expected_mac->len + 1,
3385 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003386
Gilles Peskine7be11a72022-04-14 00:12:57 +02003387 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003388 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003389 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003390
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003392
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003393 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003394 psa_set_key_algorithm( &attributes, alg );
3395 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003396
Ronald Cron5425a212020-08-04 14:58:35 +02003397 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3398 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003399
Gilles Peskine8b356b52020-08-25 23:44:59 +02003400 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3401 {
3402 const size_t output_size = output_sizes_to_test[i];
3403 psa_status_t expected_status =
3404 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3405 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003406
Chris Jones9634bb12021-01-20 15:56:42 +00003407 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003408 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003409
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003410 /* Calculate the MAC, one-shot case. */
3411 TEST_EQUAL( psa_mac_compute( key, alg,
3412 input->x, input->len,
3413 actual_mac, output_size, &mac_length ),
3414 expected_status );
3415 if( expected_status == PSA_SUCCESS )
3416 {
3417 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3418 actual_mac, mac_length );
3419 }
3420
3421 if( output_size > 0 )
3422 memset( actual_mac, 0, output_size );
3423
3424 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003425 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003426 PSA_ASSERT( psa_mac_update( &operation,
3427 input->x, input->len ) );
3428 TEST_EQUAL( psa_mac_sign_finish( &operation,
3429 actual_mac, output_size,
3430 &mac_length ),
3431 expected_status );
3432 PSA_ASSERT( psa_mac_abort( &operation ) );
3433
3434 if( expected_status == PSA_SUCCESS )
3435 {
3436 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3437 actual_mac, mac_length );
3438 }
3439 mbedtls_free( actual_mac );
3440 actual_mac = NULL;
3441 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003442
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003443exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003444 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003445 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003446 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003447 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003448}
3449/* END_CASE */
3450
3451/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003452void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003453 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003454 int alg_arg,
3455 data_t *input,
3456 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003457{
Ronald Cron5425a212020-08-04 14:58:35 +02003458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003459 psa_key_type_t key_type = key_type_arg;
3460 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003461 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003463 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003464
Gilles Peskine7be11a72022-04-14 00:12:57 +02003465 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003466
Gilles Peskine8817f612018-12-18 00:18:46 +01003467 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003468
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003469 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003470 psa_set_key_algorithm( &attributes, alg );
3471 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003472
Ronald Cron5425a212020-08-04 14:58:35 +02003473 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3474 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003475
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003476 /* Verify correct MAC, one-shot case. */
3477 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3478 expected_mac->x, expected_mac->len ) );
3479
3480 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003481 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003482 PSA_ASSERT( psa_mac_update( &operation,
3483 input->x, input->len ) );
3484 PSA_ASSERT( psa_mac_verify_finish( &operation,
3485 expected_mac->x,
3486 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003487
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003488 /* Test a MAC that's too short, one-shot case. */
3489 TEST_EQUAL( psa_mac_verify( key, alg,
3490 input->x, input->len,
3491 expected_mac->x,
3492 expected_mac->len - 1 ),
3493 PSA_ERROR_INVALID_SIGNATURE );
3494
3495 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003496 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003497 PSA_ASSERT( psa_mac_update( &operation,
3498 input->x, input->len ) );
3499 TEST_EQUAL( psa_mac_verify_finish( &operation,
3500 expected_mac->x,
3501 expected_mac->len - 1 ),
3502 PSA_ERROR_INVALID_SIGNATURE );
3503
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003504 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003505 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3506 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003507 TEST_EQUAL( psa_mac_verify( key, alg,
3508 input->x, input->len,
3509 perturbed_mac, expected_mac->len + 1 ),
3510 PSA_ERROR_INVALID_SIGNATURE );
3511
3512 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003513 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003514 PSA_ASSERT( psa_mac_update( &operation,
3515 input->x, input->len ) );
3516 TEST_EQUAL( psa_mac_verify_finish( &operation,
3517 perturbed_mac,
3518 expected_mac->len + 1 ),
3519 PSA_ERROR_INVALID_SIGNATURE );
3520
3521 /* Test changing one byte. */
3522 for( size_t i = 0; i < expected_mac->len; i++ )
3523 {
Chris Jones9634bb12021-01-20 15:56:42 +00003524 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003525 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003526
3527 TEST_EQUAL( psa_mac_verify( key, alg,
3528 input->x, input->len,
3529 perturbed_mac, expected_mac->len ),
3530 PSA_ERROR_INVALID_SIGNATURE );
3531
Ronald Cron5425a212020-08-04 14:58:35 +02003532 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003533 PSA_ASSERT( psa_mac_update( &operation,
3534 input->x, input->len ) );
3535 TEST_EQUAL( psa_mac_verify_finish( &operation,
3536 perturbed_mac,
3537 expected_mac->len ),
3538 PSA_ERROR_INVALID_SIGNATURE );
3539 perturbed_mac[i] ^= 1;
3540 }
3541
Gilles Peskine8c9def32018-02-08 10:02:12 +01003542exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003543 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003544 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003545 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003546 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003547}
3548/* END_CASE */
3549
3550/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003551void cipher_operation_init( )
3552{
Jaeden Ameroab439972019-02-15 14:12:05 +00003553 const uint8_t input[1] = { 0 };
3554 unsigned char output[1] = { 0 };
3555 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003556 /* Test each valid way of initializing the object, except for `= {0}`, as
3557 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3558 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003559 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003560 psa_cipher_operation_t func = psa_cipher_operation_init( );
3561 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3562 psa_cipher_operation_t zero;
3563
3564 memset( &zero, 0, sizeof( zero ) );
3565
Jaeden Ameroab439972019-02-15 14:12:05 +00003566 /* A freshly-initialized cipher operation should not be usable. */
3567 TEST_EQUAL( psa_cipher_update( &func,
3568 input, sizeof( input ),
3569 output, sizeof( output ),
3570 &output_length ),
3571 PSA_ERROR_BAD_STATE );
3572 TEST_EQUAL( psa_cipher_update( &init,
3573 input, sizeof( input ),
3574 output, sizeof( output ),
3575 &output_length ),
3576 PSA_ERROR_BAD_STATE );
3577 TEST_EQUAL( psa_cipher_update( &zero,
3578 input, sizeof( input ),
3579 output, sizeof( output ),
3580 &output_length ),
3581 PSA_ERROR_BAD_STATE );
3582
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003583 /* A default cipher operation should be abortable without error. */
3584 PSA_ASSERT( psa_cipher_abort( &func ) );
3585 PSA_ASSERT( psa_cipher_abort( &init ) );
3586 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003587}
3588/* END_CASE */
3589
3590/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003591void cipher_setup( int key_type_arg,
3592 data_t *key,
3593 int alg_arg,
3594 int expected_status_arg )
3595{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003596 psa_key_type_t key_type = key_type_arg;
3597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003598 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003599 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003600 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003601#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003602 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3603#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003604
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003606
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003607 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3608 &operation, &status ) )
3609 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003610 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003611
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003612 /* The operation object should be reusable. */
3613#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3614 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3615 smoke_test_key_data,
3616 sizeof( smoke_test_key_data ),
3617 KNOWN_SUPPORTED_CIPHER_ALG,
3618 &operation, &status ) )
3619 goto exit;
3620 TEST_EQUAL( status, PSA_SUCCESS );
3621#endif
3622
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003623exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003624 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003625 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003626}
3627/* END_CASE */
3628
Ronald Cronee414c72021-03-18 18:50:08 +01003629/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003630void cipher_bad_order( )
3631{
Ronald Cron5425a212020-08-04 14:58:35 +02003632 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003633 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3634 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003636 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003637 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003638 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003639 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3640 0xaa, 0xaa, 0xaa, 0xaa };
3641 const uint8_t text[] = {
3642 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3643 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003644 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003645 size_t length = 0;
3646
3647 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003648 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3649 psa_set_key_algorithm( &attributes, alg );
3650 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003651 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3652 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003653
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003654 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003655 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003656 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003657 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003658 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003659 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003660 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003661 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003662
3663 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003664 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003665 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003666 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003667 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003668 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003669 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003670 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003671
Jaeden Ameroab439972019-02-15 14:12:05 +00003672 /* Generate an IV without calling setup beforehand. */
3673 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3674 buffer, sizeof( buffer ),
3675 &length ),
3676 PSA_ERROR_BAD_STATE );
3677 PSA_ASSERT( psa_cipher_abort( &operation ) );
3678
3679 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003680 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003681 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3682 buffer, sizeof( buffer ),
3683 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003684 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003685 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3686 buffer, sizeof( buffer ),
3687 &length ),
3688 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003689 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003690 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003691 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003692
3693 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003694 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003695 PSA_ASSERT( psa_cipher_set_iv( &operation,
3696 iv, sizeof( iv ) ) );
3697 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3698 buffer, sizeof( buffer ),
3699 &length ),
3700 PSA_ERROR_BAD_STATE );
3701 PSA_ASSERT( psa_cipher_abort( &operation ) );
3702
3703 /* Set an IV without calling setup beforehand. */
3704 TEST_EQUAL( psa_cipher_set_iv( &operation,
3705 iv, sizeof( iv ) ),
3706 PSA_ERROR_BAD_STATE );
3707 PSA_ASSERT( psa_cipher_abort( &operation ) );
3708
3709 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003710 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003711 PSA_ASSERT( psa_cipher_set_iv( &operation,
3712 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003713 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003714 TEST_EQUAL( psa_cipher_set_iv( &operation,
3715 iv, sizeof( iv ) ),
3716 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003717 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003718 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003719 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003720
3721 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003722 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003723 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3724 buffer, sizeof( buffer ),
3725 &length ) );
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 /* Call update without calling setup beforehand. */
3732 TEST_EQUAL( psa_cipher_update( &operation,
3733 text, sizeof( text ),
3734 buffer, sizeof( buffer ),
3735 &length ),
3736 PSA_ERROR_BAD_STATE );
3737 PSA_ASSERT( psa_cipher_abort( &operation ) );
3738
3739 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003740 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003741 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003742 TEST_EQUAL( psa_cipher_update( &operation,
3743 text, sizeof( text ),
3744 buffer, sizeof( buffer ),
3745 &length ),
3746 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003747 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003748 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003749 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003750
3751 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003752 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003753 PSA_ASSERT( psa_cipher_set_iv( &operation,
3754 iv, sizeof( iv ) ) );
3755 PSA_ASSERT( psa_cipher_finish( &operation,
3756 buffer, sizeof( buffer ), &length ) );
3757 TEST_EQUAL( psa_cipher_update( &operation,
3758 text, sizeof( text ),
3759 buffer, sizeof( buffer ),
3760 &length ),
3761 PSA_ERROR_BAD_STATE );
3762 PSA_ASSERT( psa_cipher_abort( &operation ) );
3763
3764 /* Call finish without calling setup beforehand. */
3765 TEST_EQUAL( psa_cipher_finish( &operation,
3766 buffer, sizeof( buffer ), &length ),
3767 PSA_ERROR_BAD_STATE );
3768 PSA_ASSERT( psa_cipher_abort( &operation ) );
3769
3770 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003771 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003772 /* Not calling update means we are encrypting an empty buffer, which is OK
3773 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003774 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003775 TEST_EQUAL( psa_cipher_finish( &operation,
3776 buffer, sizeof( buffer ), &length ),
3777 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003778 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003779 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003780 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003781
3782 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003783 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003784 PSA_ASSERT( psa_cipher_set_iv( &operation,
3785 iv, sizeof( iv ) ) );
3786 PSA_ASSERT( psa_cipher_finish( &operation,
3787 buffer, sizeof( buffer ), &length ) );
3788 TEST_EQUAL( psa_cipher_finish( &operation,
3789 buffer, sizeof( buffer ), &length ),
3790 PSA_ERROR_BAD_STATE );
3791 PSA_ASSERT( psa_cipher_abort( &operation ) );
3792
Ronald Cron5425a212020-08-04 14:58:35 +02003793 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003794
Jaeden Ameroab439972019-02-15 14:12:05 +00003795exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003796 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003797 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003798}
3799/* END_CASE */
3800
3801/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003802void cipher_encrypt_fail( int alg_arg,
3803 int key_type_arg,
3804 data_t *key_data,
3805 data_t *input,
3806 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003807{
Ronald Cron5425a212020-08-04 14:58:35 +02003808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003809 psa_status_t status;
3810 psa_key_type_t key_type = key_type_arg;
3811 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003812 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003813 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3814 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3815 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003816 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003817 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003818 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003819 size_t function_output_length;
3820 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3822
3823 if ( PSA_ERROR_BAD_STATE != expected_status )
3824 {
3825 PSA_ASSERT( psa_crypto_init( ) );
3826
3827 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3828 psa_set_key_algorithm( &attributes, alg );
3829 psa_set_key_type( &attributes, key_type );
3830
3831 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3832 input->len );
3833 ASSERT_ALLOC( output, output_buffer_size );
3834
3835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3836 &key ) );
3837 }
3838
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003839 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003840 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3841 output_buffer_size, &output_length );
3842
3843 TEST_EQUAL( status, expected_status );
3844
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003845 /* Encrypt, multi-part */
3846 status = psa_cipher_encrypt_setup( &operation, key, alg );
3847 if( status == PSA_SUCCESS )
3848 {
3849 if( alg != PSA_ALG_ECB_NO_PADDING )
3850 {
3851 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3852 iv, iv_size,
3853 &iv_length ) );
3854 }
3855
3856 status = psa_cipher_update( &operation, input->x, input->len,
3857 output, output_buffer_size,
3858 &function_output_length );
3859 if( status == PSA_SUCCESS )
3860 {
3861 output_length += function_output_length;
3862
3863 status = psa_cipher_finish( &operation, output + output_length,
3864 output_buffer_size - output_length,
3865 &function_output_length );
3866
3867 TEST_EQUAL( status, expected_status );
3868 }
3869 else
3870 {
3871 TEST_EQUAL( status, expected_status );
3872 }
3873 }
3874 else
3875 {
3876 TEST_EQUAL( status, expected_status );
3877 }
3878
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003879exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003880 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003881 mbedtls_free( output );
3882 psa_destroy_key( key );
3883 PSA_DONE( );
3884}
3885/* END_CASE */
3886
3887/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003888void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3889 data_t *input, int iv_length,
3890 int expected_result )
3891{
3892 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3893 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3895 size_t output_buffer_size = 0;
3896 unsigned char *output = NULL;
3897
3898 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3899 ASSERT_ALLOC( output, output_buffer_size );
3900
3901 PSA_ASSERT( psa_crypto_init( ) );
3902
3903 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3904 psa_set_key_algorithm( &attributes, alg );
3905 psa_set_key_type( &attributes, key_type );
3906
3907 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3908 &key ) );
3909 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3910 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3911 iv_length ) );
3912
3913exit:
3914 psa_cipher_abort( &operation );
3915 mbedtls_free( output );
3916 psa_destroy_key( key );
3917 PSA_DONE( );
3918}
3919/* END_CASE */
3920
3921/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003922void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3923 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003924{
3925 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3926 psa_key_type_t key_type = key_type_arg;
3927 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003928 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3929 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003930 unsigned char *output = NULL;
3931 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003932 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3934
3935 PSA_ASSERT( psa_crypto_init( ) );
3936
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003937 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003938 TEST_LE_U( ciphertext->len,
3939 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3940 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003941 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003942 TEST_LE_U( plaintext->len,
3943 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3944 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3945 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003946
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003947
3948 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003949 psa_set_key_usage_flags( &attributes,
3950 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003951 psa_set_key_algorithm( &attributes, alg );
3952 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003953 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3954 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003955 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3956 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003957 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003958
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003959 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003960 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3961 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3962 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003963 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3964 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003965 PSA_ERROR_BAD_STATE );
3966
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003967 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003968 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3969 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003970 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003971 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003972 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3973 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003974 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003975 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003976
Gilles Peskine286c3142022-04-20 17:09:38 +02003977 /* Multipart encryption */
3978 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3979 output_length = 0;
3980 length = ~0;
3981 PSA_ASSERT( psa_cipher_update( &operation,
3982 plaintext->x, plaintext->len,
3983 output, output_buffer_size,
3984 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003985 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003986 output_length += length;
3987 PSA_ASSERT( psa_cipher_finish( &operation,
3988 output + output_length,
3989 output_buffer_size - output_length,
3990 &length ) );
3991 output_length += length;
3992 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003993 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003994
Gilles Peskine286c3142022-04-20 17:09:38 +02003995 /* Multipart encryption */
3996 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3997 output_length = 0;
3998 length = ~0;
3999 PSA_ASSERT( psa_cipher_update( &operation,
4000 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004001 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02004002 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004003 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02004004 output_length += length;
4005 PSA_ASSERT( psa_cipher_finish( &operation,
4006 output + output_length,
4007 output_buffer_size - output_length,
4008 &length ) );
4009 output_length += length;
4010 ASSERT_COMPARE( plaintext->x, plaintext->len,
4011 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004012
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004013 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004014 output_length = ~0;
4015 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
4016 output, output_buffer_size,
4017 &output_length ) );
4018 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
4019 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004020
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004021 /* One-shot decryption */
4022 output_length = ~0;
4023 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4024 output, output_buffer_size,
4025 &output_length ) );
4026 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004027 output, output_length );
4028
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004029exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004030 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004031 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004032 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004033 psa_destroy_key( key );
4034 PSA_DONE( );
4035}
4036/* END_CASE */
4037
4038/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004039void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4040{
4041 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4042 psa_algorithm_t alg = alg_arg;
4043 psa_key_type_t key_type = key_type_arg;
4044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4045 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4046 psa_status_t status;
4047
4048 PSA_ASSERT( psa_crypto_init( ) );
4049
4050 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4051 psa_set_key_algorithm( &attributes, alg );
4052 psa_set_key_type( &attributes, key_type );
4053
4054 /* Usage of either of these two size macros would cause divide by zero
4055 * with incorrect key types previously. Input length should be irrelevant
4056 * here. */
4057 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4058 0 );
4059 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4060
4061
4062 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4063 &key ) );
4064
4065 /* Should fail due to invalid alg type (to support invalid key type).
4066 * Encrypt or decrypt will end up in the same place. */
4067 status = psa_cipher_encrypt_setup( &operation, key, alg );
4068
4069 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4070
4071exit:
4072 psa_cipher_abort( &operation );
4073 psa_destroy_key( key );
4074 PSA_DONE( );
4075}
4076/* END_CASE */
4077
4078/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004079void cipher_encrypt_validation( int alg_arg,
4080 int key_type_arg,
4081 data_t *key_data,
4082 data_t *input )
4083{
4084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4085 psa_key_type_t key_type = key_type_arg;
4086 psa_algorithm_t alg = alg_arg;
4087 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4088 unsigned char *output1 = NULL;
4089 size_t output1_buffer_size = 0;
4090 size_t output1_length = 0;
4091 unsigned char *output2 = NULL;
4092 size_t output2_buffer_size = 0;
4093 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004094 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004095 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004097
Gilles Peskine8817f612018-12-18 00:18:46 +01004098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004099
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4101 psa_set_key_algorithm( &attributes, alg );
4102 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004103
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004104 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4105 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4106 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4107 ASSERT_ALLOC( output1, output1_buffer_size );
4108 ASSERT_ALLOC( output2, output2_buffer_size );
4109
Ronald Cron5425a212020-08-04 14:58:35 +02004110 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4111 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004112
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004113 /* The one-shot cipher encryption uses generated iv so validating
4114 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004115 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4116 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004117 TEST_LE_U( output1_length,
4118 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4119 TEST_LE_U( output1_length,
4120 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004121
4122 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4123 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004124
Gilles Peskine8817f612018-12-18 00:18:46 +01004125 PSA_ASSERT( psa_cipher_update( &operation,
4126 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004127 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004128 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004129 TEST_LE_U( function_output_length,
4130 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4131 TEST_LE_U( function_output_length,
4132 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004133 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004134
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004135 PSA_ASSERT( psa_cipher_finish( &operation,
4136 output2 + output2_length,
4137 output2_buffer_size - output2_length,
4138 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004139 TEST_LE_U( function_output_length,
4140 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4141 TEST_LE_U( function_output_length,
4142 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004143 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004144
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004145 PSA_ASSERT( psa_cipher_abort( &operation ) );
4146 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4147 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004148
Gilles Peskine50e586b2018-06-08 14:28:46 +02004149exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004150 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004151 mbedtls_free( output1 );
4152 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004153 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004154 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004155}
4156/* END_CASE */
4157
4158/* BEGIN_CASE */
4159void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004160 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004161 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004162 int first_part_size_arg,
4163 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004164 data_t *expected_output,
4165 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004166{
Ronald Cron5425a212020-08-04 14:58:35 +02004167 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004168 psa_key_type_t key_type = key_type_arg;
4169 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004170 psa_status_t status;
4171 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004172 size_t first_part_size = first_part_size_arg;
4173 size_t output1_length = output1_length_arg;
4174 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004175 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004176 size_t output_buffer_size = 0;
4177 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004178 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004179 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004181
Gilles Peskine8817f612018-12-18 00:18:46 +01004182 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004183
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004184 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4185 psa_set_key_algorithm( &attributes, alg );
4186 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004187
Ronald Cron5425a212020-08-04 14:58:35 +02004188 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4189 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004190
Ronald Cron5425a212020-08-04 14:58:35 +02004191 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004192
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004193 if( iv->len > 0 )
4194 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004195 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004196 }
4197
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004198 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4199 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004200 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004201
Gilles Peskine7be11a72022-04-14 00:12:57 +02004202 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004203 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4204 output, output_buffer_size,
4205 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004206 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004207 TEST_LE_U( function_output_length,
4208 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4209 TEST_LE_U( function_output_length,
4210 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004211 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004212
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004213 if( first_part_size < input->len )
4214 {
4215 PSA_ASSERT( psa_cipher_update( &operation,
4216 input->x + first_part_size,
4217 input->len - first_part_size,
4218 ( output_buffer_size == 0 ? NULL :
4219 output + total_output_length ),
4220 output_buffer_size - total_output_length,
4221 &function_output_length ) );
4222 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004223 TEST_LE_U( function_output_length,
4224 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4225 alg,
4226 input->len - first_part_size ) );
4227 TEST_LE_U( function_output_length,
4228 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004229 total_output_length += function_output_length;
4230 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004231
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004232 status = psa_cipher_finish( &operation,
4233 ( output_buffer_size == 0 ? NULL :
4234 output + total_output_length ),
4235 output_buffer_size - total_output_length,
4236 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004237 TEST_LE_U( function_output_length,
4238 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4239 TEST_LE_U( function_output_length,
4240 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004241 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004242 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004243
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004244 if( expected_status == PSA_SUCCESS )
4245 {
4246 PSA_ASSERT( psa_cipher_abort( &operation ) );
4247
4248 ASSERT_COMPARE( expected_output->x, expected_output->len,
4249 output, total_output_length );
4250 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004251
4252exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004253 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004254 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004255 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004256 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004257}
4258/* END_CASE */
4259
4260/* BEGIN_CASE */
4261void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004262 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004263 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004264 int first_part_size_arg,
4265 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004266 data_t *expected_output,
4267 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268{
Ronald Cron5425a212020-08-04 14:58:35 +02004269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004270 psa_key_type_t key_type = key_type_arg;
4271 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004272 psa_status_t status;
4273 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004274 size_t first_part_size = first_part_size_arg;
4275 size_t output1_length = output1_length_arg;
4276 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004277 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004278 size_t output_buffer_size = 0;
4279 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004280 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004281 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004283
Gilles Peskine8817f612018-12-18 00:18:46 +01004284 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004285
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4287 psa_set_key_algorithm( &attributes, alg );
4288 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004289
Ronald Cron5425a212020-08-04 14:58:35 +02004290 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4291 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004292
Ronald Cron5425a212020-08-04 14:58:35 +02004293 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004294
Steven Cooreman177deba2020-09-07 17:14:14 +02004295 if( iv->len > 0 )
4296 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004297 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004298 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004299
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004300 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4301 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004302 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004303
Gilles Peskine7be11a72022-04-14 00:12:57 +02004304 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004305 PSA_ASSERT( psa_cipher_update( &operation,
4306 input->x, first_part_size,
4307 output, output_buffer_size,
4308 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004309 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004310 TEST_LE_U( function_output_length,
4311 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4312 TEST_LE_U( function_output_length,
4313 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004314 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004315
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004316 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004317 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004318 PSA_ASSERT( psa_cipher_update( &operation,
4319 input->x + first_part_size,
4320 input->len - first_part_size,
4321 ( output_buffer_size == 0 ? NULL :
4322 output + total_output_length ),
4323 output_buffer_size - total_output_length,
4324 &function_output_length ) );
4325 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004326 TEST_LE_U( function_output_length,
4327 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4328 alg,
4329 input->len - first_part_size ) );
4330 TEST_LE_U( function_output_length,
4331 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004332 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004333 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004334
Gilles Peskine50e586b2018-06-08 14:28:46 +02004335 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004336 ( output_buffer_size == 0 ? NULL :
4337 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004338 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004339 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004340 TEST_LE_U( function_output_length,
4341 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4342 TEST_LE_U( function_output_length,
4343 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004344 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004345 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004346
4347 if( expected_status == PSA_SUCCESS )
4348 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004349 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004350
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004351 ASSERT_COMPARE( expected_output->x, expected_output->len,
4352 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004353 }
4354
Gilles Peskine50e586b2018-06-08 14:28:46 +02004355exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004356 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004357 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004358 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004359 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004360}
4361/* END_CASE */
4362
Gilles Peskine50e586b2018-06-08 14:28:46 +02004363/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004364void cipher_decrypt_fail( int alg_arg,
4365 int key_type_arg,
4366 data_t *key_data,
4367 data_t *iv,
4368 data_t *input_arg,
4369 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004370{
4371 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4372 psa_status_t status;
4373 psa_key_type_t key_type = key_type_arg;
4374 psa_algorithm_t alg = alg_arg;
4375 psa_status_t expected_status = expected_status_arg;
4376 unsigned char *input = NULL;
4377 size_t input_buffer_size = 0;
4378 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004379 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004380 size_t output_buffer_size = 0;
4381 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004382 size_t function_output_length;
4383 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4385
4386 if ( PSA_ERROR_BAD_STATE != expected_status )
4387 {
4388 PSA_ASSERT( psa_crypto_init( ) );
4389
4390 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4391 psa_set_key_algorithm( &attributes, alg );
4392 psa_set_key_type( &attributes, key_type );
4393
4394 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4395 &key ) );
4396 }
4397
4398 /* Allocate input buffer and copy the iv and the plaintext */
4399 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4400 if ( input_buffer_size > 0 )
4401 {
4402 ASSERT_ALLOC( input, input_buffer_size );
4403 memcpy( input, iv->x, iv->len );
4404 memcpy( input + iv->len, input_arg->x, input_arg->len );
4405 }
4406
4407 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4408 ASSERT_ALLOC( output, output_buffer_size );
4409
Neil Armstrong66a479f2022-02-07 15:41:19 +01004410 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004411 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4412 output_buffer_size, &output_length );
4413 TEST_EQUAL( status, expected_status );
4414
Neil Armstrong66a479f2022-02-07 15:41:19 +01004415 /* Decrypt, multi-part */
4416 status = psa_cipher_decrypt_setup( &operation, key, alg );
4417 if( status == PSA_SUCCESS )
4418 {
4419 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4420 input_arg->len ) +
4421 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4422 ASSERT_ALLOC( output_multi, output_buffer_size );
4423
4424 if( iv->len > 0 )
4425 {
4426 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4427
4428 if( status != PSA_SUCCESS )
4429 TEST_EQUAL( status, expected_status );
4430 }
4431
4432 if( status == PSA_SUCCESS )
4433 {
4434 status = psa_cipher_update( &operation,
4435 input_arg->x, input_arg->len,
4436 output_multi, output_buffer_size,
4437 &function_output_length );
4438 if( status == PSA_SUCCESS )
4439 {
4440 output_length = function_output_length;
4441
4442 status = psa_cipher_finish( &operation,
4443 output_multi + output_length,
4444 output_buffer_size - output_length,
4445 &function_output_length );
4446
4447 TEST_EQUAL( status, expected_status );
4448 }
4449 else
4450 {
4451 TEST_EQUAL( status, expected_status );
4452 }
4453 }
4454 else
4455 {
4456 TEST_EQUAL( status, expected_status );
4457 }
4458 }
4459 else
4460 {
4461 TEST_EQUAL( status, expected_status );
4462 }
4463
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004464exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004465 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004466 mbedtls_free( input );
4467 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004468 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004469 psa_destroy_key( key );
4470 PSA_DONE( );
4471}
4472/* END_CASE */
4473
4474/* BEGIN_CASE */
4475void cipher_decrypt( int alg_arg,
4476 int key_type_arg,
4477 data_t *key_data,
4478 data_t *iv,
4479 data_t *input_arg,
4480 data_t *expected_output )
4481{
4482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4483 psa_key_type_t key_type = key_type_arg;
4484 psa_algorithm_t alg = alg_arg;
4485 unsigned char *input = NULL;
4486 size_t input_buffer_size = 0;
4487 unsigned char *output = NULL;
4488 size_t output_buffer_size = 0;
4489 size_t output_length = 0;
4490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4491
4492 PSA_ASSERT( psa_crypto_init( ) );
4493
4494 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4495 psa_set_key_algorithm( &attributes, alg );
4496 psa_set_key_type( &attributes, key_type );
4497
4498 /* Allocate input buffer and copy the iv and the plaintext */
4499 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4500 if ( input_buffer_size > 0 )
4501 {
4502 ASSERT_ALLOC( input, input_buffer_size );
4503 memcpy( input, iv->x, iv->len );
4504 memcpy( input + iv->len, input_arg->x, input_arg->len );
4505 }
4506
4507 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4508 ASSERT_ALLOC( output, output_buffer_size );
4509
4510 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4511 &key ) );
4512
4513 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4514 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004515 TEST_LE_U( output_length,
4516 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4517 TEST_LE_U( output_length,
4518 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004519
4520 ASSERT_COMPARE( expected_output->x, expected_output->len,
4521 output, output_length );
4522exit:
4523 mbedtls_free( input );
4524 mbedtls_free( output );
4525 psa_destroy_key( key );
4526 PSA_DONE( );
4527}
4528/* END_CASE */
4529
4530/* BEGIN_CASE */
4531void cipher_verify_output( int alg_arg,
4532 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004533 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004534 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004535{
Ronald Cron5425a212020-08-04 14:58:35 +02004536 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004537 psa_key_type_t key_type = key_type_arg;
4538 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004539 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004540 size_t output1_size = 0;
4541 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004542 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004543 size_t output2_size = 0;
4544 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004546
Gilles Peskine8817f612018-12-18 00:18:46 +01004547 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004548
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004549 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4550 psa_set_key_algorithm( &attributes, alg );
4551 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004552
Ronald Cron5425a212020-08-04 14:58:35 +02004553 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4554 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004555 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004556 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004557
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004558 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4559 output1, output1_size,
4560 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004561 TEST_LE_U( output1_length,
4562 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4563 TEST_LE_U( output1_length,
4564 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004565
4566 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004567 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004568
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004569 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4570 output2, output2_size,
4571 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004572 TEST_LE_U( output2_length,
4573 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4574 TEST_LE_U( output2_length,
4575 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004576
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004577 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004578
4579exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004580 mbedtls_free( output1 );
4581 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004582 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004583 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004584}
4585/* END_CASE */
4586
4587/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004588void cipher_verify_output_multipart( int alg_arg,
4589 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004590 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004591 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004592 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004593{
Ronald Cron5425a212020-08-04 14:58:35 +02004594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004595 psa_key_type_t key_type = key_type_arg;
4596 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004597 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004598 unsigned char iv[16] = {0};
4599 size_t iv_size = 16;
4600 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004601 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004602 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004603 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004604 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004606 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004607 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004608 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4609 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004611
Gilles Peskine8817f612018-12-18 00:18:46 +01004612 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004613
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004614 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4615 psa_set_key_algorithm( &attributes, alg );
4616 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004617
Ronald Cron5425a212020-08-04 14:58:35 +02004618 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4619 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004620
Ronald Cron5425a212020-08-04 14:58:35 +02004621 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4622 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004623
Steven Cooreman177deba2020-09-07 17:14:14 +02004624 if( alg != PSA_ALG_ECB_NO_PADDING )
4625 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004626 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4627 iv, iv_size,
4628 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004629 }
4630
gabor-mezei-armceface22021-01-21 12:26:17 +01004631 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004632 TEST_LE_U( output1_buffer_size,
4633 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004634 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004635
Gilles Peskine7be11a72022-04-14 00:12:57 +02004636 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004637
Gilles Peskine8817f612018-12-18 00:18:46 +01004638 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4639 output1, output1_buffer_size,
4640 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004641 TEST_LE_U( function_output_length,
4642 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4643 TEST_LE_U( function_output_length,
4644 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004645 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004646
Gilles Peskine8817f612018-12-18 00:18:46 +01004647 PSA_ASSERT( psa_cipher_update( &operation1,
4648 input->x + first_part_size,
4649 input->len - first_part_size,
4650 output1, output1_buffer_size,
4651 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004652 TEST_LE_U( function_output_length,
4653 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4654 alg,
4655 input->len - first_part_size ) );
4656 TEST_LE_U( function_output_length,
4657 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004658 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004659
Gilles Peskine8817f612018-12-18 00:18:46 +01004660 PSA_ASSERT( psa_cipher_finish( &operation1,
4661 output1 + output1_length,
4662 output1_buffer_size - output1_length,
4663 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004664 TEST_LE_U( function_output_length,
4665 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4666 TEST_LE_U( function_output_length,
4667 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004668 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004669
Gilles Peskine8817f612018-12-18 00:18:46 +01004670 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004671
Gilles Peskine048b7f02018-06-08 14:20:49 +02004672 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004673 TEST_LE_U( output2_buffer_size,
4674 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4675 TEST_LE_U( output2_buffer_size,
4676 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004677 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004678
Steven Cooreman177deba2020-09-07 17:14:14 +02004679 if( iv_length > 0 )
4680 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004681 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4682 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004683 }
Moran Pekerded84402018-06-06 16:36:50 +03004684
Gilles Peskine8817f612018-12-18 00:18:46 +01004685 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4686 output2, output2_buffer_size,
4687 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004688 TEST_LE_U( function_output_length,
4689 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4690 TEST_LE_U( function_output_length,
4691 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004692 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004693
Gilles Peskine8817f612018-12-18 00:18:46 +01004694 PSA_ASSERT( psa_cipher_update( &operation2,
4695 output1 + first_part_size,
4696 output1_length - first_part_size,
4697 output2, output2_buffer_size,
4698 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004699 TEST_LE_U( function_output_length,
4700 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4701 alg,
4702 output1_length - first_part_size ) );
4703 TEST_LE_U( function_output_length,
4704 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004705 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004706
Gilles Peskine8817f612018-12-18 00:18:46 +01004707 PSA_ASSERT( psa_cipher_finish( &operation2,
4708 output2 + output2_length,
4709 output2_buffer_size - output2_length,
4710 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004711 TEST_LE_U( function_output_length,
4712 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4713 TEST_LE_U( function_output_length,
4714 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004715 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004716
Gilles Peskine8817f612018-12-18 00:18:46 +01004717 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004718
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004719 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004720
4721exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004722 psa_cipher_abort( &operation1 );
4723 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004724 mbedtls_free( output1 );
4725 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004726 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004727 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004728}
4729/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004730
Gilles Peskine20035e32018-02-03 22:44:14 +01004731/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004732void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004733 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004734 data_t *nonce,
4735 data_t *additional_data,
4736 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004737 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004738{
Ronald Cron5425a212020-08-04 14:58:35 +02004739 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004740 psa_key_type_t key_type = key_type_arg;
4741 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004742 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004743 unsigned char *output_data = NULL;
4744 size_t output_size = 0;
4745 size_t output_length = 0;
4746 unsigned char *output_data2 = NULL;
4747 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004748 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004749 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004751
Gilles Peskine8817f612018-12-18 00:18:46 +01004752 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004753
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004754 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4755 psa_set_key_algorithm( &attributes, alg );
4756 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004757
Gilles Peskine049c7532019-05-15 20:22:09 +02004758 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004759 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004760 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4761 key_bits = psa_get_key_bits( &attributes );
4762
4763 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4764 alg );
4765 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4766 * should be exact. */
4767 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4768 expected_result != PSA_ERROR_NOT_SUPPORTED )
4769 {
4770 TEST_EQUAL( output_size,
4771 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004772 TEST_LE_U( output_size,
4773 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004774 }
4775 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004776
Steven Cooremanf49478b2021-02-15 15:19:25 +01004777 status = psa_aead_encrypt( key, alg,
4778 nonce->x, nonce->len,
4779 additional_data->x,
4780 additional_data->len,
4781 input_data->x, input_data->len,
4782 output_data, output_size,
4783 &output_length );
4784
4785 /* If the operation is not supported, just skip and not fail in case the
4786 * encryption involves a common limitation of cryptography hardwares and
4787 * an alternative implementation. */
4788 if( status == PSA_ERROR_NOT_SUPPORTED )
4789 {
4790 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4791 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4792 }
4793
4794 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795
4796 if( PSA_SUCCESS == expected_result )
4797 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004798 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004799
Gilles Peskine003a4a92019-05-14 16:09:40 +02004800 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4801 * should be exact. */
4802 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004803 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004804
Gilles Peskine7be11a72022-04-14 00:12:57 +02004805 TEST_LE_U( input_data->len,
4806 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004807
Ronald Cron5425a212020-08-04 14:58:35 +02004808 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004809 nonce->x, nonce->len,
4810 additional_data->x,
4811 additional_data->len,
4812 output_data, output_length,
4813 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004814 &output_length2 ),
4815 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004816
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004817 ASSERT_COMPARE( input_data->x, input_data->len,
4818 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004819 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004820
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004822 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004823 mbedtls_free( output_data );
4824 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004825 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004826}
4827/* END_CASE */
4828
4829/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004830void aead_encrypt( int key_type_arg, data_t *key_data,
4831 int alg_arg,
4832 data_t *nonce,
4833 data_t *additional_data,
4834 data_t *input_data,
4835 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004836{
Ronald Cron5425a212020-08-04 14:58:35 +02004837 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004838 psa_key_type_t key_type = key_type_arg;
4839 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004840 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004841 unsigned char *output_data = NULL;
4842 size_t output_size = 0;
4843 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004845 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004846
Gilles Peskine8817f612018-12-18 00:18:46 +01004847 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004849 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4850 psa_set_key_algorithm( &attributes, alg );
4851 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852
Gilles Peskine049c7532019-05-15 20:22:09 +02004853 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004854 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004855 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4856 key_bits = psa_get_key_bits( &attributes );
4857
4858 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4859 alg );
4860 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4861 * should be exact. */
4862 TEST_EQUAL( output_size,
4863 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004864 TEST_LE_U( output_size,
4865 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004866 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867
Steven Cooremand588ea12021-01-11 19:36:04 +01004868 status = psa_aead_encrypt( key, alg,
4869 nonce->x, nonce->len,
4870 additional_data->x, additional_data->len,
4871 input_data->x, input_data->len,
4872 output_data, output_size,
4873 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004874
Ronald Cron28a45ed2021-02-09 20:35:42 +01004875 /* If the operation is not supported, just skip and not fail in case the
4876 * encryption involves a common limitation of cryptography hardwares and
4877 * an alternative implementation. */
4878 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004879 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004880 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4881 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004882 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004883
4884 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004885 ASSERT_COMPARE( expected_result->x, expected_result->len,
4886 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004887
Gilles Peskinea1cac842018-06-11 19:33:02 +02004888exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004889 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004890 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004891 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004892}
4893/* END_CASE */
4894
4895/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004896void aead_decrypt( int key_type_arg, data_t *key_data,
4897 int alg_arg,
4898 data_t *nonce,
4899 data_t *additional_data,
4900 data_t *input_data,
4901 data_t *expected_data,
4902 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004903{
Ronald Cron5425a212020-08-04 14:58:35 +02004904 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004905 psa_key_type_t key_type = key_type_arg;
4906 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004907 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004908 unsigned char *output_data = NULL;
4909 size_t output_size = 0;
4910 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004912 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004913 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004914
Gilles Peskine8817f612018-12-18 00:18:46 +01004915 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004916
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004917 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4918 psa_set_key_algorithm( &attributes, alg );
4919 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004920
Gilles Peskine049c7532019-05-15 20:22:09 +02004921 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004922 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004923 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4924 key_bits = psa_get_key_bits( &attributes );
4925
4926 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4927 alg );
4928 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4929 expected_result != PSA_ERROR_NOT_SUPPORTED )
4930 {
4931 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4932 * should be exact. */
4933 TEST_EQUAL( output_size,
4934 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004935 TEST_LE_U( output_size,
4936 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004937 }
4938 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004939
Steven Cooremand588ea12021-01-11 19:36:04 +01004940 status = psa_aead_decrypt( key, alg,
4941 nonce->x, nonce->len,
4942 additional_data->x,
4943 additional_data->len,
4944 input_data->x, input_data->len,
4945 output_data, output_size,
4946 &output_length );
4947
Ronald Cron28a45ed2021-02-09 20:35:42 +01004948 /* If the operation is not supported, just skip and not fail in case the
4949 * decryption involves a common limitation of cryptography hardwares and
4950 * an alternative implementation. */
4951 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004952 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004953 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4954 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004955 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004956
4957 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004958
Gilles Peskine2d277862018-06-18 15:41:12 +02004959 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004960 ASSERT_COMPARE( expected_data->x, expected_data->len,
4961 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004962
Gilles Peskinea1cac842018-06-11 19:33:02 +02004963exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004964 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004965 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004966 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004967}
4968/* END_CASE */
4969
4970/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4972 int alg_arg,
4973 data_t *nonce,
4974 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004975 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004976 int do_set_lengths,
4977 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004978{
Paul Elliottd3f82412021-06-16 16:52:21 +01004979 size_t ad_part_len = 0;
4980 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004981 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004982
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004984 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004985 mbedtls_test_set_step( ad_part_len );
4986
4987 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004988 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004989 if( ad_part_len & 0x01 )
4990 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4991 else
4992 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004993 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004994
4995 /* Split ad into length(ad_part_len) parts. */
4996 if( !aead_multipart_internal_func( key_type_arg, key_data,
4997 alg_arg, nonce,
4998 additional_data,
4999 ad_part_len,
5000 input_data, -1,
5001 set_lengths_method,
5002 expected_output,
5003 1, 0 ) )
5004 break;
5005
5006 /* length(0) part, length(ad_part_len) part, length(0) part... */
5007 mbedtls_test_set_step( 1000 + ad_part_len );
5008
5009 if( !aead_multipart_internal_func( key_type_arg, key_data,
5010 alg_arg, nonce,
5011 additional_data,
5012 ad_part_len,
5013 input_data, -1,
5014 set_lengths_method,
5015 expected_output,
5016 1, 1 ) )
5017 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005018 }
Paul Elliottd3f82412021-06-16 16:52:21 +01005019
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005021 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005022 /* Split data into length(data_part_len) parts. */
5023 mbedtls_test_set_step( 2000 + data_part_len );
5024
5025 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005026 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005027 if( data_part_len & 0x01 )
5028 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5029 else
5030 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005031 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005032
Paul Elliott32f46ba2021-09-23 18:24:36 +01005033 if( !aead_multipart_internal_func( key_type_arg, key_data,
5034 alg_arg, nonce,
5035 additional_data, -1,
5036 input_data, data_part_len,
5037 set_lengths_method,
5038 expected_output,
5039 1, 0 ) )
5040 break;
5041
5042 /* length(0) part, length(data_part_len) part, length(0) part... */
5043 mbedtls_test_set_step( 3000 + data_part_len );
5044
5045 if( !aead_multipart_internal_func( key_type_arg, key_data,
5046 alg_arg, nonce,
5047 additional_data, -1,
5048 input_data, data_part_len,
5049 set_lengths_method,
5050 expected_output,
5051 1, 1 ) )
5052 break;
5053 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005054
Paul Elliott8fc45162021-06-23 16:06:01 +01005055 /* Goto is required to silence warnings about unused labels, as we
5056 * don't actually do any test assertions in this function. */
5057 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005058}
5059/* END_CASE */
5060
5061/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005062void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5063 int alg_arg,
5064 data_t *nonce,
5065 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005066 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005067 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005068 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005069{
Paul Elliottd3f82412021-06-16 16:52:21 +01005070 size_t ad_part_len = 0;
5071 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005072 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005073
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005075 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005076 /* Split ad into length(ad_part_len) parts. */
5077 mbedtls_test_set_step( ad_part_len );
5078
5079 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005080 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005081 if( ad_part_len & 0x01 )
5082 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5083 else
5084 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005085 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005086
5087 if( !aead_multipart_internal_func( key_type_arg, key_data,
5088 alg_arg, nonce,
5089 additional_data,
5090 ad_part_len,
5091 input_data, -1,
5092 set_lengths_method,
5093 expected_output,
5094 0, 0 ) )
5095 break;
5096
5097 /* length(0) part, length(ad_part_len) part, length(0) part... */
5098 mbedtls_test_set_step( 1000 + ad_part_len );
5099
5100 if( !aead_multipart_internal_func( key_type_arg, key_data,
5101 alg_arg, nonce,
5102 additional_data,
5103 ad_part_len,
5104 input_data, -1,
5105 set_lengths_method,
5106 expected_output,
5107 0, 1 ) )
5108 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005109 }
5110
Paul Elliott32f46ba2021-09-23 18:24:36 +01005111 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005112 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005113 /* Split data into length(data_part_len) parts. */
5114 mbedtls_test_set_step( 2000 + data_part_len );
5115
5116 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005117 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005118 if( data_part_len & 0x01 )
5119 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5120 else
5121 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005122 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005123
5124 if( !aead_multipart_internal_func( key_type_arg, key_data,
5125 alg_arg, nonce,
5126 additional_data, -1,
5127 input_data, data_part_len,
5128 set_lengths_method,
5129 expected_output,
5130 0, 0 ) )
5131 break;
5132
5133 /* length(0) part, length(data_part_len) part, length(0) part... */
5134 mbedtls_test_set_step( 3000 + data_part_len );
5135
5136 if( !aead_multipart_internal_func( key_type_arg, key_data,
5137 alg_arg, nonce,
5138 additional_data, -1,
5139 input_data, data_part_len,
5140 set_lengths_method,
5141 expected_output,
5142 0, 1 ) )
5143 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005144 }
5145
Paul Elliott8fc45162021-06-23 16:06:01 +01005146 /* Goto is required to silence warnings about unused labels, as we
5147 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005148 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005149}
5150/* END_CASE */
5151
5152/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005153void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5154 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005155 int nonce_length,
5156 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005157 data_t *additional_data,
5158 data_t *input_data,
5159 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005160{
5161
5162 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5163 psa_key_type_t key_type = key_type_arg;
5164 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005165 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005166 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5168 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005169 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005170 size_t actual_nonce_length = 0;
5171 size_t expected_nonce_length = expected_nonce_length_arg;
5172 unsigned char *output = NULL;
5173 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005174 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005175 size_t ciphertext_size = 0;
5176 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005177 size_t tag_length = 0;
5178 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005179
5180 PSA_ASSERT( psa_crypto_init( ) );
5181
5182 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5183 psa_set_key_algorithm( & attributes, alg );
5184 psa_set_key_type( & attributes, key_type );
5185
5186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5187 &key ) );
5188
5189 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5190
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005191 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5192
Paul Elliottf1277632021-08-24 18:11:37 +01005193 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005194
Paul Elliottf1277632021-08-24 18:11:37 +01005195 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005196
Gilles Peskine7be11a72022-04-14 00:12:57 +02005197 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005198
Paul Elliottf1277632021-08-24 18:11:37 +01005199 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005200
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005201 status = psa_aead_encrypt_setup( &operation, key, alg );
5202
5203 /* If the operation is not supported, just skip and not fail in case the
5204 * encryption involves a common limitation of cryptography hardwares and
5205 * an alternative implementation. */
5206 if( status == PSA_ERROR_NOT_SUPPORTED )
5207 {
5208 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005209 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005210 }
5211
5212 PSA_ASSERT( status );
5213
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005214 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005215 nonce_length,
5216 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005217
Paul Elliott693bf312021-07-23 17:40:41 +01005218 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005219
Paul Elliottf1277632021-08-24 18:11:37 +01005220 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005221
Paul Elliott88ecbe12021-09-22 17:23:03 +01005222 if( expected_status == PSA_SUCCESS )
5223 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5224 alg ) );
5225
Gilles Peskine7be11a72022-04-14 00:12:57 +02005226 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005227
Paul Elliott693bf312021-07-23 17:40:41 +01005228 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005229 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005230 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005231 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5232 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005233
5234 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5235 additional_data->len ) );
5236
5237 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005238 output, output_size,
5239 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005240
Paul Elliottf1277632021-08-24 18:11:37 +01005241 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5242 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005243 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5244 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005245
5246exit:
5247 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005248 mbedtls_free( output );
5249 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005250 psa_aead_abort( &operation );
5251 PSA_DONE( );
5252}
5253/* END_CASE */
5254
5255/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005256void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5257 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005258 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005259 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005260 data_t *additional_data,
5261 data_t *input_data,
5262 int expected_status_arg )
5263{
5264
5265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5266 psa_key_type_t key_type = key_type_arg;
5267 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005268 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005269 uint8_t *nonce_buffer = NULL;
5270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5271 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5272 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005273 unsigned char *output = NULL;
5274 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005275 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005276 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005277 size_t ciphertext_size = 0;
5278 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005279 size_t tag_length = 0;
5280 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005281 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005282 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005283
5284 PSA_ASSERT( psa_crypto_init( ) );
5285
5286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5287 psa_set_key_algorithm( &attributes, alg );
5288 psa_set_key_type( &attributes, key_type );
5289
5290 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5291 &key ) );
5292
5293 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5294
5295 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5296
Paul Elliott6f0e7202021-08-25 12:57:18 +01005297 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005298
Paul Elliott6f0e7202021-08-25 12:57:18 +01005299 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005300
Gilles Peskine7be11a72022-04-14 00:12:57 +02005301 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005302
Paul Elliott6f0e7202021-08-25 12:57:18 +01005303 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005304
Paul Elliott863864a2021-07-23 17:28:31 +01005305 status = psa_aead_encrypt_setup( &operation, key, alg );
5306
5307 /* If the operation is not supported, just skip and not fail in case the
5308 * encryption involves a common limitation of cryptography hardwares and
5309 * an alternative implementation. */
5310 if( status == PSA_ERROR_NOT_SUPPORTED )
5311 {
5312 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005313 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005314 }
5315
5316 PSA_ASSERT( status );
5317
Paul Elliott4023ffd2021-09-10 16:21:22 +01005318 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5319 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005320 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005321 /* Arbitrary size buffer, to test zero length valid buffer. */
5322 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005323 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005324 }
5325 else
5326 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005327 /* If length is zero, then this will return NULL. */
5328 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005329 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005330
Paul Elliott4023ffd2021-09-10 16:21:22 +01005331 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005332 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005333 for( index = 0; index < nonce_length - 1; ++index )
5334 {
5335 nonce_buffer[index] = 'a' + index;
5336 }
Paul Elliott66696b52021-08-16 18:42:41 +01005337 }
Paul Elliott863864a2021-07-23 17:28:31 +01005338 }
5339
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005340 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5341 {
5342 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5343 input_data->len ) );
5344 }
5345
Paul Elliott6f0e7202021-08-25 12:57:18 +01005346 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005347
Paul Elliott693bf312021-07-23 17:40:41 +01005348 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005349
5350 if( expected_status == PSA_SUCCESS )
5351 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005352 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5353 {
5354 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5355 input_data->len ) );
5356 }
5357 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5358 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005359
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005360 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5361 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5362 additional_data->len ),
5363 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005364
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005365 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005366 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005367 &ciphertext_length ),
5368 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005369
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005370 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005371 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005372 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5373 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005374 }
5375
5376exit:
5377 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005378 mbedtls_free( output );
5379 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005380 mbedtls_free( nonce_buffer );
5381 psa_aead_abort( &operation );
5382 PSA_DONE( );
5383}
5384/* END_CASE */
5385
5386/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005387void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5388 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005389 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005390 data_t *nonce,
5391 data_t *additional_data,
5392 data_t *input_data,
5393 int expected_status_arg )
5394{
5395
5396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5397 psa_key_type_t key_type = key_type_arg;
5398 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005399 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5401 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5402 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005403 unsigned char *output = NULL;
5404 unsigned char *ciphertext = NULL;
5405 size_t output_size = output_size_arg;
5406 size_t ciphertext_size = 0;
5407 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005408 size_t tag_length = 0;
5409 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5410
5411 PSA_ASSERT( psa_crypto_init( ) );
5412
5413 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5414 psa_set_key_algorithm( &attributes, alg );
5415 psa_set_key_type( &attributes, key_type );
5416
5417 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5418 &key ) );
5419
5420 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5421
Paul Elliottc6d11d02021-09-01 12:04:23 +01005422 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005423
Paul Elliottc6d11d02021-09-01 12:04:23 +01005424 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005425
Paul Elliottc6d11d02021-09-01 12:04:23 +01005426 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005427
Paul Elliott43fbda62021-07-23 18:30:59 +01005428 status = psa_aead_encrypt_setup( &operation, key, alg );
5429
5430 /* If the operation is not supported, just skip and not fail in case the
5431 * encryption involves a common limitation of cryptography hardwares and
5432 * an alternative implementation. */
5433 if( status == PSA_ERROR_NOT_SUPPORTED )
5434 {
5435 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5436 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5437 }
5438
5439 PSA_ASSERT( status );
5440
Paul Elliott47b9a142021-10-07 15:04:57 +01005441 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5442 input_data->len ) );
5443
Paul Elliott43fbda62021-07-23 18:30:59 +01005444 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5445
5446 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5447 additional_data->len ) );
5448
5449 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005450 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005451
5452 TEST_EQUAL( status, expected_status );
5453
5454 if( expected_status == PSA_SUCCESS )
5455 {
5456 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005457 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5458 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005459 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5460 }
5461
5462exit:
5463 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005464 mbedtls_free( output );
5465 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005466 psa_aead_abort( &operation );
5467 PSA_DONE( );
5468}
5469/* END_CASE */
5470
Paul Elliott91b021e2021-07-23 18:52:31 +01005471/* BEGIN_CASE */
5472void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5473 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005474 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005475 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005476 data_t *nonce,
5477 data_t *additional_data,
5478 data_t *input_data,
5479 int expected_status_arg )
5480{
5481
5482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5483 psa_key_type_t key_type = key_type_arg;
5484 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005485 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5487 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5488 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005489 unsigned char *ciphertext = NULL;
5490 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005491 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005492 size_t ciphertext_size = 0;
5493 size_t ciphertext_length = 0;
5494 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005495 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005496 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005497
5498 PSA_ASSERT( psa_crypto_init( ) );
5499
5500 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5501 psa_set_key_algorithm( &attributes, alg );
5502 psa_set_key_type( &attributes, key_type );
5503
5504 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5505 &key ) );
5506
5507 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5508
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005509 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005510
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005511 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005512
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005513 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005514
Paul Elliott719c1322021-09-13 18:27:22 +01005515 ASSERT_ALLOC( tag_buffer, tag_size );
5516
Paul Elliott91b021e2021-07-23 18:52:31 +01005517 status = psa_aead_encrypt_setup( &operation, key, alg );
5518
5519 /* If the operation is not supported, just skip and not fail in case the
5520 * encryption involves a common limitation of cryptography hardwares and
5521 * an alternative implementation. */
5522 if( status == PSA_ERROR_NOT_SUPPORTED )
5523 {
5524 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5525 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5526 }
5527
5528 PSA_ASSERT( status );
5529
5530 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5531
Paul Elliott76bda482021-10-07 17:07:23 +01005532 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5533 input_data->len ) );
5534
Paul Elliott91b021e2021-07-23 18:52:31 +01005535 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5536 additional_data->len ) );
5537
5538 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005539 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005540
5541 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005542 status = psa_aead_finish( &operation, finish_ciphertext,
5543 finish_ciphertext_size,
5544 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005545 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005546
5547 TEST_EQUAL( status, expected_status );
5548
5549exit:
5550 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005551 mbedtls_free( ciphertext );
5552 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005553 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005554 psa_aead_abort( &operation );
5555 PSA_DONE( );
5556}
5557/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005558
5559/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005560void aead_multipart_verify( int key_type_arg, data_t *key_data,
5561 int alg_arg,
5562 data_t *nonce,
5563 data_t *additional_data,
5564 data_t *input_data,
5565 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005566 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005567 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005568 int expected_status_arg )
5569{
5570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5571 psa_key_type_t key_type = key_type_arg;
5572 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005573 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5575 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5576 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005577 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005578 unsigned char *plaintext = NULL;
5579 unsigned char *finish_plaintext = NULL;
5580 size_t plaintext_size = 0;
5581 size_t plaintext_length = 0;
5582 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005583 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005584 unsigned char *tag_buffer = NULL;
5585 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005586
5587 PSA_ASSERT( psa_crypto_init( ) );
5588
5589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5590 psa_set_key_algorithm( &attributes, alg );
5591 psa_set_key_type( &attributes, key_type );
5592
5593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5594 &key ) );
5595
5596 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5597
5598 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5599 input_data->len );
5600
5601 ASSERT_ALLOC( plaintext, plaintext_size );
5602
5603 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5604
5605 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5606
Paul Elliott9961a662021-09-17 19:19:02 +01005607 status = psa_aead_decrypt_setup( &operation, key, alg );
5608
5609 /* If the operation is not supported, just skip and not fail in case the
5610 * encryption involves a common limitation of cryptography hardwares and
5611 * an alternative implementation. */
5612 if( status == PSA_ERROR_NOT_SUPPORTED )
5613 {
5614 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5615 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5616 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005617 TEST_EQUAL( status, expected_setup_status );
5618
5619 if( status != PSA_SUCCESS )
5620 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005621
5622 PSA_ASSERT( status );
5623
5624 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5625
Paul Elliottfec6f372021-10-06 17:15:02 +01005626 status = psa_aead_set_lengths( &operation, additional_data->len,
5627 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005628 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005629
Paul Elliott9961a662021-09-17 19:19:02 +01005630 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5631 additional_data->len ) );
5632
5633 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5634 input_data->len,
5635 plaintext, plaintext_size,
5636 &plaintext_length ) );
5637
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005638 if( tag_usage == USE_GIVEN_TAG )
5639 {
5640 tag_buffer = tag->x;
5641 tag_size = tag->len;
5642 }
5643
Paul Elliott9961a662021-09-17 19:19:02 +01005644 status = psa_aead_verify( &operation, finish_plaintext,
5645 verify_plaintext_size,
5646 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005647 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005648
5649 TEST_EQUAL( status, expected_status );
5650
5651exit:
5652 psa_destroy_key( key );
5653 mbedtls_free( plaintext );
5654 mbedtls_free( finish_plaintext );
5655 psa_aead_abort( &operation );
5656 PSA_DONE( );
5657}
5658/* END_CASE */
5659
Paul Elliott9961a662021-09-17 19:19:02 +01005660/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005661void aead_multipart_setup( int key_type_arg, data_t *key_data,
5662 int alg_arg, int expected_status_arg )
5663{
5664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5665 psa_key_type_t key_type = key_type_arg;
5666 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005667 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5669 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5670 psa_status_t expected_status = expected_status_arg;
5671
5672 PSA_ASSERT( psa_crypto_init( ) );
5673
5674 psa_set_key_usage_flags( &attributes,
5675 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5676 psa_set_key_algorithm( &attributes, alg );
5677 psa_set_key_type( &attributes, key_type );
5678
5679 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5680 &key ) );
5681
Paul Elliott5221ef62021-09-19 17:33:03 +01005682 status = psa_aead_encrypt_setup( &operation, key, alg );
5683
5684 TEST_EQUAL( status, expected_status );
5685
5686 psa_aead_abort( &operation );
5687
Paul Elliott5221ef62021-09-19 17:33:03 +01005688 status = psa_aead_decrypt_setup( &operation, key, alg );
5689
5690 TEST_EQUAL(status, expected_status );
5691
5692exit:
5693 psa_destroy_key( key );
5694 psa_aead_abort( &operation );
5695 PSA_DONE( );
5696}
5697/* END_CASE */
5698
5699/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005700void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5701 int alg_arg,
5702 data_t *nonce,
5703 data_t *additional_data,
5704 data_t *input_data )
5705{
5706 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5707 psa_key_type_t key_type = key_type_arg;
5708 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005709 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005710 unsigned char *output_data = NULL;
5711 unsigned char *final_data = NULL;
5712 size_t output_size = 0;
5713 size_t finish_output_size = 0;
5714 size_t output_length = 0;
5715 size_t key_bits = 0;
5716 size_t tag_length = 0;
5717 size_t tag_size = 0;
5718 size_t nonce_length = 0;
5719 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5720 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5721 size_t output_part_length = 0;
5722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5723
5724 PSA_ASSERT( psa_crypto_init( ) );
5725
5726 psa_set_key_usage_flags( & attributes,
5727 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5728 psa_set_key_algorithm( & attributes, alg );
5729 psa_set_key_type( & attributes, key_type );
5730
5731 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5732 &key ) );
5733
5734 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5735 key_bits = psa_get_key_bits( &attributes );
5736
5737 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5738
Gilles Peskine7be11a72022-04-14 00:12:57 +02005739 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005740
5741 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5742
5743 ASSERT_ALLOC( output_data, output_size );
5744
5745 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5746
Gilles Peskine7be11a72022-04-14 00:12:57 +02005747 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005748
5749 ASSERT_ALLOC( final_data, finish_output_size );
5750
5751 /* Test all operations error without calling setup first. */
5752
Paul Elliottc23a9a02021-06-21 18:32:46 +01005753 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5754 PSA_ERROR_BAD_STATE );
5755
5756 psa_aead_abort( &operation );
5757
Paul Elliottc23a9a02021-06-21 18:32:46 +01005758 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5759 PSA_AEAD_NONCE_MAX_SIZE,
5760 &nonce_length ),
5761 PSA_ERROR_BAD_STATE );
5762
5763 psa_aead_abort( &operation );
5764
Paul Elliott481be342021-07-16 17:38:47 +01005765 /* ------------------------------------------------------- */
5766
Paul Elliottc23a9a02021-06-21 18:32:46 +01005767 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5768 input_data->len ),
5769 PSA_ERROR_BAD_STATE );
5770
5771 psa_aead_abort( &operation );
5772
Paul Elliott481be342021-07-16 17:38:47 +01005773 /* ------------------------------------------------------- */
5774
Paul Elliottc23a9a02021-06-21 18:32:46 +01005775 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5776 additional_data->len ),
5777 PSA_ERROR_BAD_STATE );
5778
5779 psa_aead_abort( &operation );
5780
Paul Elliott481be342021-07-16 17:38:47 +01005781 /* ------------------------------------------------------- */
5782
Paul Elliottc23a9a02021-06-21 18:32:46 +01005783 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5784 input_data->len, output_data,
5785 output_size, &output_length ),
5786 PSA_ERROR_BAD_STATE );
5787
5788 psa_aead_abort( &operation );
5789
Paul Elliott481be342021-07-16 17:38:47 +01005790 /* ------------------------------------------------------- */
5791
Paul Elliottc23a9a02021-06-21 18:32:46 +01005792 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5793 finish_output_size,
5794 &output_part_length,
5795 tag_buffer, tag_length,
5796 &tag_size ),
5797 PSA_ERROR_BAD_STATE );
5798
5799 psa_aead_abort( &operation );
5800
Paul Elliott481be342021-07-16 17:38:47 +01005801 /* ------------------------------------------------------- */
5802
Paul Elliottc23a9a02021-06-21 18:32:46 +01005803 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5804 finish_output_size,
5805 &output_part_length,
5806 tag_buffer,
5807 tag_length ),
5808 PSA_ERROR_BAD_STATE );
5809
5810 psa_aead_abort( &operation );
5811
5812 /* Test for double setups. */
5813
Paul Elliottc23a9a02021-06-21 18:32:46 +01005814 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5815
5816 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5817 PSA_ERROR_BAD_STATE );
5818
5819 psa_aead_abort( &operation );
5820
Paul Elliott481be342021-07-16 17:38:47 +01005821 /* ------------------------------------------------------- */
5822
Paul Elliottc23a9a02021-06-21 18:32:46 +01005823 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5824
5825 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5826 PSA_ERROR_BAD_STATE );
5827
5828 psa_aead_abort( &operation );
5829
Paul Elliott374a2be2021-07-16 17:53:40 +01005830 /* ------------------------------------------------------- */
5831
Paul Elliott374a2be2021-07-16 17:53:40 +01005832 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5833
5834 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5835 PSA_ERROR_BAD_STATE );
5836
5837 psa_aead_abort( &operation );
5838
5839 /* ------------------------------------------------------- */
5840
Paul Elliott374a2be2021-07-16 17:53:40 +01005841 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5842
5843 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5844 PSA_ERROR_BAD_STATE );
5845
5846 psa_aead_abort( &operation );
5847
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848 /* Test for not setting a nonce. */
5849
Paul Elliottc23a9a02021-06-21 18:32:46 +01005850 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5851
5852 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5853 additional_data->len ),
5854 PSA_ERROR_BAD_STATE );
5855
5856 psa_aead_abort( &operation );
5857
Paul Elliott7f628422021-09-01 12:08:29 +01005858 /* ------------------------------------------------------- */
5859
Paul Elliott7f628422021-09-01 12:08:29 +01005860 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5861
5862 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5863 input_data->len, output_data,
5864 output_size, &output_length ),
5865 PSA_ERROR_BAD_STATE );
5866
5867 psa_aead_abort( &operation );
5868
Paul Elliottbdc2c682021-09-21 18:37:10 +01005869 /* ------------------------------------------------------- */
5870
Paul Elliottbdc2c682021-09-21 18:37:10 +01005871 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5872
5873 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5874 finish_output_size,
5875 &output_part_length,
5876 tag_buffer, tag_length,
5877 &tag_size ),
5878 PSA_ERROR_BAD_STATE );
5879
5880 psa_aead_abort( &operation );
5881
5882 /* ------------------------------------------------------- */
5883
Paul Elliottbdc2c682021-09-21 18:37:10 +01005884 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5885
5886 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5887 finish_output_size,
5888 &output_part_length,
5889 tag_buffer,
5890 tag_length ),
5891 PSA_ERROR_BAD_STATE );
5892
5893 psa_aead_abort( &operation );
5894
Paul Elliottc23a9a02021-06-21 18:32:46 +01005895 /* Test for double setting nonce. */
5896
Paul Elliottc23a9a02021-06-21 18:32:46 +01005897 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5898
5899 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5900
5901 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5902 PSA_ERROR_BAD_STATE );
5903
5904 psa_aead_abort( &operation );
5905
Paul Elliott374a2be2021-07-16 17:53:40 +01005906 /* Test for double generating nonce. */
5907
Paul Elliott374a2be2021-07-16 17:53:40 +01005908 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5909
5910 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5911 PSA_AEAD_NONCE_MAX_SIZE,
5912 &nonce_length ) );
5913
5914 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5915 PSA_AEAD_NONCE_MAX_SIZE,
5916 &nonce_length ),
5917 PSA_ERROR_BAD_STATE );
5918
5919
5920 psa_aead_abort( &operation );
5921
5922 /* Test for generate nonce then set and vice versa */
5923
Paul Elliott374a2be2021-07-16 17:53:40 +01005924 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5925
5926 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5927 PSA_AEAD_NONCE_MAX_SIZE,
5928 &nonce_length ) );
5929
5930 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5931 PSA_ERROR_BAD_STATE );
5932
5933 psa_aead_abort( &operation );
5934
Andrzej Kurekad837522021-12-15 15:28:49 +01005935 /* Test for generating nonce after calling set lengths */
5936
5937 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5938
5939 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5940 input_data->len ) );
5941
5942 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5943 PSA_AEAD_NONCE_MAX_SIZE,
5944 &nonce_length ) );
5945
5946 psa_aead_abort( &operation );
5947
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005948 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005949
5950 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5951
5952 if( operation.alg == PSA_ALG_CCM )
5953 {
5954 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5955 input_data->len ),
5956 PSA_ERROR_INVALID_ARGUMENT );
5957 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5958 PSA_AEAD_NONCE_MAX_SIZE,
5959 &nonce_length ),
5960 PSA_ERROR_BAD_STATE );
5961 }
5962 else
5963 {
5964 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5965 input_data->len ) );
5966 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5967 PSA_AEAD_NONCE_MAX_SIZE,
5968 &nonce_length ) );
5969 }
5970
5971 psa_aead_abort( &operation );
5972
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005973 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005974#if SIZE_MAX > UINT32_MAX
5975 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5976
5977 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5978 {
5979 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5980 input_data->len ),
5981 PSA_ERROR_INVALID_ARGUMENT );
5982 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5983 PSA_AEAD_NONCE_MAX_SIZE,
5984 &nonce_length ),
5985 PSA_ERROR_BAD_STATE );
5986 }
5987 else
5988 {
5989 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5990 input_data->len ) );
5991 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5992 PSA_AEAD_NONCE_MAX_SIZE,
5993 &nonce_length ) );
5994 }
5995
5996 psa_aead_abort( &operation );
5997#endif
5998
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005999 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006000
6001 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6002
6003 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6004 PSA_AEAD_NONCE_MAX_SIZE,
6005 &nonce_length ) );
6006
6007 if( operation.alg == PSA_ALG_CCM )
6008 {
6009 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6010 input_data->len ),
6011 PSA_ERROR_INVALID_ARGUMENT );
6012 }
6013 else
6014 {
6015 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6016 input_data->len ) );
6017 }
6018
6019 psa_aead_abort( &operation );
6020
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006021 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006022 /* Test for setting nonce after calling set lengths */
6023
6024 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6025
6026 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6027 input_data->len ) );
6028
6029 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6030
6031 psa_aead_abort( &operation );
6032
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006033 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006034
6035 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6036
6037 if( operation.alg == PSA_ALG_CCM )
6038 {
6039 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6040 input_data->len ),
6041 PSA_ERROR_INVALID_ARGUMENT );
6042 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6043 PSA_ERROR_BAD_STATE );
6044 }
6045 else
6046 {
6047 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6048 input_data->len ) );
6049 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6050 }
6051
6052 psa_aead_abort( &operation );
6053
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006054 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006055#if SIZE_MAX > UINT32_MAX
6056 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6057
6058 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6059 {
6060 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6061 input_data->len ),
6062 PSA_ERROR_INVALID_ARGUMENT );
6063 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6064 PSA_ERROR_BAD_STATE );
6065 }
6066 else
6067 {
6068 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6069 input_data->len ) );
6070 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6071 }
6072
6073 psa_aead_abort( &operation );
6074#endif
6075
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006076 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006077
6078 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6079
6080 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6081
6082 if( operation.alg == PSA_ALG_CCM )
6083 {
6084 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6085 input_data->len ),
6086 PSA_ERROR_INVALID_ARGUMENT );
6087 }
6088 else
6089 {
6090 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6091 input_data->len ) );
6092 }
6093
6094 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006095
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006096 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006097#if SIZE_MAX > UINT32_MAX
6098 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6099
6100 if( operation.alg == PSA_ALG_GCM )
6101 {
6102 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6103 SIZE_MAX ),
6104 PSA_ERROR_INVALID_ARGUMENT );
6105 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6106 PSA_ERROR_BAD_STATE );
6107 }
6108 else if ( operation.alg != PSA_ALG_CCM )
6109 {
6110 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6111 SIZE_MAX ) );
6112 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6113 }
6114
6115 psa_aead_abort( &operation );
6116
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006117 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006118 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6119
6120 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
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 }
6128 else if ( operation.alg != PSA_ALG_CCM )
6129 {
6130 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6131 SIZE_MAX ) );
6132 }
6133
6134 psa_aead_abort( &operation );
6135#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006136
6137 /* ------------------------------------------------------- */
6138
Paul Elliott374a2be2021-07-16 17:53:40 +01006139 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6140
6141 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6142
6143 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6144 PSA_AEAD_NONCE_MAX_SIZE,
6145 &nonce_length ),
6146 PSA_ERROR_BAD_STATE );
6147
6148 psa_aead_abort( &operation );
6149
Paul Elliott7220cae2021-06-22 17:25:57 +01006150 /* Test for generating nonce in decrypt setup. */
6151
Paul Elliott7220cae2021-06-22 17:25:57 +01006152 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6153
6154 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6155 PSA_AEAD_NONCE_MAX_SIZE,
6156 &nonce_length ),
6157 PSA_ERROR_BAD_STATE );
6158
6159 psa_aead_abort( &operation );
6160
Paul Elliottc23a9a02021-06-21 18:32:46 +01006161 /* Test for setting lengths twice. */
6162
Paul Elliottc23a9a02021-06-21 18:32:46 +01006163 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6164
6165 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6166
6167 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6168 input_data->len ) );
6169
6170 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6171 input_data->len ),
6172 PSA_ERROR_BAD_STATE );
6173
6174 psa_aead_abort( &operation );
6175
Andrzej Kurekad837522021-12-15 15:28:49 +01006176 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006177
Paul Elliottc23a9a02021-06-21 18:32:46 +01006178 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6179
6180 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6181
Andrzej Kurekad837522021-12-15 15:28:49 +01006182 if( operation.alg == PSA_ALG_CCM )
6183 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006184
Andrzej Kurekad837522021-12-15 15:28:49 +01006185 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6186 additional_data->len ),
6187 PSA_ERROR_BAD_STATE );
6188 }
6189 else
6190 {
6191 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6192 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006193
Andrzej Kurekad837522021-12-15 15:28:49 +01006194 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6195 input_data->len ),
6196 PSA_ERROR_BAD_STATE );
6197 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006198 psa_aead_abort( &operation );
6199
6200 /* ------------------------------------------------------- */
6201
Paul Elliottf94bd992021-09-19 18:15:59 +01006202 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6203
6204 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6205
Andrzej Kurekad837522021-12-15 15:28:49 +01006206 if( operation.alg == PSA_ALG_CCM )
6207 {
6208 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6209 input_data->len, output_data,
6210 output_size, &output_length ),
6211 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006212
Andrzej Kurekad837522021-12-15 15:28:49 +01006213 }
6214 else
6215 {
6216 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6217 input_data->len, output_data,
6218 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006219
Andrzej Kurekad837522021-12-15 15:28:49 +01006220 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6221 input_data->len ),
6222 PSA_ERROR_BAD_STATE );
6223 }
6224 psa_aead_abort( &operation );
6225
6226 /* ------------------------------------------------------- */
6227
6228 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6229
6230 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6231
6232 if( operation.alg == PSA_ALG_CCM )
6233 {
6234 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6235 finish_output_size,
6236 &output_part_length,
6237 tag_buffer, tag_length,
6238 &tag_size ) );
6239 }
6240 else
6241 {
6242 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6243 finish_output_size,
6244 &output_part_length,
6245 tag_buffer, tag_length,
6246 &tag_size ) );
6247
6248 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6249 input_data->len ),
6250 PSA_ERROR_BAD_STATE );
6251 }
6252 psa_aead_abort( &operation );
6253
6254 /* Test for setting lengths after generating nonce + already starting data. */
6255
6256 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6257
6258 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6259 PSA_AEAD_NONCE_MAX_SIZE,
6260 &nonce_length ) );
6261 if( operation.alg == PSA_ALG_CCM )
6262 {
6263
6264 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6265 additional_data->len ),
6266 PSA_ERROR_BAD_STATE );
6267 }
6268 else
6269 {
6270 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6271 additional_data->len ) );
6272
6273 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6274 input_data->len ),
6275 PSA_ERROR_BAD_STATE );
6276 }
6277 psa_aead_abort( &operation );
6278
6279 /* ------------------------------------------------------- */
6280
6281 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6282
6283 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6284 PSA_AEAD_NONCE_MAX_SIZE,
6285 &nonce_length ) );
6286 if( operation.alg == PSA_ALG_CCM )
6287 {
6288 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6289 input_data->len, output_data,
6290 output_size, &output_length ),
6291 PSA_ERROR_BAD_STATE );
6292
6293 }
6294 else
6295 {
6296 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6297 input_data->len, output_data,
6298 output_size, &output_length ) );
6299
6300 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6301 input_data->len ),
6302 PSA_ERROR_BAD_STATE );
6303 }
6304 psa_aead_abort( &operation );
6305
6306 /* ------------------------------------------------------- */
6307
6308 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6309
6310 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6311 PSA_AEAD_NONCE_MAX_SIZE,
6312 &nonce_length ) );
6313 if( operation.alg == PSA_ALG_CCM )
6314 {
6315 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6316 finish_output_size,
6317 &output_part_length,
6318 tag_buffer, tag_length,
6319 &tag_size ) );
6320 }
6321 else
6322 {
6323 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6324 finish_output_size,
6325 &output_part_length,
6326 tag_buffer, tag_length,
6327 &tag_size ) );
6328
6329 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6330 input_data->len ),
6331 PSA_ERROR_BAD_STATE );
6332 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006333 psa_aead_abort( &operation );
6334
Paul Elliott243080c2021-07-21 19:01:17 +01006335 /* Test for not sending any additional data or data after setting non zero
6336 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006337
Paul Elliottc23a9a02021-06-21 18:32:46 +01006338 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6339
6340 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6341
6342 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6343 input_data->len ) );
6344
6345 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6346 finish_output_size,
6347 &output_part_length,
6348 tag_buffer, tag_length,
6349 &tag_size ),
6350 PSA_ERROR_INVALID_ARGUMENT );
6351
6352 psa_aead_abort( &operation );
6353
Paul Elliott243080c2021-07-21 19:01:17 +01006354 /* Test for not sending any additional data or data after setting non-zero
6355 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006356
Paul Elliottc23a9a02021-06-21 18:32:46 +01006357 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6358
6359 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6360
6361 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6362 input_data->len ) );
6363
6364 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6365 finish_output_size,
6366 &output_part_length,
6367 tag_buffer,
6368 tag_length ),
6369 PSA_ERROR_INVALID_ARGUMENT );
6370
6371 psa_aead_abort( &operation );
6372
Paul Elliott243080c2021-07-21 19:01:17 +01006373 /* Test for not sending any additional data after setting a non-zero length
6374 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006375
Paul Elliottc23a9a02021-06-21 18:32:46 +01006376 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6377
6378 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6379
6380 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6381 input_data->len ) );
6382
6383 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6384 input_data->len, output_data,
6385 output_size, &output_length ),
6386 PSA_ERROR_INVALID_ARGUMENT );
6387
6388 psa_aead_abort( &operation );
6389
Paul Elliottf94bd992021-09-19 18:15:59 +01006390 /* Test for not sending any data after setting a non-zero length for it.*/
6391
Paul Elliottf94bd992021-09-19 18:15:59 +01006392 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6393
6394 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6395
6396 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6397 input_data->len ) );
6398
6399 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6400 additional_data->len ) );
6401
6402 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6403 finish_output_size,
6404 &output_part_length,
6405 tag_buffer, tag_length,
6406 &tag_size ),
6407 PSA_ERROR_INVALID_ARGUMENT );
6408
6409 psa_aead_abort( &operation );
6410
Paul Elliottb0450fe2021-09-01 15:06:26 +01006411 /* Test for sending too much additional data after setting lengths. */
6412
Paul Elliottb0450fe2021-09-01 15:06:26 +01006413 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6414
6415 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6416
6417 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6418
6419
6420 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6421 additional_data->len ),
6422 PSA_ERROR_INVALID_ARGUMENT );
6423
6424 psa_aead_abort( &operation );
6425
Paul Elliotta2a09b02021-09-22 14:56:40 +01006426 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006427
6428 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6429
6430 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6431
6432 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6433 input_data->len ) );
6434
6435 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6436 additional_data->len ) );
6437
6438 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6439 1 ),
6440 PSA_ERROR_INVALID_ARGUMENT );
6441
6442 psa_aead_abort( &operation );
6443
Paul Elliottb0450fe2021-09-01 15:06:26 +01006444 /* Test for sending too much data after setting lengths. */
6445
Paul Elliottb0450fe2021-09-01 15:06:26 +01006446 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6447
6448 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6449
6450 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6451
6452 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6453 input_data->len, output_data,
6454 output_size, &output_length ),
6455 PSA_ERROR_INVALID_ARGUMENT );
6456
6457 psa_aead_abort( &operation );
6458
Paul Elliotta2a09b02021-09-22 14:56:40 +01006459 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006460
6461 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6462
6463 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6464
6465 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6466 input_data->len ) );
6467
6468 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6469 additional_data->len ) );
6470
6471 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6472 input_data->len, output_data,
6473 output_size, &output_length ) );
6474
6475 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6476 1, output_data,
6477 output_size, &output_length ),
6478 PSA_ERROR_INVALID_ARGUMENT );
6479
6480 psa_aead_abort( &operation );
6481
Paul Elliottc23a9a02021-06-21 18:32:46 +01006482 /* Test sending additional data after data. */
6483
Paul Elliottc23a9a02021-06-21 18:32:46 +01006484 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6485
6486 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6487
Andrzej Kurekad837522021-12-15 15:28:49 +01006488 if( operation.alg != PSA_ALG_CCM )
6489 {
6490 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6491 input_data->len, output_data,
6492 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006493
Andrzej Kurekad837522021-12-15 15:28:49 +01006494 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6495 additional_data->len ),
6496 PSA_ERROR_BAD_STATE );
6497 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006498 psa_aead_abort( &operation );
6499
Paul Elliott534d0b42021-06-22 19:15:20 +01006500 /* Test calling finish on decryption. */
6501
Paul Elliott534d0b42021-06-22 19:15:20 +01006502 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6503
6504 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6505
6506 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6507 finish_output_size,
6508 &output_part_length,
6509 tag_buffer, tag_length,
6510 &tag_size ),
6511 PSA_ERROR_BAD_STATE );
6512
6513 psa_aead_abort( &operation );
6514
6515 /* Test calling verify on encryption. */
6516
Paul Elliott534d0b42021-06-22 19:15:20 +01006517 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6518
6519 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6520
6521 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6522 finish_output_size,
6523 &output_part_length,
6524 tag_buffer,
6525 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006526 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006527
6528 psa_aead_abort( &operation );
6529
6530
Paul Elliottc23a9a02021-06-21 18:32:46 +01006531exit:
6532 psa_destroy_key( key );
6533 psa_aead_abort( &operation );
6534 mbedtls_free( output_data );
6535 mbedtls_free( final_data );
6536 PSA_DONE( );
6537}
6538/* END_CASE */
6539
6540/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006541void signature_size( int type_arg,
6542 int bits,
6543 int alg_arg,
6544 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006545{
6546 psa_key_type_t type = type_arg;
6547 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006548 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006549
Gilles Peskinefe11b722018-12-18 00:24:04 +01006550 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006551
Gilles Peskinee59236f2018-01-27 23:32:46 +01006552exit:
6553 ;
6554}
6555/* END_CASE */
6556
6557/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006558void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6559 int alg_arg, data_t *input_data,
6560 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006561{
Ronald Cron5425a212020-08-04 14:58:35 +02006562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006563 psa_key_type_t key_type = key_type_arg;
6564 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006565 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006566 unsigned char *signature = NULL;
6567 size_t signature_size;
6568 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006570
Gilles Peskine8817f612018-12-18 00:18:46 +01006571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006572
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006574 psa_set_key_algorithm( &attributes, alg );
6575 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006576
Gilles Peskine049c7532019-05-15 20:22:09 +02006577 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006578 &key ) );
6579 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006580 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006581
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006582 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006583 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006584 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006585 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006586 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006587 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006588 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006589
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006590 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006591 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006592 input_data->x, input_data->len,
6593 signature, signature_size,
6594 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006595 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006596 ASSERT_COMPARE( output_data->x, output_data->len,
6597 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006598
6599exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006600 /*
6601 * Key attributes may have been returned by psa_get_key_attributes()
6602 * thus reset them as required.
6603 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006604 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006605
Ronald Cron5425a212020-08-04 14:58:35 +02006606 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006607 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006608 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006609}
6610/* END_CASE */
6611
6612/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006613void sign_hash_fail( int key_type_arg, data_t *key_data,
6614 int alg_arg, data_t *input_data,
6615 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006616{
Ronald Cron5425a212020-08-04 14:58:35 +02006617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006618 psa_key_type_t key_type = key_type_arg;
6619 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006620 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006621 psa_status_t actual_status;
6622 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006623 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006624 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006626
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006627 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006628
Gilles Peskine8817f612018-12-18 00:18:46 +01006629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006630
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006632 psa_set_key_algorithm( &attributes, alg );
6633 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006634
Gilles Peskine049c7532019-05-15 20:22:09 +02006635 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006636 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006637
Ronald Cron5425a212020-08-04 14:58:35 +02006638 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006639 input_data->x, input_data->len,
6640 signature, signature_size,
6641 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006642 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006643 /* The value of *signature_length is unspecified on error, but
6644 * whatever it is, it should be less than signature_size, so that
6645 * if the caller tries to read *signature_length bytes without
6646 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006647 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006648
6649exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006650 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006651 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006652 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006653 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006654}
6655/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006656
6657/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006658void sign_verify_hash( int key_type_arg, data_t *key_data,
6659 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006660{
Ronald Cron5425a212020-08-04 14:58:35 +02006661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006662 psa_key_type_t key_type = key_type_arg;
6663 psa_algorithm_t alg = alg_arg;
6664 size_t key_bits;
6665 unsigned char *signature = NULL;
6666 size_t signature_size;
6667 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006668 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006669
Gilles Peskine8817f612018-12-18 00:18:46 +01006670 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006671
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006672 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006673 psa_set_key_algorithm( &attributes, alg );
6674 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006675
Gilles Peskine049c7532019-05-15 20:22:09 +02006676 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006677 &key ) );
6678 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006679 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006680
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006681 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006682 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006683 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006684 key_bits, alg );
6685 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006686 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006687 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006688
6689 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006690 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006691 input_data->x, input_data->len,
6692 signature, signature_size,
6693 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006694 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006695 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006696 TEST_ASSERT( signature_length > 0 );
6697
6698 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006699 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006700 input_data->x, input_data->len,
6701 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006702
6703 if( input_data->len != 0 )
6704 {
6705 /* Flip a bit in the input and verify that the signature is now
6706 * detected as invalid. Flip a bit at the beginning, not at the end,
6707 * because ECDSA may ignore the last few bits of the input. */
6708 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006709 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006710 input_data->x, input_data->len,
6711 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006712 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006713 }
6714
6715exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006716 /*
6717 * Key attributes may have been returned by psa_get_key_attributes()
6718 * thus reset them as required.
6719 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006720 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006721
Ronald Cron5425a212020-08-04 14:58:35 +02006722 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006723 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006724 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006725}
6726/* END_CASE */
6727
6728/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006729void verify_hash( int key_type_arg, data_t *key_data,
6730 int alg_arg, data_t *hash_data,
6731 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006732{
Ronald Cron5425a212020-08-04 14:58:35 +02006733 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006734 psa_key_type_t key_type = key_type_arg;
6735 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006737
Gilles Peskine7be11a72022-04-14 00:12:57 +02006738 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006739
Gilles Peskine8817f612018-12-18 00:18:46 +01006740 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006741
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006742 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006743 psa_set_key_algorithm( &attributes, alg );
6744 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006745
Gilles Peskine049c7532019-05-15 20:22:09 +02006746 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006747 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006748
Ronald Cron5425a212020-08-04 14:58:35 +02006749 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006750 hash_data->x, hash_data->len,
6751 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006752
itayzafrir5c753392018-05-08 11:18:38 +03006753exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006754 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006755 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006756 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006757}
6758/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006759
6760/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006761void verify_hash_fail( int key_type_arg, data_t *key_data,
6762 int alg_arg, data_t *hash_data,
6763 data_t *signature_data,
6764 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006765{
Ronald Cron5425a212020-08-04 14:58:35 +02006766 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006767 psa_key_type_t key_type = key_type_arg;
6768 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006769 psa_status_t actual_status;
6770 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006772
Gilles Peskine8817f612018-12-18 00:18:46 +01006773 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006774
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006775 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006776 psa_set_key_algorithm( &attributes, alg );
6777 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006778
Gilles Peskine049c7532019-05-15 20:22:09 +02006779 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006780 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006781
Ronald Cron5425a212020-08-04 14:58:35 +02006782 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006783 hash_data->x, hash_data->len,
6784 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006785 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006786
6787exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006788 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006789 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006790 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006791}
6792/* END_CASE */
6793
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006794/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006795void sign_message_deterministic( int key_type_arg,
6796 data_t *key_data,
6797 int alg_arg,
6798 data_t *input_data,
6799 data_t *output_data )
6800{
6801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6802 psa_key_type_t key_type = key_type_arg;
6803 psa_algorithm_t alg = alg_arg;
6804 size_t key_bits;
6805 unsigned char *signature = NULL;
6806 size_t signature_size;
6807 size_t signature_length = 0xdeadbeef;
6808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6809
6810 PSA_ASSERT( psa_crypto_init( ) );
6811
6812 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6813 psa_set_key_algorithm( &attributes, alg );
6814 psa_set_key_type( &attributes, key_type );
6815
6816 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6817 &key ) );
6818 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6819 key_bits = psa_get_key_bits( &attributes );
6820
6821 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6822 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006823 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006824 ASSERT_ALLOC( signature, signature_size );
6825
6826 PSA_ASSERT( psa_sign_message( key, alg,
6827 input_data->x, input_data->len,
6828 signature, signature_size,
6829 &signature_length ) );
6830
6831 ASSERT_COMPARE( output_data->x, output_data->len,
6832 signature, signature_length );
6833
6834exit:
6835 psa_reset_key_attributes( &attributes );
6836
6837 psa_destroy_key( key );
6838 mbedtls_free( signature );
6839 PSA_DONE( );
6840
6841}
6842/* END_CASE */
6843
6844/* BEGIN_CASE */
6845void sign_message_fail( int key_type_arg,
6846 data_t *key_data,
6847 int alg_arg,
6848 data_t *input_data,
6849 int signature_size_arg,
6850 int expected_status_arg )
6851{
6852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6853 psa_key_type_t key_type = key_type_arg;
6854 psa_algorithm_t alg = alg_arg;
6855 size_t signature_size = signature_size_arg;
6856 psa_status_t actual_status;
6857 psa_status_t expected_status = expected_status_arg;
6858 unsigned char *signature = NULL;
6859 size_t signature_length = 0xdeadbeef;
6860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6861
6862 ASSERT_ALLOC( signature, signature_size );
6863
6864 PSA_ASSERT( psa_crypto_init( ) );
6865
6866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6867 psa_set_key_algorithm( &attributes, alg );
6868 psa_set_key_type( &attributes, key_type );
6869
6870 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6871 &key ) );
6872
6873 actual_status = psa_sign_message( key, alg,
6874 input_data->x, input_data->len,
6875 signature, signature_size,
6876 &signature_length );
6877 TEST_EQUAL( actual_status, expected_status );
6878 /* The value of *signature_length is unspecified on error, but
6879 * whatever it is, it should be less than signature_size, so that
6880 * if the caller tries to read *signature_length bytes without
6881 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006882 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006883
6884exit:
6885 psa_reset_key_attributes( &attributes );
6886 psa_destroy_key( key );
6887 mbedtls_free( signature );
6888 PSA_DONE( );
6889}
6890/* END_CASE */
6891
6892/* BEGIN_CASE */
6893void sign_verify_message( int key_type_arg,
6894 data_t *key_data,
6895 int alg_arg,
6896 data_t *input_data )
6897{
6898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6899 psa_key_type_t key_type = key_type_arg;
6900 psa_algorithm_t alg = alg_arg;
6901 size_t key_bits;
6902 unsigned char *signature = NULL;
6903 size_t signature_size;
6904 size_t signature_length = 0xdeadbeef;
6905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6906
6907 PSA_ASSERT( psa_crypto_init( ) );
6908
6909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6910 PSA_KEY_USAGE_VERIFY_MESSAGE );
6911 psa_set_key_algorithm( &attributes, alg );
6912 psa_set_key_type( &attributes, key_type );
6913
6914 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6915 &key ) );
6916 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6917 key_bits = psa_get_key_bits( &attributes );
6918
6919 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6920 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006921 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006922 ASSERT_ALLOC( signature, signature_size );
6923
6924 PSA_ASSERT( psa_sign_message( key, alg,
6925 input_data->x, input_data->len,
6926 signature, signature_size,
6927 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006928 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006929 TEST_ASSERT( signature_length > 0 );
6930
6931 PSA_ASSERT( psa_verify_message( key, alg,
6932 input_data->x, input_data->len,
6933 signature, signature_length ) );
6934
6935 if( input_data->len != 0 )
6936 {
6937 /* Flip a bit in the input and verify that the signature is now
6938 * detected as invalid. Flip a bit at the beginning, not at the end,
6939 * because ECDSA may ignore the last few bits of the input. */
6940 input_data->x[0] ^= 1;
6941 TEST_EQUAL( psa_verify_message( key, alg,
6942 input_data->x, input_data->len,
6943 signature, signature_length ),
6944 PSA_ERROR_INVALID_SIGNATURE );
6945 }
6946
6947exit:
6948 psa_reset_key_attributes( &attributes );
6949
6950 psa_destroy_key( key );
6951 mbedtls_free( signature );
6952 PSA_DONE( );
6953}
6954/* END_CASE */
6955
6956/* BEGIN_CASE */
6957void verify_message( int key_type_arg,
6958 data_t *key_data,
6959 int alg_arg,
6960 data_t *input_data,
6961 data_t *signature_data )
6962{
6963 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6964 psa_key_type_t key_type = key_type_arg;
6965 psa_algorithm_t alg = alg_arg;
6966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6967
Gilles Peskine7be11a72022-04-14 00:12:57 +02006968 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006969
6970 PSA_ASSERT( psa_crypto_init( ) );
6971
6972 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6973 psa_set_key_algorithm( &attributes, alg );
6974 psa_set_key_type( &attributes, key_type );
6975
6976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6977 &key ) );
6978
6979 PSA_ASSERT( psa_verify_message( key, alg,
6980 input_data->x, input_data->len,
6981 signature_data->x, signature_data->len ) );
6982
6983exit:
6984 psa_reset_key_attributes( &attributes );
6985 psa_destroy_key( key );
6986 PSA_DONE( );
6987}
6988/* END_CASE */
6989
6990/* BEGIN_CASE */
6991void verify_message_fail( int key_type_arg,
6992 data_t *key_data,
6993 int alg_arg,
6994 data_t *hash_data,
6995 data_t *signature_data,
6996 int expected_status_arg )
6997{
6998 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6999 psa_key_type_t key_type = key_type_arg;
7000 psa_algorithm_t alg = alg_arg;
7001 psa_status_t actual_status;
7002 psa_status_t expected_status = expected_status_arg;
7003 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7004
7005 PSA_ASSERT( psa_crypto_init( ) );
7006
7007 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
7008 psa_set_key_algorithm( &attributes, alg );
7009 psa_set_key_type( &attributes, key_type );
7010
7011 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
7012 &key ) );
7013
7014 actual_status = psa_verify_message( key, alg,
7015 hash_data->x, hash_data->len,
7016 signature_data->x,
7017 signature_data->len );
7018 TEST_EQUAL( actual_status, expected_status );
7019
7020exit:
7021 psa_reset_key_attributes( &attributes );
7022 psa_destroy_key( key );
7023 PSA_DONE( );
7024}
7025/* END_CASE */
7026
7027/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007028void asymmetric_encrypt( int key_type_arg,
7029 data_t *key_data,
7030 int alg_arg,
7031 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007032 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007033 int expected_output_length_arg,
7034 int expected_status_arg )
7035{
Ronald Cron5425a212020-08-04 14:58:35 +02007036 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007037 psa_key_type_t key_type = key_type_arg;
7038 psa_algorithm_t alg = alg_arg;
7039 size_t expected_output_length = expected_output_length_arg;
7040 size_t key_bits;
7041 unsigned char *output = NULL;
7042 size_t output_size;
7043 size_t output_length = ~0;
7044 psa_status_t actual_status;
7045 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007047
Gilles Peskine8817f612018-12-18 00:18:46 +01007048 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007049
Gilles Peskine656896e2018-06-29 19:12:28 +02007050 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007051 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7052 psa_set_key_algorithm( &attributes, alg );
7053 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007054 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007055 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007056
7057 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007058 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007059 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007060
Gilles Peskine656896e2018-06-29 19:12:28 +02007061 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007062 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007063 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007064
7065 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007066 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007067 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007068 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007069 output, output_size,
7070 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007071 TEST_EQUAL( actual_status, expected_status );
7072 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007073
Gilles Peskine68428122018-06-30 18:42:41 +02007074 /* If the label is empty, the test framework puts a non-null pointer
7075 * in label->x. Test that a null pointer works as well. */
7076 if( label->len == 0 )
7077 {
7078 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007079 if( output_size != 0 )
7080 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007081 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007082 input_data->x, input_data->len,
7083 NULL, label->len,
7084 output, output_size,
7085 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007086 TEST_EQUAL( actual_status, expected_status );
7087 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007088 }
7089
Gilles Peskine656896e2018-06-29 19:12:28 +02007090exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007091 /*
7092 * Key attributes may have been returned by psa_get_key_attributes()
7093 * thus reset them as required.
7094 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007095 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007096
Ronald Cron5425a212020-08-04 14:58:35 +02007097 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007098 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007099 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007100}
7101/* END_CASE */
7102
7103/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007104void asymmetric_encrypt_decrypt( int key_type_arg,
7105 data_t *key_data,
7106 int alg_arg,
7107 data_t *input_data,
7108 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007109{
Ronald Cron5425a212020-08-04 14:58:35 +02007110 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007111 psa_key_type_t key_type = key_type_arg;
7112 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007113 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007114 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007115 size_t output_size;
7116 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007117 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007118 size_t output2_size;
7119 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007121
Gilles Peskine8817f612018-12-18 00:18:46 +01007122 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007124 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7125 psa_set_key_algorithm( &attributes, alg );
7126 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007127
Gilles Peskine049c7532019-05-15 20:22:09 +02007128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007129 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007130
7131 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007132 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007133 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007134
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007135 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007136 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007137 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007138
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007139 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007140 TEST_LE_U( output2_size,
7141 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7142 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007143 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007144
Gilles Peskineeebd7382018-06-08 18:11:54 +02007145 /* We test encryption by checking that encrypt-then-decrypt gives back
7146 * the original plaintext because of the non-optional random
7147 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007148 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007149 input_data->x, input_data->len,
7150 label->x, label->len,
7151 output, output_size,
7152 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007153 /* We don't know what ciphertext length to expect, but check that
7154 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007155 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007156
Ronald Cron5425a212020-08-04 14:58:35 +02007157 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007158 output, output_length,
7159 label->x, label->len,
7160 output2, output2_size,
7161 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007162 ASSERT_COMPARE( input_data->x, input_data->len,
7163 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007164
7165exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007166 /*
7167 * Key attributes may have been returned by psa_get_key_attributes()
7168 * thus reset them as required.
7169 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007170 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007171
Ronald Cron5425a212020-08-04 14:58:35 +02007172 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007173 mbedtls_free( output );
7174 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007175 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007176}
7177/* END_CASE */
7178
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007179/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007180void asymmetric_decrypt( int key_type_arg,
7181 data_t *key_data,
7182 int alg_arg,
7183 data_t *input_data,
7184 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007185 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007186{
Ronald Cron5425a212020-08-04 14:58:35 +02007187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007188 psa_key_type_t key_type = key_type_arg;
7189 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007190 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007191 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007192 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007193 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007194 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007195
Gilles Peskine8817f612018-12-18 00:18:46 +01007196 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007197
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7199 psa_set_key_algorithm( &attributes, alg );
7200 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007201
Gilles Peskine049c7532019-05-15 20:22:09 +02007202 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007203 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007204
gabor-mezei-armceface22021-01-21 12:26:17 +01007205 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7206 key_bits = psa_get_key_bits( &attributes );
7207
7208 /* Determine the maximum ciphertext length */
7209 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007210 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007211 ASSERT_ALLOC( output, output_size );
7212
Ronald Cron5425a212020-08-04 14:58:35 +02007213 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007214 input_data->x, input_data->len,
7215 label->x, label->len,
7216 output,
7217 output_size,
7218 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007219 ASSERT_COMPARE( expected_data->x, expected_data->len,
7220 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007221
Gilles Peskine68428122018-06-30 18:42:41 +02007222 /* If the label is empty, the test framework puts a non-null pointer
7223 * in label->x. Test that a null pointer works as well. */
7224 if( label->len == 0 )
7225 {
7226 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007227 if( output_size != 0 )
7228 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007229 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007230 input_data->x, input_data->len,
7231 NULL, label->len,
7232 output,
7233 output_size,
7234 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007235 ASSERT_COMPARE( expected_data->x, expected_data->len,
7236 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007237 }
7238
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007239exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007240 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007241 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007242 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007243 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007244}
7245/* END_CASE */
7246
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007247/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007248void asymmetric_decrypt_fail( int key_type_arg,
7249 data_t *key_data,
7250 int alg_arg,
7251 data_t *input_data,
7252 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007253 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007254 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007255{
Ronald Cron5425a212020-08-04 14:58:35 +02007256 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007257 psa_key_type_t key_type = key_type_arg;
7258 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007259 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007260 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007261 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007262 psa_status_t actual_status;
7263 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007265
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007266 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007267
Gilles Peskine8817f612018-12-18 00:18:46 +01007268 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007269
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7271 psa_set_key_algorithm( &attributes, alg );
7272 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007273
Gilles Peskine049c7532019-05-15 20:22:09 +02007274 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007275 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007276
Ronald Cron5425a212020-08-04 14:58:35 +02007277 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007278 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007279 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007280 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007281 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007282 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007283 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007284
Gilles Peskine68428122018-06-30 18:42:41 +02007285 /* If the label is empty, the test framework puts a non-null pointer
7286 * in label->x. Test that a null pointer works as well. */
7287 if( label->len == 0 )
7288 {
7289 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007290 if( output_size != 0 )
7291 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007292 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007293 input_data->x, input_data->len,
7294 NULL, label->len,
7295 output, output_size,
7296 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007297 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007298 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007299 }
7300
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007301exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007302 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007303 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007304 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007305 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007306}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007307/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007308
7309/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007310void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007311{
7312 /* Test each valid way of initializing the object, except for `= {0}`, as
7313 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7314 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007315 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007316 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007317 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7318 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7319 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007320
7321 memset( &zero, 0, sizeof( zero ) );
7322
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007323 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007324 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007325 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007326 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007327 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007328 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007329 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007330
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007331 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007332 PSA_ASSERT( psa_key_derivation_abort(&func) );
7333 PSA_ASSERT( psa_key_derivation_abort(&init) );
7334 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007335}
7336/* END_CASE */
7337
Janos Follath16de4a42019-06-13 16:32:24 +01007338/* BEGIN_CASE */
7339void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007340{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007341 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007342 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007343 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007344
Gilles Peskine8817f612018-12-18 00:18:46 +01007345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007346
Janos Follath16de4a42019-06-13 16:32:24 +01007347 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007348 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007349
7350exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007351 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007352 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007353}
7354/* END_CASE */
7355
Janos Follathaf3c2a02019-06-12 12:34:34 +01007356/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007357void derive_set_capacity( int alg_arg, int capacity_arg,
7358 int expected_status_arg )
7359{
7360 psa_algorithm_t alg = alg_arg;
7361 size_t capacity = capacity_arg;
7362 psa_status_t expected_status = expected_status_arg;
7363 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7364
7365 PSA_ASSERT( psa_crypto_init( ) );
7366
7367 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7368
7369 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7370 expected_status );
7371
7372exit:
7373 psa_key_derivation_abort( &operation );
7374 PSA_DONE( );
7375}
7376/* END_CASE */
7377
7378/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007379void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007380 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007381 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007382 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007383 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007384 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007385 int expected_status_arg3,
7386 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007387{
7388 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007389 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7390 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007391 psa_status_t expected_statuses[] = {expected_status_arg1,
7392 expected_status_arg2,
7393 expected_status_arg3};
7394 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007395 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7396 MBEDTLS_SVC_KEY_ID_INIT,
7397 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007398 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7400 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007401 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007402 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007403 psa_status_t expected_output_status = expected_output_status_arg;
7404 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007405
7406 PSA_ASSERT( psa_crypto_init( ) );
7407
7408 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7409 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007410
7411 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7412
7413 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7414 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007415 mbedtls_test_set_step( i );
7416 if( steps[i] == 0 )
7417 {
7418 /* Skip this step */
7419 }
7420 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007421 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007422 psa_set_key_type( &attributes, key_types[i] );
7423 PSA_ASSERT( psa_import_key( &attributes,
7424 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007425 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007426 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7427 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7428 {
7429 // When taking a private key as secret input, use key agreement
7430 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007431 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7432 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007433 expected_statuses[i] );
7434 }
7435 else
7436 {
7437 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007438 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007439 expected_statuses[i] );
7440 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007441 }
7442 else
7443 {
7444 TEST_EQUAL( psa_key_derivation_input_bytes(
7445 &operation, steps[i],
7446 inputs[i]->x, inputs[i]->len ),
7447 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007448 }
7449 }
7450
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007451 if( output_key_type != PSA_KEY_TYPE_NONE )
7452 {
7453 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007454 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007455 psa_set_key_bits( &attributes, 8 );
7456 actual_output_status =
7457 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007458 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007459 }
7460 else
7461 {
7462 uint8_t buffer[1];
7463 actual_output_status =
7464 psa_key_derivation_output_bytes( &operation,
7465 buffer, sizeof( buffer ) );
7466 }
7467 TEST_EQUAL( actual_output_status, expected_output_status );
7468
Janos Follathaf3c2a02019-06-12 12:34:34 +01007469exit:
7470 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007471 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7472 psa_destroy_key( keys[i] );
7473 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007474 PSA_DONE( );
7475}
7476/* END_CASE */
7477
Janos Follathd958bb72019-07-03 15:02:16 +01007478/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007479void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007480{
Janos Follathd958bb72019-07-03 15:02:16 +01007481 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007482 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007483 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007484 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007485 unsigned char input1[] = "Input 1";
7486 size_t input1_length = sizeof( input1 );
7487 unsigned char input2[] = "Input 2";
7488 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007489 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007490 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007491 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7492 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7493 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007495
Gilles Peskine8817f612018-12-18 00:18:46 +01007496 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007497
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007498 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7499 psa_set_key_algorithm( &attributes, alg );
7500 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007501
Gilles Peskine73676cb2019-05-15 20:15:10 +02007502 PSA_ASSERT( psa_import_key( &attributes,
7503 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007504 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007505
7506 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007507 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7508 input1, input1_length,
7509 input2, input2_length,
7510 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007511 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007512
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007513 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007514 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007515 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007516
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007517 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007518
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007519 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007520 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007521
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007522exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007523 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007524 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007525 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007526}
7527/* END_CASE */
7528
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007529/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007530void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007531{
7532 uint8_t output_buffer[16];
7533 size_t buffer_size = 16;
7534 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007535 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007536
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007537 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7538 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007539 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007540
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007541 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007542 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007543
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007544 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007545
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007546 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7547 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007548 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007549
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007550 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007551 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007552
7553exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007554 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007555}
7556/* END_CASE */
7557
7558/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007559void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007560 int step1_arg, data_t *input1, int expected_status_arg1,
7561 int step2_arg, data_t *input2, int expected_status_arg2,
7562 int step3_arg, data_t *input3, int expected_status_arg3,
7563 int step4_arg, data_t *input4, int expected_status_arg4,
7564 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007565 int requested_capacity_arg,
7566 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007567 data_t *expected_output2,
7568 int other_key_input_type,
7569 int key_input_type,
7570 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007571{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007572 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007573 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7574 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007575 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7576 MBEDTLS_SVC_KEY_ID_INIT,
7577 MBEDTLS_SVC_KEY_ID_INIT,
7578 MBEDTLS_SVC_KEY_ID_INIT};
7579 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7580 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007581 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007582 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007583 uint8_t *expected_outputs[2] =
7584 {expected_output1->x, expected_output2->x};
7585 size_t output_sizes[2] =
7586 {expected_output1->len, expected_output2->len};
7587 size_t output_buffer_size = 0;
7588 uint8_t *output_buffer = NULL;
7589 size_t expected_capacity;
7590 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007591 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7592 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7593 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7594 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007595 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007596 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007597 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007598
7599 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7600 {
7601 if( output_sizes[i] > output_buffer_size )
7602 output_buffer_size = output_sizes[i];
7603 if( output_sizes[i] == 0 )
7604 expected_outputs[i] = NULL;
7605 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007606 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007607 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007608
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007609 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007610 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7611 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7612 requested_capacity ) );
7613 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007614 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007615 switch( steps[i] )
7616 {
7617 case 0:
7618 break;
7619 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007620 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007621 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007622 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007623 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007624 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007625 inputs[i]->x, inputs[i]->len ),
7626 statuses[i] );
7627
7628 if( statuses[i] != PSA_SUCCESS )
7629 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007630 break;
7631 case 1: // input key
7632 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7633 psa_set_key_algorithm( &attributes1, alg );
7634 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7635
7636 PSA_ASSERT( psa_import_key( &attributes1,
7637 inputs[i]->x, inputs[i]->len,
7638 &keys[i] ) );
7639
Przemek Stekiel38647de2022-04-19 13:27:47 +02007640 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007641 {
7642 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007643 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7644 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007645 }
7646
Przemek Stekiel38647de2022-04-19 13:27:47 +02007647 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7648 steps[i],
7649 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007650 break;
7651 default:
7652 TEST_ASSERT( ! "default case not supported" );
7653 break;
7654 }
7655 break;
7656 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007657 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007658 {
7659 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007660 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7661 steps[i],
7662 inputs[i]->x,
7663 inputs[i]->len ),
7664 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007665 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007666 case 1: // input key, type DERIVE
7667 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007668 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7669 psa_set_key_algorithm( &attributes2, alg );
7670 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7671
7672 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007673 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007674 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7675
7676 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007677 inputs[i]->x, inputs[i]->len,
7678 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007679
Przemek Stekiel38647de2022-04-19 13:27:47 +02007680 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7681 steps[i],
7682 keys[i] ),
7683 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007684 break;
7685 case 2: // key agreement
7686 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7687 psa_set_key_algorithm( &attributes3, alg );
7688 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7689
7690 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007691 inputs[i]->x, inputs[i]->len,
7692 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007693
7694 TEST_EQUAL( psa_key_derivation_key_agreement(
7695 &operation,
7696 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7697 keys[i], key_agreement_peer_key->x,
7698 key_agreement_peer_key->len ), statuses[i] );
7699 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007700 default:
7701 TEST_ASSERT( ! "default case not supported" );
7702 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007703 }
7704
Przemek Stekiel38647de2022-04-19 13:27:47 +02007705 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007706 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007707 break;
7708 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007709 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007710 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007711 inputs[i]->x, inputs[i]->len ), statuses[i] );
7712
7713 if( statuses[i] != PSA_SUCCESS )
7714 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007715 break;
7716 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007717 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007718
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007719 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007720 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007721 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007722 expected_capacity = requested_capacity;
7723
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007724 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007725 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007726 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7727
7728 /* For output key derivation secret must be provided using
7729 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007730 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007731 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007732
7733 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7734 psa_set_key_algorithm( &attributes4, alg );
7735 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007736 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007737
7738 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007739 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007740 }
7741 else // output bytes
7742 {
7743 /* Expansion phase. */
7744 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007745 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007746 /* Read some bytes. */
7747 status = psa_key_derivation_output_bytes( &operation,
7748 output_buffer, output_sizes[i] );
7749 if( expected_capacity == 0 && output_sizes[i] == 0 )
7750 {
7751 /* Reading 0 bytes when 0 bytes are available can go either way. */
7752 TEST_ASSERT( status == PSA_SUCCESS ||
7753 status == PSA_ERROR_INSUFFICIENT_DATA );
7754 continue;
7755 }
7756 else if( expected_capacity == 0 ||
7757 output_sizes[i] > expected_capacity )
7758 {
7759 /* Capacity exceeded. */
7760 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7761 expected_capacity = 0;
7762 continue;
7763 }
7764 /* Success. Check the read data. */
7765 PSA_ASSERT( status );
7766 if( output_sizes[i] != 0 )
7767 ASSERT_COMPARE( output_buffer, output_sizes[i],
7768 expected_outputs[i], output_sizes[i] );
7769 /* Check the operation status. */
7770 expected_capacity -= output_sizes[i];
7771 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7772 &current_capacity ) );
7773 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007774 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007775 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007776 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007777
7778exit:
7779 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007780 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007781 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7782 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007783 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007784 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007785}
7786/* END_CASE */
7787
7788/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007789void derive_full( int alg_arg,
7790 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007791 data_t *input1,
7792 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007793 int requested_capacity_arg )
7794{
Ronald Cron5425a212020-08-04 14:58:35 +02007795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007796 psa_algorithm_t alg = alg_arg;
7797 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007798 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007799 unsigned char output_buffer[16];
7800 size_t expected_capacity = requested_capacity;
7801 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007803
Gilles Peskine8817f612018-12-18 00:18:46 +01007804 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007805
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007806 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7807 psa_set_key_algorithm( &attributes, alg );
7808 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007809
Gilles Peskine049c7532019-05-15 20:22:09 +02007810 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007811 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007812
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007813 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7814 input1->x, input1->len,
7815 input2->x, input2->len,
7816 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007817 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007818
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007819 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007820 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007821 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007822
7823 /* Expansion phase. */
7824 while( current_capacity > 0 )
7825 {
7826 size_t read_size = sizeof( output_buffer );
7827 if( read_size > current_capacity )
7828 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007829 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007830 output_buffer,
7831 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007832 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007833 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007834 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007835 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007836 }
7837
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007838 /* Check that the operation refuses to go over capacity. */
7839 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007840 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007841
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007842 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007843
7844exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007845 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007846 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007847 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007848}
7849/* END_CASE */
7850
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007851/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007852void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007853 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007854 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007855 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007856 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007857{
7858 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7859 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007860 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007861 uint8_t *output_buffer = NULL;
7862 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007863 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7864 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7865 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007866
7867 ASSERT_ALLOC( output_buffer, expected_output->len );
7868 PSA_ASSERT( psa_crypto_init() );
7869
7870 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007871 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007872 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007873
7874 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007875 step, input->x, input->len ),
7876 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007877
7878 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7879 goto exit;
7880
7881 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7882 expected_output->len );
7883
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007884 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007885 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7886 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7887 expected_output->len );
7888
7889exit:
7890 mbedtls_free( output_buffer );
7891 psa_key_derivation_abort( &operation );
7892 PSA_DONE();
7893}
7894/* END_CASE */
7895
Janos Follathe60c9052019-07-03 13:51:30 +01007896/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007897void derive_key_exercise( int alg_arg,
7898 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007899 data_t *input1,
7900 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007901 int derived_type_arg,
7902 int derived_bits_arg,
7903 int derived_usage_arg,
7904 int derived_alg_arg )
7905{
Ronald Cron5425a212020-08-04 14:58:35 +02007906 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7907 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007908 psa_algorithm_t alg = alg_arg;
7909 psa_key_type_t derived_type = derived_type_arg;
7910 size_t derived_bits = derived_bits_arg;
7911 psa_key_usage_t derived_usage = derived_usage_arg;
7912 psa_algorithm_t derived_alg = derived_alg_arg;
7913 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007914 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007916 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007917
Gilles Peskine8817f612018-12-18 00:18:46 +01007918 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007919
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007920 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7921 psa_set_key_algorithm( &attributes, alg );
7922 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007923 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007924 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007925
7926 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007927 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7928 input1->x, input1->len,
7929 input2->x, input2->len,
7930 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007931 goto exit;
7932
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007933 psa_set_key_usage_flags( &attributes, derived_usage );
7934 psa_set_key_algorithm( &attributes, derived_alg );
7935 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007936 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007937 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007938 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007939
7940 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007941 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007942 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7943 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007944
7945 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007946 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007947 goto exit;
7948
7949exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007950 /*
7951 * Key attributes may have been returned by psa_get_key_attributes()
7952 * thus reset them as required.
7953 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007954 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007955
7956 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007957 psa_destroy_key( base_key );
7958 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007959 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007960}
7961/* END_CASE */
7962
Janos Follath42fd8882019-07-03 14:17:09 +01007963/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007964void derive_key_export( int alg_arg,
7965 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007966 data_t *input1,
7967 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007968 int bytes1_arg,
7969 int bytes2_arg )
7970{
Ronald Cron5425a212020-08-04 14:58:35 +02007971 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7972 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007973 psa_algorithm_t alg = alg_arg;
7974 size_t bytes1 = bytes1_arg;
7975 size_t bytes2 = bytes2_arg;
7976 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007977 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007978 uint8_t *output_buffer = NULL;
7979 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007980 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7981 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007982 size_t length;
7983
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007984 ASSERT_ALLOC( output_buffer, capacity );
7985 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007986 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007987
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007988 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7989 psa_set_key_algorithm( &base_attributes, alg );
7990 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007991 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007992 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007993
7994 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007995 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7996 input1->x, input1->len,
7997 input2->x, input2->len,
7998 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007999 goto exit;
8000
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008001 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008002 output_buffer,
8003 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008004 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008005
8006 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008007 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8008 input1->x, input1->len,
8009 input2->x, input2->len,
8010 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008011 goto exit;
8012
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008013 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8014 psa_set_key_algorithm( &derived_attributes, 0 );
8015 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008016 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008017 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008018 &derived_key ) );
8019 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008020 export_buffer, bytes1,
8021 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008022 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02008023 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008024 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008025 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008026 &derived_key ) );
8027 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008028 export_buffer + bytes1, bytes2,
8029 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008030 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008031
8032 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008033 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8034 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008035
8036exit:
8037 mbedtls_free( output_buffer );
8038 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008039 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008040 psa_destroy_key( base_key );
8041 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008042 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008043}
8044/* END_CASE */
8045
8046/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008047void derive_key_type( int alg_arg,
8048 data_t *key_data,
8049 data_t *input1,
8050 data_t *input2,
8051 int key_type_arg, int bits_arg,
8052 data_t *expected_export )
8053{
8054 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8055 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8056 const psa_algorithm_t alg = alg_arg;
8057 const psa_key_type_t key_type = key_type_arg;
8058 const size_t bits = bits_arg;
8059 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8060 const size_t export_buffer_size =
8061 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8062 uint8_t *export_buffer = NULL;
8063 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8064 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8065 size_t export_length;
8066
8067 ASSERT_ALLOC( export_buffer, export_buffer_size );
8068 PSA_ASSERT( psa_crypto_init( ) );
8069
8070 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8071 psa_set_key_algorithm( &base_attributes, alg );
8072 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8073 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8074 &base_key ) );
8075
Przemek Stekielc85f0912022-03-08 11:37:54 +01008076 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008077 &operation, base_key, alg,
8078 input1->x, input1->len,
8079 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008080 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008081 goto exit;
8082
8083 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8084 psa_set_key_algorithm( &derived_attributes, 0 );
8085 psa_set_key_type( &derived_attributes, key_type );
8086 psa_set_key_bits( &derived_attributes, bits );
8087 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8088 &derived_key ) );
8089
8090 PSA_ASSERT( psa_export_key( derived_key,
8091 export_buffer, export_buffer_size,
8092 &export_length ) );
8093 ASSERT_COMPARE( export_buffer, export_length,
8094 expected_export->x, expected_export->len );
8095
8096exit:
8097 mbedtls_free( export_buffer );
8098 psa_key_derivation_abort( &operation );
8099 psa_destroy_key( base_key );
8100 psa_destroy_key( derived_key );
8101 PSA_DONE( );
8102}
8103/* END_CASE */
8104
8105/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008106void derive_key( int alg_arg,
8107 data_t *key_data, data_t *input1, data_t *input2,
8108 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008109 int expected_status_arg,
8110 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008111{
Ronald Cron5425a212020-08-04 14:58:35 +02008112 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8113 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008114 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008115 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008116 size_t bits = bits_arg;
8117 psa_status_t expected_status = expected_status_arg;
8118 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8119 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8120 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8121
8122 PSA_ASSERT( psa_crypto_init( ) );
8123
8124 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8125 psa_set_key_algorithm( &base_attributes, alg );
8126 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8127 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008128 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008129
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008130 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8131 input1->x, input1->len,
8132 input2->x, input2->len,
8133 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008134 goto exit;
8135
8136 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8137 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008138 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008139 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008140
8141 psa_status_t status =
8142 psa_key_derivation_output_key( &derived_attributes,
8143 &operation,
8144 &derived_key );
8145 if( is_large_output > 0 )
8146 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8147 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008148
8149exit:
8150 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008151 psa_destroy_key( base_key );
8152 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008153 PSA_DONE( );
8154}
8155/* END_CASE */
8156
8157/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008158void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008159 int our_key_type_arg, int our_key_alg_arg,
8160 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008161 int expected_status_arg )
8162{
Ronald Cron5425a212020-08-04 14:58:35 +02008163 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008164 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008165 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008166 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008167 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008168 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008169 psa_status_t expected_status = expected_status_arg;
8170 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008171
Gilles Peskine8817f612018-12-18 00:18:46 +01008172 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008173
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008175 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008176 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008177 PSA_ASSERT( psa_import_key( &attributes,
8178 our_key_data->x, our_key_data->len,
8179 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008180
Gilles Peskine77f40d82019-04-11 21:27:06 +02008181 /* The tests currently include inputs that should fail at either step.
8182 * Test cases that fail at the setup step should be changed to call
8183 * key_derivation_setup instead, and this function should be renamed
8184 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008185 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008186 if( status == PSA_SUCCESS )
8187 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008188 TEST_EQUAL( psa_key_derivation_key_agreement(
8189 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8190 our_key,
8191 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008192 expected_status );
8193 }
8194 else
8195 {
8196 TEST_ASSERT( status == expected_status );
8197 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008198
8199exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008200 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008201 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008202 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008203}
8204/* END_CASE */
8205
8206/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008207void raw_key_agreement( int alg_arg,
8208 int our_key_type_arg, data_t *our_key_data,
8209 data_t *peer_key_data,
8210 data_t *expected_output )
8211{
Ronald Cron5425a212020-08-04 14:58:35 +02008212 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008213 psa_algorithm_t alg = alg_arg;
8214 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008216 unsigned char *output = NULL;
8217 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008218 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008219
Gilles Peskinef0cba732019-04-11 22:12:38 +02008220 PSA_ASSERT( psa_crypto_init( ) );
8221
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8223 psa_set_key_algorithm( &attributes, alg );
8224 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008225 PSA_ASSERT( psa_import_key( &attributes,
8226 our_key_data->x, our_key_data->len,
8227 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008228
gabor-mezei-armceface22021-01-21 12:26:17 +01008229 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8230 key_bits = psa_get_key_bits( &attributes );
8231
Gilles Peskine992bee82022-04-13 23:25:52 +02008232 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008233 TEST_LE_U( expected_output->len,
8234 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8235 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8236 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008237
8238 /* Good case with exact output size */
8239 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008240 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8241 peer_key_data->x, peer_key_data->len,
8242 output, expected_output->len,
8243 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008244 ASSERT_COMPARE( output, output_length,
8245 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008246 mbedtls_free( output );
8247 output = NULL;
8248 output_length = ~0;
8249
8250 /* Larger buffer */
8251 ASSERT_ALLOC( output, expected_output->len + 1 );
8252 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8253 peer_key_data->x, peer_key_data->len,
8254 output, expected_output->len + 1,
8255 &output_length ) );
8256 ASSERT_COMPARE( output, output_length,
8257 expected_output->x, expected_output->len );
8258 mbedtls_free( output );
8259 output = NULL;
8260 output_length = ~0;
8261
8262 /* Buffer too small */
8263 ASSERT_ALLOC( output, expected_output->len - 1 );
8264 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8265 peer_key_data->x, peer_key_data->len,
8266 output, expected_output->len - 1,
8267 &output_length ),
8268 PSA_ERROR_BUFFER_TOO_SMALL );
8269 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008270 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008271 mbedtls_free( output );
8272 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008273
8274exit:
8275 mbedtls_free( output );
8276 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008277 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008278}
8279/* END_CASE */
8280
8281/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008282void key_agreement_capacity( int alg_arg,
8283 int our_key_type_arg, data_t *our_key_data,
8284 data_t *peer_key_data,
8285 int expected_capacity_arg )
8286{
Ronald Cron5425a212020-08-04 14:58:35 +02008287 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008288 psa_algorithm_t alg = alg_arg;
8289 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008290 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008291 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008292 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008293 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008294
Gilles Peskine8817f612018-12-18 00:18:46 +01008295 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008296
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8298 psa_set_key_algorithm( &attributes, alg );
8299 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008300 PSA_ASSERT( psa_import_key( &attributes,
8301 our_key_data->x, our_key_data->len,
8302 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008303
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008304 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008305 PSA_ASSERT( psa_key_derivation_key_agreement(
8306 &operation,
8307 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8308 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008309 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8310 {
8311 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008312 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008313 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008314 NULL, 0 ) );
8315 }
Gilles Peskine59685592018-09-18 12:11:34 +02008316
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008317 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008318 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008319 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008320 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008321
Gilles Peskinebf491972018-10-25 22:36:12 +02008322 /* Test the actual capacity by reading the output. */
8323 while( actual_capacity > sizeof( output ) )
8324 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008325 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008326 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008327 actual_capacity -= sizeof( output );
8328 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008329 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008330 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008331 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008332 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008333
Gilles Peskine59685592018-09-18 12:11:34 +02008334exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008335 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008336 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008337 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008338}
8339/* END_CASE */
8340
8341/* BEGIN_CASE */
8342void key_agreement_output( int alg_arg,
8343 int our_key_type_arg, data_t *our_key_data,
8344 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008345 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008346{
Ronald Cron5425a212020-08-04 14:58:35 +02008347 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008348 psa_algorithm_t alg = alg_arg;
8349 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008350 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008352 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008353
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008354 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8355 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008356
Gilles Peskine8817f612018-12-18 00:18:46 +01008357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008358
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008359 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8360 psa_set_key_algorithm( &attributes, alg );
8361 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008362 PSA_ASSERT( psa_import_key( &attributes,
8363 our_key_data->x, our_key_data->len,
8364 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008365
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008366 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008367 PSA_ASSERT( psa_key_derivation_key_agreement(
8368 &operation,
8369 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8370 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008371 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8372 {
8373 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008374 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008375 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008376 NULL, 0 ) );
8377 }
Gilles Peskine59685592018-09-18 12:11:34 +02008378
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008379 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008380 actual_output,
8381 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008382 ASSERT_COMPARE( actual_output, expected_output1->len,
8383 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008384 if( expected_output2->len != 0 )
8385 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008386 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008387 actual_output,
8388 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008389 ASSERT_COMPARE( actual_output, expected_output2->len,
8390 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008391 }
Gilles Peskine59685592018-09-18 12:11:34 +02008392
8393exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008394 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008395 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008396 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008397 mbedtls_free( actual_output );
8398}
8399/* END_CASE */
8400
8401/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008402void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008403{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008404 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008405 unsigned char *output = NULL;
8406 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008407 size_t i;
8408 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008409
Simon Butcher49f8e312020-03-03 15:51:50 +00008410 TEST_ASSERT( bytes_arg >= 0 );
8411
Gilles Peskine91892022021-02-08 19:50:26 +01008412 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008413 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008414
Gilles Peskine8817f612018-12-18 00:18:46 +01008415 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008416
Gilles Peskinea50d7392018-06-21 10:22:13 +02008417 /* Run several times, to ensure that every output byte will be
8418 * nonzero at least once with overwhelming probability
8419 * (2^(-8*number_of_runs)). */
8420 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008421 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008422 if( bytes != 0 )
8423 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008424 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008425
Gilles Peskinea50d7392018-06-21 10:22:13 +02008426 for( i = 0; i < bytes; i++ )
8427 {
8428 if( output[i] != 0 )
8429 ++changed[i];
8430 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008431 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008432
8433 /* Check that every byte was changed to nonzero at least once. This
8434 * validates that psa_generate_random is overwriting every byte of
8435 * the output buffer. */
8436 for( i = 0; i < bytes; i++ )
8437 {
8438 TEST_ASSERT( changed[i] != 0 );
8439 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008440
8441exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008442 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008443 mbedtls_free( output );
8444 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008445}
8446/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008447
8448/* BEGIN_CASE */
8449void generate_key( int type_arg,
8450 int bits_arg,
8451 int usage_arg,
8452 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008453 int expected_status_arg,
8454 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008455{
Ronald Cron5425a212020-08-04 14:58:35 +02008456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008457 psa_key_type_t type = type_arg;
8458 psa_key_usage_t usage = usage_arg;
8459 size_t bits = bits_arg;
8460 psa_algorithm_t alg = alg_arg;
8461 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008463 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008464
Gilles Peskine8817f612018-12-18 00:18:46 +01008465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008466
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008467 psa_set_key_usage_flags( &attributes, usage );
8468 psa_set_key_algorithm( &attributes, alg );
8469 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008470 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008471
8472 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008473 psa_status_t status = psa_generate_key( &attributes, &key );
8474
8475 if( is_large_key > 0 )
8476 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8477 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008478 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008479 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008480
8481 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008482 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008483 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8484 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008485
Gilles Peskine818ca122018-06-20 18:16:48 +02008486 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008487 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008488 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008489
8490exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008491 /*
8492 * Key attributes may have been returned by psa_get_key_attributes()
8493 * thus reset them as required.
8494 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008495 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008496
Ronald Cron5425a212020-08-04 14:58:35 +02008497 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008498 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008499}
8500/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008501
Ronald Cronee414c72021-03-18 18:50:08 +01008502/* 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 +02008503void generate_key_rsa( int bits_arg,
8504 data_t *e_arg,
8505 int expected_status_arg )
8506{
Ronald Cron5425a212020-08-04 14:58:35 +02008507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008508 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008509 size_t bits = bits_arg;
8510 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8511 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8512 psa_status_t expected_status = expected_status_arg;
8513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8514 uint8_t *exported = NULL;
8515 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008516 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008517 size_t exported_length = SIZE_MAX;
8518 uint8_t *e_read_buffer = NULL;
8519 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008520 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008521 size_t e_read_length = SIZE_MAX;
8522
8523 if( e_arg->len == 0 ||
8524 ( e_arg->len == 3 &&
8525 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8526 {
8527 is_default_public_exponent = 1;
8528 e_read_size = 0;
8529 }
8530 ASSERT_ALLOC( e_read_buffer, e_read_size );
8531 ASSERT_ALLOC( exported, exported_size );
8532
8533 PSA_ASSERT( psa_crypto_init( ) );
8534
8535 psa_set_key_usage_flags( &attributes, usage );
8536 psa_set_key_algorithm( &attributes, alg );
8537 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8538 e_arg->x, e_arg->len ) );
8539 psa_set_key_bits( &attributes, bits );
8540
8541 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008542 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008543 if( expected_status != PSA_SUCCESS )
8544 goto exit;
8545
8546 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008547 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008548 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8549 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8550 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8551 e_read_buffer, e_read_size,
8552 &e_read_length ) );
8553 if( is_default_public_exponent )
8554 TEST_EQUAL( e_read_length, 0 );
8555 else
8556 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8557
8558 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008559 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008560 goto exit;
8561
8562 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008563 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008564 exported, exported_size,
8565 &exported_length ) );
8566 {
8567 uint8_t *p = exported;
8568 uint8_t *end = exported + exported_length;
8569 size_t len;
8570 /* RSAPublicKey ::= SEQUENCE {
8571 * modulus INTEGER, -- n
8572 * publicExponent INTEGER } -- e
8573 */
8574 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008575 MBEDTLS_ASN1_SEQUENCE |
8576 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008577 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008578 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8579 MBEDTLS_ASN1_INTEGER ) );
8580 if( len >= 1 && p[0] == 0 )
8581 {
8582 ++p;
8583 --len;
8584 }
8585 if( e_arg->len == 0 )
8586 {
8587 TEST_EQUAL( len, 3 );
8588 TEST_EQUAL( p[0], 1 );
8589 TEST_EQUAL( p[1], 0 );
8590 TEST_EQUAL( p[2], 1 );
8591 }
8592 else
8593 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8594 }
8595
8596exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008597 /*
8598 * Key attributes may have been returned by psa_get_key_attributes() or
8599 * set by psa_set_key_domain_parameters() thus reset them as required.
8600 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008601 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008602
Ronald Cron5425a212020-08-04 14:58:35 +02008603 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008604 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008605 mbedtls_free( e_read_buffer );
8606 mbedtls_free( exported );
8607}
8608/* END_CASE */
8609
Darryl Greend49a4992018-06-18 17:27:26 +01008610/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008611void persistent_key_load_key_from_storage( data_t *data,
8612 int type_arg, int bits_arg,
8613 int usage_flags_arg, int alg_arg,
8614 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008615{
Ronald Cron71016a92020-08-28 19:01:50 +02008616 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008618 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8619 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008620 psa_key_type_t type = type_arg;
8621 size_t bits = bits_arg;
8622 psa_key_usage_t usage_flags = usage_flags_arg;
8623 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008624 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008625 unsigned char *first_export = NULL;
8626 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008627 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008628 size_t first_exported_length;
8629 size_t second_exported_length;
8630
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008631 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8632 {
8633 ASSERT_ALLOC( first_export, export_size );
8634 ASSERT_ALLOC( second_export, export_size );
8635 }
Darryl Greend49a4992018-06-18 17:27:26 +01008636
Gilles Peskine8817f612018-12-18 00:18:46 +01008637 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008638
Gilles Peskinec87af662019-05-15 16:12:22 +02008639 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008640 psa_set_key_usage_flags( &attributes, usage_flags );
8641 psa_set_key_algorithm( &attributes, alg );
8642 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008643 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008644
Darryl Green0c6575a2018-11-07 16:05:30 +00008645 switch( generation_method )
8646 {
8647 case IMPORT_KEY:
8648 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008649 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008650 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008651 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008652
Darryl Green0c6575a2018-11-07 16:05:30 +00008653 case GENERATE_KEY:
8654 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008655 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008656 break;
8657
8658 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008659#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008660 {
8661 /* Create base key */
8662 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8663 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8664 psa_set_key_usage_flags( &base_attributes,
8665 PSA_KEY_USAGE_DERIVE );
8666 psa_set_key_algorithm( &base_attributes, derive_alg );
8667 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008668 PSA_ASSERT( psa_import_key( &base_attributes,
8669 data->x, data->len,
8670 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008671 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008672 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008673 PSA_ASSERT( psa_key_derivation_input_key(
8674 &operation,
8675 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008676 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008677 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008678 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008679 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8680 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008681 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008682 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008683 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008684 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008685 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008686#else
8687 TEST_ASSUME( ! "KDF not supported in this configuration" );
8688#endif
8689 break;
8690
8691 default:
8692 TEST_ASSERT( ! "generation_method not implemented in test" );
8693 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008694 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008695 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008696
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008697 /* Export the key if permitted by the key policy. */
8698 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8699 {
Ronald Cron5425a212020-08-04 14:58:35 +02008700 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008701 first_export, export_size,
8702 &first_exported_length ) );
8703 if( generation_method == IMPORT_KEY )
8704 ASSERT_COMPARE( data->x, data->len,
8705 first_export, first_exported_length );
8706 }
Darryl Greend49a4992018-06-18 17:27:26 +01008707
8708 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008709 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008710 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008711 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008712
Darryl Greend49a4992018-06-18 17:27:26 +01008713 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008714 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008715 TEST_ASSERT( mbedtls_svc_key_id_equal(
8716 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008717 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8718 PSA_KEY_LIFETIME_PERSISTENT );
8719 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8720 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008721 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008722 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008723 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008724
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008725 /* Export the key again if permitted by the key policy. */
8726 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008727 {
Ronald Cron5425a212020-08-04 14:58:35 +02008728 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008729 second_export, export_size,
8730 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008731 ASSERT_COMPARE( first_export, first_exported_length,
8732 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008733 }
8734
8735 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008736 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008737 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008738
8739exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008740 /*
8741 * Key attributes may have been returned by psa_get_key_attributes()
8742 * thus reset them as required.
8743 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008744 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008745
Darryl Greend49a4992018-06-18 17:27:26 +01008746 mbedtls_free( first_export );
8747 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008748 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008749 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008750 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008751 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008752}
8753/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008754
Neil Armstronga557cb82022-06-10 08:58:32 +02008755/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008756void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8757 int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008758 int input_first, data_t *pw_data,
Neil Armstrong2a73f212022-09-06 11:34:54 +02008759 int expected_status_setup_arg,
8760 int expected_status_set_role_arg,
8761 int expected_status_set_password_key_arg,
8762 int expected_status_input_output_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008763{
8764 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8765 psa_pake_operation_t operation = psa_pake_operation_init();
8766 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008767 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008768 psa_key_type_t key_type_pw = key_type_pw_arg;
8769 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008770 psa_algorithm_t hash_alg = hash_arg;
8771 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008772 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008774 psa_status_t expected_status_setup = expected_status_setup_arg;
8775 psa_status_t expected_status_set_role = expected_status_set_role_arg;
8776 psa_status_t expected_status_set_password_key =
8777 expected_status_set_password_key_arg;
8778 psa_status_t expected_status_input_output =
8779 expected_status_input_output_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008780 unsigned char *output_buffer = NULL;
8781 size_t output_len = 0;
8782
8783 PSA_INIT( );
8784
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008785 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8786 PSA_PAKE_STEP_KEY_SHARE);
8787 ASSERT_ALLOC( output_buffer, buf_size );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008788
8789 if( pw_data->len > 0 )
8790 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008791 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008792 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008793 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008794 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8795 &key ) );
8796 }
8797
8798 psa_pake_cs_set_algorithm( &cipher_suite, alg );
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008799 psa_pake_cs_set_primitive( &cipher_suite, primitive );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008800 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8801
Neil Armstrong645cccd2022-06-08 17:36:23 +02008802 PSA_ASSERT( psa_pake_abort( &operation ) );
8803
8804 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8805 PSA_ERROR_BAD_STATE );
8806 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8807 PSA_ERROR_BAD_STATE );
8808 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8809 PSA_ERROR_BAD_STATE );
8810 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8811 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008812 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8813 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008814 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008815 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008816 PSA_ERROR_BAD_STATE );
8817
8818 PSA_ASSERT( psa_pake_abort( &operation ) );
8819
Neil Armstrong2a73f212022-09-06 11:34:54 +02008820 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8821 expected_status_setup );
8822 if( expected_status_setup != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008823 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008824
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008825 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8826 PSA_ERROR_BAD_STATE );
8827
Neil Armstrong2a73f212022-09-06 11:34:54 +02008828 TEST_EQUAL( psa_pake_set_role( &operation, role),
8829 expected_status_set_role );
8830 if( expected_status_set_role != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008831 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008832
8833 if( pw_data->len > 0 )
8834 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008835 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8836 expected_status_set_password_key );
8837 if( expected_status_set_password_key != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008838 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008839 }
8840
Neil Armstrong707d9572022-06-08 17:31:49 +02008841 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8842 PSA_ERROR_INVALID_ARGUMENT );
8843 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8844 PSA_ERROR_INVALID_ARGUMENT );
8845
8846 const uint8_t unsupported_id[] = "abcd";
8847
8848 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8849 PSA_ERROR_NOT_SUPPORTED );
8850 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8851 PSA_ERROR_NOT_SUPPORTED );
8852
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008853 const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive,
8854 PSA_PAKE_STEP_KEY_SHARE );
8855 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive,
8856 PSA_PAKE_STEP_ZK_PUBLIC );
8857 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive,
8858 PSA_PAKE_STEP_ZK_PROOF );
8859
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008860 /* First round */
8861 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008862 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008863 /* Invalid parameters (input) */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008864 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008865 NULL, 0 ),
8866 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008867 /* Invalid parameters (step) */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008868 ecjpake_operation_setup( &operation, &cipher_suite, role,
8869 key, pw_data->len );
8870 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008871 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008872 PSA_ERROR_INVALID_ARGUMENT );
8873 /* Invalid first step */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008874 ecjpake_operation_setup( &operation, &cipher_suite, role,
8875 key, pw_data->len );
8876 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008877 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008878 PSA_ERROR_BAD_STATE );
8879
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008880 /* Possibly valid */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008881 ecjpake_operation_setup( &operation, &cipher_suite, role,
8882 key, pw_data->len );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008883 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008884 output_buffer, size_key_share ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008885 expected_status_input_output);
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008886
Neil Armstrong2a73f212022-09-06 11:34:54 +02008887 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008888 {
8889 /* Buffer too large */
8890 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008891 output_buffer, size_zk_public + 1 ),
8892 PSA_ERROR_INVALID_ARGUMENT );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008893
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008894 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008895 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008896 output_buffer, size_zk_public ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008897 PSA_ERROR_BAD_STATE );
8898 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008899 }
8900 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008901 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008902 /* Invalid parameters (output) */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008903 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008904 NULL, 0, NULL ),
8905 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008906 /* Invalid parameters (step) */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008907 ecjpake_operation_setup( &operation, &cipher_suite, role,
8908 key, pw_data->len );
8909 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008910 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008911 PSA_ERROR_INVALID_ARGUMENT );
8912 /* Invalid first step */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008913 ecjpake_operation_setup( &operation, &cipher_suite, role,
8914 key, pw_data->len );
8915 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008916 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008917 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008918
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008919 /* Possibly valid */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008920 ecjpake_operation_setup( &operation, &cipher_suite, role,
8921 key, pw_data->len );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008922 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008923 output_buffer, buf_size, &output_len ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008924 expected_status_input_output );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008925
Neil Armstrong2a73f212022-09-06 11:34:54 +02008926 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008927 {
8928 TEST_ASSERT( output_len > 0 );
8929
8930 /* Buffer too small */
8931 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008932 output_buffer, size_zk_public - 1, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008933 PSA_ERROR_BUFFER_TOO_SMALL );
8934
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008935 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008936 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008937 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008938 PSA_ERROR_BAD_STATE );
8939 }
8940 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008941
8942exit:
8943 PSA_ASSERT( psa_destroy_key( key ) );
8944 PSA_ASSERT( psa_pake_abort( &operation ) );
8945 mbedtls_free( output_buffer );
8946 PSA_DONE( );
8947}
8948/* END_CASE */
8949
Neil Armstronga557cb82022-06-10 08:58:32 +02008950/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008951void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8952 int client_input_first, int inject_error,
8953 data_t *pw_data )
8954{
8955 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8956 psa_pake_operation_t server = psa_pake_operation_init();
8957 psa_pake_operation_t client = psa_pake_operation_init();
8958 psa_algorithm_t alg = alg_arg;
8959 psa_algorithm_t hash_alg = hash_arg;
8960 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8962
8963 PSA_INIT( );
8964
8965 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8966 psa_set_key_algorithm( &attributes, alg );
8967 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8968 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8969 &key ) );
8970
8971 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8972 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8973 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8974
8975
8976 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8977 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8978
8979 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8980 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8981
8982 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8983 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8984
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008985 ecjpake_do_round( alg, primitive_arg, &server, &client,
8986 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008987
8988 if( inject_error == 1 || inject_error == 2 )
8989 goto exit;
8990
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008991 ecjpake_do_round( alg, primitive_arg, &server, &client,
8992 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008993
8994exit:
8995 psa_destroy_key( key );
8996 psa_pake_abort( &server );
8997 psa_pake_abort( &client );
8998 PSA_DONE( );
8999}
9000/* END_CASE */
9001
9002/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009003void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02009004 int derive_alg_arg, data_t *pw_data,
Przemek Stekielcd356c32022-11-20 19:05:20 +01009005 int client_input_first, int destroy_key )
Neil Armstrongd597bc72022-05-25 11:28:39 +02009006{
9007 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9008 psa_pake_operation_t server = psa_pake_operation_init();
9009 psa_pake_operation_t client = psa_pake_operation_init();
9010 psa_algorithm_t alg = alg_arg;
9011 psa_algorithm_t hash_alg = hash_arg;
9012 psa_algorithm_t derive_alg = derive_alg_arg;
9013 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9015 psa_key_derivation_operation_t server_derive =
9016 PSA_KEY_DERIVATION_OPERATION_INIT;
9017 psa_key_derivation_operation_t client_derive =
9018 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009019
9020 PSA_INIT( );
9021
Neil Armstrongd597bc72022-05-25 11:28:39 +02009022 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9023 psa_set_key_algorithm( &attributes, alg );
9024 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9025 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9026 &key ) );
9027
9028 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9029 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9030 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9031
Neil Armstrong1e855602022-06-15 11:32:11 +02009032 /* Get shared key */
9033 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
9034 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
9035
9036 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
9037 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
9038 {
9039 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
9040 PSA_KEY_DERIVATION_INPUT_SEED,
9041 (const uint8_t*) "", 0) );
9042 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
9043 PSA_KEY_DERIVATION_INPUT_SEED,
9044 (const uint8_t*) "", 0) );
9045 }
9046
Neil Armstrongd597bc72022-05-25 11:28:39 +02009047 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9048 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9049
9050 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9051 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9052
9053 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9054 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9055
Przemek Stekielcd356c32022-11-20 19:05:20 +01009056 if( destroy_key == 1 )
9057 psa_destroy_key( key );
9058
Neil Armstrong1e855602022-06-15 11:32:11 +02009059 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9060 PSA_ERROR_BAD_STATE );
9061 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9062 PSA_ERROR_BAD_STATE );
9063
Neil Armstrongf983caf2022-06-15 15:27:48 +02009064 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009065 ecjpake_do_round( alg, primitive_arg, &server, &client,
9066 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009067
Neil Armstrong1e855602022-06-15 11:32:11 +02009068 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9069 PSA_ERROR_BAD_STATE );
9070 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9071 PSA_ERROR_BAD_STATE );
9072
Neil Armstrongf983caf2022-06-15 15:27:48 +02009073 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009074 ecjpake_do_round( alg, primitive_arg, &server, &client,
9075 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009076
Neil Armstrongd597bc72022-05-25 11:28:39 +02009077 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9078 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9079
9080exit:
9081 psa_key_derivation_abort( &server_derive );
9082 psa_key_derivation_abort( &client_derive );
9083 psa_destroy_key( key );
9084 psa_pake_abort( &server );
9085 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009086 PSA_DONE( );
9087}
9088/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009089
9090/* BEGIN_CASE */
9091void ecjpake_size_macros( )
9092{
9093 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9094 const size_t bits = 256;
9095 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9096 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9097 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9098 PSA_ECC_FAMILY_SECP_R1 );
9099
9100 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9101 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9102 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9103 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9104 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9105 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9106 /* The output for ZK_PROOF is the same bitsize as the curve */
9107 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9108 PSA_BITS_TO_BYTES( bits ) );
9109
9110 /* Input sizes are the same as output sizes */
9111 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9112 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9113 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9114 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9115 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9116 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9117
9118 /* These inequalities will always hold even when other PAKEs are added */
9119 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9120 PSA_PAKE_OUTPUT_MAX_SIZE );
9121 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9122 PSA_PAKE_OUTPUT_MAX_SIZE );
9123 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9124 PSA_PAKE_OUTPUT_MAX_SIZE );
9125 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9126 PSA_PAKE_INPUT_MAX_SIZE );
9127 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9128 PSA_PAKE_INPUT_MAX_SIZE );
9129 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9130 PSA_PAKE_INPUT_MAX_SIZE );
9131}
9132/* END_CASE */