blob: ad1ec8c681c8e937f7aad30f4ac30ff86230fd93 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020024#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010025
Gilles Peskine4023c012021-05-27 13:21:20 +020026/* If this comes up, it's a bug in the test code or in the test data. */
27#define UNUSED 0xdeadbeef
28
Dave Rodgman647791d2021-06-23 12:49:59 +010029/* Assert that an operation is (not) active.
30 * This serves as a proxy for checking if the operation is aborted. */
31#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
32#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010033
34/** An invalid export length that will never be set by psa_export_key(). */
35static const size_t INVALID_EXPORT_LENGTH = ~0U;
36
Gilles Peskinea7aa4422018-08-14 15:17:54 +020037/** Test if a buffer contains a constant byte value.
38 *
39 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 *
41 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020042 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 * \param size Size of the buffer in bytes.
44 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020045 * \return 1 if the buffer is all-bits-zero.
46 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020047 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020048static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020049{
50 size_t i;
51 for( i = 0; i < size; i++ )
52 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020053 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020054 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020055 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020056 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020057}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010058#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020059/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
60static int asn1_write_10x( unsigned char **p,
61 unsigned char *start,
62 size_t bits,
63 unsigned char x )
64{
65 int ret;
66 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020067 if( bits == 0 )
68 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
69 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020070 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030071 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020072 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
73 *p -= len;
74 ( *p )[len-1] = x;
75 if( bits % 8 == 0 )
76 ( *p )[1] |= 1;
77 else
78 ( *p )[0] |= 1 << ( bits % 8 );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
80 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
81 MBEDTLS_ASN1_INTEGER ) );
82 return( len );
83}
84
85static int construct_fake_rsa_key( unsigned char *buffer,
86 size_t buffer_size,
87 unsigned char **p,
88 size_t bits,
89 int keypair )
90{
91 size_t half_bits = ( bits + 1 ) / 2;
92 int ret;
93 int len = 0;
94 /* Construct something that looks like a DER encoding of
95 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
96 * RSAPrivateKey ::= SEQUENCE {
97 * version Version,
98 * modulus INTEGER, -- n
99 * publicExponent INTEGER, -- e
100 * privateExponent INTEGER, -- d
101 * prime1 INTEGER, -- p
102 * prime2 INTEGER, -- q
103 * exponent1 INTEGER, -- d mod (p-1)
104 * exponent2 INTEGER, -- d mod (q-1)
105 * coefficient INTEGER, -- (inverse of q) mod p
106 * otherPrimeInfos OtherPrimeInfos OPTIONAL
107 * }
108 * Or, for a public key, the same structure with only
109 * version, modulus and publicExponent.
110 */
111 *p = buffer + buffer_size;
112 if( keypair )
113 {
114 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
115 asn1_write_10x( p, buffer, half_bits, 1 ) );
116 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
117 asn1_write_10x( p, buffer, half_bits, 1 ) );
118 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
119 asn1_write_10x( p, buffer, half_bits, 1 ) );
120 MBEDTLS_ASN1_CHK_ADD( len, /* q */
121 asn1_write_10x( p, buffer, half_bits, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
123 asn1_write_10x( p, buffer, half_bits, 3 ) );
124 MBEDTLS_ASN1_CHK_ADD( len, /* d */
125 asn1_write_10x( p, buffer, bits, 1 ) );
126 }
127 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
128 asn1_write_10x( p, buffer, 17, 1 ) );
129 MBEDTLS_ASN1_CHK_ADD( len, /* n */
130 asn1_write_10x( p, buffer, bits, 1 ) );
131 if( keypair )
132 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
133 mbedtls_asn1_write_int( p, buffer, 0 ) );
134 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
135 {
136 const unsigned char tag =
137 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
138 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
139 }
140 return( len );
141}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100142#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200143
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100144int exercise_mac_setup( psa_key_type_t key_type,
145 const unsigned char *key_bytes,
146 size_t key_length,
147 psa_algorithm_t alg,
148 psa_mac_operation_t *operation,
149 psa_status_t *status )
150{
Ronald Cron5425a212020-08-04 14:58:35 +0200151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200152 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100153
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100154 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200155 psa_set_key_algorithm( &attributes, alg );
156 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200157 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100158
Ronald Cron5425a212020-08-04 14:58:35 +0200159 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100160 /* Whether setup succeeded or failed, abort must succeed. */
161 PSA_ASSERT( psa_mac_abort( operation ) );
162 /* If setup failed, reproduce the failure, so that the caller can
163 * test the resulting state of the operation object. */
164 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100165 {
Ronald Cron5425a212020-08-04 14:58:35 +0200166 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100167 }
168
Ronald Cron5425a212020-08-04 14:58:35 +0200169 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100170 return( 1 );
171
172exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200173 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100174 return( 0 );
175}
176
177int exercise_cipher_setup( psa_key_type_t key_type,
178 const unsigned char *key_bytes,
179 size_t key_length,
180 psa_algorithm_t alg,
181 psa_cipher_operation_t *operation,
182 psa_status_t *status )
183{
Ronald Cron5425a212020-08-04 14:58:35 +0200184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100186
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
188 psa_set_key_algorithm( &attributes, alg );
189 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200190 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191
Ronald Cron5425a212020-08-04 14:58:35 +0200192 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100193 /* Whether setup succeeded or failed, abort must succeed. */
194 PSA_ASSERT( psa_cipher_abort( operation ) );
195 /* If setup failed, reproduce the failure, so that the caller can
196 * test the resulting state of the operation object. */
197 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100198 {
Ronald Cron5425a212020-08-04 14:58:35 +0200199 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100200 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100201 }
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100204 return( 1 );
205
206exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200207 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100208 return( 0 );
209}
210
Ronald Cron5425a212020-08-04 14:58:35 +0200211static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212{
213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200214 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200215 uint8_t buffer[1];
216 size_t length;
217 int ok = 0;
218
Ronald Cronecfb2372020-07-23 17:13:42 +0200219 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
221 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
222 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200223 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000224 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200225 TEST_EQUAL(
226 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
227 TEST_EQUAL(
228 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200229 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
233 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
234
Ronald Cron5425a212020-08-04 14:58:35 +0200235 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000236 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200237 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200238 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000239 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241 ok = 1;
242
243exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100244 /*
245 * Key attributes may have been returned by psa_get_key_attributes()
246 * thus reset them as required.
247 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200248 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100249
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200250 return( ok );
251}
252
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200253/* Assert that a key isn't reported as having a slot number. */
254#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
255#define ASSERT_NO_SLOT_NUMBER( attributes ) \
256 do \
257 { \
258 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
259 TEST_EQUAL( psa_get_key_slot_number( \
260 attributes, \
261 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
262 PSA_ERROR_INVALID_ARGUMENT ); \
263 } \
264 while( 0 )
265#else /* MBEDTLS_PSA_CRYPTO_SE_C */
266#define ASSERT_NO_SLOT_NUMBER( attributes ) \
267 ( (void) 0 )
268#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
269
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100270/* An overapproximation of the amount of storage needed for a key of the
271 * given type and with the given content. The API doesn't make it easy
272 * to find a good value for the size. The current implementation doesn't
273 * care about the value anyway. */
274#define KEY_BITS_FROM_DATA( type, data ) \
275 ( data )->len
276
Darryl Green0c6575a2018-11-07 16:05:30 +0000277typedef enum {
278 IMPORT_KEY = 0,
279 GENERATE_KEY = 1,
280 DERIVE_KEY = 2
281} generate_method;
282
Paul Elliott33746aa2021-09-15 16:40:40 +0100283typedef enum
284{
285 DO_NOT_SET_LENGTHS = 0,
286 SET_LENGTHS_BEFORE_NONCE = 1,
287 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100288} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100289
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100290typedef enum
291{
292 USE_NULL_TAG = 0,
293 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100294} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100295
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100296/*!
297 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100298 * \param key_type_arg Type of key passed in
299 * \param key_data The encryption / decryption key data
300 * \param alg_arg The type of algorithm used
301 * \param nonce Nonce data
302 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100303 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100304 * feed additional data in to be encrypted /
305 * decrypted. If -1, no chunking.
306 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100307 * \param data_part_len_arg If not -1, the length of chunks to feed
308 * the data in to be encrypted / decrypted. If
309 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100310 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100311 * expected here, this controls whether or not
312 * to set lengths, and in what order with
313 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100315 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100316 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100317 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319 */
320static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
321 int alg_arg,
322 data_t *nonce,
323 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100324 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100326 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100327 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100328 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100329 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100330 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100331{
332 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
333 psa_key_type_t key_type = key_type_arg;
334 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100335 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100336 unsigned char *output_data = NULL;
337 unsigned char *part_data = NULL;
338 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100340 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100341 size_t output_size = 0;
342 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100343 size_t output_length = 0;
344 size_t key_bits = 0;
345 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100346 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100347 size_t part_length = 0;
348 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100349 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100350 size_t ad_part_len = 0;
351 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100352 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
354 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
355
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100356 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100357 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100358
Paul Elliottd3f82412021-06-16 16:52:21 +0100359 PSA_ASSERT( psa_crypto_init( ) );
360
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100361 if( is_encrypt )
362 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
363 else
364 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
365
Paul Elliottd3f82412021-06-16 16:52:21 +0100366 psa_set_key_algorithm( &attributes, alg );
367 psa_set_key_type( &attributes, key_type );
368
369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
370 &key ) );
371
372 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
373 key_bits = psa_get_key_bits( &attributes );
374
375 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
376
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100377 if( is_encrypt )
378 {
379 /* Tag gets written at end of buffer. */
380 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
381 ( input_data->len +
382 tag_length ) );
383 data_true_size = input_data->len;
384 }
385 else
386 {
387 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
388 ( input_data->len -
389 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100390
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100391 /* Do not want to attempt to decrypt tag. */
392 data_true_size = input_data->len - tag_length;
393 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100394
395 ASSERT_ALLOC( output_data, output_size );
396
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100397 if( is_encrypt )
398 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100399 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200400 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100401 }
402 else
403 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100404 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200405 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100406 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100407
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100408 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100409
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100410 if( is_encrypt )
411 status = psa_aead_encrypt_setup( &operation, key, alg );
412 else
413 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100414
415 /* If the operation is not supported, just skip and not fail in case the
416 * encryption involves a common limitation of cryptography hardwares and
417 * an alternative implementation. */
418 if( status == PSA_ERROR_NOT_SUPPORTED )
419 {
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
421 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
422 }
423
424 PSA_ASSERT( status );
425
Paul Elliott33746aa2021-09-15 16:40:40 +0100426 if( set_lengths_method == DO_NOT_SET_LENGTHS )
427 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
428 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100429 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100430 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
431 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100432 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
433 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100434 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100435 {
436 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
437
Paul Elliott33746aa2021-09-15 16:40:40 +0100438 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
439 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100440 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100441
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100442 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100443 {
444 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100445 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100446
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100447 for( part_offset = 0, part_count = 0;
448 part_offset < additional_data->len;
449 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100450 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100451 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100452 {
Paul Elliott329d5382021-07-22 17:10:45 +0100453 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100454 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100455 else if( additional_data->len - part_offset < ad_part_len )
456 {
457 part_length = additional_data->len - part_offset;
458 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100459 else
460 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100461 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100462 }
463
464 PSA_ASSERT( psa_aead_update_ad( &operation,
465 additional_data->x + part_offset,
466 part_length ) );
467
Paul Elliottd3f82412021-06-16 16:52:21 +0100468 }
469 }
470 else
471 {
472 /* Pass additional data in one go. */
473 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
474 additional_data->len ) );
475 }
476
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100477 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100478 {
479 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100480 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100481 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100482 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100483
484 ASSERT_ALLOC( part_data, part_data_size );
485
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100486 for( part_offset = 0, part_count = 0;
487 part_offset < data_true_size;
488 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100489 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100490 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100491 {
Paul Elliott329d5382021-07-22 17:10:45 +0100492 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100493 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100494 else if( ( data_true_size - part_offset ) < data_part_len )
495 {
496 part_length = ( data_true_size - part_offset );
497 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100498 else
499 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100500 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100501 }
502
503 PSA_ASSERT( psa_aead_update( &operation,
504 ( input_data->x + part_offset ),
505 part_length, part_data,
506 part_data_size,
507 &output_part_length ) );
508
509 if( output_data && output_part_length )
510 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100511 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100512 output_part_length );
513 }
514
Paul Elliottd3f82412021-06-16 16:52:21 +0100515 output_length += output_part_length;
516 }
517 }
518 else
519 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100520 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100521 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100522 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100523 output_size, &output_length ) );
524 }
525
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100526 if( is_encrypt )
527 PSA_ASSERT( psa_aead_finish( &operation, final_data,
528 final_output_size,
529 &output_part_length,
530 tag_buffer, tag_length,
531 &tag_size ) );
532 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100533 {
Paul Elliott9961a662021-09-17 19:19:02 +0100534 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100535 final_output_size,
536 &output_part_length,
537 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100538 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100539 }
540
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100541 if( output_data && output_part_length )
542 memcpy( ( output_data + output_length ), final_data,
543 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100544
545 output_length += output_part_length;
546
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100547
548 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
549 * should be exact.*/
550 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100551 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100552 TEST_EQUAL( tag_length, tag_size );
553
554 if( output_data && tag_length )
555 memcpy( ( output_data + output_length ), tag_buffer,
556 tag_length );
557
558 output_length += tag_length;
559
560 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200561 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
562 input_data->len ) );
563 TEST_LE_U( output_length,
564 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100565 }
566 else
567 {
568 TEST_EQUAL( output_length,
569 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
570 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200571 TEST_LE_U( output_length,
572 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100573 }
574
Paul Elliottd3f82412021-06-16 16:52:21 +0100575
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100576 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100577 output_data, output_length );
578
Paul Elliottd3f82412021-06-16 16:52:21 +0100579
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100580 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100581
582exit:
583 psa_destroy_key( key );
584 psa_aead_abort( &operation );
585 mbedtls_free( output_data );
586 mbedtls_free( part_data );
587 mbedtls_free( final_data );
588 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100589
590 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100591}
592
Neil Armstrong4766f992022-02-28 16:23:59 +0100593/*!
594 * \brief Internal Function for MAC multipart tests.
595 * \param key_type_arg Type of key passed in
596 * \param key_data The encryption / decryption key data
597 * \param alg_arg The type of algorithm used
598 * \param input_data Data to encrypt / decrypt
599 * \param data_part_len_arg If not -1, the length of chunks to feed
600 * the data in to be encrypted / decrypted. If
601 * -1, no chunking
602 * \param expected_output Expected output
603 * \param is_verify If non-zero this is an verify operation.
604 * \param do_zero_parts If non-zero, interleave zero length chunks
605 * with normal length chunks.
606 * \return int Zero on failure, non-zero on success.
607 */
608static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
609 int alg_arg,
610 data_t *input_data,
611 int data_part_len_arg,
612 data_t *expected_output,
613 int is_verify,
614 int do_zero_parts )
615{
616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
617 psa_key_type_t key_type = key_type_arg;
618 psa_algorithm_t alg = alg_arg;
619 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
620 unsigned char mac[PSA_MAC_MAX_SIZE];
621 size_t part_offset = 0;
622 size_t part_length = 0;
623 size_t data_part_len = 0;
624 size_t mac_len = 0;
625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
626 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
627
628 int test_ok = 0;
629 size_t part_count = 0;
630
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100631 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100632
633 if( is_verify )
634 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
635 else
636 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
637
638 psa_set_key_algorithm( &attributes, alg );
639 psa_set_key_type( &attributes, key_type );
640
641 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
642 &key ) );
643
644 if( is_verify )
645 status = psa_mac_verify_setup( &operation, key, alg );
646 else
647 status = psa_mac_sign_setup( &operation, key, alg );
648
649 PSA_ASSERT( status );
650
651 if( data_part_len_arg != -1 )
652 {
653 /* Pass data in parts */
654 data_part_len = ( size_t ) data_part_len_arg;
655
656 for( part_offset = 0, part_count = 0;
657 part_offset < input_data->len;
658 part_offset += part_length, part_count++ )
659 {
660 if( do_zero_parts && ( part_count & 0x01 ) )
661 {
662 part_length = 0;
663 }
664 else if( ( input_data->len - part_offset ) < data_part_len )
665 {
666 part_length = ( input_data->len - part_offset );
667 }
668 else
669 {
670 part_length = data_part_len;
671 }
672
673 PSA_ASSERT( psa_mac_update( &operation,
674 ( input_data->x + part_offset ),
675 part_length ) );
676 }
677 }
678 else
679 {
680 /* Pass all data in one go. */
681 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
682 input_data->len ) );
683 }
684
685 if( is_verify )
686 {
687 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
688 expected_output->len ) );
689 }
690 else
691 {
692 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
693 PSA_MAC_MAX_SIZE, &mac_len ) );
694
695 ASSERT_COMPARE( expected_output->x, expected_output->len,
696 mac, mac_len );
697 }
698
699 test_ok = 1;
700
701exit:
702 psa_destroy_key( key );
703 psa_mac_abort( &operation );
704 PSA_DONE( );
705
706 return( test_ok );
707}
708
Neil Armstrong75673ab2022-06-15 17:39:01 +0200709#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200710static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
711 psa_pake_operation_t *server,
712 psa_pake_operation_t *client,
713 int client_input_first,
714 int round, int inject_error )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200715{
716 unsigned char *buffer0 = NULL, *buffer1 = NULL;
717 size_t buffer_length = (
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
719 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
720 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200721 /* The output should be exactly this size according to the spec */
722 const size_t expected_size_key_share =
723 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
724 /* The output should be exactly this size according to the spec */
725 const size_t expected_size_zk_public =
726 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
727 /* The output can be smaller: the spec allows stripping leading zeroes */
728 const size_t max_expected_size_zk_proof =
729 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200730 size_t buffer0_off = 0;
731 size_t buffer1_off = 0;
732 size_t s_g1_len, s_g2_len, s_a_len;
733 size_t s_g1_off, s_g2_off, s_a_off;
734 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
735 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
736 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
737 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
738 size_t c_g1_len, c_g2_len, c_a_len;
739 size_t c_g1_off, c_g2_off, c_a_off;
740 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
741 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
742 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
743 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
744 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200745 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200746
747 ASSERT_ALLOC( buffer0, buffer_length );
748 ASSERT_ALLOC( buffer1, buffer_length );
749
750 switch( round )
751 {
752 case 1:
753 /* Server first round Output */
754 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
755 buffer0 + buffer0_off,
756 512 - buffer0_off, &s_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200757 TEST_EQUAL( s_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200758 s_g1_off = buffer0_off;
759 buffer0_off += s_g1_len;
760 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
761 buffer0 + buffer0_off,
762 512 - buffer0_off, &s_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200763 TEST_EQUAL( s_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200764 s_x1_pk_off = buffer0_off;
765 buffer0_off += s_x1_pk_len;
766 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
767 buffer0 + buffer0_off,
768 512 - buffer0_off, &s_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200769 TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200770 s_x1_pr_off = buffer0_off;
771 buffer0_off += s_x1_pr_len;
772 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
773 buffer0 + buffer0_off,
774 512 - buffer0_off, &s_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200775 TEST_EQUAL( s_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200776 s_g2_off = buffer0_off;
777 buffer0_off += s_g2_len;
778 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
779 buffer0 + buffer0_off,
780 512 - buffer0_off, &s_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200781 TEST_EQUAL( s_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200782 s_x2_pk_off = buffer0_off;
783 buffer0_off += s_x2_pk_len;
784 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
785 buffer0 + buffer0_off,
786 512 - buffer0_off, &s_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200787 TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200788 s_x2_pr_off = buffer0_off;
789 buffer0_off += s_x2_pr_len;
790
791 if( inject_error == 1 )
792 {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500793 buffer0[s_x1_pr_off + 8] ^= 1;
794 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200795 expected_status = PSA_ERROR_DATA_INVALID;
796 }
797
Neil Armstrong51009d72022-09-05 17:59:54 +0200798 /*
799 * When injecting errors in inputs, the implementation is
800 * free to detect it right away of with a delay.
801 * This permits delaying the error until the end of the input
802 * sequence, if no error appears then, this will be treated
803 * as an error.
804 */
805
Neil Armstrongf983caf2022-06-15 15:27:48 +0200806 if( client_input_first == 1 )
807 {
808 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200809 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
810 buffer0 + s_g1_off, s_g1_len );
811 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200812 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200813 TEST_EQUAL( status, expected_status );
814 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200815 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200816 else
817 {
818 TEST_EQUAL( status, PSA_SUCCESS );
819 }
820
821 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
822 buffer0 + s_x1_pk_off,
823 s_x1_pk_len );
824 if( inject_error == 1 && status != PSA_SUCCESS )
825 {
826 TEST_EQUAL( status, expected_status );
827 break;
828 }
829 else
830 {
831 TEST_EQUAL( status, PSA_SUCCESS );
832 }
833
834 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
835 buffer0 + s_x1_pr_off,
836 s_x1_pr_len );
837 if( inject_error == 1 && status != PSA_SUCCESS )
838 {
839 TEST_EQUAL( status, expected_status );
840 break;
841 }
842 else
843 {
844 TEST_EQUAL( status, PSA_SUCCESS );
845 }
846
847 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
848 buffer0 + s_g2_off,
849 s_g2_len );
850 if( inject_error == 1 && status != PSA_SUCCESS )
851 {
852 TEST_EQUAL( status, expected_status );
853 break;
854 }
855 else
856 {
857 TEST_EQUAL( status, PSA_SUCCESS );
858 }
859
860 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
861 buffer0 + s_x2_pk_off,
862 s_x2_pk_len );
863 if( inject_error == 1 && status != PSA_SUCCESS )
864 {
865 TEST_EQUAL( status, expected_status );
866 break;
867 }
868 else
869 {
870 TEST_EQUAL( status, PSA_SUCCESS );
871 }
872
873 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
874 buffer0 + s_x2_pr_off,
875 s_x2_pr_len );
876 if( inject_error == 1 && status != PSA_SUCCESS )
877 {
878 TEST_EQUAL( status, expected_status );
879 break;
880 }
881 else
882 {
883 TEST_EQUAL( status, PSA_SUCCESS );
884 }
885
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200886 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200887 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200888 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200889 }
890
891 /* Client first round Output */
892 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
893 buffer1 + buffer1_off,
894 512 - buffer1_off, &c_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200895 TEST_EQUAL( c_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200896 c_g1_off = buffer1_off;
897 buffer1_off += c_g1_len;
898 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
899 buffer1 + buffer1_off,
900 512 - buffer1_off, &c_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200901 TEST_EQUAL( c_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200902 c_x1_pk_off = buffer1_off;
903 buffer1_off += c_x1_pk_len;
904 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
905 buffer1 + buffer1_off,
906 512 - buffer1_off, &c_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200907 TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200908 c_x1_pr_off = buffer1_off;
909 buffer1_off += c_x1_pr_len;
910 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
911 buffer1 + buffer1_off,
912 512 - buffer1_off, &c_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200913 TEST_EQUAL( c_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200914 c_g2_off = buffer1_off;
915 buffer1_off += c_g2_len;
916 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
917 buffer1 + buffer1_off,
918 512 - buffer1_off, &c_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200919 TEST_EQUAL( c_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200920 c_x2_pk_off = buffer1_off;
921 buffer1_off += c_x2_pk_len;
922 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
923 buffer1 + buffer1_off,
924 512 - buffer1_off, &c_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200925 TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200926 c_x2_pr_off = buffer1_off;
927 buffer1_off += c_x2_pr_len;
928
929 if( client_input_first == 0 )
930 {
931 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200932 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
933 buffer0 + s_g1_off, s_g1_len );
934 if( inject_error == 1 && status != PSA_SUCCESS )
935 {
936 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200937 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200938 }
939 else
940 {
941 TEST_EQUAL( status, PSA_SUCCESS );
942 }
943
944 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
945 buffer0 + s_x1_pk_off,
946 s_x1_pk_len );
947 if( inject_error == 1 && status != PSA_SUCCESS )
948 {
949 TEST_EQUAL( status, expected_status );
950 break;
951 }
952 else
953 {
954 TEST_EQUAL( status, PSA_SUCCESS );
955 }
956
957 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
958 buffer0 + s_x1_pr_off,
959 s_x1_pr_len );
960 if( inject_error == 1 && status != PSA_SUCCESS )
961 {
962 TEST_EQUAL( status, expected_status );
963 break;
964 }
965 else
966 {
967 TEST_EQUAL( status, PSA_SUCCESS );
968 }
969
970 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
971 buffer0 + s_g2_off,
972 s_g2_len );
973 if( inject_error == 1 && status != PSA_SUCCESS )
974 {
975 TEST_EQUAL( status, expected_status );
976 break;
977 }
978 else
979 {
980 TEST_EQUAL( status, PSA_SUCCESS );
981 }
982
983 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
984 buffer0 + s_x2_pk_off,
985 s_x2_pk_len );
986 if( inject_error == 1 && status != PSA_SUCCESS )
987 {
988 TEST_EQUAL( status, expected_status );
989 break;
990 }
991 else
992 {
993 TEST_EQUAL( status, PSA_SUCCESS );
994 }
995
996 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
997 buffer0 + s_x2_pr_off,
998 s_x2_pr_len );
999 if( inject_error == 1 && status != PSA_SUCCESS )
1000 {
1001 TEST_EQUAL( status, expected_status );
1002 break;
1003 }
1004 else
1005 {
1006 TEST_EQUAL( status, PSA_SUCCESS );
1007 }
1008
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001009 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001010 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001011 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001012 }
1013
1014 if( inject_error == 2 )
1015 {
Andrzej Kurekc0182042022-11-08 08:12:56 -05001016 buffer1[c_x1_pr_off + 12] ^= 1;
1017 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001018 expected_status = PSA_ERROR_DATA_INVALID;
1019 }
1020
1021 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001022 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1023 buffer1 + c_g1_off, c_g1_len );
1024 if( inject_error == 2 && status != PSA_SUCCESS )
1025 {
1026 TEST_EQUAL( status, expected_status );
1027 break;
1028 }
1029 else
1030 {
1031 TEST_EQUAL( status, PSA_SUCCESS );
1032 }
1033
1034 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1035 buffer1 + c_x1_pk_off, c_x1_pk_len );
1036 if( inject_error == 2 && status != PSA_SUCCESS )
1037 {
1038 TEST_EQUAL( status, expected_status );
1039 break;
1040 }
1041 else
1042 {
1043 TEST_EQUAL( status, PSA_SUCCESS );
1044 }
1045
1046 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1047 buffer1 + c_x1_pr_off, c_x1_pr_len );
1048 if( inject_error == 2 && status != PSA_SUCCESS )
1049 {
1050 TEST_EQUAL( status, expected_status );
1051 break;
1052 }
1053 else
1054 {
1055 TEST_EQUAL( status, PSA_SUCCESS );
1056 }
1057
1058 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1059 buffer1 + c_g2_off, c_g2_len );
1060 if( inject_error == 2 && status != PSA_SUCCESS )
1061 {
1062 TEST_EQUAL( status, expected_status );
1063 break;
1064 }
1065 else
1066 {
1067 TEST_EQUAL( status, PSA_SUCCESS );
1068 }
1069
1070 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1071 buffer1 + c_x2_pk_off, c_x2_pk_len );
1072 if( inject_error == 2 && status != PSA_SUCCESS )
1073 {
1074 TEST_EQUAL( status, expected_status );
1075 break;
1076 }
1077 else
1078 {
1079 TEST_EQUAL( status, PSA_SUCCESS );
1080 }
1081
1082 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1083 buffer1 + c_x2_pr_off, c_x2_pr_len );
1084 if( inject_error == 2 && status != PSA_SUCCESS )
1085 {
1086 TEST_EQUAL( status, expected_status );
1087 break;
1088 }
1089 else
1090 {
1091 TEST_EQUAL( status, PSA_SUCCESS );
1092 }
1093
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001094 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001095 if( inject_error == 2 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001096 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001097
1098 break;
1099
1100 case 2:
1101 /* Server second round Output */
1102 buffer0_off = 0;
1103
1104 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1105 buffer0 + buffer0_off,
1106 512 - buffer0_off, &s_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001107 TEST_EQUAL( s_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001108 s_a_off = buffer0_off;
1109 buffer0_off += s_a_len;
1110 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1111 buffer0 + buffer0_off,
1112 512 - buffer0_off, &s_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001113 TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001114 s_x2s_pk_off = buffer0_off;
1115 buffer0_off += s_x2s_pk_len;
1116 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1117 buffer0 + buffer0_off,
1118 512 - buffer0_off, &s_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001119 TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001120 s_x2s_pr_off = buffer0_off;
1121 buffer0_off += s_x2s_pr_len;
1122
1123 if( inject_error == 3 )
1124 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001125 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001126 expected_status = PSA_ERROR_DATA_INVALID;
1127 }
1128
1129 if( client_input_first == 1 )
1130 {
1131 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001132 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1133 buffer0 + s_a_off, s_a_len );
1134 if( inject_error == 3 && status != PSA_SUCCESS )
1135 {
1136 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001137 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001138 }
1139 else
1140 {
1141 TEST_EQUAL( status, PSA_SUCCESS );
1142 }
1143
1144 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1145 buffer0 + s_x2s_pk_off,
1146 s_x2s_pk_len );
1147 if( inject_error == 3 && status != PSA_SUCCESS )
1148 {
1149 TEST_EQUAL( status, expected_status );
1150 break;
1151 }
1152 else
1153 {
1154 TEST_EQUAL( status, PSA_SUCCESS );
1155 }
1156
1157 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1158 buffer0 + s_x2s_pr_off,
1159 s_x2s_pr_len );
1160 if( inject_error == 3 && status != PSA_SUCCESS )
1161 {
1162 TEST_EQUAL( status, expected_status );
1163 break;
1164 }
1165 else
1166 {
1167 TEST_EQUAL( status, PSA_SUCCESS );
1168 }
1169
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001170 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001171 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001172 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001173 }
1174
1175 /* Client second round Output */
1176 buffer1_off = 0;
1177
1178 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1179 buffer1 + buffer1_off,
1180 512 - buffer1_off, &c_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001181 TEST_EQUAL( c_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001182 c_a_off = buffer1_off;
1183 buffer1_off += c_a_len;
1184 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1185 buffer1 + buffer1_off,
1186 512 - buffer1_off, &c_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001187 TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001188 c_x2s_pk_off = buffer1_off;
1189 buffer1_off += c_x2s_pk_len;
1190 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1191 buffer1 + buffer1_off,
1192 512 - buffer1_off, &c_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001193 TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001194 c_x2s_pr_off = buffer1_off;
1195 buffer1_off += c_x2s_pr_len;
1196
1197 if( client_input_first == 0 )
1198 {
1199 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001200 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1201 buffer0 + s_a_off, s_a_len );
1202 if( inject_error == 3 && status != PSA_SUCCESS )
1203 {
1204 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001205 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001206 }
1207 else
1208 {
1209 TEST_EQUAL( status, PSA_SUCCESS );
1210 }
1211
1212 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1213 buffer0 + s_x2s_pk_off,
1214 s_x2s_pk_len );
1215 if( inject_error == 3 && status != PSA_SUCCESS )
1216 {
1217 TEST_EQUAL( status, expected_status );
1218 break;
1219 }
1220 else
1221 {
1222 TEST_EQUAL( status, PSA_SUCCESS );
1223 }
1224
1225 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1226 buffer0 + s_x2s_pr_off,
1227 s_x2s_pr_len );
1228 if( inject_error == 3 && status != PSA_SUCCESS )
1229 {
1230 TEST_EQUAL( status, expected_status );
1231 break;
1232 }
1233 else
1234 {
1235 TEST_EQUAL( status, PSA_SUCCESS );
1236 }
1237
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001238 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001239 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001240 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001241 }
1242
1243 if( inject_error == 4 )
1244 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001245 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001246 expected_status = PSA_ERROR_DATA_INVALID;
1247 }
1248
1249 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001250 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1251 buffer1 + c_a_off, c_a_len );
1252 if( inject_error == 4 && status != PSA_SUCCESS )
1253 {
1254 TEST_EQUAL( status, expected_status );
1255 break;
1256 }
1257 else
1258 {
1259 TEST_EQUAL( status, PSA_SUCCESS );
1260 }
1261
1262 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1263 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1264 if( inject_error == 4 && status != PSA_SUCCESS )
1265 {
1266 TEST_EQUAL( status, expected_status );
1267 break;
1268 }
1269 else
1270 {
1271 TEST_EQUAL( status, PSA_SUCCESS );
1272 }
1273
1274 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1275 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1276 if( inject_error == 4 && status != PSA_SUCCESS )
1277 {
1278 TEST_EQUAL( status, expected_status );
1279 break;
1280 }
1281 else
1282 {
1283 TEST_EQUAL( status, PSA_SUCCESS );
1284 }
1285
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001286 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001287 if( inject_error == 4 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001288 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001289
1290 break;
1291
1292 }
1293
Neil Armstrongf983caf2022-06-15 15:27:48 +02001294exit:
1295 mbedtls_free( buffer0 );
1296 mbedtls_free( buffer1 );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001297}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001298#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001299
Gilles Peskinee59236f2018-01-27 23:32:46 +01001300/* END_HEADER */
1301
1302/* BEGIN_DEPENDENCIES
1303 * depends_on:MBEDTLS_PSA_CRYPTO_C
1304 * END_DEPENDENCIES
1305 */
1306
1307/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001308void static_checks( )
1309{
1310 size_t max_truncated_mac_size =
1311 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1312
1313 /* Check that the length for a truncated MAC always fits in the algorithm
1314 * encoding. The shifted mask is the maximum truncated value. The
1315 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001316 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001317}
1318/* END_CASE */
1319
1320/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001321void import_with_policy( int type_arg,
1322 int usage_arg, int alg_arg,
1323 int expected_status_arg )
1324{
1325 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1326 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001327 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001328 psa_key_type_t type = type_arg;
1329 psa_key_usage_t usage = usage_arg;
1330 psa_algorithm_t alg = alg_arg;
1331 psa_status_t expected_status = expected_status_arg;
1332 const uint8_t key_material[16] = {0};
1333 psa_status_t status;
1334
1335 PSA_ASSERT( psa_crypto_init( ) );
1336
1337 psa_set_key_type( &attributes, type );
1338 psa_set_key_usage_flags( &attributes, usage );
1339 psa_set_key_algorithm( &attributes, alg );
1340
1341 status = psa_import_key( &attributes,
1342 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001343 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001344 TEST_EQUAL( status, expected_status );
1345 if( status != PSA_SUCCESS )
1346 goto exit;
1347
Ronald Cron5425a212020-08-04 14:58:35 +02001348 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001349 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001350 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001351 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001352 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001353 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001354
Ronald Cron5425a212020-08-04 14:58:35 +02001355 PSA_ASSERT( psa_destroy_key( key ) );
1356 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001357
1358exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001359 /*
1360 * Key attributes may have been returned by psa_get_key_attributes()
1361 * thus reset them as required.
1362 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001363 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001364
1365 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001366 PSA_DONE( );
1367}
1368/* END_CASE */
1369
1370/* BEGIN_CASE */
1371void import_with_data( data_t *data, int type_arg,
1372 int attr_bits_arg,
1373 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374{
1375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1376 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001377 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001378 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001379 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001380 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001381 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382
Gilles Peskine8817f612018-12-18 00:18:46 +01001383 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384
Gilles Peskine4747d192019-04-17 15:05:45 +02001385 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001386 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001387
Ronald Cron5425a212020-08-04 14:58:35 +02001388 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001389 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001390 if( status != PSA_SUCCESS )
1391 goto exit;
1392
Ronald Cron5425a212020-08-04 14:58:35 +02001393 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001394 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001395 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001396 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001397 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001398
Ronald Cron5425a212020-08-04 14:58:35 +02001399 PSA_ASSERT( psa_destroy_key( key ) );
1400 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001401
1402exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001403 /*
1404 * Key attributes may have been returned by psa_get_key_attributes()
1405 * thus reset them as required.
1406 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001407 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001408
1409 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001410 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Gilles Peskine07510f52022-11-11 16:37:16 +01001415/* Construct and attempt to import a large unstructured key. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001416void import_large_key( int type_arg, int byte_size_arg,
1417 int expected_status_arg )
1418{
1419 psa_key_type_t type = type_arg;
1420 size_t byte_size = byte_size_arg;
1421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1422 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001423 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001424 psa_status_t status;
1425 uint8_t *buffer = NULL;
1426 size_t buffer_size = byte_size + 1;
1427 size_t n;
1428
Steven Cooreman69967ce2021-01-18 18:01:08 +01001429 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001430 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001431 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001432 memset( buffer, 'K', byte_size );
1433
1434 PSA_ASSERT( psa_crypto_init( ) );
1435
1436 /* Try importing the key */
1437 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1438 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001439 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001440 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001441 TEST_EQUAL( status, expected_status );
1442
1443 if( status == PSA_SUCCESS )
1444 {
Ronald Cron5425a212020-08-04 14:58:35 +02001445 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001446 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1447 TEST_EQUAL( psa_get_key_bits( &attributes ),
1448 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001449 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001450 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001451 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001452 for( n = 0; n < byte_size; n++ )
1453 TEST_EQUAL( buffer[n], 'K' );
1454 for( n = byte_size; n < buffer_size; n++ )
1455 TEST_EQUAL( buffer[n], 0 );
1456 }
1457
1458exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001459 /*
1460 * Key attributes may have been returned by psa_get_key_attributes()
1461 * thus reset them as required.
1462 */
1463 psa_reset_key_attributes( &attributes );
1464
Ronald Cron5425a212020-08-04 14:58:35 +02001465 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001466 PSA_DONE( );
1467 mbedtls_free( buffer );
1468}
1469/* END_CASE */
1470
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001471/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine07510f52022-11-11 16:37:16 +01001472/* Import an RSA key with a valid structure (but not valid numbers
1473 * inside, beyond having sensible size and parity). This is expected to
1474 * fail for large keys. */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001475void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1476{
Ronald Cron5425a212020-08-04 14:58:35 +02001477 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001478 size_t bits = bits_arg;
1479 psa_status_t expected_status = expected_status_arg;
1480 psa_status_t status;
1481 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001482 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001483 size_t buffer_size = /* Slight overapproximations */
1484 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001485 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001486 unsigned char *p;
1487 int ret;
1488 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001490
Gilles Peskine8817f612018-12-18 00:18:46 +01001491 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001492 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001493
1494 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1495 bits, keypair ) ) >= 0 );
1496 length = ret;
1497
1498 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001499 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001500 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001501 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001502
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001503 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001504 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001505
1506exit:
1507 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001508 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001513void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001514 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001515 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301516 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001517 int expected_bits,
1518 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001519 int expected_export_status_arg,
Gilles Peskine07510f52022-11-11 16:37:16 +01001520 /*whether reexport must give the original input exactly*/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001521 int canonical_input )
1522{
Ronald Cron5425a212020-08-04 14:58:35 +02001523 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001525 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001526 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301528 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001529 unsigned char *exported = NULL;
1530 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001532 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001533 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001535 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536
Moran Pekercb088e72018-07-17 17:36:59 +03001537 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001538 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001539 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001540 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001541 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542
Archana4d7ae1d2021-07-07 02:50:22 +05301543 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001544 psa_set_key_usage_flags( &attributes, usage_arg );
1545 psa_set_key_algorithm( &attributes, alg );
1546 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001547
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001548 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001549 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550
1551 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001552 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001553 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1554 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001555 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556
1557 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001558 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001559 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001560
1561 /* The exported length must be set by psa_export_key() to a value between 0
1562 * and export_size. On errors, the exported length must be 0. */
1563 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1564 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001565 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001566
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001567 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001568 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001569 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001570 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001571 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001573 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001574
Gilles Peskineea38a922021-02-13 00:05:16 +01001575 /* Run sanity checks on the exported key. For non-canonical inputs,
1576 * this validates the canonical representations. For canonical inputs,
1577 * this doesn't directly validate the implementation, but it still helps
1578 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301579 if( !psa_key_lifetime_is_external( lifetime ) )
1580 {
1581 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1582 goto exit;
1583 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001584
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001586 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587 else
1588 {
Ronald Cron5425a212020-08-04 14:58:35 +02001589 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001590 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001591 &key2 ) );
1592 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001593 reexported,
1594 export_size,
1595 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001596 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301597 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001598 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001599 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001600 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301601 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301602 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001603 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001604
1605destroy:
1606 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001607 PSA_ASSERT( psa_destroy_key( key ) );
1608 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609
1610exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001611 /*
1612 * Key attributes may have been returned by psa_get_key_attributes()
1613 * thus reset them as required.
1614 */
1615 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301616 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001617 mbedtls_free( exported );
1618 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001619 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001620}
1621/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001622
Moran Pekerf709f4a2018-06-06 17:26:04 +03001623/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001624void import_export_public_key( data_t *data,
Gilles Peskine07510f52022-11-11 16:37:16 +01001625 int type_arg, // key pair or public key
Gilles Peskine2d277862018-06-18 15:41:12 +02001626 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301627 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001628 int export_size_delta,
1629 int expected_export_status_arg,
1630 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001631{
Ronald Cron5425a212020-08-04 14:58:35 +02001632 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001633 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001634 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001635 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001636 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301637 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001638 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001639 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001640 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001642
Gilles Peskine8817f612018-12-18 00:18:46 +01001643 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001644
Archana4d7ae1d2021-07-07 02:50:22 +05301645 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1647 psa_set_key_algorithm( &attributes, alg );
1648 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001649
1650 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001651 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001652
Gilles Peskine49c25912018-10-29 15:15:31 +01001653 /* Export the public key */
1654 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001655 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001656 exported, export_size,
1657 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001658 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001659 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001660 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001661 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001662 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001663 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001664 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001665 TEST_LE_U( expected_public_key->len,
1666 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1667 TEST_LE_U( expected_public_key->len,
1668 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1669 TEST_LE_U( expected_public_key->len,
1670 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001671 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1672 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001673 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001674exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001675 /*
1676 * Key attributes may have been returned by psa_get_key_attributes()
1677 * thus reset them as required.
1678 */
1679 psa_reset_key_attributes( &attributes );
1680
itayzafrir3e02b3b2018-06-12 17:06:52 +03001681 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001682 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001683 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001684}
1685/* END_CASE */
1686
Gilles Peskine20035e32018-02-03 22:44:14 +01001687/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001688void import_and_exercise_key( data_t *data,
1689 int type_arg,
1690 int bits_arg,
1691 int alg_arg )
1692{
Ronald Cron5425a212020-08-04 14:58:35 +02001693 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001694 psa_key_type_t type = type_arg;
1695 size_t bits = bits_arg;
1696 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001697 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001699 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001700
Gilles Peskine8817f612018-12-18 00:18:46 +01001701 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001702
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 psa_set_key_usage_flags( &attributes, usage );
1704 psa_set_key_algorithm( &attributes, alg );
1705 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001706
1707 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001708 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001709
1710 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001711 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001712 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1713 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001714
1715 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001716 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001717 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001718
Ronald Cron5425a212020-08-04 14:58:35 +02001719 PSA_ASSERT( psa_destroy_key( key ) );
1720 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001721
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001722exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001723 /*
1724 * Key attributes may have been returned by psa_get_key_attributes()
1725 * thus reset them as required.
1726 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001727 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001728
1729 psa_reset_key_attributes( &attributes );
1730 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001731 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001732}
1733/* END_CASE */
1734
1735/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001736void effective_key_attributes( int type_arg, int expected_type_arg,
1737 int bits_arg, int expected_bits_arg,
1738 int usage_arg, int expected_usage_arg,
1739 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001740{
Ronald Cron5425a212020-08-04 14:58:35 +02001741 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001742 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001743 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001744 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001745 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001746 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001747 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001748 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001749 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001750 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001751
Gilles Peskine8817f612018-12-18 00:18:46 +01001752 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001753
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001754 psa_set_key_usage_flags( &attributes, usage );
1755 psa_set_key_algorithm( &attributes, alg );
1756 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001757 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001758
Ronald Cron5425a212020-08-04 14:58:35 +02001759 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001760 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001761
Ronald Cron5425a212020-08-04 14:58:35 +02001762 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001763 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1764 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1765 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1766 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001767
1768exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001769 /*
1770 * Key attributes may have been returned by psa_get_key_attributes()
1771 * thus reset them as required.
1772 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001773 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001774
1775 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001776 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001777}
1778/* END_CASE */
1779
1780/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001781void check_key_policy( int type_arg, int bits_arg,
1782 int usage_arg, int alg_arg )
1783{
1784 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001785 usage_arg,
1786 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001787 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001788 goto exit;
1789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001793void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001794{
1795 /* Test each valid way of initializing the object, except for `= {0}`, as
1796 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1797 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001798 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001799 psa_key_attributes_t func = psa_key_attributes_init( );
1800 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1801 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001802
1803 memset( &zero, 0, sizeof( zero ) );
1804
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001805 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1806 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1807 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001808
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001809 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1810 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1811 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1812
1813 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1814 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1815 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1816
1817 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1818 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1819 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1820
1821 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1822 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1823 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001824}
1825/* END_CASE */
1826
1827/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001828void mac_key_policy( int policy_usage_arg,
1829 int policy_alg_arg,
1830 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001831 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001832 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001833 int expected_status_sign_arg,
1834 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001835{
Ronald Cron5425a212020-08-04 14:58:35 +02001836 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001838 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001839 psa_key_type_t key_type = key_type_arg;
1840 psa_algorithm_t policy_alg = policy_alg_arg;
1841 psa_algorithm_t exercise_alg = exercise_alg_arg;
1842 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001843 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001844 psa_status_t expected_status_sign = expected_status_sign_arg;
1845 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001846 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001847
Gilles Peskine8817f612018-12-18 00:18:46 +01001848 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001849
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001850 psa_set_key_usage_flags( &attributes, policy_usage );
1851 psa_set_key_algorithm( &attributes, policy_alg );
1852 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001853
Gilles Peskine049c7532019-05-15 20:22:09 +02001854 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001855 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001856
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001857 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1858 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001859
Ronald Cron5425a212020-08-04 14:58:35 +02001860 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001861 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001862
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001863 /* Calculate the MAC, one-shot case. */
1864 uint8_t input[128] = {0};
1865 size_t mac_len;
1866 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1867 input, 128,
1868 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1869 expected_status_sign );
1870
Neil Armstrong3af9b972022-02-07 12:20:21 +01001871 /* Calculate the MAC, multi-part case. */
1872 PSA_ASSERT( psa_mac_abort( &operation ) );
1873 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1874 if( status == PSA_SUCCESS )
1875 {
1876 status = psa_mac_update( &operation, input, 128 );
1877 if( status == PSA_SUCCESS )
1878 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1879 &mac_len ),
1880 expected_status_sign );
1881 else
1882 TEST_EQUAL( status, expected_status_sign );
1883 }
1884 else
1885 {
1886 TEST_EQUAL( status, expected_status_sign );
1887 }
1888 PSA_ASSERT( psa_mac_abort( &operation ) );
1889
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001890 /* Verify correct MAC, one-shot case. */
1891 status = psa_mac_verify( key, exercise_alg, input, 128,
1892 mac, mac_len );
1893
1894 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1895 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001896 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001897 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001898
Neil Armstrong3af9b972022-02-07 12:20:21 +01001899 /* Verify correct MAC, multi-part case. */
1900 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1901 if( status == PSA_SUCCESS )
1902 {
1903 status = psa_mac_update( &operation, input, 128 );
1904 if( status == PSA_SUCCESS )
1905 {
1906 status = psa_mac_verify_finish( &operation, mac, mac_len );
1907 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1908 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1909 else
1910 TEST_EQUAL( status, expected_status_verify );
1911 }
1912 else
1913 {
1914 TEST_EQUAL( status, expected_status_verify );
1915 }
1916 }
1917 else
1918 {
1919 TEST_EQUAL( status, expected_status_verify );
1920 }
1921
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001923
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001925 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001926 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001927
1928exit:
1929 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001930 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001931 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932}
1933/* END_CASE */
1934
1935/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001936void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001937 int policy_alg,
1938 int key_type,
1939 data_t *key_data,
1940 int exercise_alg )
1941{
Ronald Cron5425a212020-08-04 14:58:35 +02001942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001943 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001944 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001945 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001946 size_t output_buffer_size = 0;
1947 size_t input_buffer_size = 0;
1948 size_t output_length = 0;
1949 uint8_t *output = NULL;
1950 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951 psa_status_t status;
1952
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001953 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1954 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1955 input_buffer_size );
1956
1957 ASSERT_ALLOC( input, input_buffer_size );
1958 ASSERT_ALLOC( output, output_buffer_size );
1959
Gilles Peskine8817f612018-12-18 00:18:46 +01001960 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001962 psa_set_key_usage_flags( &attributes, policy_usage );
1963 psa_set_key_algorithm( &attributes, policy_alg );
1964 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001965
Gilles Peskine049c7532019-05-15 20:22:09 +02001966 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001967 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001968
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001969 /* Check if no key usage flag implication is done */
1970 TEST_EQUAL( policy_usage,
1971 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001972
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001973 /* Encrypt check, one-shot */
1974 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1975 output, output_buffer_size,
1976 &output_length);
1977 if( policy_alg == exercise_alg &&
1978 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1979 PSA_ASSERT( status );
1980 else
1981 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1982
1983 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001984 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 if( policy_alg == exercise_alg &&
1986 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001987 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990 psa_cipher_abort( &operation );
1991
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001992 /* Decrypt check, one-shot */
1993 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1994 input, input_buffer_size,
1995 &output_length);
1996 if( policy_alg == exercise_alg &&
1997 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1998 PSA_ASSERT( status );
1999 else
2000 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2001
2002 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002003 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004 if( policy_alg == exercise_alg &&
2005 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002006 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002007 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002008 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009
2010exit:
2011 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002012 mbedtls_free( input );
2013 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002014 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002015 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016}
2017/* END_CASE */
2018
2019/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002020void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021 int policy_alg,
2022 int key_type,
2023 data_t *key_data,
2024 int nonce_length_arg,
2025 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002026 int exercise_alg,
2027 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002028{
Ronald Cron5425a212020-08-04 14:58:35 +02002029 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002031 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002032 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002034 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035 unsigned char nonce[16] = {0};
2036 size_t nonce_length = nonce_length_arg;
2037 unsigned char tag[16];
2038 size_t tag_length = tag_length_arg;
2039 size_t output_length;
2040
Gilles Peskine7be11a72022-04-14 00:12:57 +02002041 TEST_LE_U( nonce_length, sizeof( nonce ) );
2042 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
Gilles Peskine8817f612018-12-18 00:18:46 +01002044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002046 psa_set_key_usage_flags( &attributes, policy_usage );
2047 psa_set_key_algorithm( &attributes, policy_alg );
2048 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002049
Gilles Peskine049c7532019-05-15 20:22:09 +02002050 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002051 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002052
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002053 /* Check if no key usage implication is done */
2054 TEST_EQUAL( policy_usage,
2055 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002056
Neil Armstrong752d8112022-02-07 14:51:11 +01002057 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002058 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002059 nonce, nonce_length,
2060 NULL, 0,
2061 NULL, 0,
2062 tag, tag_length,
2063 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002064 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2065 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002067 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068
Neil Armstrong752d8112022-02-07 14:51:11 +01002069 /* Encrypt check, multi-part */
2070 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2071 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2072 TEST_EQUAL( status, expected_status );
2073 else
2074 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2075
2076 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002078 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079 nonce, nonce_length,
2080 NULL, 0,
2081 tag, tag_length,
2082 NULL, 0,
2083 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002084 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2085 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2086 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002087 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002088 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002089 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090
Neil Armstrong752d8112022-02-07 14:51:11 +01002091 /* Decrypt check, multi-part */
2092 PSA_ASSERT( psa_aead_abort( &operation ) );
2093 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2094 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2095 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2096 else
2097 TEST_EQUAL( status, expected_status );
2098
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002100 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002101 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002102 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103}
2104/* END_CASE */
2105
2106/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002107void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108 int policy_alg,
2109 int key_type,
2110 data_t *key_data,
2111 int exercise_alg )
2112{
Ronald Cron5425a212020-08-04 14:58:35 +02002113 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002114 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002115 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116 psa_status_t status;
2117 size_t key_bits;
2118 size_t buffer_length;
2119 unsigned char *buffer = NULL;
2120 size_t output_length;
2121
Gilles Peskine8817f612018-12-18 00:18:46 +01002122 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002124 psa_set_key_usage_flags( &attributes, policy_usage );
2125 psa_set_key_algorithm( &attributes, policy_alg );
2126 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002127
Gilles Peskine049c7532019-05-15 20:22:09 +02002128 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002129 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002131 /* Check if no key usage implication is done */
2132 TEST_EQUAL( policy_usage,
2133 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002134
Ronald Cron5425a212020-08-04 14:58:35 +02002135 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002136 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2138 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002139 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002140
Ronald Cron5425a212020-08-04 14:58:35 +02002141 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002142 NULL, 0,
2143 NULL, 0,
2144 buffer, buffer_length,
2145 &output_length );
2146 if( policy_alg == exercise_alg &&
2147 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002148 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002149 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002150 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002151
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002152 if( buffer_length != 0 )
2153 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002154 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002155 buffer, buffer_length,
2156 NULL, 0,
2157 buffer, buffer_length,
2158 &output_length );
2159 if( policy_alg == exercise_alg &&
2160 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002161 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002162 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002163 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002164
2165exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002166 /*
2167 * Key attributes may have been returned by psa_get_key_attributes()
2168 * thus reset them as required.
2169 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002170 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002171
2172 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002173 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002174 mbedtls_free( buffer );
2175}
2176/* END_CASE */
2177
2178/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002179void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180 int policy_alg,
2181 int key_type,
2182 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002183 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002184 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002185 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002186{
Ronald Cron5425a212020-08-04 14:58:35 +02002187 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002189 psa_key_usage_t policy_usage = policy_usage_arg;
2190 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002192 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2193 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2194 * compatible with the policy and `payload_length_arg` is supposed to be
2195 * a valid input length to sign. If `payload_length_arg <= 0`,
2196 * `exercise_alg` is supposed to be forbidden by the policy. */
2197 int compatible_alg = payload_length_arg > 0;
2198 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002199 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200 size_t signature_length;
2201
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002202 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002203 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002204 TEST_EQUAL( expected_usage,
2205 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002206
Gilles Peskine8817f612018-12-18 00:18:46 +01002207 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002209 psa_set_key_usage_flags( &attributes, policy_usage );
2210 psa_set_key_algorithm( &attributes, policy_alg );
2211 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002212
Gilles Peskine049c7532019-05-15 20:22:09 +02002213 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002214 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002215
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002216 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2217
Ronald Cron5425a212020-08-04 14:58:35 +02002218 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002219 payload, payload_length,
2220 signature, sizeof( signature ),
2221 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002222 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002223 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002224 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002225 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002226
2227 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002228 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002229 payload, payload_length,
2230 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002231 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002232 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002233 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002234 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002235
Gilles Peskinef7b41372021-09-22 16:15:05 +02002236 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002237 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002238 {
2239 status = psa_sign_message( key, exercise_alg,
2240 payload, payload_length,
2241 signature, sizeof( signature ),
2242 &signature_length );
2243 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2244 PSA_ASSERT( status );
2245 else
2246 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2247
2248 memset( signature, 0, sizeof( signature ) );
2249 status = psa_verify_message( key, exercise_alg,
2250 payload, payload_length,
2251 signature, sizeof( signature ) );
2252 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2253 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2254 else
2255 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2256 }
2257
Gilles Peskined5b33222018-06-18 22:20:03 +02002258exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002259 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002260 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002261}
2262/* END_CASE */
2263
Janos Follathba3fab92019-06-11 14:50:16 +01002264/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002265void derive_key_policy( int policy_usage,
2266 int policy_alg,
2267 int key_type,
2268 data_t *key_data,
2269 int exercise_alg )
2270{
Ronald Cron5425a212020-08-04 14:58:35 +02002271 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002273 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002274 psa_status_t status;
2275
Gilles Peskine8817f612018-12-18 00:18:46 +01002276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002277
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002278 psa_set_key_usage_flags( &attributes, policy_usage );
2279 psa_set_key_algorithm( &attributes, policy_alg );
2280 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002281
Gilles Peskine049c7532019-05-15 20:22:09 +02002282 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002283 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002284
Janos Follathba3fab92019-06-11 14:50:16 +01002285 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2286
2287 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2288 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002289 {
Janos Follathba3fab92019-06-11 14:50:16 +01002290 PSA_ASSERT( psa_key_derivation_input_bytes(
2291 &operation,
2292 PSA_KEY_DERIVATION_INPUT_SEED,
2293 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002294 }
Janos Follathba3fab92019-06-11 14:50:16 +01002295
2296 status = psa_key_derivation_input_key( &operation,
2297 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002298 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002299
Gilles Peskineea0fb492018-07-12 17:17:20 +02002300 if( policy_alg == exercise_alg &&
2301 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002302 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002303 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002304 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002305
2306exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002307 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002308 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002309 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002310}
2311/* END_CASE */
2312
2313/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002314void agreement_key_policy( int policy_usage,
2315 int policy_alg,
2316 int key_type_arg,
2317 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002318 int exercise_alg,
2319 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002320{
Ronald Cron5425a212020-08-04 14:58:35 +02002321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002323 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002324 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002325 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002326 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327
Gilles Peskine8817f612018-12-18 00:18:46 +01002328 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002329
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002330 psa_set_key_usage_flags( &attributes, policy_usage );
2331 psa_set_key_algorithm( &attributes, policy_alg );
2332 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002333
Gilles Peskine049c7532019-05-15 20:22:09 +02002334 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002335 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002336
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002337 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002338 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002339
Steven Cooremance48e852020-10-05 16:02:45 +02002340 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341
2342exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002343 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002344 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002345 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002346}
2347/* END_CASE */
2348
2349/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002350void key_policy_alg2( int key_type_arg, data_t *key_data,
2351 int usage_arg, int alg_arg, int alg2_arg )
2352{
Ronald Cron5425a212020-08-04 14:58:35 +02002353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002354 psa_key_type_t key_type = key_type_arg;
2355 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2356 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2357 psa_key_usage_t usage = usage_arg;
2358 psa_algorithm_t alg = alg_arg;
2359 psa_algorithm_t alg2 = alg2_arg;
2360
2361 PSA_ASSERT( psa_crypto_init( ) );
2362
2363 psa_set_key_usage_flags( &attributes, usage );
2364 psa_set_key_algorithm( &attributes, alg );
2365 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2366 psa_set_key_type( &attributes, key_type );
2367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002368 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002369
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002370 /* Update the usage flags to obtain implicit usage flags */
2371 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002372 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002373 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2374 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2375 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2376
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002377 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002378 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002379 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002380 goto exit;
2381
2382exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002383 /*
2384 * Key attributes may have been returned by psa_get_key_attributes()
2385 * thus reset them as required.
2386 */
2387 psa_reset_key_attributes( &got_attributes );
2388
Ronald Cron5425a212020-08-04 14:58:35 +02002389 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002390 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002391}
2392/* END_CASE */
2393
2394/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002395void raw_agreement_key_policy( int policy_usage,
2396 int policy_alg,
2397 int key_type_arg,
2398 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002399 int exercise_alg,
2400 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002401{
Ronald Cron5425a212020-08-04 14:58:35 +02002402 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002403 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002404 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002405 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002406 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002407 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002408
2409 PSA_ASSERT( psa_crypto_init( ) );
2410
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002411 psa_set_key_usage_flags( &attributes, policy_usage );
2412 psa_set_key_algorithm( &attributes, policy_alg );
2413 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002414
Gilles Peskine049c7532019-05-15 20:22:09 +02002415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002416 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002417
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002418 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002419
Steven Cooremance48e852020-10-05 16:02:45 +02002420 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002421
2422exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002423 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002424 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002425 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002426}
2427/* END_CASE */
2428
2429/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002430void copy_success( int source_usage_arg,
2431 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302432 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002433 int type_arg, data_t *material,
2434 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002435 int target_usage_arg,
2436 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302437 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002438 int expected_usage_arg,
2439 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002440{
Gilles Peskineca25db92019-04-19 11:43:08 +02002441 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2442 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443 psa_key_usage_t expected_usage = expected_usage_arg;
2444 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002445 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302446 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2447 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002448 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2449 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450 uint8_t *export_buffer = NULL;
2451
Gilles Peskine57ab7212019-01-28 13:03:09 +01002452 PSA_ASSERT( psa_crypto_init( ) );
2453
Gilles Peskineca25db92019-04-19 11:43:08 +02002454 /* Prepare the source key. */
2455 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2456 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002457 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002458 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302459 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002460 PSA_ASSERT( psa_import_key( &source_attributes,
2461 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002462 &source_key ) );
2463 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464
Gilles Peskineca25db92019-04-19 11:43:08 +02002465 /* Prepare the target attributes. */
2466 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002467 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002468 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002469 }
Archana8a180362021-07-05 02:18:48 +05302470 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002471
Gilles Peskineca25db92019-04-19 11:43:08 +02002472 if( target_usage_arg != -1 )
2473 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2474 if( target_alg_arg != -1 )
2475 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002476 if( target_alg2_arg != -1 )
2477 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002478
Archana8a180362021-07-05 02:18:48 +05302479
Gilles Peskine57ab7212019-01-28 13:03:09 +01002480 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002481 PSA_ASSERT( psa_copy_key( source_key,
2482 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002483
2484 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002485 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486
2487 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002488 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002489 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2490 psa_get_key_type( &target_attributes ) );
2491 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2492 psa_get_key_bits( &target_attributes ) );
2493 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2494 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002495 TEST_EQUAL( expected_alg2,
2496 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002497 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2498 {
2499 size_t length;
2500 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002501 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002502 material->len, &length ) );
2503 ASSERT_COMPARE( material->x, material->len,
2504 export_buffer, length );
2505 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002506
Archana8a180362021-07-05 02:18:48 +05302507 if( !psa_key_lifetime_is_external( target_lifetime ) )
2508 {
2509 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2510 goto exit;
2511 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2512 goto exit;
2513 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002514
Ronald Cron5425a212020-08-04 14:58:35 +02002515 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002516
2517exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002518 /*
2519 * Source and target key attributes may have been returned by
2520 * psa_get_key_attributes() thus reset them as required.
2521 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002522 psa_reset_key_attributes( &source_attributes );
2523 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002524
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002525 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002526 mbedtls_free( export_buffer );
2527}
2528/* END_CASE */
2529
2530/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002531void copy_fail( int source_usage_arg,
2532 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302533 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002534 int type_arg, data_t *material,
2535 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002536 int target_usage_arg,
2537 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002538 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002539 int expected_status_arg )
2540{
2541 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2542 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002543 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2544 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002545 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002546
2547 PSA_ASSERT( psa_crypto_init( ) );
2548
2549 /* Prepare the source key. */
2550 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2551 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002552 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002553 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302554 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002555 PSA_ASSERT( psa_import_key( &source_attributes,
2556 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002557 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002558
2559 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002560 psa_set_key_id( &target_attributes, key_id );
2561 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002562 psa_set_key_type( &target_attributes, target_type_arg );
2563 psa_set_key_bits( &target_attributes, target_bits_arg );
2564 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2565 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002566 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002567
2568 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002569 TEST_EQUAL( psa_copy_key( source_key,
2570 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002571 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002572
Ronald Cron5425a212020-08-04 14:58:35 +02002573 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002574
Gilles Peskine4a644642019-05-03 17:14:08 +02002575exit:
2576 psa_reset_key_attributes( &source_attributes );
2577 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002578 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002579}
2580/* END_CASE */
2581
2582/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002583void hash_operation_init( )
2584{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002585 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002586 /* Test each valid way of initializing the object, except for `= {0}`, as
2587 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2588 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002589 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002590 psa_hash_operation_t func = psa_hash_operation_init( );
2591 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2592 psa_hash_operation_t zero;
2593
2594 memset( &zero, 0, sizeof( zero ) );
2595
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002596 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002597 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2598 PSA_ERROR_BAD_STATE );
2599 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2600 PSA_ERROR_BAD_STATE );
2601 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2602 PSA_ERROR_BAD_STATE );
2603
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002604 /* A default hash operation should be abortable without error. */
2605 PSA_ASSERT( psa_hash_abort( &func ) );
2606 PSA_ASSERT( psa_hash_abort( &init ) );
2607 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002608}
2609/* END_CASE */
2610
2611/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002612void hash_setup( int alg_arg,
2613 int expected_status_arg )
2614{
2615 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002616 uint8_t *output = NULL;
2617 size_t output_size = 0;
2618 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002619 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002620 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002621 psa_status_t status;
2622
Gilles Peskine8817f612018-12-18 00:18:46 +01002623 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002624
Neil Armstrongedb20862022-02-07 15:47:44 +01002625 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002626 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002627 ASSERT_ALLOC( output, output_size );
2628
2629 status = psa_hash_compute( alg, NULL, 0,
2630 output, output_size, &output_length );
2631 TEST_EQUAL( status, expected_status );
2632
2633 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002634 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002635 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002636
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002637 /* Whether setup succeeded or failed, abort must succeed. */
2638 PSA_ASSERT( psa_hash_abort( &operation ) );
2639
2640 /* If setup failed, reproduce the failure, so as to
2641 * test the resulting state of the operation object. */
2642 if( status != PSA_SUCCESS )
2643 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2644
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002645 /* Now the operation object should be reusable. */
2646#if defined(KNOWN_SUPPORTED_HASH_ALG)
2647 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2648 PSA_ASSERT( psa_hash_abort( &operation ) );
2649#endif
2650
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002651exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002652 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002653 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654}
2655/* END_CASE */
2656
2657/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002658void hash_compute_fail( int alg_arg, data_t *input,
2659 int output_size_arg, int expected_status_arg )
2660{
2661 psa_algorithm_t alg = alg_arg;
2662 uint8_t *output = NULL;
2663 size_t output_size = output_size_arg;
2664 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002665 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002666 psa_status_t expected_status = expected_status_arg;
2667 psa_status_t status;
2668
2669 ASSERT_ALLOC( output, output_size );
2670
2671 PSA_ASSERT( psa_crypto_init( ) );
2672
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002673 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002674 status = psa_hash_compute( alg, input->x, input->len,
2675 output, output_size, &output_length );
2676 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002677 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002678
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002679 /* Hash Compute, multi-part */
2680 status = psa_hash_setup( &operation, alg );
2681 if( status == PSA_SUCCESS )
2682 {
2683 status = psa_hash_update( &operation, input->x, input->len );
2684 if( status == PSA_SUCCESS )
2685 {
2686 status = psa_hash_finish( &operation, output, output_size,
2687 &output_length );
2688 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002689 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002690 else
2691 TEST_EQUAL( status, expected_status );
2692 }
2693 else
2694 {
2695 TEST_EQUAL( status, expected_status );
2696 }
2697 }
2698 else
2699 {
2700 TEST_EQUAL( status, expected_status );
2701 }
2702
Gilles Peskine0a749c82019-11-28 19:33:58 +01002703exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002704 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002705 mbedtls_free( output );
2706 PSA_DONE( );
2707}
2708/* END_CASE */
2709
2710/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002711void hash_compare_fail( int alg_arg, data_t *input,
2712 data_t *reference_hash,
2713 int expected_status_arg )
2714{
2715 psa_algorithm_t alg = alg_arg;
2716 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002717 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002718 psa_status_t status;
2719
2720 PSA_ASSERT( psa_crypto_init( ) );
2721
Neil Armstrong55a1be12022-02-07 11:23:20 +01002722 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002723 status = psa_hash_compare( alg, input->x, input->len,
2724 reference_hash->x, reference_hash->len );
2725 TEST_EQUAL( status, expected_status );
2726
Neil Armstrong55a1be12022-02-07 11:23:20 +01002727 /* Hash Compare, multi-part */
2728 status = psa_hash_setup( &operation, alg );
2729 if( status == PSA_SUCCESS )
2730 {
2731 status = psa_hash_update( &operation, input->x, input->len );
2732 if( status == PSA_SUCCESS )
2733 {
2734 status = psa_hash_verify( &operation, reference_hash->x,
2735 reference_hash->len );
2736 TEST_EQUAL( status, expected_status );
2737 }
2738 else
2739 {
2740 TEST_EQUAL( status, expected_status );
2741 }
2742 }
2743 else
2744 {
2745 TEST_EQUAL( status, expected_status );
2746 }
2747
Gilles Peskine88e08462020-01-28 20:43:00 +01002748exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002749 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002750 PSA_DONE( );
2751}
2752/* END_CASE */
2753
2754/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002755void hash_compute_compare( int alg_arg, data_t *input,
2756 data_t *expected_output )
2757{
2758 psa_algorithm_t alg = alg_arg;
2759 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2760 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002761 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002762 size_t i;
2763
2764 PSA_ASSERT( psa_crypto_init( ) );
2765
Neil Armstrongca30a002022-02-07 11:40:23 +01002766 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002767 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002768 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002769 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002770 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002771 ASSERT_COMPARE( output, output_length,
2772 expected_output->x, expected_output->len );
2773
Neil Armstrongca30a002022-02-07 11:40:23 +01002774 /* Compute with tight buffer, multi-part */
2775 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2776 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2777 PSA_ASSERT( psa_hash_finish( &operation, output,
2778 PSA_HASH_LENGTH( alg ),
2779 &output_length ) );
2780 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2781 ASSERT_COMPARE( output, output_length,
2782 expected_output->x, expected_output->len );
2783
2784 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002785 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2786 output, sizeof( output ),
2787 &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 larger 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 sizeof( output ), &output_length ) );
2797 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2798 ASSERT_COMPARE( output, output_length,
2799 expected_output->x, expected_output->len );
2800
2801 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002802 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2803 output, output_length ) );
2804
Neil Armstrongca30a002022-02-07 11:40:23 +01002805 /* Compare with correct hash, multi-part */
2806 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2807 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2808 PSA_ASSERT( psa_hash_verify( &operation, output,
2809 output_length ) );
2810
2811 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002812 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2813 output, output_length + 1 ),
2814 PSA_ERROR_INVALID_SIGNATURE );
2815
Neil Armstrongca30a002022-02-07 11:40:23 +01002816 /* Compare with trailing garbage, multi-part */
2817 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2818 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2819 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2820 PSA_ERROR_INVALID_SIGNATURE );
2821
2822 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002823 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2824 output, output_length - 1 ),
2825 PSA_ERROR_INVALID_SIGNATURE );
2826
Neil Armstrongca30a002022-02-07 11:40:23 +01002827 /* Compare with truncated hash, multi-part */
2828 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2829 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2830 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2831 PSA_ERROR_INVALID_SIGNATURE );
2832
Gilles Peskine0a749c82019-11-28 19:33:58 +01002833 /* Compare with corrupted value */
2834 for( i = 0; i < output_length; i++ )
2835 {
Chris Jones9634bb12021-01-20 15:56:42 +00002836 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002837 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002838
2839 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002840 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2841 output, output_length ),
2842 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002843
2844 /* Multi-Part */
2845 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2846 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2847 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2848 PSA_ERROR_INVALID_SIGNATURE );
2849
Gilles Peskine0a749c82019-11-28 19:33:58 +01002850 output[i] ^= 1;
2851 }
2852
2853exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002854 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002855 PSA_DONE( );
2856}
2857/* END_CASE */
2858
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002859/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002860void hash_bad_order( )
2861{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002863 unsigned char input[] = "";
2864 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002865 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002866 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2867 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2868 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002869 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002870 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002871 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002874
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002875 /* Call setup twice in a row. */
2876 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002877 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002878 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2879 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002880 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002881 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002882 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002883
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002884 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002885 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002886 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002887 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002888
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002889 /* Check that update calls abort on error. */
2890 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002891 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002892 ASSERT_OPERATION_IS_ACTIVE( operation );
2893 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2894 PSA_ERROR_BAD_STATE );
2895 ASSERT_OPERATION_IS_INACTIVE( operation );
2896 PSA_ASSERT( psa_hash_abort( &operation ) );
2897 ASSERT_OPERATION_IS_INACTIVE( operation );
2898
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002899 /* Call update after finish. */
2900 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2901 PSA_ASSERT( psa_hash_finish( &operation,
2902 hash, sizeof( hash ), &hash_len ) );
2903 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
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002907 /* Call verify without calling setup beforehand. */
2908 TEST_EQUAL( psa_hash_verify( &operation,
2909 valid_hash, sizeof( valid_hash ) ),
2910 PSA_ERROR_BAD_STATE );
2911 PSA_ASSERT( psa_hash_abort( &operation ) );
2912
2913 /* Call verify after finish. */
2914 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2915 PSA_ASSERT( psa_hash_finish( &operation,
2916 hash, sizeof( hash ), &hash_len ) );
2917 TEST_EQUAL( psa_hash_verify( &operation,
2918 valid_hash, sizeof( valid_hash ) ),
2919 PSA_ERROR_BAD_STATE );
2920 PSA_ASSERT( psa_hash_abort( &operation ) );
2921
2922 /* Call verify twice in a row. */
2923 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002924 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002925 PSA_ASSERT( psa_hash_verify( &operation,
2926 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002927 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002928 TEST_EQUAL( psa_hash_verify( &operation,
2929 valid_hash, sizeof( valid_hash ) ),
2930 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002931 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002932 PSA_ASSERT( psa_hash_abort( &operation ) );
2933
2934 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002935 TEST_EQUAL( psa_hash_finish( &operation,
2936 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002937 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002938 PSA_ASSERT( psa_hash_abort( &operation ) );
2939
2940 /* Call finish twice in a row. */
2941 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2942 PSA_ASSERT( psa_hash_finish( &operation,
2943 hash, sizeof( hash ), &hash_len ) );
2944 TEST_EQUAL( psa_hash_finish( &operation,
2945 hash, sizeof( hash ), &hash_len ),
2946 PSA_ERROR_BAD_STATE );
2947 PSA_ASSERT( psa_hash_abort( &operation ) );
2948
2949 /* Call finish after calling verify. */
2950 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2951 PSA_ASSERT( psa_hash_verify( &operation,
2952 valid_hash, sizeof( valid_hash ) ) );
2953 TEST_EQUAL( psa_hash_finish( &operation,
2954 hash, sizeof( hash ), &hash_len ),
2955 PSA_ERROR_BAD_STATE );
2956 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002957
2958exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002959 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002960}
2961/* END_CASE */
2962
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002963/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002964void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002965{
2966 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002967 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2968 * appended to it */
2969 unsigned char hash[] = {
2970 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2971 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2972 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002973 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002974 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002975
Gilles Peskine8817f612018-12-18 00:18:46 +01002976 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002977
itayzafrir27e69452018-11-01 14:26:34 +02002978 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002979 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002980 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002981 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002982 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002983 ASSERT_OPERATION_IS_INACTIVE( operation );
2984 PSA_ASSERT( psa_hash_abort( &operation ) );
2985 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002986
itayzafrir27e69452018-11-01 14:26:34 +02002987 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002988 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002989 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002990 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002991
itayzafrir27e69452018-11-01 14:26:34 +02002992 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002994 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002995 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002996
itayzafrirec93d302018-10-18 18:01:10 +03002997exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002998 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002999}
3000/* END_CASE */
3001
Ronald Cronee414c72021-03-18 18:50:08 +01003002/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003003void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03003004{
3005 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003006 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003007 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003008 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003009 size_t hash_len;
3010
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003012
itayzafrir58028322018-10-25 10:22:01 +03003013 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003015 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003016 hash, expected_size - 1, &hash_len ),
3017 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003018
3019exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003020 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003021}
3022/* END_CASE */
3023
Ronald Cronee414c72021-03-18 18:50:08 +01003024/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003025void hash_clone_source_state( )
3026{
3027 psa_algorithm_t alg = PSA_ALG_SHA_256;
3028 unsigned char hash[PSA_HASH_MAX_SIZE];
3029 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3030 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3031 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3032 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3033 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3034 size_t hash_len;
3035
3036 PSA_ASSERT( psa_crypto_init( ) );
3037 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3038
3039 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3040 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3041 PSA_ASSERT( psa_hash_finish( &op_finished,
3042 hash, sizeof( hash ), &hash_len ) );
3043 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3044 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3045
3046 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3047 PSA_ERROR_BAD_STATE );
3048
3049 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3050 PSA_ASSERT( psa_hash_finish( &op_init,
3051 hash, sizeof( hash ), &hash_len ) );
3052 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3053 PSA_ASSERT( psa_hash_finish( &op_finished,
3054 hash, sizeof( hash ), &hash_len ) );
3055 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3056 PSA_ASSERT( psa_hash_finish( &op_aborted,
3057 hash, sizeof( hash ), &hash_len ) );
3058
3059exit:
3060 psa_hash_abort( &op_source );
3061 psa_hash_abort( &op_init );
3062 psa_hash_abort( &op_setup );
3063 psa_hash_abort( &op_finished );
3064 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003065 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003066}
3067/* END_CASE */
3068
Ronald Cronee414c72021-03-18 18:50:08 +01003069/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003070void hash_clone_target_state( )
3071{
3072 psa_algorithm_t alg = PSA_ALG_SHA_256;
3073 unsigned char hash[PSA_HASH_MAX_SIZE];
3074 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3075 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3076 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3077 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3078 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3079 size_t hash_len;
3080
3081 PSA_ASSERT( psa_crypto_init( ) );
3082
3083 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3084 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3085 PSA_ASSERT( psa_hash_finish( &op_finished,
3086 hash, sizeof( hash ), &hash_len ) );
3087 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3088 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3089
3090 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3091 PSA_ASSERT( psa_hash_finish( &op_target,
3092 hash, sizeof( hash ), &hash_len ) );
3093
3094 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3095 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3096 PSA_ERROR_BAD_STATE );
3097 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3098 PSA_ERROR_BAD_STATE );
3099
3100exit:
3101 psa_hash_abort( &op_target );
3102 psa_hash_abort( &op_init );
3103 psa_hash_abort( &op_setup );
3104 psa_hash_abort( &op_finished );
3105 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003106 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003107}
3108/* END_CASE */
3109
itayzafrir58028322018-10-25 10:22:01 +03003110/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003111void mac_operation_init( )
3112{
Jaeden Amero252ef282019-02-15 14:05:35 +00003113 const uint8_t input[1] = { 0 };
3114
Jaeden Amero769ce272019-01-04 11:48:03 +00003115 /* Test each valid way of initializing the object, except for `= {0}`, as
3116 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3117 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003118 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003119 psa_mac_operation_t func = psa_mac_operation_init( );
3120 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3121 psa_mac_operation_t zero;
3122
3123 memset( &zero, 0, sizeof( zero ) );
3124
Jaeden Amero252ef282019-02-15 14:05:35 +00003125 /* A freshly-initialized MAC operation should not be usable. */
3126 TEST_EQUAL( psa_mac_update( &func,
3127 input, sizeof( input ) ),
3128 PSA_ERROR_BAD_STATE );
3129 TEST_EQUAL( psa_mac_update( &init,
3130 input, sizeof( input ) ),
3131 PSA_ERROR_BAD_STATE );
3132 TEST_EQUAL( psa_mac_update( &zero,
3133 input, sizeof( input ) ),
3134 PSA_ERROR_BAD_STATE );
3135
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003136 /* A default MAC operation should be abortable without error. */
3137 PSA_ASSERT( psa_mac_abort( &func ) );
3138 PSA_ASSERT( psa_mac_abort( &init ) );
3139 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003140}
3141/* END_CASE */
3142
3143/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003144void mac_setup( int key_type_arg,
3145 data_t *key,
3146 int alg_arg,
3147 int expected_status_arg )
3148{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003149 psa_key_type_t key_type = key_type_arg;
3150 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003151 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003152 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003153 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3154#if defined(KNOWN_SUPPORTED_MAC_ALG)
3155 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3156#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003160 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3161 &operation, &status ) )
3162 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003163 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003165 /* The operation object should be reusable. */
3166#if defined(KNOWN_SUPPORTED_MAC_ALG)
3167 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3168 smoke_test_key_data,
3169 sizeof( smoke_test_key_data ),
3170 KNOWN_SUPPORTED_MAC_ALG,
3171 &operation, &status ) )
3172 goto exit;
3173 TEST_EQUAL( status, PSA_SUCCESS );
3174#endif
3175
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003176exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003177 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003178}
3179/* END_CASE */
3180
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003181/* 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 +00003182void mac_bad_order( )
3183{
Ronald Cron5425a212020-08-04 14:58:35 +02003184 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003185 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3186 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003187 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003188 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3189 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3190 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003192 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3193 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3194 size_t sign_mac_length = 0;
3195 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3196 const uint8_t verify_mac[] = {
3197 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3198 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3199 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3200
3201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003202 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003203 psa_set_key_algorithm( &attributes, alg );
3204 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003205
Ronald Cron5425a212020-08-04 14:58:35 +02003206 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3207 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003208
Jaeden Amero252ef282019-02-15 14:05:35 +00003209 /* Call update without calling setup beforehand. */
3210 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3211 PSA_ERROR_BAD_STATE );
3212 PSA_ASSERT( psa_mac_abort( &operation ) );
3213
3214 /* Call sign finish without calling setup beforehand. */
3215 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3216 &sign_mac_length),
3217 PSA_ERROR_BAD_STATE );
3218 PSA_ASSERT( psa_mac_abort( &operation ) );
3219
3220 /* Call verify finish without calling setup beforehand. */
3221 TEST_EQUAL( psa_mac_verify_finish( &operation,
3222 verify_mac, sizeof( verify_mac ) ),
3223 PSA_ERROR_BAD_STATE );
3224 PSA_ASSERT( psa_mac_abort( &operation ) );
3225
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003226 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003227 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003228 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003229 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003230 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003231 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003232 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003233 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003234
Jaeden Amero252ef282019-02-15 14:05:35 +00003235 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003236 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003237 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3238 PSA_ASSERT( psa_mac_sign_finish( &operation,
3239 sign_mac, sizeof( sign_mac ),
3240 &sign_mac_length ) );
3241 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3242 PSA_ERROR_BAD_STATE );
3243 PSA_ASSERT( psa_mac_abort( &operation ) );
3244
3245 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003246 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003247 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3248 PSA_ASSERT( psa_mac_verify_finish( &operation,
3249 verify_mac, sizeof( verify_mac ) ) );
3250 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3251 PSA_ERROR_BAD_STATE );
3252 PSA_ASSERT( psa_mac_abort( &operation ) );
3253
3254 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003255 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003256 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3257 PSA_ASSERT( psa_mac_sign_finish( &operation,
3258 sign_mac, sizeof( sign_mac ),
3259 &sign_mac_length ) );
3260 TEST_EQUAL( psa_mac_sign_finish( &operation,
3261 sign_mac, sizeof( sign_mac ),
3262 &sign_mac_length ),
3263 PSA_ERROR_BAD_STATE );
3264 PSA_ASSERT( psa_mac_abort( &operation ) );
3265
3266 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003267 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003268 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3269 PSA_ASSERT( psa_mac_verify_finish( &operation,
3270 verify_mac, sizeof( verify_mac ) ) );
3271 TEST_EQUAL( psa_mac_verify_finish( &operation,
3272 verify_mac, sizeof( verify_mac ) ),
3273 PSA_ERROR_BAD_STATE );
3274 PSA_ASSERT( psa_mac_abort( &operation ) );
3275
3276 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003277 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003278 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003279 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003280 TEST_EQUAL( psa_mac_verify_finish( &operation,
3281 verify_mac, sizeof( verify_mac ) ),
3282 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003283 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003284 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003285 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003286
3287 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003288 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003289 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003290 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003291 TEST_EQUAL( psa_mac_sign_finish( &operation,
3292 sign_mac, sizeof( sign_mac ),
3293 &sign_mac_length ),
3294 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003295 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003296 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003297 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003298
Ronald Cron5425a212020-08-04 14:58:35 +02003299 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003300
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003301exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003302 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003307void mac_sign_verify_multi( int key_type_arg,
3308 data_t *key_data,
3309 int alg_arg,
3310 data_t *input,
3311 int is_verify,
3312 data_t *expected_mac )
3313{
3314 size_t data_part_len = 0;
3315
3316 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3317 {
3318 /* Split data into length(data_part_len) parts. */
3319 mbedtls_test_set_step( 2000 + data_part_len );
3320
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003321 if( mac_multipart_internal_func( key_type_arg, key_data,
3322 alg_arg,
3323 input, data_part_len,
3324 expected_mac,
3325 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003326 break;
3327
3328 /* length(0) part, length(data_part_len) part, length(0) part... */
3329 mbedtls_test_set_step( 3000 + data_part_len );
3330
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003331 if( mac_multipart_internal_func( key_type_arg, key_data,
3332 alg_arg,
3333 input, data_part_len,
3334 expected_mac,
3335 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003336 break;
3337 }
3338
3339 /* Goto is required to silence warnings about unused labels, as we
3340 * don't actually do any test assertions in this function. */
3341 goto exit;
3342}
3343/* END_CASE */
3344
3345/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003346void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003347 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003348 int alg_arg,
3349 data_t *input,
3350 data_t *expected_mac )
3351{
Ronald Cron5425a212020-08-04 14:58:35 +02003352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003353 psa_key_type_t key_type = key_type_arg;
3354 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003355 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003357 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003359 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003360 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003361 const size_t output_sizes_to_test[] = {
3362 0,
3363 1,
3364 expected_mac->len - 1,
3365 expected_mac->len,
3366 expected_mac->len + 1,
3367 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003368
Gilles Peskine7be11a72022-04-14 00:12:57 +02003369 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003370 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003371 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003372
Gilles Peskine8817f612018-12-18 00:18:46 +01003373 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003374
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003375 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003376 psa_set_key_algorithm( &attributes, alg );
3377 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003378
Ronald Cron5425a212020-08-04 14:58:35 +02003379 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3380 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003381
Gilles Peskine8b356b52020-08-25 23:44:59 +02003382 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3383 {
3384 const size_t output_size = output_sizes_to_test[i];
3385 psa_status_t expected_status =
3386 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3387 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003388
Chris Jones9634bb12021-01-20 15:56:42 +00003389 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003390 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003391
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003392 /* Calculate the MAC, one-shot case. */
3393 TEST_EQUAL( psa_mac_compute( key, alg,
3394 input->x, input->len,
3395 actual_mac, output_size, &mac_length ),
3396 expected_status );
3397 if( expected_status == PSA_SUCCESS )
3398 {
3399 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3400 actual_mac, mac_length );
3401 }
3402
3403 if( output_size > 0 )
3404 memset( actual_mac, 0, output_size );
3405
3406 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003407 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003408 PSA_ASSERT( psa_mac_update( &operation,
3409 input->x, input->len ) );
3410 TEST_EQUAL( psa_mac_sign_finish( &operation,
3411 actual_mac, output_size,
3412 &mac_length ),
3413 expected_status );
3414 PSA_ASSERT( psa_mac_abort( &operation ) );
3415
3416 if( expected_status == PSA_SUCCESS )
3417 {
3418 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3419 actual_mac, mac_length );
3420 }
3421 mbedtls_free( actual_mac );
3422 actual_mac = NULL;
3423 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003424
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003425exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003426 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003427 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003428 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003429 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003430}
3431/* END_CASE */
3432
3433/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003434void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003435 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003436 int alg_arg,
3437 data_t *input,
3438 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003439{
Ronald Cron5425a212020-08-04 14:58:35 +02003440 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003441 psa_key_type_t key_type = key_type_arg;
3442 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003443 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003445 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003446
Gilles Peskine7be11a72022-04-14 00:12:57 +02003447 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003448
Gilles Peskine8817f612018-12-18 00:18:46 +01003449 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003450
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003452 psa_set_key_algorithm( &attributes, alg );
3453 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003454
Ronald Cron5425a212020-08-04 14:58:35 +02003455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3456 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003457
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003458 /* Verify correct MAC, one-shot case. */
3459 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3460 expected_mac->x, expected_mac->len ) );
3461
3462 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003463 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003464 PSA_ASSERT( psa_mac_update( &operation,
3465 input->x, input->len ) );
3466 PSA_ASSERT( psa_mac_verify_finish( &operation,
3467 expected_mac->x,
3468 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003469
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003470 /* Test a MAC that's too short, one-shot case. */
3471 TEST_EQUAL( psa_mac_verify( key, alg,
3472 input->x, input->len,
3473 expected_mac->x,
3474 expected_mac->len - 1 ),
3475 PSA_ERROR_INVALID_SIGNATURE );
3476
3477 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003478 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003479 PSA_ASSERT( psa_mac_update( &operation,
3480 input->x, input->len ) );
3481 TEST_EQUAL( psa_mac_verify_finish( &operation,
3482 expected_mac->x,
3483 expected_mac->len - 1 ),
3484 PSA_ERROR_INVALID_SIGNATURE );
3485
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003486 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003487 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3488 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003489 TEST_EQUAL( psa_mac_verify( key, alg,
3490 input->x, input->len,
3491 perturbed_mac, expected_mac->len + 1 ),
3492 PSA_ERROR_INVALID_SIGNATURE );
3493
3494 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003495 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003496 PSA_ASSERT( psa_mac_update( &operation,
3497 input->x, input->len ) );
3498 TEST_EQUAL( psa_mac_verify_finish( &operation,
3499 perturbed_mac,
3500 expected_mac->len + 1 ),
3501 PSA_ERROR_INVALID_SIGNATURE );
3502
3503 /* Test changing one byte. */
3504 for( size_t i = 0; i < expected_mac->len; i++ )
3505 {
Chris Jones9634bb12021-01-20 15:56:42 +00003506 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003507 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003508
3509 TEST_EQUAL( psa_mac_verify( key, alg,
3510 input->x, input->len,
3511 perturbed_mac, expected_mac->len ),
3512 PSA_ERROR_INVALID_SIGNATURE );
3513
Ronald Cron5425a212020-08-04 14:58:35 +02003514 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003515 PSA_ASSERT( psa_mac_update( &operation,
3516 input->x, input->len ) );
3517 TEST_EQUAL( psa_mac_verify_finish( &operation,
3518 perturbed_mac,
3519 expected_mac->len ),
3520 PSA_ERROR_INVALID_SIGNATURE );
3521 perturbed_mac[i] ^= 1;
3522 }
3523
Gilles Peskine8c9def32018-02-08 10:02:12 +01003524exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003525 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003526 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003527 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003528 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003529}
3530/* END_CASE */
3531
3532/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003533void cipher_operation_init( )
3534{
Jaeden Ameroab439972019-02-15 14:12:05 +00003535 const uint8_t input[1] = { 0 };
3536 unsigned char output[1] = { 0 };
3537 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003538 /* Test each valid way of initializing the object, except for `= {0}`, as
3539 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3540 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003541 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003542 psa_cipher_operation_t func = psa_cipher_operation_init( );
3543 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3544 psa_cipher_operation_t zero;
3545
3546 memset( &zero, 0, sizeof( zero ) );
3547
Jaeden Ameroab439972019-02-15 14:12:05 +00003548 /* A freshly-initialized cipher operation should not be usable. */
3549 TEST_EQUAL( psa_cipher_update( &func,
3550 input, sizeof( input ),
3551 output, sizeof( output ),
3552 &output_length ),
3553 PSA_ERROR_BAD_STATE );
3554 TEST_EQUAL( psa_cipher_update( &init,
3555 input, sizeof( input ),
3556 output, sizeof( output ),
3557 &output_length ),
3558 PSA_ERROR_BAD_STATE );
3559 TEST_EQUAL( psa_cipher_update( &zero,
3560 input, sizeof( input ),
3561 output, sizeof( output ),
3562 &output_length ),
3563 PSA_ERROR_BAD_STATE );
3564
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003565 /* A default cipher operation should be abortable without error. */
3566 PSA_ASSERT( psa_cipher_abort( &func ) );
3567 PSA_ASSERT( psa_cipher_abort( &init ) );
3568 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003569}
3570/* END_CASE */
3571
3572/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003573void cipher_setup( int key_type_arg,
3574 data_t *key,
3575 int alg_arg,
3576 int expected_status_arg )
3577{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003578 psa_key_type_t key_type = key_type_arg;
3579 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003580 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003581 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003582 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003583#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003584 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3585#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003586
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003588
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003589 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3590 &operation, &status ) )
3591 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003592 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003593
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003594 /* The operation object should be reusable. */
3595#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3596 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3597 smoke_test_key_data,
3598 sizeof( smoke_test_key_data ),
3599 KNOWN_SUPPORTED_CIPHER_ALG,
3600 &operation, &status ) )
3601 goto exit;
3602 TEST_EQUAL( status, PSA_SUCCESS );
3603#endif
3604
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003605exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003606 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003607 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003608}
3609/* END_CASE */
3610
Ronald Cronee414c72021-03-18 18:50:08 +01003611/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003612void cipher_bad_order( )
3613{
Ronald Cron5425a212020-08-04 14:58:35 +02003614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003615 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3616 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003618 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003619 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003620 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003621 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3622 0xaa, 0xaa, 0xaa, 0xaa };
3623 const uint8_t text[] = {
3624 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3625 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003626 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003627 size_t length = 0;
3628
3629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3631 psa_set_key_algorithm( &attributes, alg );
3632 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003633 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3634 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003635
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003636 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003637 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003638 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003639 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003640 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003641 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003642 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003643 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003644
3645 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003646 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003647 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003648 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003649 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003650 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003651 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003652 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003653
Jaeden Ameroab439972019-02-15 14:12:05 +00003654 /* Generate an IV without calling setup beforehand. */
3655 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3656 buffer, sizeof( buffer ),
3657 &length ),
3658 PSA_ERROR_BAD_STATE );
3659 PSA_ASSERT( psa_cipher_abort( &operation ) );
3660
3661 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003662 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003663 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3664 buffer, sizeof( buffer ),
3665 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003666 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003667 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3668 buffer, sizeof( buffer ),
3669 &length ),
3670 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003671 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003672 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003673 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003674
3675 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003676 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003677 PSA_ASSERT( psa_cipher_set_iv( &operation,
3678 iv, sizeof( iv ) ) );
3679 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3680 buffer, sizeof( buffer ),
3681 &length ),
3682 PSA_ERROR_BAD_STATE );
3683 PSA_ASSERT( psa_cipher_abort( &operation ) );
3684
3685 /* Set an IV without calling setup beforehand. */
3686 TEST_EQUAL( psa_cipher_set_iv( &operation,
3687 iv, sizeof( iv ) ),
3688 PSA_ERROR_BAD_STATE );
3689 PSA_ASSERT( psa_cipher_abort( &operation ) );
3690
3691 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003692 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003693 PSA_ASSERT( psa_cipher_set_iv( &operation,
3694 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003695 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003696 TEST_EQUAL( psa_cipher_set_iv( &operation,
3697 iv, sizeof( iv ) ),
3698 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003699 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003700 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003701 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003702
3703 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003704 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003705 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3706 buffer, sizeof( buffer ),
3707 &length ) );
3708 TEST_EQUAL( psa_cipher_set_iv( &operation,
3709 iv, sizeof( iv ) ),
3710 PSA_ERROR_BAD_STATE );
3711 PSA_ASSERT( psa_cipher_abort( &operation ) );
3712
3713 /* Call update without calling setup beforehand. */
3714 TEST_EQUAL( psa_cipher_update( &operation,
3715 text, sizeof( text ),
3716 buffer, sizeof( buffer ),
3717 &length ),
3718 PSA_ERROR_BAD_STATE );
3719 PSA_ASSERT( psa_cipher_abort( &operation ) );
3720
3721 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003722 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003723 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003724 TEST_EQUAL( psa_cipher_update( &operation,
3725 text, sizeof( text ),
3726 buffer, sizeof( buffer ),
3727 &length ),
3728 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003729 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003730 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003731 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003732
3733 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003734 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003735 PSA_ASSERT( psa_cipher_set_iv( &operation,
3736 iv, sizeof( iv ) ) );
3737 PSA_ASSERT( psa_cipher_finish( &operation,
3738 buffer, sizeof( buffer ), &length ) );
3739 TEST_EQUAL( psa_cipher_update( &operation,
3740 text, sizeof( text ),
3741 buffer, sizeof( buffer ),
3742 &length ),
3743 PSA_ERROR_BAD_STATE );
3744 PSA_ASSERT( psa_cipher_abort( &operation ) );
3745
3746 /* Call finish without calling setup beforehand. */
3747 TEST_EQUAL( psa_cipher_finish( &operation,
3748 buffer, sizeof( buffer ), &length ),
3749 PSA_ERROR_BAD_STATE );
3750 PSA_ASSERT( psa_cipher_abort( &operation ) );
3751
3752 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003753 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003754 /* Not calling update means we are encrypting an empty buffer, which is OK
3755 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003756 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003757 TEST_EQUAL( psa_cipher_finish( &operation,
3758 buffer, sizeof( buffer ), &length ),
3759 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003760 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003761 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003762 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003763
3764 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003765 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003766 PSA_ASSERT( psa_cipher_set_iv( &operation,
3767 iv, sizeof( iv ) ) );
3768 PSA_ASSERT( psa_cipher_finish( &operation,
3769 buffer, sizeof( buffer ), &length ) );
3770 TEST_EQUAL( psa_cipher_finish( &operation,
3771 buffer, sizeof( buffer ), &length ),
3772 PSA_ERROR_BAD_STATE );
3773 PSA_ASSERT( psa_cipher_abort( &operation ) );
3774
Ronald Cron5425a212020-08-04 14:58:35 +02003775 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003776
Jaeden Ameroab439972019-02-15 14:12:05 +00003777exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003778 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003779 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003780}
3781/* END_CASE */
3782
3783/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003784void cipher_encrypt_fail( int alg_arg,
3785 int key_type_arg,
3786 data_t *key_data,
3787 data_t *input,
3788 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003789{
Ronald Cron5425a212020-08-04 14:58:35 +02003790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003791 psa_status_t status;
3792 psa_key_type_t key_type = key_type_arg;
3793 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003794 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003795 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3796 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3797 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003798 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003799 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003800 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003801 size_t function_output_length;
3802 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003803 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3804
3805 if ( PSA_ERROR_BAD_STATE != expected_status )
3806 {
3807 PSA_ASSERT( psa_crypto_init( ) );
3808
3809 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3810 psa_set_key_algorithm( &attributes, alg );
3811 psa_set_key_type( &attributes, key_type );
3812
3813 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3814 input->len );
3815 ASSERT_ALLOC( output, output_buffer_size );
3816
3817 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3818 &key ) );
3819 }
3820
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003821 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003822 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3823 output_buffer_size, &output_length );
3824
3825 TEST_EQUAL( status, expected_status );
3826
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003827 /* Encrypt, multi-part */
3828 status = psa_cipher_encrypt_setup( &operation, key, alg );
3829 if( status == PSA_SUCCESS )
3830 {
3831 if( alg != PSA_ALG_ECB_NO_PADDING )
3832 {
3833 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3834 iv, iv_size,
3835 &iv_length ) );
3836 }
3837
3838 status = psa_cipher_update( &operation, input->x, input->len,
3839 output, output_buffer_size,
3840 &function_output_length );
3841 if( status == PSA_SUCCESS )
3842 {
3843 output_length += function_output_length;
3844
3845 status = psa_cipher_finish( &operation, output + output_length,
3846 output_buffer_size - output_length,
3847 &function_output_length );
3848
3849 TEST_EQUAL( status, expected_status );
3850 }
3851 else
3852 {
3853 TEST_EQUAL( status, expected_status );
3854 }
3855 }
3856 else
3857 {
3858 TEST_EQUAL( status, expected_status );
3859 }
3860
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003861exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003862 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003863 mbedtls_free( output );
3864 psa_destroy_key( key );
3865 PSA_DONE( );
3866}
3867/* END_CASE */
3868
3869/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003870void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3871 data_t *input, int iv_length,
3872 int expected_result )
3873{
3874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3875 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3877 size_t output_buffer_size = 0;
3878 unsigned char *output = NULL;
3879
3880 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3881 ASSERT_ALLOC( output, output_buffer_size );
3882
3883 PSA_ASSERT( psa_crypto_init( ) );
3884
3885 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3886 psa_set_key_algorithm( &attributes, alg );
3887 psa_set_key_type( &attributes, key_type );
3888
3889 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3890 &key ) );
3891 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3892 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3893 iv_length ) );
3894
3895exit:
3896 psa_cipher_abort( &operation );
3897 mbedtls_free( output );
3898 psa_destroy_key( key );
3899 PSA_DONE( );
3900}
3901/* END_CASE */
3902
3903/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003904void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3905 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003906{
3907 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3908 psa_key_type_t key_type = key_type_arg;
3909 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003910 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3911 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003912 unsigned char *output = NULL;
3913 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003914 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3916
3917 PSA_ASSERT( psa_crypto_init( ) );
3918
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003919 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003920 TEST_LE_U( ciphertext->len,
3921 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3922 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003923 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003924 TEST_LE_U( plaintext->len,
3925 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3926 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3927 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003928
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003929
3930 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003931 psa_set_key_usage_flags( &attributes,
3932 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003933 psa_set_key_algorithm( &attributes, alg );
3934 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003935 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3936 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003937 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3938 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003939 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003940
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003941 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003942 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3943 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3944 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003945 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3946 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003947 PSA_ERROR_BAD_STATE );
3948
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003949 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003950 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3951 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003952 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003953 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003954 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3955 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003956 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003957 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003958
Gilles Peskine286c3142022-04-20 17:09:38 +02003959 /* Multipart encryption */
3960 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3961 output_length = 0;
3962 length = ~0;
3963 PSA_ASSERT( psa_cipher_update( &operation,
3964 plaintext->x, plaintext->len,
3965 output, output_buffer_size,
3966 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003967 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 output_length += length;
3969 PSA_ASSERT( psa_cipher_finish( &operation,
3970 output + output_length,
3971 output_buffer_size - output_length,
3972 &length ) );
3973 output_length += length;
3974 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003975 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003976
Gilles Peskine286c3142022-04-20 17:09:38 +02003977 /* Multipart encryption */
3978 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3979 output_length = 0;
3980 length = ~0;
3981 PSA_ASSERT( psa_cipher_update( &operation,
3982 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003983 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003984 &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( plaintext->x, plaintext->len,
3993 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003994
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003995 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003996 output_length = ~0;
3997 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3998 output, output_buffer_size,
3999 &output_length ) );
4000 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
4001 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004002
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004003 /* One-shot decryption */
4004 output_length = ~0;
4005 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4006 output, output_buffer_size,
4007 &output_length ) );
4008 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004009 output, output_length );
4010
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004011exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004012 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004013 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004014 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004015 psa_destroy_key( key );
4016 PSA_DONE( );
4017}
4018/* END_CASE */
4019
4020/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004021void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4022{
4023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4024 psa_algorithm_t alg = alg_arg;
4025 psa_key_type_t key_type = key_type_arg;
4026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4027 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4028 psa_status_t status;
4029
4030 PSA_ASSERT( psa_crypto_init( ) );
4031
4032 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4033 psa_set_key_algorithm( &attributes, alg );
4034 psa_set_key_type( &attributes, key_type );
4035
4036 /* Usage of either of these two size macros would cause divide by zero
4037 * with incorrect key types previously. Input length should be irrelevant
4038 * here. */
4039 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4040 0 );
4041 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4042
4043
4044 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4045 &key ) );
4046
4047 /* Should fail due to invalid alg type (to support invalid key type).
4048 * Encrypt or decrypt will end up in the same place. */
4049 status = psa_cipher_encrypt_setup( &operation, key, alg );
4050
4051 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4052
4053exit:
4054 psa_cipher_abort( &operation );
4055 psa_destroy_key( key );
4056 PSA_DONE( );
4057}
4058/* END_CASE */
4059
4060/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004061void cipher_encrypt_validation( int alg_arg,
4062 int key_type_arg,
4063 data_t *key_data,
4064 data_t *input )
4065{
4066 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4067 psa_key_type_t key_type = key_type_arg;
4068 psa_algorithm_t alg = alg_arg;
4069 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4070 unsigned char *output1 = NULL;
4071 size_t output1_buffer_size = 0;
4072 size_t output1_length = 0;
4073 unsigned char *output2 = NULL;
4074 size_t output2_buffer_size = 0;
4075 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004076 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004077 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004078 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004079
Gilles Peskine8817f612018-12-18 00:18:46 +01004080 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004081
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004082 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4083 psa_set_key_algorithm( &attributes, alg );
4084 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004085
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004086 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4087 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4088 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4089 ASSERT_ALLOC( output1, output1_buffer_size );
4090 ASSERT_ALLOC( output2, output2_buffer_size );
4091
Ronald Cron5425a212020-08-04 14:58:35 +02004092 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4093 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004094
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004095 /* The one-shot cipher encryption uses generated iv so validating
4096 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004097 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4098 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004099 TEST_LE_U( output1_length,
4100 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4101 TEST_LE_U( output1_length,
4102 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004103
4104 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4105 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004106
Gilles Peskine8817f612018-12-18 00:18:46 +01004107 PSA_ASSERT( psa_cipher_update( &operation,
4108 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004109 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004110 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004111 TEST_LE_U( function_output_length,
4112 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4113 TEST_LE_U( function_output_length,
4114 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004115 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004116
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004117 PSA_ASSERT( psa_cipher_finish( &operation,
4118 output2 + output2_length,
4119 output2_buffer_size - output2_length,
4120 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004121 TEST_LE_U( function_output_length,
4122 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4123 TEST_LE_U( function_output_length,
4124 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004125 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004126
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004127 PSA_ASSERT( psa_cipher_abort( &operation ) );
4128 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4129 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004130
Gilles Peskine50e586b2018-06-08 14:28:46 +02004131exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004132 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004133 mbedtls_free( output1 );
4134 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004135 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004136 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004137}
4138/* END_CASE */
4139
4140/* BEGIN_CASE */
4141void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004142 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004143 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004144 int first_part_size_arg,
4145 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004146 data_t *expected_output,
4147 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004148{
Ronald Cron5425a212020-08-04 14:58:35 +02004149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004150 psa_key_type_t key_type = key_type_arg;
4151 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004152 psa_status_t status;
4153 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004154 size_t first_part_size = first_part_size_arg;
4155 size_t output1_length = output1_length_arg;
4156 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004157 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004158 size_t output_buffer_size = 0;
4159 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004160 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004161 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004163
Gilles Peskine8817f612018-12-18 00:18:46 +01004164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004165
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004166 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4167 psa_set_key_algorithm( &attributes, alg );
4168 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004169
Ronald Cron5425a212020-08-04 14:58:35 +02004170 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4171 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004172
Ronald Cron5425a212020-08-04 14:58:35 +02004173 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004174
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004175 if( iv->len > 0 )
4176 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004177 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004178 }
4179
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004180 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4181 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004182 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004183
Gilles Peskine7be11a72022-04-14 00:12:57 +02004184 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004185 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4186 output, output_buffer_size,
4187 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004188 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004189 TEST_LE_U( function_output_length,
4190 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4191 TEST_LE_U( function_output_length,
4192 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004193 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004194
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004195 if( first_part_size < input->len )
4196 {
4197 PSA_ASSERT( psa_cipher_update( &operation,
4198 input->x + first_part_size,
4199 input->len - first_part_size,
4200 ( output_buffer_size == 0 ? NULL :
4201 output + total_output_length ),
4202 output_buffer_size - total_output_length,
4203 &function_output_length ) );
4204 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004205 TEST_LE_U( function_output_length,
4206 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4207 alg,
4208 input->len - first_part_size ) );
4209 TEST_LE_U( function_output_length,
4210 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004211 total_output_length += function_output_length;
4212 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004213
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004214 status = psa_cipher_finish( &operation,
4215 ( output_buffer_size == 0 ? NULL :
4216 output + total_output_length ),
4217 output_buffer_size - total_output_length,
4218 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004219 TEST_LE_U( function_output_length,
4220 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4221 TEST_LE_U( function_output_length,
4222 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004223 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004224 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004225
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004226 if( expected_status == PSA_SUCCESS )
4227 {
4228 PSA_ASSERT( psa_cipher_abort( &operation ) );
4229
4230 ASSERT_COMPARE( expected_output->x, expected_output->len,
4231 output, total_output_length );
4232 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233
4234exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004235 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004236 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004237 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004238 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004239}
4240/* END_CASE */
4241
4242/* BEGIN_CASE */
4243void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004244 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004245 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004246 int first_part_size_arg,
4247 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004248 data_t *expected_output,
4249 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004250{
Ronald Cron5425a212020-08-04 14:58:35 +02004251 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004252 psa_key_type_t key_type = key_type_arg;
4253 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004254 psa_status_t status;
4255 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004256 size_t first_part_size = first_part_size_arg;
4257 size_t output1_length = output1_length_arg;
4258 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004259 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004260 size_t output_buffer_size = 0;
4261 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004262 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004263 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004264 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004265
Gilles Peskine8817f612018-12-18 00:18:46 +01004266 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004268 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4269 psa_set_key_algorithm( &attributes, alg );
4270 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004271
Ronald Cron5425a212020-08-04 14:58:35 +02004272 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4273 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004274
Ronald Cron5425a212020-08-04 14:58:35 +02004275 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004276
Steven Cooreman177deba2020-09-07 17:14:14 +02004277 if( iv->len > 0 )
4278 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004279 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004280 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004281
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004282 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4283 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004284 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004285
Gilles Peskine7be11a72022-04-14 00:12:57 +02004286 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004287 PSA_ASSERT( psa_cipher_update( &operation,
4288 input->x, first_part_size,
4289 output, output_buffer_size,
4290 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004291 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004292 TEST_LE_U( function_output_length,
4293 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4294 TEST_LE_U( function_output_length,
4295 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004296 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004297
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004298 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004299 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004300 PSA_ASSERT( psa_cipher_update( &operation,
4301 input->x + first_part_size,
4302 input->len - first_part_size,
4303 ( output_buffer_size == 0 ? NULL :
4304 output + total_output_length ),
4305 output_buffer_size - total_output_length,
4306 &function_output_length ) );
4307 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004308 TEST_LE_U( function_output_length,
4309 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4310 alg,
4311 input->len - first_part_size ) );
4312 TEST_LE_U( function_output_length,
4313 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004314 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004315 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316
Gilles Peskine50e586b2018-06-08 14:28:46 +02004317 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004318 ( output_buffer_size == 0 ? NULL :
4319 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004320 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004321 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004322 TEST_LE_U( function_output_length,
4323 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4324 TEST_LE_U( function_output_length,
4325 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004326 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004327 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004328
4329 if( expected_status == PSA_SUCCESS )
4330 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004331 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004332
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004333 ASSERT_COMPARE( expected_output->x, expected_output->len,
4334 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004335 }
4336
Gilles Peskine50e586b2018-06-08 14:28:46 +02004337exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004338 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004339 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004340 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004341 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004342}
4343/* END_CASE */
4344
Gilles Peskine50e586b2018-06-08 14:28:46 +02004345/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004346void cipher_decrypt_fail( int alg_arg,
4347 int key_type_arg,
4348 data_t *key_data,
4349 data_t *iv,
4350 data_t *input_arg,
4351 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004352{
4353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4354 psa_status_t status;
4355 psa_key_type_t key_type = key_type_arg;
4356 psa_algorithm_t alg = alg_arg;
4357 psa_status_t expected_status = expected_status_arg;
4358 unsigned char *input = NULL;
4359 size_t input_buffer_size = 0;
4360 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004361 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004362 size_t output_buffer_size = 0;
4363 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004364 size_t function_output_length;
4365 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4367
4368 if ( PSA_ERROR_BAD_STATE != expected_status )
4369 {
4370 PSA_ASSERT( psa_crypto_init( ) );
4371
4372 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4373 psa_set_key_algorithm( &attributes, alg );
4374 psa_set_key_type( &attributes, key_type );
4375
4376 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4377 &key ) );
4378 }
4379
4380 /* Allocate input buffer and copy the iv and the plaintext */
4381 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4382 if ( input_buffer_size > 0 )
4383 {
4384 ASSERT_ALLOC( input, input_buffer_size );
4385 memcpy( input, iv->x, iv->len );
4386 memcpy( input + iv->len, input_arg->x, input_arg->len );
4387 }
4388
4389 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4390 ASSERT_ALLOC( output, output_buffer_size );
4391
Neil Armstrong66a479f2022-02-07 15:41:19 +01004392 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004393 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4394 output_buffer_size, &output_length );
4395 TEST_EQUAL( status, expected_status );
4396
Neil Armstrong66a479f2022-02-07 15:41:19 +01004397 /* Decrypt, multi-part */
4398 status = psa_cipher_decrypt_setup( &operation, key, alg );
4399 if( status == PSA_SUCCESS )
4400 {
4401 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4402 input_arg->len ) +
4403 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4404 ASSERT_ALLOC( output_multi, output_buffer_size );
4405
4406 if( iv->len > 0 )
4407 {
4408 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4409
4410 if( status != PSA_SUCCESS )
4411 TEST_EQUAL( status, expected_status );
4412 }
4413
4414 if( status == PSA_SUCCESS )
4415 {
4416 status = psa_cipher_update( &operation,
4417 input_arg->x, input_arg->len,
4418 output_multi, output_buffer_size,
4419 &function_output_length );
4420 if( status == PSA_SUCCESS )
4421 {
4422 output_length = function_output_length;
4423
4424 status = psa_cipher_finish( &operation,
4425 output_multi + output_length,
4426 output_buffer_size - output_length,
4427 &function_output_length );
4428
4429 TEST_EQUAL( status, expected_status );
4430 }
4431 else
4432 {
4433 TEST_EQUAL( status, expected_status );
4434 }
4435 }
4436 else
4437 {
4438 TEST_EQUAL( status, expected_status );
4439 }
4440 }
4441 else
4442 {
4443 TEST_EQUAL( status, expected_status );
4444 }
4445
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004446exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004447 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004448 mbedtls_free( input );
4449 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004450 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004451 psa_destroy_key( key );
4452 PSA_DONE( );
4453}
4454/* END_CASE */
4455
4456/* BEGIN_CASE */
4457void cipher_decrypt( int alg_arg,
4458 int key_type_arg,
4459 data_t *key_data,
4460 data_t *iv,
4461 data_t *input_arg,
4462 data_t *expected_output )
4463{
4464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4465 psa_key_type_t key_type = key_type_arg;
4466 psa_algorithm_t alg = alg_arg;
4467 unsigned char *input = NULL;
4468 size_t input_buffer_size = 0;
4469 unsigned char *output = NULL;
4470 size_t output_buffer_size = 0;
4471 size_t output_length = 0;
4472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4473
4474 PSA_ASSERT( psa_crypto_init( ) );
4475
4476 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4477 psa_set_key_algorithm( &attributes, alg );
4478 psa_set_key_type( &attributes, key_type );
4479
4480 /* Allocate input buffer and copy the iv and the plaintext */
4481 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4482 if ( input_buffer_size > 0 )
4483 {
4484 ASSERT_ALLOC( input, input_buffer_size );
4485 memcpy( input, iv->x, iv->len );
4486 memcpy( input + iv->len, input_arg->x, input_arg->len );
4487 }
4488
4489 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4490 ASSERT_ALLOC( output, output_buffer_size );
4491
4492 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4493 &key ) );
4494
4495 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4496 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004497 TEST_LE_U( output_length,
4498 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4499 TEST_LE_U( output_length,
4500 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004501
4502 ASSERT_COMPARE( expected_output->x, expected_output->len,
4503 output, output_length );
4504exit:
4505 mbedtls_free( input );
4506 mbedtls_free( output );
4507 psa_destroy_key( key );
4508 PSA_DONE( );
4509}
4510/* END_CASE */
4511
4512/* BEGIN_CASE */
4513void cipher_verify_output( int alg_arg,
4514 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004515 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004516 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004517{
Ronald Cron5425a212020-08-04 14:58:35 +02004518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004519 psa_key_type_t key_type = key_type_arg;
4520 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004521 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004522 size_t output1_size = 0;
4523 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004524 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004525 size_t output2_size = 0;
4526 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004528
Gilles Peskine8817f612018-12-18 00:18:46 +01004529 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004530
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004531 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4532 psa_set_key_algorithm( &attributes, alg );
4533 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004534
Ronald Cron5425a212020-08-04 14:58:35 +02004535 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4536 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004537 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004538 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004539
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004540 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4541 output1, output1_size,
4542 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004543 TEST_LE_U( output1_length,
4544 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4545 TEST_LE_U( output1_length,
4546 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004547
4548 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004549 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004550
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004551 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4552 output2, output2_size,
4553 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004554 TEST_LE_U( output2_length,
4555 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4556 TEST_LE_U( output2_length,
4557 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004558
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004559 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004560
4561exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004562 mbedtls_free( output1 );
4563 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004564 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004565 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004566}
4567/* END_CASE */
4568
4569/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004570void cipher_verify_output_multipart( int alg_arg,
4571 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004572 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004573 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004574 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004575{
Ronald Cron5425a212020-08-04 14:58:35 +02004576 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004577 psa_key_type_t key_type = key_type_arg;
4578 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004579 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004580 unsigned char iv[16] = {0};
4581 size_t iv_size = 16;
4582 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004583 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004584 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004585 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004586 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004587 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004588 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004589 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004590 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4591 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004593
Gilles Peskine8817f612018-12-18 00:18:46 +01004594 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004595
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004596 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4597 psa_set_key_algorithm( &attributes, alg );
4598 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004599
Ronald Cron5425a212020-08-04 14:58:35 +02004600 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4601 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004602
Ronald Cron5425a212020-08-04 14:58:35 +02004603 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4604 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004605
Steven Cooreman177deba2020-09-07 17:14:14 +02004606 if( alg != PSA_ALG_ECB_NO_PADDING )
4607 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004608 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4609 iv, iv_size,
4610 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004611 }
4612
gabor-mezei-armceface22021-01-21 12:26:17 +01004613 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004614 TEST_LE_U( output1_buffer_size,
4615 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004616 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004617
Gilles Peskine7be11a72022-04-14 00:12:57 +02004618 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004619
Gilles Peskine8817f612018-12-18 00:18:46 +01004620 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4621 output1, output1_buffer_size,
4622 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004623 TEST_LE_U( function_output_length,
4624 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4625 TEST_LE_U( function_output_length,
4626 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004627 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004628
Gilles Peskine8817f612018-12-18 00:18:46 +01004629 PSA_ASSERT( psa_cipher_update( &operation1,
4630 input->x + first_part_size,
4631 input->len - first_part_size,
4632 output1, output1_buffer_size,
4633 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004634 TEST_LE_U( function_output_length,
4635 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4636 alg,
4637 input->len - first_part_size ) );
4638 TEST_LE_U( function_output_length,
4639 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004640 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004641
Gilles Peskine8817f612018-12-18 00:18:46 +01004642 PSA_ASSERT( psa_cipher_finish( &operation1,
4643 output1 + output1_length,
4644 output1_buffer_size - output1_length,
4645 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004646 TEST_LE_U( function_output_length,
4647 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4648 TEST_LE_U( function_output_length,
4649 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004650 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004651
Gilles Peskine8817f612018-12-18 00:18:46 +01004652 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004653
Gilles Peskine048b7f02018-06-08 14:20:49 +02004654 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004655 TEST_LE_U( output2_buffer_size,
4656 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4657 TEST_LE_U( output2_buffer_size,
4658 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004659 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004660
Steven Cooreman177deba2020-09-07 17:14:14 +02004661 if( iv_length > 0 )
4662 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004663 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4664 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004665 }
Moran Pekerded84402018-06-06 16:36:50 +03004666
Gilles Peskine8817f612018-12-18 00:18:46 +01004667 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4668 output2, output2_buffer_size,
4669 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004670 TEST_LE_U( function_output_length,
4671 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4672 TEST_LE_U( function_output_length,
4673 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004674 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004675
Gilles Peskine8817f612018-12-18 00:18:46 +01004676 PSA_ASSERT( psa_cipher_update( &operation2,
4677 output1 + first_part_size,
4678 output1_length - first_part_size,
4679 output2, output2_buffer_size,
4680 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004681 TEST_LE_U( function_output_length,
4682 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4683 alg,
4684 output1_length - first_part_size ) );
4685 TEST_LE_U( function_output_length,
4686 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004687 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004688
Gilles Peskine8817f612018-12-18 00:18:46 +01004689 PSA_ASSERT( psa_cipher_finish( &operation2,
4690 output2 + output2_length,
4691 output2_buffer_size - output2_length,
4692 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004693 TEST_LE_U( function_output_length,
4694 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4695 TEST_LE_U( function_output_length,
4696 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004697 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004698
Gilles Peskine8817f612018-12-18 00:18:46 +01004699 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004700
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004701 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004702
4703exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004704 psa_cipher_abort( &operation1 );
4705 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004706 mbedtls_free( output1 );
4707 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004708 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004709 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004710}
4711/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004712
Gilles Peskine20035e32018-02-03 22:44:14 +01004713/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004714void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004715 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004716 data_t *nonce,
4717 data_t *additional_data,
4718 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004719 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004720{
Ronald Cron5425a212020-08-04 14:58:35 +02004721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004722 psa_key_type_t key_type = key_type_arg;
4723 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004724 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725 unsigned char *output_data = NULL;
4726 size_t output_size = 0;
4727 size_t output_length = 0;
4728 unsigned char *output_data2 = NULL;
4729 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004730 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004731 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004733
Gilles Peskine8817f612018-12-18 00:18:46 +01004734 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004735
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004736 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4737 psa_set_key_algorithm( &attributes, alg );
4738 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004739
Gilles Peskine049c7532019-05-15 20:22:09 +02004740 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004741 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004742 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4743 key_bits = psa_get_key_bits( &attributes );
4744
4745 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4746 alg );
4747 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4748 * should be exact. */
4749 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4750 expected_result != PSA_ERROR_NOT_SUPPORTED )
4751 {
4752 TEST_EQUAL( output_size,
4753 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004754 TEST_LE_U( output_size,
4755 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004756 }
4757 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004758
Steven Cooremanf49478b2021-02-15 15:19:25 +01004759 status = psa_aead_encrypt( key, alg,
4760 nonce->x, nonce->len,
4761 additional_data->x,
4762 additional_data->len,
4763 input_data->x, input_data->len,
4764 output_data, output_size,
4765 &output_length );
4766
4767 /* If the operation is not supported, just skip and not fail in case the
4768 * encryption involves a common limitation of cryptography hardwares and
4769 * an alternative implementation. */
4770 if( status == PSA_ERROR_NOT_SUPPORTED )
4771 {
4772 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4773 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4774 }
4775
4776 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004777
4778 if( PSA_SUCCESS == expected_result )
4779 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004780 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004781
Gilles Peskine003a4a92019-05-14 16:09:40 +02004782 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4783 * should be exact. */
4784 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004785 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004786
Gilles Peskine7be11a72022-04-14 00:12:57 +02004787 TEST_LE_U( input_data->len,
4788 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004789
Ronald Cron5425a212020-08-04 14:58:35 +02004790 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004791 nonce->x, nonce->len,
4792 additional_data->x,
4793 additional_data->len,
4794 output_data, output_length,
4795 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004796 &output_length2 ),
4797 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004798
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004799 ASSERT_COMPARE( input_data->x, input_data->len,
4800 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004801 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004802
Gilles Peskinea1cac842018-06-11 19:33:02 +02004803exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004804 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004805 mbedtls_free( output_data );
4806 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004807 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004808}
4809/* END_CASE */
4810
4811/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004812void aead_encrypt( int key_type_arg, data_t *key_data,
4813 int alg_arg,
4814 data_t *nonce,
4815 data_t *additional_data,
4816 data_t *input_data,
4817 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004818{
Ronald Cron5425a212020-08-04 14:58:35 +02004819 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004820 psa_key_type_t key_type = key_type_arg;
4821 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004822 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004823 unsigned char *output_data = NULL;
4824 size_t output_size = 0;
4825 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004826 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004827 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004828
Gilles Peskine8817f612018-12-18 00:18:46 +01004829 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004830
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4832 psa_set_key_algorithm( &attributes, alg );
4833 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004834
Gilles Peskine049c7532019-05-15 20:22:09 +02004835 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004836 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004837 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4838 key_bits = psa_get_key_bits( &attributes );
4839
4840 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4841 alg );
4842 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4843 * should be exact. */
4844 TEST_EQUAL( output_size,
4845 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004846 TEST_LE_U( output_size,
4847 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004848 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004849
Steven Cooremand588ea12021-01-11 19:36:04 +01004850 status = psa_aead_encrypt( key, alg,
4851 nonce->x, nonce->len,
4852 additional_data->x, additional_data->len,
4853 input_data->x, input_data->len,
4854 output_data, output_size,
4855 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004856
Ronald Cron28a45ed2021-02-09 20:35:42 +01004857 /* If the operation is not supported, just skip and not fail in case the
4858 * encryption involves a common limitation of cryptography hardwares and
4859 * an alternative implementation. */
4860 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004861 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004862 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4863 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004864 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004865
4866 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004867 ASSERT_COMPARE( expected_result->x, expected_result->len,
4868 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004869
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004871 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004872 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004873 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004874}
4875/* END_CASE */
4876
4877/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004878void aead_decrypt( int key_type_arg, data_t *key_data,
4879 int alg_arg,
4880 data_t *nonce,
4881 data_t *additional_data,
4882 data_t *input_data,
4883 data_t *expected_data,
4884 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004885{
Ronald Cron5425a212020-08-04 14:58:35 +02004886 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004887 psa_key_type_t key_type = key_type_arg;
4888 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004889 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004890 unsigned char *output_data = NULL;
4891 size_t output_size = 0;
4892 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004894 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004895 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004896
Gilles Peskine8817f612018-12-18 00:18:46 +01004897 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004898
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004899 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4900 psa_set_key_algorithm( &attributes, alg );
4901 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004902
Gilles Peskine049c7532019-05-15 20:22:09 +02004903 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004904 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004905 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4906 key_bits = psa_get_key_bits( &attributes );
4907
4908 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4909 alg );
4910 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4911 expected_result != PSA_ERROR_NOT_SUPPORTED )
4912 {
4913 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4914 * should be exact. */
4915 TEST_EQUAL( output_size,
4916 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004917 TEST_LE_U( output_size,
4918 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004919 }
4920 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004921
Steven Cooremand588ea12021-01-11 19:36:04 +01004922 status = psa_aead_decrypt( key, alg,
4923 nonce->x, nonce->len,
4924 additional_data->x,
4925 additional_data->len,
4926 input_data->x, input_data->len,
4927 output_data, output_size,
4928 &output_length );
4929
Ronald Cron28a45ed2021-02-09 20:35:42 +01004930 /* If the operation is not supported, just skip and not fail in case the
4931 * decryption involves a common limitation of cryptography hardwares and
4932 * an alternative implementation. */
4933 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004934 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004935 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4936 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004937 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004938
4939 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004940
Gilles Peskine2d277862018-06-18 15:41:12 +02004941 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004942 ASSERT_COMPARE( expected_data->x, expected_data->len,
4943 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004944
Gilles Peskinea1cac842018-06-11 19:33:02 +02004945exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004946 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004947 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004948 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004949}
4950/* END_CASE */
4951
4952/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004953void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4954 int alg_arg,
4955 data_t *nonce,
4956 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004957 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004958 int do_set_lengths,
4959 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004960{
Paul Elliottd3f82412021-06-16 16:52:21 +01004961 size_t ad_part_len = 0;
4962 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004963 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004964
Paul Elliott32f46ba2021-09-23 18:24:36 +01004965 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004966 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004967 mbedtls_test_set_step( ad_part_len );
4968
4969 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004970 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004971 if( ad_part_len & 0x01 )
4972 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4973 else
4974 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004975 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004976
4977 /* Split ad into length(ad_part_len) parts. */
4978 if( !aead_multipart_internal_func( key_type_arg, key_data,
4979 alg_arg, nonce,
4980 additional_data,
4981 ad_part_len,
4982 input_data, -1,
4983 set_lengths_method,
4984 expected_output,
4985 1, 0 ) )
4986 break;
4987
4988 /* length(0) part, length(ad_part_len) part, length(0) part... */
4989 mbedtls_test_set_step( 1000 + ad_part_len );
4990
4991 if( !aead_multipart_internal_func( key_type_arg, key_data,
4992 alg_arg, nonce,
4993 additional_data,
4994 ad_part_len,
4995 input_data, -1,
4996 set_lengths_method,
4997 expected_output,
4998 1, 1 ) )
4999 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005000 }
Paul Elliottd3f82412021-06-16 16:52:21 +01005001
Paul Elliott32f46ba2021-09-23 18:24:36 +01005002 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005003 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005004 /* Split data into length(data_part_len) parts. */
5005 mbedtls_test_set_step( 2000 + data_part_len );
5006
5007 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005008 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005009 if( data_part_len & 0x01 )
5010 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5011 else
5012 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005013 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005014
Paul Elliott32f46ba2021-09-23 18:24:36 +01005015 if( !aead_multipart_internal_func( key_type_arg, key_data,
5016 alg_arg, nonce,
5017 additional_data, -1,
5018 input_data, data_part_len,
5019 set_lengths_method,
5020 expected_output,
5021 1, 0 ) )
5022 break;
5023
5024 /* length(0) part, length(data_part_len) part, length(0) part... */
5025 mbedtls_test_set_step( 3000 + data_part_len );
5026
5027 if( !aead_multipart_internal_func( key_type_arg, key_data,
5028 alg_arg, nonce,
5029 additional_data, -1,
5030 input_data, data_part_len,
5031 set_lengths_method,
5032 expected_output,
5033 1, 1 ) )
5034 break;
5035 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005036
Paul Elliott8fc45162021-06-23 16:06:01 +01005037 /* Goto is required to silence warnings about unused labels, as we
5038 * don't actually do any test assertions in this function. */
5039 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005040}
5041/* END_CASE */
5042
5043/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005044void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5045 int alg_arg,
5046 data_t *nonce,
5047 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005048 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005049 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005050 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005051{
Paul Elliottd3f82412021-06-16 16:52:21 +01005052 size_t ad_part_len = 0;
5053 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005054 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005055
Paul Elliott32f46ba2021-09-23 18:24:36 +01005056 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005057 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005058 /* Split ad into length(ad_part_len) parts. */
5059 mbedtls_test_set_step( ad_part_len );
5060
5061 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005062 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063 if( ad_part_len & 0x01 )
5064 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5065 else
5066 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005067 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005068
5069 if( !aead_multipart_internal_func( key_type_arg, key_data,
5070 alg_arg, nonce,
5071 additional_data,
5072 ad_part_len,
5073 input_data, -1,
5074 set_lengths_method,
5075 expected_output,
5076 0, 0 ) )
5077 break;
5078
5079 /* length(0) part, length(ad_part_len) part, length(0) part... */
5080 mbedtls_test_set_step( 1000 + ad_part_len );
5081
5082 if( !aead_multipart_internal_func( key_type_arg, key_data,
5083 alg_arg, nonce,
5084 additional_data,
5085 ad_part_len,
5086 input_data, -1,
5087 set_lengths_method,
5088 expected_output,
5089 0, 1 ) )
5090 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005091 }
5092
Paul Elliott32f46ba2021-09-23 18:24:36 +01005093 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005094 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005095 /* Split data into length(data_part_len) parts. */
5096 mbedtls_test_set_step( 2000 + data_part_len );
5097
5098 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005099 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005100 if( data_part_len & 0x01 )
5101 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5102 else
5103 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005104 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005105
5106 if( !aead_multipart_internal_func( key_type_arg, key_data,
5107 alg_arg, nonce,
5108 additional_data, -1,
5109 input_data, data_part_len,
5110 set_lengths_method,
5111 expected_output,
5112 0, 0 ) )
5113 break;
5114
5115 /* length(0) part, length(data_part_len) part, length(0) part... */
5116 mbedtls_test_set_step( 3000 + data_part_len );
5117
5118 if( !aead_multipart_internal_func( key_type_arg, key_data,
5119 alg_arg, nonce,
5120 additional_data, -1,
5121 input_data, data_part_len,
5122 set_lengths_method,
5123 expected_output,
5124 0, 1 ) )
5125 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005126 }
5127
Paul Elliott8fc45162021-06-23 16:06:01 +01005128 /* Goto is required to silence warnings about unused labels, as we
5129 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005130 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005131}
5132/* END_CASE */
5133
5134/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005135void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5136 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005137 int nonce_length,
5138 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005139 data_t *additional_data,
5140 data_t *input_data,
5141 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005142{
5143
5144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5145 psa_key_type_t key_type = key_type_arg;
5146 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005147 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005148 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5150 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005151 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005152 size_t actual_nonce_length = 0;
5153 size_t expected_nonce_length = expected_nonce_length_arg;
5154 unsigned char *output = NULL;
5155 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005156 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005157 size_t ciphertext_size = 0;
5158 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005159 size_t tag_length = 0;
5160 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005161
5162 PSA_ASSERT( psa_crypto_init( ) );
5163
5164 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5165 psa_set_key_algorithm( & attributes, alg );
5166 psa_set_key_type( & attributes, key_type );
5167
5168 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5169 &key ) );
5170
5171 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5172
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005173 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5174
Paul Elliottf1277632021-08-24 18:11:37 +01005175 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005176
Paul Elliottf1277632021-08-24 18:11:37 +01005177 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005178
Gilles Peskine7be11a72022-04-14 00:12:57 +02005179 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005180
Paul Elliottf1277632021-08-24 18:11:37 +01005181 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005182
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005183 status = psa_aead_encrypt_setup( &operation, key, alg );
5184
5185 /* If the operation is not supported, just skip and not fail in case the
5186 * encryption involves a common limitation of cryptography hardwares and
5187 * an alternative implementation. */
5188 if( status == PSA_ERROR_NOT_SUPPORTED )
5189 {
5190 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005191 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005192 }
5193
5194 PSA_ASSERT( status );
5195
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005196 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005197 nonce_length,
5198 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005199
Paul Elliott693bf312021-07-23 17:40:41 +01005200 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005201
Paul Elliottf1277632021-08-24 18:11:37 +01005202 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005203
Paul Elliott88ecbe12021-09-22 17:23:03 +01005204 if( expected_status == PSA_SUCCESS )
5205 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5206 alg ) );
5207
Gilles Peskine7be11a72022-04-14 00:12:57 +02005208 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005209
Paul Elliott693bf312021-07-23 17:40:41 +01005210 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005211 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005212 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005213 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5214 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005215
5216 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5217 additional_data->len ) );
5218
5219 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005220 output, output_size,
5221 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005222
Paul Elliottf1277632021-08-24 18:11:37 +01005223 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5224 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005225 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5226 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005227
5228exit:
5229 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005230 mbedtls_free( output );
5231 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005232 psa_aead_abort( &operation );
5233 PSA_DONE( );
5234}
5235/* END_CASE */
5236
5237/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005238void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5239 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005240 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005241 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005242 data_t *additional_data,
5243 data_t *input_data,
5244 int expected_status_arg )
5245{
5246
5247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5248 psa_key_type_t key_type = key_type_arg;
5249 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005250 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005251 uint8_t *nonce_buffer = NULL;
5252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5253 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5254 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005255 unsigned char *output = NULL;
5256 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005257 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005258 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005259 size_t ciphertext_size = 0;
5260 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005261 size_t tag_length = 0;
5262 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005263 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005264 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005265
5266 PSA_ASSERT( psa_crypto_init( ) );
5267
5268 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5269 psa_set_key_algorithm( &attributes, alg );
5270 psa_set_key_type( &attributes, key_type );
5271
5272 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5273 &key ) );
5274
5275 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5276
5277 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5278
Paul Elliott6f0e7202021-08-25 12:57:18 +01005279 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005280
Paul Elliott6f0e7202021-08-25 12:57:18 +01005281 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005282
Gilles Peskine7be11a72022-04-14 00:12:57 +02005283 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005284
Paul Elliott6f0e7202021-08-25 12:57:18 +01005285 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005286
Paul Elliott863864a2021-07-23 17:28:31 +01005287 status = psa_aead_encrypt_setup( &operation, key, alg );
5288
5289 /* If the operation is not supported, just skip and not fail in case the
5290 * encryption involves a common limitation of cryptography hardwares and
5291 * an alternative implementation. */
5292 if( status == PSA_ERROR_NOT_SUPPORTED )
5293 {
5294 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005295 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005296 }
5297
5298 PSA_ASSERT( status );
5299
Paul Elliott4023ffd2021-09-10 16:21:22 +01005300 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5301 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005302 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005303 /* Arbitrary size buffer, to test zero length valid buffer. */
5304 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005305 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005306 }
5307 else
5308 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005309 /* If length is zero, then this will return NULL. */
5310 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005311 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005312
Paul Elliott4023ffd2021-09-10 16:21:22 +01005313 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005314 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005315 for( index = 0; index < nonce_length - 1; ++index )
5316 {
5317 nonce_buffer[index] = 'a' + index;
5318 }
Paul Elliott66696b52021-08-16 18:42:41 +01005319 }
Paul Elliott863864a2021-07-23 17:28:31 +01005320 }
5321
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005322 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5323 {
5324 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5325 input_data->len ) );
5326 }
5327
Paul Elliott6f0e7202021-08-25 12:57:18 +01005328 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005329
Paul Elliott693bf312021-07-23 17:40:41 +01005330 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005331
5332 if( expected_status == PSA_SUCCESS )
5333 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005334 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5335 {
5336 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5337 input_data->len ) );
5338 }
5339 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5340 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005341
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005342 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5343 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5344 additional_data->len ),
5345 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005346
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005347 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005348 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005349 &ciphertext_length ),
5350 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005351
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005352 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005353 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005354 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5355 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005356 }
5357
5358exit:
5359 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005360 mbedtls_free( output );
5361 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005362 mbedtls_free( nonce_buffer );
5363 psa_aead_abort( &operation );
5364 PSA_DONE( );
5365}
5366/* END_CASE */
5367
5368/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005369void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5370 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005371 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005372 data_t *nonce,
5373 data_t *additional_data,
5374 data_t *input_data,
5375 int expected_status_arg )
5376{
5377
5378 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5379 psa_key_type_t key_type = key_type_arg;
5380 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005381 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5383 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5384 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005385 unsigned char *output = NULL;
5386 unsigned char *ciphertext = NULL;
5387 size_t output_size = output_size_arg;
5388 size_t ciphertext_size = 0;
5389 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005390 size_t tag_length = 0;
5391 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5392
5393 PSA_ASSERT( psa_crypto_init( ) );
5394
5395 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5396 psa_set_key_algorithm( &attributes, alg );
5397 psa_set_key_type( &attributes, key_type );
5398
5399 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5400 &key ) );
5401
5402 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5403
Paul Elliottc6d11d02021-09-01 12:04:23 +01005404 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005405
Paul Elliottc6d11d02021-09-01 12:04:23 +01005406 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005407
Paul Elliottc6d11d02021-09-01 12:04:23 +01005408 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005409
Paul Elliott43fbda62021-07-23 18:30:59 +01005410 status = psa_aead_encrypt_setup( &operation, key, alg );
5411
5412 /* If the operation is not supported, just skip and not fail in case the
5413 * encryption involves a common limitation of cryptography hardwares and
5414 * an alternative implementation. */
5415 if( status == PSA_ERROR_NOT_SUPPORTED )
5416 {
5417 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5418 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5419 }
5420
5421 PSA_ASSERT( status );
5422
Paul Elliott47b9a142021-10-07 15:04:57 +01005423 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5424 input_data->len ) );
5425
Paul Elliott43fbda62021-07-23 18:30:59 +01005426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5427
5428 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5429 additional_data->len ) );
5430
5431 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005432 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005433
5434 TEST_EQUAL( status, expected_status );
5435
5436 if( expected_status == PSA_SUCCESS )
5437 {
5438 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005439 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5440 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005441 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5442 }
5443
5444exit:
5445 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005446 mbedtls_free( output );
5447 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005448 psa_aead_abort( &operation );
5449 PSA_DONE( );
5450}
5451/* END_CASE */
5452
Paul Elliott91b021e2021-07-23 18:52:31 +01005453/* BEGIN_CASE */
5454void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5455 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005456 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005457 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005458 data_t *nonce,
5459 data_t *additional_data,
5460 data_t *input_data,
5461 int expected_status_arg )
5462{
5463
5464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5465 psa_key_type_t key_type = key_type_arg;
5466 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005467 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005468 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5469 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5470 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005471 unsigned char *ciphertext = NULL;
5472 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005473 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005474 size_t ciphertext_size = 0;
5475 size_t ciphertext_length = 0;
5476 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005477 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005478 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005479
5480 PSA_ASSERT( psa_crypto_init( ) );
5481
5482 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5483 psa_set_key_algorithm( &attributes, alg );
5484 psa_set_key_type( &attributes, key_type );
5485
5486 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5487 &key ) );
5488
5489 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5490
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005491 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005492
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005493 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005494
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005495 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005496
Paul Elliott719c1322021-09-13 18:27:22 +01005497 ASSERT_ALLOC( tag_buffer, tag_size );
5498
Paul Elliott91b021e2021-07-23 18:52:31 +01005499 status = psa_aead_encrypt_setup( &operation, key, alg );
5500
5501 /* If the operation is not supported, just skip and not fail in case the
5502 * encryption involves a common limitation of cryptography hardwares and
5503 * an alternative implementation. */
5504 if( status == PSA_ERROR_NOT_SUPPORTED )
5505 {
5506 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5507 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5508 }
5509
5510 PSA_ASSERT( status );
5511
5512 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5513
Paul Elliott76bda482021-10-07 17:07:23 +01005514 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5515 input_data->len ) );
5516
Paul Elliott91b021e2021-07-23 18:52:31 +01005517 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5518 additional_data->len ) );
5519
5520 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005521 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005522
5523 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005524 status = psa_aead_finish( &operation, finish_ciphertext,
5525 finish_ciphertext_size,
5526 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005527 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005528
5529 TEST_EQUAL( status, expected_status );
5530
5531exit:
5532 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005533 mbedtls_free( ciphertext );
5534 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005535 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005536 psa_aead_abort( &operation );
5537 PSA_DONE( );
5538}
5539/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005540
5541/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005542void aead_multipart_verify( int key_type_arg, data_t *key_data,
5543 int alg_arg,
5544 data_t *nonce,
5545 data_t *additional_data,
5546 data_t *input_data,
5547 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005548 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005549 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005550 int expected_status_arg )
5551{
5552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5553 psa_key_type_t key_type = key_type_arg;
5554 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005555 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005556 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5557 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5558 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005559 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005560 unsigned char *plaintext = NULL;
5561 unsigned char *finish_plaintext = NULL;
5562 size_t plaintext_size = 0;
5563 size_t plaintext_length = 0;
5564 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005565 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005566 unsigned char *tag_buffer = NULL;
5567 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005568
5569 PSA_ASSERT( psa_crypto_init( ) );
5570
5571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5572 psa_set_key_algorithm( &attributes, alg );
5573 psa_set_key_type( &attributes, key_type );
5574
5575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5576 &key ) );
5577
5578 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5579
5580 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5581 input_data->len );
5582
5583 ASSERT_ALLOC( plaintext, plaintext_size );
5584
5585 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5586
5587 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5588
Paul Elliott9961a662021-09-17 19:19:02 +01005589 status = psa_aead_decrypt_setup( &operation, key, alg );
5590
5591 /* If the operation is not supported, just skip and not fail in case the
5592 * encryption involves a common limitation of cryptography hardwares and
5593 * an alternative implementation. */
5594 if( status == PSA_ERROR_NOT_SUPPORTED )
5595 {
5596 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5597 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5598 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005599 TEST_EQUAL( status, expected_setup_status );
5600
5601 if( status != PSA_SUCCESS )
5602 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005603
5604 PSA_ASSERT( status );
5605
5606 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5607
Paul Elliottfec6f372021-10-06 17:15:02 +01005608 status = psa_aead_set_lengths( &operation, additional_data->len,
5609 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005610 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005611
Paul Elliott9961a662021-09-17 19:19:02 +01005612 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5613 additional_data->len ) );
5614
5615 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5616 input_data->len,
5617 plaintext, plaintext_size,
5618 &plaintext_length ) );
5619
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005620 if( tag_usage == USE_GIVEN_TAG )
5621 {
5622 tag_buffer = tag->x;
5623 tag_size = tag->len;
5624 }
5625
Paul Elliott9961a662021-09-17 19:19:02 +01005626 status = psa_aead_verify( &operation, finish_plaintext,
5627 verify_plaintext_size,
5628 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005629 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005630
5631 TEST_EQUAL( status, expected_status );
5632
5633exit:
5634 psa_destroy_key( key );
5635 mbedtls_free( plaintext );
5636 mbedtls_free( finish_plaintext );
5637 psa_aead_abort( &operation );
5638 PSA_DONE( );
5639}
5640/* END_CASE */
5641
Paul Elliott9961a662021-09-17 19:19:02 +01005642/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005643void aead_multipart_setup( int key_type_arg, data_t *key_data,
5644 int alg_arg, int expected_status_arg )
5645{
5646 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5647 psa_key_type_t key_type = key_type_arg;
5648 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005649 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5651 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5652 psa_status_t expected_status = expected_status_arg;
5653
5654 PSA_ASSERT( psa_crypto_init( ) );
5655
5656 psa_set_key_usage_flags( &attributes,
5657 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5658 psa_set_key_algorithm( &attributes, alg );
5659 psa_set_key_type( &attributes, key_type );
5660
5661 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5662 &key ) );
5663
Paul Elliott5221ef62021-09-19 17:33:03 +01005664 status = psa_aead_encrypt_setup( &operation, key, alg );
5665
5666 TEST_EQUAL( status, expected_status );
5667
5668 psa_aead_abort( &operation );
5669
Paul Elliott5221ef62021-09-19 17:33:03 +01005670 status = psa_aead_decrypt_setup( &operation, key, alg );
5671
5672 TEST_EQUAL(status, expected_status );
5673
5674exit:
5675 psa_destroy_key( key );
5676 psa_aead_abort( &operation );
5677 PSA_DONE( );
5678}
5679/* END_CASE */
5680
5681/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005682void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5683 int alg_arg,
5684 data_t *nonce,
5685 data_t *additional_data,
5686 data_t *input_data )
5687{
5688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5689 psa_key_type_t key_type = key_type_arg;
5690 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005691 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005692 unsigned char *output_data = NULL;
5693 unsigned char *final_data = NULL;
5694 size_t output_size = 0;
5695 size_t finish_output_size = 0;
5696 size_t output_length = 0;
5697 size_t key_bits = 0;
5698 size_t tag_length = 0;
5699 size_t tag_size = 0;
5700 size_t nonce_length = 0;
5701 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5702 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5703 size_t output_part_length = 0;
5704 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5705
5706 PSA_ASSERT( psa_crypto_init( ) );
5707
5708 psa_set_key_usage_flags( & attributes,
5709 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5710 psa_set_key_algorithm( & attributes, alg );
5711 psa_set_key_type( & attributes, key_type );
5712
5713 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5714 &key ) );
5715
5716 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5717 key_bits = psa_get_key_bits( &attributes );
5718
5719 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5720
Gilles Peskine7be11a72022-04-14 00:12:57 +02005721 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005722
5723 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5724
5725 ASSERT_ALLOC( output_data, output_size );
5726
5727 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5728
Gilles Peskine7be11a72022-04-14 00:12:57 +02005729 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005730
5731 ASSERT_ALLOC( final_data, finish_output_size );
5732
5733 /* Test all operations error without calling setup first. */
5734
Paul Elliottc23a9a02021-06-21 18:32:46 +01005735 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5736 PSA_ERROR_BAD_STATE );
5737
5738 psa_aead_abort( &operation );
5739
Paul Elliottc23a9a02021-06-21 18:32:46 +01005740 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5741 PSA_AEAD_NONCE_MAX_SIZE,
5742 &nonce_length ),
5743 PSA_ERROR_BAD_STATE );
5744
5745 psa_aead_abort( &operation );
5746
Paul Elliott481be342021-07-16 17:38:47 +01005747 /* ------------------------------------------------------- */
5748
Paul Elliottc23a9a02021-06-21 18:32:46 +01005749 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5750 input_data->len ),
5751 PSA_ERROR_BAD_STATE );
5752
5753 psa_aead_abort( &operation );
5754
Paul Elliott481be342021-07-16 17:38:47 +01005755 /* ------------------------------------------------------- */
5756
Paul Elliottc23a9a02021-06-21 18:32:46 +01005757 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5758 additional_data->len ),
5759 PSA_ERROR_BAD_STATE );
5760
5761 psa_aead_abort( &operation );
5762
Paul Elliott481be342021-07-16 17:38:47 +01005763 /* ------------------------------------------------------- */
5764
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5766 input_data->len, output_data,
5767 output_size, &output_length ),
5768 PSA_ERROR_BAD_STATE );
5769
5770 psa_aead_abort( &operation );
5771
Paul Elliott481be342021-07-16 17:38:47 +01005772 /* ------------------------------------------------------- */
5773
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5775 finish_output_size,
5776 &output_part_length,
5777 tag_buffer, tag_length,
5778 &tag_size ),
5779 PSA_ERROR_BAD_STATE );
5780
5781 psa_aead_abort( &operation );
5782
Paul Elliott481be342021-07-16 17:38:47 +01005783 /* ------------------------------------------------------- */
5784
Paul Elliottc23a9a02021-06-21 18:32:46 +01005785 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5786 finish_output_size,
5787 &output_part_length,
5788 tag_buffer,
5789 tag_length ),
5790 PSA_ERROR_BAD_STATE );
5791
5792 psa_aead_abort( &operation );
5793
5794 /* Test for double setups. */
5795
Paul Elliottc23a9a02021-06-21 18:32:46 +01005796 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5797
5798 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5799 PSA_ERROR_BAD_STATE );
5800
5801 psa_aead_abort( &operation );
5802
Paul Elliott481be342021-07-16 17:38:47 +01005803 /* ------------------------------------------------------- */
5804
Paul Elliottc23a9a02021-06-21 18:32:46 +01005805 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5806
5807 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5808 PSA_ERROR_BAD_STATE );
5809
5810 psa_aead_abort( &operation );
5811
Paul Elliott374a2be2021-07-16 17:53:40 +01005812 /* ------------------------------------------------------- */
5813
Paul Elliott374a2be2021-07-16 17:53:40 +01005814 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5815
5816 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5817 PSA_ERROR_BAD_STATE );
5818
5819 psa_aead_abort( &operation );
5820
5821 /* ------------------------------------------------------- */
5822
Paul Elliott374a2be2021-07-16 17:53:40 +01005823 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5824
5825 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5826 PSA_ERROR_BAD_STATE );
5827
5828 psa_aead_abort( &operation );
5829
Paul Elliottc23a9a02021-06-21 18:32:46 +01005830 /* Test for not setting a nonce. */
5831
Paul Elliottc23a9a02021-06-21 18:32:46 +01005832 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5833
5834 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5835 additional_data->len ),
5836 PSA_ERROR_BAD_STATE );
5837
5838 psa_aead_abort( &operation );
5839
Paul Elliott7f628422021-09-01 12:08:29 +01005840 /* ------------------------------------------------------- */
5841
Paul Elliott7f628422021-09-01 12:08:29 +01005842 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5843
5844 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5845 input_data->len, output_data,
5846 output_size, &output_length ),
5847 PSA_ERROR_BAD_STATE );
5848
5849 psa_aead_abort( &operation );
5850
Paul Elliottbdc2c682021-09-21 18:37:10 +01005851 /* ------------------------------------------------------- */
5852
Paul Elliottbdc2c682021-09-21 18:37:10 +01005853 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5854
5855 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5856 finish_output_size,
5857 &output_part_length,
5858 tag_buffer, tag_length,
5859 &tag_size ),
5860 PSA_ERROR_BAD_STATE );
5861
5862 psa_aead_abort( &operation );
5863
5864 /* ------------------------------------------------------- */
5865
Paul Elliottbdc2c682021-09-21 18:37:10 +01005866 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5867
5868 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5869 finish_output_size,
5870 &output_part_length,
5871 tag_buffer,
5872 tag_length ),
5873 PSA_ERROR_BAD_STATE );
5874
5875 psa_aead_abort( &operation );
5876
Paul Elliottc23a9a02021-06-21 18:32:46 +01005877 /* Test for double setting nonce. */
5878
Paul Elliottc23a9a02021-06-21 18:32:46 +01005879 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5880
5881 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5882
5883 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5884 PSA_ERROR_BAD_STATE );
5885
5886 psa_aead_abort( &operation );
5887
Paul Elliott374a2be2021-07-16 17:53:40 +01005888 /* Test for double generating nonce. */
5889
Paul Elliott374a2be2021-07-16 17:53:40 +01005890 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5891
5892 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5893 PSA_AEAD_NONCE_MAX_SIZE,
5894 &nonce_length ) );
5895
5896 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5897 PSA_AEAD_NONCE_MAX_SIZE,
5898 &nonce_length ),
5899 PSA_ERROR_BAD_STATE );
5900
5901
5902 psa_aead_abort( &operation );
5903
5904 /* Test for generate nonce then set and vice versa */
5905
Paul Elliott374a2be2021-07-16 17:53:40 +01005906 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5907
5908 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5909 PSA_AEAD_NONCE_MAX_SIZE,
5910 &nonce_length ) );
5911
5912 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5913 PSA_ERROR_BAD_STATE );
5914
5915 psa_aead_abort( &operation );
5916
Andrzej Kurekad837522021-12-15 15:28:49 +01005917 /* Test for generating nonce after calling set lengths */
5918
5919 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5920
5921 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5922 input_data->len ) );
5923
5924 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5925 PSA_AEAD_NONCE_MAX_SIZE,
5926 &nonce_length ) );
5927
5928 psa_aead_abort( &operation );
5929
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005930 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005931
5932 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5933
5934 if( operation.alg == PSA_ALG_CCM )
5935 {
5936 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5937 input_data->len ),
5938 PSA_ERROR_INVALID_ARGUMENT );
5939 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5940 PSA_AEAD_NONCE_MAX_SIZE,
5941 &nonce_length ),
5942 PSA_ERROR_BAD_STATE );
5943 }
5944 else
5945 {
5946 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5947 input_data->len ) );
5948 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5949 PSA_AEAD_NONCE_MAX_SIZE,
5950 &nonce_length ) );
5951 }
5952
5953 psa_aead_abort( &operation );
5954
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005955 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005956#if SIZE_MAX > UINT32_MAX
5957 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5958
5959 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5960 {
5961 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5962 input_data->len ),
5963 PSA_ERROR_INVALID_ARGUMENT );
5964 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5965 PSA_AEAD_NONCE_MAX_SIZE,
5966 &nonce_length ),
5967 PSA_ERROR_BAD_STATE );
5968 }
5969 else
5970 {
5971 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5972 input_data->len ) );
5973 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5974 PSA_AEAD_NONCE_MAX_SIZE,
5975 &nonce_length ) );
5976 }
5977
5978 psa_aead_abort( &operation );
5979#endif
5980
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005981 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005982
5983 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5984
5985 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5986 PSA_AEAD_NONCE_MAX_SIZE,
5987 &nonce_length ) );
5988
5989 if( operation.alg == PSA_ALG_CCM )
5990 {
5991 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5992 input_data->len ),
5993 PSA_ERROR_INVALID_ARGUMENT );
5994 }
5995 else
5996 {
5997 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5998 input_data->len ) );
5999 }
6000
6001 psa_aead_abort( &operation );
6002
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006003 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006004 /* Test for setting nonce after calling set lengths */
6005
6006 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6007
6008 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6009 input_data->len ) );
6010
6011 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6012
6013 psa_aead_abort( &operation );
6014
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006015 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006016
6017 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6018
6019 if( operation.alg == PSA_ALG_CCM )
6020 {
6021 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6022 input_data->len ),
6023 PSA_ERROR_INVALID_ARGUMENT );
6024 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6025 PSA_ERROR_BAD_STATE );
6026 }
6027 else
6028 {
6029 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6030 input_data->len ) );
6031 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6032 }
6033
6034 psa_aead_abort( &operation );
6035
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006036 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006037#if SIZE_MAX > UINT32_MAX
6038 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6039
6040 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6041 {
6042 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6043 input_data->len ),
6044 PSA_ERROR_INVALID_ARGUMENT );
6045 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6046 PSA_ERROR_BAD_STATE );
6047 }
6048 else
6049 {
6050 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6051 input_data->len ) );
6052 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6053 }
6054
6055 psa_aead_abort( &operation );
6056#endif
6057
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006058 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006059
6060 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6061
6062 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6063
6064 if( operation.alg == PSA_ALG_CCM )
6065 {
6066 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6067 input_data->len ),
6068 PSA_ERROR_INVALID_ARGUMENT );
6069 }
6070 else
6071 {
6072 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6073 input_data->len ) );
6074 }
6075
6076 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006077
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006078 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006079#if SIZE_MAX > UINT32_MAX
6080 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6081
6082 if( operation.alg == PSA_ALG_GCM )
6083 {
6084 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6085 SIZE_MAX ),
6086 PSA_ERROR_INVALID_ARGUMENT );
6087 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6088 PSA_ERROR_BAD_STATE );
6089 }
6090 else if ( operation.alg != PSA_ALG_CCM )
6091 {
6092 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6093 SIZE_MAX ) );
6094 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6095 }
6096
6097 psa_aead_abort( &operation );
6098
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006099 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006100 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6101
6102 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6103
6104 if( operation.alg == PSA_ALG_GCM )
6105 {
6106 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6107 SIZE_MAX ),
6108 PSA_ERROR_INVALID_ARGUMENT );
6109 }
6110 else if ( operation.alg != PSA_ALG_CCM )
6111 {
6112 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6113 SIZE_MAX ) );
6114 }
6115
6116 psa_aead_abort( &operation );
6117#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006118
6119 /* ------------------------------------------------------- */
6120
Paul Elliott374a2be2021-07-16 17:53:40 +01006121 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6122
6123 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6124
6125 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6126 PSA_AEAD_NONCE_MAX_SIZE,
6127 &nonce_length ),
6128 PSA_ERROR_BAD_STATE );
6129
6130 psa_aead_abort( &operation );
6131
Paul Elliott7220cae2021-06-22 17:25:57 +01006132 /* Test for generating nonce in decrypt setup. */
6133
Paul Elliott7220cae2021-06-22 17:25:57 +01006134 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6135
6136 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6137 PSA_AEAD_NONCE_MAX_SIZE,
6138 &nonce_length ),
6139 PSA_ERROR_BAD_STATE );
6140
6141 psa_aead_abort( &operation );
6142
Paul Elliottc23a9a02021-06-21 18:32:46 +01006143 /* Test for setting lengths twice. */
6144
Paul Elliottc23a9a02021-06-21 18:32:46 +01006145 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6146
6147 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6148
6149 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6150 input_data->len ) );
6151
6152 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6153 input_data->len ),
6154 PSA_ERROR_BAD_STATE );
6155
6156 psa_aead_abort( &operation );
6157
Andrzej Kurekad837522021-12-15 15:28:49 +01006158 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006159
Paul Elliottc23a9a02021-06-21 18:32:46 +01006160 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6161
6162 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6163
Andrzej Kurekad837522021-12-15 15:28:49 +01006164 if( operation.alg == PSA_ALG_CCM )
6165 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006166
Andrzej Kurekad837522021-12-15 15:28:49 +01006167 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6168 additional_data->len ),
6169 PSA_ERROR_BAD_STATE );
6170 }
6171 else
6172 {
6173 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6174 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006175
Andrzej Kurekad837522021-12-15 15:28:49 +01006176 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6177 input_data->len ),
6178 PSA_ERROR_BAD_STATE );
6179 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006180 psa_aead_abort( &operation );
6181
6182 /* ------------------------------------------------------- */
6183
Paul Elliottf94bd992021-09-19 18:15:59 +01006184 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6185
6186 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6187
Andrzej Kurekad837522021-12-15 15:28:49 +01006188 if( operation.alg == PSA_ALG_CCM )
6189 {
6190 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6191 input_data->len, output_data,
6192 output_size, &output_length ),
6193 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006194
Andrzej Kurekad837522021-12-15 15:28:49 +01006195 }
6196 else
6197 {
6198 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6199 input_data->len, output_data,
6200 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006201
Andrzej Kurekad837522021-12-15 15:28:49 +01006202 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6203 input_data->len ),
6204 PSA_ERROR_BAD_STATE );
6205 }
6206 psa_aead_abort( &operation );
6207
6208 /* ------------------------------------------------------- */
6209
6210 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6211
6212 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6213
6214 if( operation.alg == PSA_ALG_CCM )
6215 {
6216 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6217 finish_output_size,
6218 &output_part_length,
6219 tag_buffer, tag_length,
6220 &tag_size ) );
6221 }
6222 else
6223 {
6224 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6225 finish_output_size,
6226 &output_part_length,
6227 tag_buffer, tag_length,
6228 &tag_size ) );
6229
6230 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6231 input_data->len ),
6232 PSA_ERROR_BAD_STATE );
6233 }
6234 psa_aead_abort( &operation );
6235
6236 /* Test for setting lengths after generating nonce + already starting data. */
6237
6238 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6239
6240 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6241 PSA_AEAD_NONCE_MAX_SIZE,
6242 &nonce_length ) );
6243 if( operation.alg == PSA_ALG_CCM )
6244 {
6245
6246 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6247 additional_data->len ),
6248 PSA_ERROR_BAD_STATE );
6249 }
6250 else
6251 {
6252 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6253 additional_data->len ) );
6254
6255 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6256 input_data->len ),
6257 PSA_ERROR_BAD_STATE );
6258 }
6259 psa_aead_abort( &operation );
6260
6261 /* ------------------------------------------------------- */
6262
6263 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6264
6265 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6266 PSA_AEAD_NONCE_MAX_SIZE,
6267 &nonce_length ) );
6268 if( operation.alg == PSA_ALG_CCM )
6269 {
6270 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6271 input_data->len, output_data,
6272 output_size, &output_length ),
6273 PSA_ERROR_BAD_STATE );
6274
6275 }
6276 else
6277 {
6278 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6279 input_data->len, output_data,
6280 output_size, &output_length ) );
6281
6282 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6283 input_data->len ),
6284 PSA_ERROR_BAD_STATE );
6285 }
6286 psa_aead_abort( &operation );
6287
6288 /* ------------------------------------------------------- */
6289
6290 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6291
6292 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6293 PSA_AEAD_NONCE_MAX_SIZE,
6294 &nonce_length ) );
6295 if( operation.alg == PSA_ALG_CCM )
6296 {
6297 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6298 finish_output_size,
6299 &output_part_length,
6300 tag_buffer, tag_length,
6301 &tag_size ) );
6302 }
6303 else
6304 {
6305 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6306 finish_output_size,
6307 &output_part_length,
6308 tag_buffer, tag_length,
6309 &tag_size ) );
6310
6311 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6312 input_data->len ),
6313 PSA_ERROR_BAD_STATE );
6314 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006315 psa_aead_abort( &operation );
6316
Paul Elliott243080c2021-07-21 19:01:17 +01006317 /* Test for not sending any additional data or data after setting non zero
6318 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006319
Paul Elliottc23a9a02021-06-21 18:32:46 +01006320 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6321
6322 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6323
6324 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6325 input_data->len ) );
6326
6327 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6328 finish_output_size,
6329 &output_part_length,
6330 tag_buffer, tag_length,
6331 &tag_size ),
6332 PSA_ERROR_INVALID_ARGUMENT );
6333
6334 psa_aead_abort( &operation );
6335
Paul Elliott243080c2021-07-21 19:01:17 +01006336 /* Test for not sending any additional data or data after setting non-zero
6337 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006338
Paul Elliottc23a9a02021-06-21 18:32:46 +01006339 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6340
6341 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6342
6343 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6344 input_data->len ) );
6345
6346 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6347 finish_output_size,
6348 &output_part_length,
6349 tag_buffer,
6350 tag_length ),
6351 PSA_ERROR_INVALID_ARGUMENT );
6352
6353 psa_aead_abort( &operation );
6354
Paul Elliott243080c2021-07-21 19:01:17 +01006355 /* Test for not sending any additional data after setting a non-zero length
6356 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006357
Paul Elliottc23a9a02021-06-21 18:32:46 +01006358 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6359
6360 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6361
6362 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6363 input_data->len ) );
6364
6365 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6366 input_data->len, output_data,
6367 output_size, &output_length ),
6368 PSA_ERROR_INVALID_ARGUMENT );
6369
6370 psa_aead_abort( &operation );
6371
Paul Elliottf94bd992021-09-19 18:15:59 +01006372 /* Test for not sending any data after setting a non-zero length for it.*/
6373
Paul Elliottf94bd992021-09-19 18:15:59 +01006374 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6375
6376 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6377
6378 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6379 input_data->len ) );
6380
6381 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6382 additional_data->len ) );
6383
6384 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6385 finish_output_size,
6386 &output_part_length,
6387 tag_buffer, tag_length,
6388 &tag_size ),
6389 PSA_ERROR_INVALID_ARGUMENT );
6390
6391 psa_aead_abort( &operation );
6392
Paul Elliottb0450fe2021-09-01 15:06:26 +01006393 /* Test for sending too much additional data after setting lengths. */
6394
Paul Elliottb0450fe2021-09-01 15:06:26 +01006395 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6396
6397 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6398
6399 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6400
6401
6402 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6403 additional_data->len ),
6404 PSA_ERROR_INVALID_ARGUMENT );
6405
6406 psa_aead_abort( &operation );
6407
Paul Elliotta2a09b02021-09-22 14:56:40 +01006408 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006409
6410 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6411
6412 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6413
6414 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6415 input_data->len ) );
6416
6417 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6418 additional_data->len ) );
6419
6420 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6421 1 ),
6422 PSA_ERROR_INVALID_ARGUMENT );
6423
6424 psa_aead_abort( &operation );
6425
Paul Elliottb0450fe2021-09-01 15:06:26 +01006426 /* Test for sending too much data after setting lengths. */
6427
Paul Elliottb0450fe2021-09-01 15:06:26 +01006428 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, 0, 0 ) );
6433
6434 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6435 input_data->len, output_data,
6436 output_size, &output_length ),
6437 PSA_ERROR_INVALID_ARGUMENT );
6438
6439 psa_aead_abort( &operation );
6440
Paul Elliotta2a09b02021-09-22 14:56:40 +01006441 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006442
6443 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6444
6445 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6446
6447 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6448 input_data->len ) );
6449
6450 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6451 additional_data->len ) );
6452
6453 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6454 input_data->len, output_data,
6455 output_size, &output_length ) );
6456
6457 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6458 1, output_data,
6459 output_size, &output_length ),
6460 PSA_ERROR_INVALID_ARGUMENT );
6461
6462 psa_aead_abort( &operation );
6463
Paul Elliottc23a9a02021-06-21 18:32:46 +01006464 /* Test sending additional data after data. */
6465
Paul Elliottc23a9a02021-06-21 18:32:46 +01006466 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6467
6468 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6469
Andrzej Kurekad837522021-12-15 15:28:49 +01006470 if( operation.alg != PSA_ALG_CCM )
6471 {
6472 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6473 input_data->len, output_data,
6474 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006475
Andrzej Kurekad837522021-12-15 15:28:49 +01006476 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6477 additional_data->len ),
6478 PSA_ERROR_BAD_STATE );
6479 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006480 psa_aead_abort( &operation );
6481
Paul Elliott534d0b42021-06-22 19:15:20 +01006482 /* Test calling finish on decryption. */
6483
Paul Elliott534d0b42021-06-22 19:15:20 +01006484 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6485
6486 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6487
6488 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6489 finish_output_size,
6490 &output_part_length,
6491 tag_buffer, tag_length,
6492 &tag_size ),
6493 PSA_ERROR_BAD_STATE );
6494
6495 psa_aead_abort( &operation );
6496
6497 /* Test calling verify on encryption. */
6498
Paul Elliott534d0b42021-06-22 19:15:20 +01006499 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6500
6501 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6502
6503 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6504 finish_output_size,
6505 &output_part_length,
6506 tag_buffer,
6507 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006508 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006509
6510 psa_aead_abort( &operation );
6511
6512
Paul Elliottc23a9a02021-06-21 18:32:46 +01006513exit:
6514 psa_destroy_key( key );
6515 psa_aead_abort( &operation );
6516 mbedtls_free( output_data );
6517 mbedtls_free( final_data );
6518 PSA_DONE( );
6519}
6520/* END_CASE */
6521
6522/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006523void signature_size( int type_arg,
6524 int bits,
6525 int alg_arg,
6526 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006527{
6528 psa_key_type_t type = type_arg;
6529 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006530 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006531
Gilles Peskinefe11b722018-12-18 00:24:04 +01006532 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006533
Gilles Peskinee59236f2018-01-27 23:32:46 +01006534exit:
6535 ;
6536}
6537/* END_CASE */
6538
6539/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006540void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6541 int alg_arg, data_t *input_data,
6542 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006543{
Ronald Cron5425a212020-08-04 14:58:35 +02006544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006545 psa_key_type_t key_type = key_type_arg;
6546 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006547 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006548 unsigned char *signature = NULL;
6549 size_t signature_size;
6550 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006552
Gilles Peskine8817f612018-12-18 00:18:46 +01006553 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006554
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006555 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006556 psa_set_key_algorithm( &attributes, alg );
6557 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006558
Gilles Peskine049c7532019-05-15 20:22:09 +02006559 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006560 &key ) );
6561 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006562 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006563
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006564 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006565 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006566 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006567 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006568 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006569 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006570 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006571
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006572 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006573 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006574 input_data->x, input_data->len,
6575 signature, signature_size,
6576 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006577 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006578 ASSERT_COMPARE( output_data->x, output_data->len,
6579 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006580
6581exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006582 /*
6583 * Key attributes may have been returned by psa_get_key_attributes()
6584 * thus reset them as required.
6585 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006586 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006587
Ronald Cron5425a212020-08-04 14:58:35 +02006588 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006589 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006590 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006591}
6592/* END_CASE */
6593
6594/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006595void sign_hash_fail( int key_type_arg, data_t *key_data,
6596 int alg_arg, data_t *input_data,
6597 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006598{
Ronald Cron5425a212020-08-04 14:58:35 +02006599 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006600 psa_key_type_t key_type = key_type_arg;
6601 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006602 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006603 psa_status_t actual_status;
6604 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006605 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006606 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006607 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006608
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006609 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006610
Gilles Peskine8817f612018-12-18 00:18:46 +01006611 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006612
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006613 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006614 psa_set_key_algorithm( &attributes, alg );
6615 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006616
Gilles Peskine049c7532019-05-15 20:22:09 +02006617 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006618 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006619
Ronald Cron5425a212020-08-04 14:58:35 +02006620 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006621 input_data->x, input_data->len,
6622 signature, signature_size,
6623 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006624 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006625 /* The value of *signature_length is unspecified on error, but
6626 * whatever it is, it should be less than signature_size, so that
6627 * if the caller tries to read *signature_length bytes without
6628 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006629 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006630
6631exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006632 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006633 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006634 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006635 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006636}
6637/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006638
6639/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006640void sign_verify_hash( int key_type_arg, data_t *key_data,
6641 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006642{
Ronald Cron5425a212020-08-04 14:58:35 +02006643 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006644 psa_key_type_t key_type = key_type_arg;
6645 psa_algorithm_t alg = alg_arg;
6646 size_t key_bits;
6647 unsigned char *signature = NULL;
6648 size_t signature_size;
6649 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006651
Gilles Peskine8817f612018-12-18 00:18:46 +01006652 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006653
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006654 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006655 psa_set_key_algorithm( &attributes, alg );
6656 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006657
Gilles Peskine049c7532019-05-15 20:22:09 +02006658 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006659 &key ) );
6660 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006661 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006662
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006663 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006664 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006665 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006666 key_bits, alg );
6667 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006668 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006669 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006670
6671 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006672 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006673 input_data->x, input_data->len,
6674 signature, signature_size,
6675 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006676 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006677 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006678 TEST_ASSERT( signature_length > 0 );
6679
6680 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006681 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006682 input_data->x, input_data->len,
6683 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006684
6685 if( input_data->len != 0 )
6686 {
6687 /* Flip a bit in the input and verify that the signature is now
6688 * detected as invalid. Flip a bit at the beginning, not at the end,
6689 * because ECDSA may ignore the last few bits of the input. */
6690 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006691 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006692 input_data->x, input_data->len,
6693 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006694 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006695 }
6696
6697exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006698 /*
6699 * Key attributes may have been returned by psa_get_key_attributes()
6700 * thus reset them as required.
6701 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006702 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006703
Ronald Cron5425a212020-08-04 14:58:35 +02006704 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006705 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006706 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006707}
6708/* END_CASE */
6709
6710/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006711void verify_hash( int key_type_arg, data_t *key_data,
6712 int alg_arg, data_t *hash_data,
6713 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006714{
Ronald Cron5425a212020-08-04 14:58:35 +02006715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006716 psa_key_type_t key_type = key_type_arg;
6717 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006719
Gilles Peskine7be11a72022-04-14 00:12:57 +02006720 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006721
Gilles Peskine8817f612018-12-18 00:18:46 +01006722 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006723
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006725 psa_set_key_algorithm( &attributes, alg );
6726 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006727
Gilles Peskine049c7532019-05-15 20:22:09 +02006728 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006729 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006730
Ronald Cron5425a212020-08-04 14:58:35 +02006731 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006732 hash_data->x, hash_data->len,
6733 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006734
itayzafrir5c753392018-05-08 11:18:38 +03006735exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006736 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006738 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006739}
6740/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006741
6742/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006743void verify_hash_fail( int key_type_arg, data_t *key_data,
6744 int alg_arg, data_t *hash_data,
6745 data_t *signature_data,
6746 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006747{
Ronald Cron5425a212020-08-04 14:58:35 +02006748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006749 psa_key_type_t key_type = key_type_arg;
6750 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006751 psa_status_t actual_status;
6752 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006754
Gilles Peskine8817f612018-12-18 00:18:46 +01006755 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006756
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006757 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006758 psa_set_key_algorithm( &attributes, alg );
6759 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006760
Gilles Peskine049c7532019-05-15 20:22:09 +02006761 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006762 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006763
Ronald Cron5425a212020-08-04 14:58:35 +02006764 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006765 hash_data->x, hash_data->len,
6766 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006767 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006768
6769exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006770 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006771 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006772 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006773}
6774/* END_CASE */
6775
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006776/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006777void sign_message_deterministic( int key_type_arg,
6778 data_t *key_data,
6779 int alg_arg,
6780 data_t *input_data,
6781 data_t *output_data )
6782{
6783 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6784 psa_key_type_t key_type = key_type_arg;
6785 psa_algorithm_t alg = alg_arg;
6786 size_t key_bits;
6787 unsigned char *signature = NULL;
6788 size_t signature_size;
6789 size_t signature_length = 0xdeadbeef;
6790 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6791
6792 PSA_ASSERT( psa_crypto_init( ) );
6793
6794 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6795 psa_set_key_algorithm( &attributes, alg );
6796 psa_set_key_type( &attributes, key_type );
6797
6798 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6799 &key ) );
6800 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6801 key_bits = psa_get_key_bits( &attributes );
6802
6803 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6804 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006805 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006806 ASSERT_ALLOC( signature, signature_size );
6807
6808 PSA_ASSERT( psa_sign_message( key, alg,
6809 input_data->x, input_data->len,
6810 signature, signature_size,
6811 &signature_length ) );
6812
6813 ASSERT_COMPARE( output_data->x, output_data->len,
6814 signature, signature_length );
6815
6816exit:
6817 psa_reset_key_attributes( &attributes );
6818
6819 psa_destroy_key( key );
6820 mbedtls_free( signature );
6821 PSA_DONE( );
6822
6823}
6824/* END_CASE */
6825
6826/* BEGIN_CASE */
6827void sign_message_fail( int key_type_arg,
6828 data_t *key_data,
6829 int alg_arg,
6830 data_t *input_data,
6831 int signature_size_arg,
6832 int expected_status_arg )
6833{
6834 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6835 psa_key_type_t key_type = key_type_arg;
6836 psa_algorithm_t alg = alg_arg;
6837 size_t signature_size = signature_size_arg;
6838 psa_status_t actual_status;
6839 psa_status_t expected_status = expected_status_arg;
6840 unsigned char *signature = NULL;
6841 size_t signature_length = 0xdeadbeef;
6842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6843
6844 ASSERT_ALLOC( signature, signature_size );
6845
6846 PSA_ASSERT( psa_crypto_init( ) );
6847
6848 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6849 psa_set_key_algorithm( &attributes, alg );
6850 psa_set_key_type( &attributes, key_type );
6851
6852 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6853 &key ) );
6854
6855 actual_status = psa_sign_message( key, alg,
6856 input_data->x, input_data->len,
6857 signature, signature_size,
6858 &signature_length );
6859 TEST_EQUAL( actual_status, expected_status );
6860 /* The value of *signature_length is unspecified on error, but
6861 * whatever it is, it should be less than signature_size, so that
6862 * if the caller tries to read *signature_length bytes without
6863 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006864 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006865
6866exit:
6867 psa_reset_key_attributes( &attributes );
6868 psa_destroy_key( key );
6869 mbedtls_free( signature );
6870 PSA_DONE( );
6871}
6872/* END_CASE */
6873
6874/* BEGIN_CASE */
6875void sign_verify_message( int key_type_arg,
6876 data_t *key_data,
6877 int alg_arg,
6878 data_t *input_data )
6879{
6880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6881 psa_key_type_t key_type = key_type_arg;
6882 psa_algorithm_t alg = alg_arg;
6883 size_t key_bits;
6884 unsigned char *signature = NULL;
6885 size_t signature_size;
6886 size_t signature_length = 0xdeadbeef;
6887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6888
6889 PSA_ASSERT( psa_crypto_init( ) );
6890
6891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6892 PSA_KEY_USAGE_VERIFY_MESSAGE );
6893 psa_set_key_algorithm( &attributes, alg );
6894 psa_set_key_type( &attributes, key_type );
6895
6896 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6897 &key ) );
6898 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6899 key_bits = psa_get_key_bits( &attributes );
6900
6901 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6902 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006903 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006904 ASSERT_ALLOC( signature, signature_size );
6905
6906 PSA_ASSERT( psa_sign_message( key, alg,
6907 input_data->x, input_data->len,
6908 signature, signature_size,
6909 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006910 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006911 TEST_ASSERT( signature_length > 0 );
6912
6913 PSA_ASSERT( psa_verify_message( key, alg,
6914 input_data->x, input_data->len,
6915 signature, signature_length ) );
6916
6917 if( input_data->len != 0 )
6918 {
6919 /* Flip a bit in the input and verify that the signature is now
6920 * detected as invalid. Flip a bit at the beginning, not at the end,
6921 * because ECDSA may ignore the last few bits of the input. */
6922 input_data->x[0] ^= 1;
6923 TEST_EQUAL( psa_verify_message( key, alg,
6924 input_data->x, input_data->len,
6925 signature, signature_length ),
6926 PSA_ERROR_INVALID_SIGNATURE );
6927 }
6928
6929exit:
6930 psa_reset_key_attributes( &attributes );
6931
6932 psa_destroy_key( key );
6933 mbedtls_free( signature );
6934 PSA_DONE( );
6935}
6936/* END_CASE */
6937
6938/* BEGIN_CASE */
6939void verify_message( int key_type_arg,
6940 data_t *key_data,
6941 int alg_arg,
6942 data_t *input_data,
6943 data_t *signature_data )
6944{
6945 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6946 psa_key_type_t key_type = key_type_arg;
6947 psa_algorithm_t alg = alg_arg;
6948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6949
Gilles Peskine7be11a72022-04-14 00:12:57 +02006950 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006951
6952 PSA_ASSERT( psa_crypto_init( ) );
6953
6954 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6955 psa_set_key_algorithm( &attributes, alg );
6956 psa_set_key_type( &attributes, key_type );
6957
6958 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6959 &key ) );
6960
6961 PSA_ASSERT( psa_verify_message( key, alg,
6962 input_data->x, input_data->len,
6963 signature_data->x, signature_data->len ) );
6964
6965exit:
6966 psa_reset_key_attributes( &attributes );
6967 psa_destroy_key( key );
6968 PSA_DONE( );
6969}
6970/* END_CASE */
6971
6972/* BEGIN_CASE */
6973void verify_message_fail( int key_type_arg,
6974 data_t *key_data,
6975 int alg_arg,
6976 data_t *hash_data,
6977 data_t *signature_data,
6978 int expected_status_arg )
6979{
6980 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6981 psa_key_type_t key_type = key_type_arg;
6982 psa_algorithm_t alg = alg_arg;
6983 psa_status_t actual_status;
6984 psa_status_t expected_status = expected_status_arg;
6985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6986
6987 PSA_ASSERT( psa_crypto_init( ) );
6988
6989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6990 psa_set_key_algorithm( &attributes, alg );
6991 psa_set_key_type( &attributes, key_type );
6992
6993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6994 &key ) );
6995
6996 actual_status = psa_verify_message( key, alg,
6997 hash_data->x, hash_data->len,
6998 signature_data->x,
6999 signature_data->len );
7000 TEST_EQUAL( actual_status, expected_status );
7001
7002exit:
7003 psa_reset_key_attributes( &attributes );
7004 psa_destroy_key( key );
7005 PSA_DONE( );
7006}
7007/* END_CASE */
7008
7009/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007010void asymmetric_encrypt( int key_type_arg,
7011 data_t *key_data,
7012 int alg_arg,
7013 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007014 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007015 int expected_output_length_arg,
7016 int expected_status_arg )
7017{
Ronald Cron5425a212020-08-04 14:58:35 +02007018 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007019 psa_key_type_t key_type = key_type_arg;
7020 psa_algorithm_t alg = alg_arg;
7021 size_t expected_output_length = expected_output_length_arg;
7022 size_t key_bits;
7023 unsigned char *output = NULL;
7024 size_t output_size;
7025 size_t output_length = ~0;
7026 psa_status_t actual_status;
7027 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007029
Gilles Peskine8817f612018-12-18 00:18:46 +01007030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007031
Gilles Peskine656896e2018-06-29 19:12:28 +02007032 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7034 psa_set_key_algorithm( &attributes, alg );
7035 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007036 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007037 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007038
7039 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007040 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007041 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007042
Gilles Peskine656896e2018-06-29 19:12:28 +02007043 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007044 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007045 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007046
7047 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007048 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007049 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007050 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007051 output, output_size,
7052 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007053 TEST_EQUAL( actual_status, expected_status );
7054 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007055
Gilles Peskine68428122018-06-30 18:42:41 +02007056 /* If the label is empty, the test framework puts a non-null pointer
7057 * in label->x. Test that a null pointer works as well. */
7058 if( label->len == 0 )
7059 {
7060 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007061 if( output_size != 0 )
7062 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007063 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007064 input_data->x, input_data->len,
7065 NULL, label->len,
7066 output, output_size,
7067 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007068 TEST_EQUAL( actual_status, expected_status );
7069 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007070 }
7071
Gilles Peskine656896e2018-06-29 19:12:28 +02007072exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007073 /*
7074 * Key attributes may have been returned by psa_get_key_attributes()
7075 * thus reset them as required.
7076 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007077 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007078
Ronald Cron5425a212020-08-04 14:58:35 +02007079 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007080 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007081 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007082}
7083/* END_CASE */
7084
7085/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007086void asymmetric_encrypt_decrypt( int key_type_arg,
7087 data_t *key_data,
7088 int alg_arg,
7089 data_t *input_data,
7090 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007091{
Ronald Cron5425a212020-08-04 14:58:35 +02007092 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007093 psa_key_type_t key_type = key_type_arg;
7094 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007095 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007096 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007097 size_t output_size;
7098 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007099 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007100 size_t output2_size;
7101 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007102 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007103
Gilles Peskine8817f612018-12-18 00:18:46 +01007104 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007105
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007106 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7107 psa_set_key_algorithm( &attributes, alg );
7108 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007109
Gilles Peskine049c7532019-05-15 20:22:09 +02007110 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007111 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007112
7113 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007114 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007115 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007116
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007117 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007118 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007119 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007120
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007121 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007122 TEST_LE_U( output2_size,
7123 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7124 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007125 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007126
Gilles Peskineeebd7382018-06-08 18:11:54 +02007127 /* We test encryption by checking that encrypt-then-decrypt gives back
7128 * the original plaintext because of the non-optional random
7129 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007130 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007131 input_data->x, input_data->len,
7132 label->x, label->len,
7133 output, output_size,
7134 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007135 /* We don't know what ciphertext length to expect, but check that
7136 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007137 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007138
Ronald Cron5425a212020-08-04 14:58:35 +02007139 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007140 output, output_length,
7141 label->x, label->len,
7142 output2, output2_size,
7143 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007144 ASSERT_COMPARE( input_data->x, input_data->len,
7145 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007146
7147exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007148 /*
7149 * Key attributes may have been returned by psa_get_key_attributes()
7150 * thus reset them as required.
7151 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007152 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007153
Ronald Cron5425a212020-08-04 14:58:35 +02007154 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007155 mbedtls_free( output );
7156 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007157 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007158}
7159/* END_CASE */
7160
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007161/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007162void asymmetric_decrypt( int key_type_arg,
7163 data_t *key_data,
7164 int alg_arg,
7165 data_t *input_data,
7166 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007167 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007168{
Ronald Cron5425a212020-08-04 14:58:35 +02007169 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007170 psa_key_type_t key_type = key_type_arg;
7171 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007172 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007173 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007174 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007175 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007177
Gilles Peskine8817f612018-12-18 00:18:46 +01007178 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007179
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7181 psa_set_key_algorithm( &attributes, alg );
7182 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007183
Gilles Peskine049c7532019-05-15 20:22:09 +02007184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007185 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007186
gabor-mezei-armceface22021-01-21 12:26:17 +01007187 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7188 key_bits = psa_get_key_bits( &attributes );
7189
7190 /* Determine the maximum ciphertext length */
7191 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007192 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007193 ASSERT_ALLOC( output, output_size );
7194
Ronald Cron5425a212020-08-04 14:58:35 +02007195 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007196 input_data->x, input_data->len,
7197 label->x, label->len,
7198 output,
7199 output_size,
7200 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007201 ASSERT_COMPARE( expected_data->x, expected_data->len,
7202 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007203
Gilles Peskine68428122018-06-30 18:42:41 +02007204 /* If the label is empty, the test framework puts a non-null pointer
7205 * in label->x. Test that a null pointer works as well. */
7206 if( label->len == 0 )
7207 {
7208 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007209 if( output_size != 0 )
7210 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007211 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007212 input_data->x, input_data->len,
7213 NULL, label->len,
7214 output,
7215 output_size,
7216 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007217 ASSERT_COMPARE( expected_data->x, expected_data->len,
7218 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007219 }
7220
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007221exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007222 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007223 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007224 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007225 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007226}
7227/* END_CASE */
7228
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007229/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007230void asymmetric_decrypt_fail( int key_type_arg,
7231 data_t *key_data,
7232 int alg_arg,
7233 data_t *input_data,
7234 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007235 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007236 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007237{
Ronald Cron5425a212020-08-04 14:58:35 +02007238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007239 psa_key_type_t key_type = key_type_arg;
7240 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007241 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007242 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007243 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007244 psa_status_t actual_status;
7245 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007247
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007248 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007249
Gilles Peskine8817f612018-12-18 00:18:46 +01007250 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007251
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007252 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7253 psa_set_key_algorithm( &attributes, alg );
7254 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007255
Gilles Peskine049c7532019-05-15 20:22:09 +02007256 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007257 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007258
Ronald Cron5425a212020-08-04 14:58:35 +02007259 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007260 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007261 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007262 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007263 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007264 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007265 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007266
Gilles Peskine68428122018-06-30 18:42:41 +02007267 /* If the label is empty, the test framework puts a non-null pointer
7268 * in label->x. Test that a null pointer works as well. */
7269 if( label->len == 0 )
7270 {
7271 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007272 if( output_size != 0 )
7273 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007274 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007275 input_data->x, input_data->len,
7276 NULL, label->len,
7277 output, output_size,
7278 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007279 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007280 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007281 }
7282
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007283exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007284 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007285 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007286 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007287 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007288}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007289/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007290
7291/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007292void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007293{
7294 /* Test each valid way of initializing the object, except for `= {0}`, as
7295 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7296 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007297 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007298 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007299 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7300 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7301 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007302
7303 memset( &zero, 0, sizeof( zero ) );
7304
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007305 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007306 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007307 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007308 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007309 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007310 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007311 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007312
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007313 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007314 PSA_ASSERT( psa_key_derivation_abort(&func) );
7315 PSA_ASSERT( psa_key_derivation_abort(&init) );
7316 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007317}
7318/* END_CASE */
7319
Janos Follath16de4a42019-06-13 16:32:24 +01007320/* BEGIN_CASE */
7321void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007322{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007323 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007324 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007325 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007326
Gilles Peskine8817f612018-12-18 00:18:46 +01007327 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007328
Janos Follath16de4a42019-06-13 16:32:24 +01007329 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007330 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007331
7332exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007333 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007334 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007335}
7336/* END_CASE */
7337
Janos Follathaf3c2a02019-06-12 12:34:34 +01007338/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007339void derive_set_capacity( int alg_arg, int capacity_arg,
7340 int expected_status_arg )
7341{
7342 psa_algorithm_t alg = alg_arg;
7343 size_t capacity = capacity_arg;
7344 psa_status_t expected_status = expected_status_arg;
7345 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7346
7347 PSA_ASSERT( psa_crypto_init( ) );
7348
7349 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7350
7351 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7352 expected_status );
7353
7354exit:
7355 psa_key_derivation_abort( &operation );
7356 PSA_DONE( );
7357}
7358/* END_CASE */
7359
7360/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007361void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007362 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007363 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007364 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007365 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007366 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007367 int expected_status_arg3,
7368 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007369{
7370 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007371 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7372 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007373 psa_status_t expected_statuses[] = {expected_status_arg1,
7374 expected_status_arg2,
7375 expected_status_arg3};
7376 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007377 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7378 MBEDTLS_SVC_KEY_ID_INIT,
7379 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007380 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7381 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7382 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007383 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007384 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007385 psa_status_t expected_output_status = expected_output_status_arg;
7386 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007387
7388 PSA_ASSERT( psa_crypto_init( ) );
7389
7390 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7391 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007392
7393 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7394
7395 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7396 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007397 mbedtls_test_set_step( i );
7398 if( steps[i] == 0 )
7399 {
7400 /* Skip this step */
7401 }
7402 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007403 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007404 psa_set_key_type( &attributes, key_types[i] );
7405 PSA_ASSERT( psa_import_key( &attributes,
7406 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007407 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007408 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7409 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7410 {
7411 // When taking a private key as secret input, use key agreement
7412 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007413 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7414 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007415 expected_statuses[i] );
7416 }
7417 else
7418 {
7419 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007420 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007421 expected_statuses[i] );
7422 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007423 }
7424 else
7425 {
7426 TEST_EQUAL( psa_key_derivation_input_bytes(
7427 &operation, steps[i],
7428 inputs[i]->x, inputs[i]->len ),
7429 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007430 }
7431 }
7432
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007433 if( output_key_type != PSA_KEY_TYPE_NONE )
7434 {
7435 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007436 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007437 psa_set_key_bits( &attributes, 8 );
7438 actual_output_status =
7439 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007440 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007441 }
7442 else
7443 {
7444 uint8_t buffer[1];
7445 actual_output_status =
7446 psa_key_derivation_output_bytes( &operation,
7447 buffer, sizeof( buffer ) );
7448 }
7449 TEST_EQUAL( actual_output_status, expected_output_status );
7450
Janos Follathaf3c2a02019-06-12 12:34:34 +01007451exit:
7452 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007453 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7454 psa_destroy_key( keys[i] );
7455 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007456 PSA_DONE( );
7457}
7458/* END_CASE */
7459
Janos Follathd958bb72019-07-03 15:02:16 +01007460/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007461void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007462{
Janos Follathd958bb72019-07-03 15:02:16 +01007463 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007464 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007465 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007466 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007467 unsigned char input1[] = "Input 1";
7468 size_t input1_length = sizeof( input1 );
7469 unsigned char input2[] = "Input 2";
7470 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007471 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007472 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007473 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7474 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7475 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007477
Gilles Peskine8817f612018-12-18 00:18:46 +01007478 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007479
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7481 psa_set_key_algorithm( &attributes, alg );
7482 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007483
Gilles Peskine73676cb2019-05-15 20:15:10 +02007484 PSA_ASSERT( psa_import_key( &attributes,
7485 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007486 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007487
7488 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007489 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7490 input1, input1_length,
7491 input2, input2_length,
7492 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007493 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007495 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007496 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007497 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007498
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007499 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007500
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007501 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007502 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007503
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007504exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007505 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007506 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007507 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007508}
7509/* END_CASE */
7510
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007511/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007512void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007513{
7514 uint8_t output_buffer[16];
7515 size_t buffer_size = 16;
7516 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007517 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007518
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007519 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7520 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007521 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007522
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007523 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007524 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007525
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007526 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007527
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007528 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7529 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007530 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007531
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007532 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007533 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007534
7535exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007536 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007537}
7538/* END_CASE */
7539
7540/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007541void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007542 int step1_arg, data_t *input1, int expected_status_arg1,
7543 int step2_arg, data_t *input2, int expected_status_arg2,
7544 int step3_arg, data_t *input3, int expected_status_arg3,
7545 int step4_arg, data_t *input4, int expected_status_arg4,
7546 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007547 int requested_capacity_arg,
7548 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007549 data_t *expected_output2,
7550 int other_key_input_type,
7551 int key_input_type,
7552 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007553{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007554 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007555 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7556 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007557 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7558 MBEDTLS_SVC_KEY_ID_INIT,
7559 MBEDTLS_SVC_KEY_ID_INIT,
7560 MBEDTLS_SVC_KEY_ID_INIT};
7561 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7562 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007563 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007564 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007565 uint8_t *expected_outputs[2] =
7566 {expected_output1->x, expected_output2->x};
7567 size_t output_sizes[2] =
7568 {expected_output1->len, expected_output2->len};
7569 size_t output_buffer_size = 0;
7570 uint8_t *output_buffer = NULL;
7571 size_t expected_capacity;
7572 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007573 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7574 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7575 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7576 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007577 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007578 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007579 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007580
7581 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7582 {
7583 if( output_sizes[i] > output_buffer_size )
7584 output_buffer_size = output_sizes[i];
7585 if( output_sizes[i] == 0 )
7586 expected_outputs[i] = NULL;
7587 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007588 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007589 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007590
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007591 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007592 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7593 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7594 requested_capacity ) );
7595 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007596 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007597 switch( steps[i] )
7598 {
7599 case 0:
7600 break;
7601 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007602 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007603 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007604 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007605 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007606 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007607 inputs[i]->x, inputs[i]->len ),
7608 statuses[i] );
7609
7610 if( statuses[i] != PSA_SUCCESS )
7611 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007612 break;
7613 case 1: // input key
7614 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7615 psa_set_key_algorithm( &attributes1, alg );
7616 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7617
7618 PSA_ASSERT( psa_import_key( &attributes1,
7619 inputs[i]->x, inputs[i]->len,
7620 &keys[i] ) );
7621
Przemek Stekiel38647de2022-04-19 13:27:47 +02007622 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007623 {
7624 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007625 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7626 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007627 }
7628
Przemek Stekiel38647de2022-04-19 13:27:47 +02007629 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7630 steps[i],
7631 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007632 break;
7633 default:
7634 TEST_ASSERT( ! "default case not supported" );
7635 break;
7636 }
7637 break;
7638 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007639 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007640 {
7641 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007642 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7643 steps[i],
7644 inputs[i]->x,
7645 inputs[i]->len ),
7646 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007647 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007648 case 1: // input key, type DERIVE
7649 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007650 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7651 psa_set_key_algorithm( &attributes2, alg );
7652 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7653
7654 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007655 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007656 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7657
7658 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007659 inputs[i]->x, inputs[i]->len,
7660 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007661
Przemek Stekiel38647de2022-04-19 13:27:47 +02007662 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7663 steps[i],
7664 keys[i] ),
7665 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007666 break;
7667 case 2: // key agreement
7668 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7669 psa_set_key_algorithm( &attributes3, alg );
7670 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7671
7672 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007673 inputs[i]->x, inputs[i]->len,
7674 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007675
7676 TEST_EQUAL( psa_key_derivation_key_agreement(
7677 &operation,
7678 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7679 keys[i], key_agreement_peer_key->x,
7680 key_agreement_peer_key->len ), statuses[i] );
7681 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007682 default:
7683 TEST_ASSERT( ! "default case not supported" );
7684 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007685 }
7686
Przemek Stekiel38647de2022-04-19 13:27:47 +02007687 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007688 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007689 break;
7690 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007691 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007692 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007693 inputs[i]->x, inputs[i]->len ), statuses[i] );
7694
7695 if( statuses[i] != PSA_SUCCESS )
7696 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007697 break;
7698 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007699 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007700
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007701 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007702 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007703 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007704 expected_capacity = requested_capacity;
7705
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007706 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007707 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007708 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7709
7710 /* For output key derivation secret must be provided using
7711 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007712 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007713 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007714
7715 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7716 psa_set_key_algorithm( &attributes4, alg );
7717 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007718 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007719
7720 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007721 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007722 }
7723 else // output bytes
7724 {
7725 /* Expansion phase. */
7726 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007727 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007728 /* Read some bytes. */
7729 status = psa_key_derivation_output_bytes( &operation,
7730 output_buffer, output_sizes[i] );
7731 if( expected_capacity == 0 && output_sizes[i] == 0 )
7732 {
7733 /* Reading 0 bytes when 0 bytes are available can go either way. */
7734 TEST_ASSERT( status == PSA_SUCCESS ||
7735 status == PSA_ERROR_INSUFFICIENT_DATA );
7736 continue;
7737 }
7738 else if( expected_capacity == 0 ||
7739 output_sizes[i] > expected_capacity )
7740 {
7741 /* Capacity exceeded. */
7742 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7743 expected_capacity = 0;
7744 continue;
7745 }
7746 /* Success. Check the read data. */
7747 PSA_ASSERT( status );
7748 if( output_sizes[i] != 0 )
7749 ASSERT_COMPARE( output_buffer, output_sizes[i],
7750 expected_outputs[i], output_sizes[i] );
7751 /* Check the operation status. */
7752 expected_capacity -= output_sizes[i];
7753 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7754 &current_capacity ) );
7755 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007756 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007757 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007758 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007759
7760exit:
7761 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007762 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007763 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7764 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007765 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007766 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007767}
7768/* END_CASE */
7769
7770/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007771void derive_full( int alg_arg,
7772 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007773 data_t *input1,
7774 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007775 int requested_capacity_arg )
7776{
Ronald Cron5425a212020-08-04 14:58:35 +02007777 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007778 psa_algorithm_t alg = alg_arg;
7779 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007780 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007781 unsigned char output_buffer[16];
7782 size_t expected_capacity = requested_capacity;
7783 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007784 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007785
Gilles Peskine8817f612018-12-18 00:18:46 +01007786 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007787
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007788 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7789 psa_set_key_algorithm( &attributes, alg );
7790 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007791
Gilles Peskine049c7532019-05-15 20:22:09 +02007792 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007793 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007794
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007795 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7796 input1->x, input1->len,
7797 input2->x, input2->len,
7798 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007799 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007800
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007801 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007802 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007803 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007804
7805 /* Expansion phase. */
7806 while( current_capacity > 0 )
7807 {
7808 size_t read_size = sizeof( output_buffer );
7809 if( read_size > current_capacity )
7810 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007811 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007812 output_buffer,
7813 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007814 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007815 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007816 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007817 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007818 }
7819
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007820 /* Check that the operation refuses to go over capacity. */
7821 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007822 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007823
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007824 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007825
7826exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007827 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007828 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007829 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007830}
7831/* END_CASE */
7832
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007833/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007834void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007835 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007836 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007837 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007838 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007839{
7840 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7841 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007842 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007843 uint8_t *output_buffer = NULL;
7844 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007845 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7846 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7847 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007848
7849 ASSERT_ALLOC( output_buffer, expected_output->len );
7850 PSA_ASSERT( psa_crypto_init() );
7851
7852 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007853 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007854 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007855
7856 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007857 step, input->x, input->len ),
7858 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007859
7860 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7861 goto exit;
7862
7863 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7864 expected_output->len );
7865
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007866 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007867 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7868 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7869 expected_output->len );
7870
7871exit:
7872 mbedtls_free( output_buffer );
7873 psa_key_derivation_abort( &operation );
7874 PSA_DONE();
7875}
7876/* END_CASE */
7877
Janos Follathe60c9052019-07-03 13:51:30 +01007878/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007879void derive_key_exercise( int alg_arg,
7880 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007881 data_t *input1,
7882 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007883 int derived_type_arg,
7884 int derived_bits_arg,
7885 int derived_usage_arg,
7886 int derived_alg_arg )
7887{
Ronald Cron5425a212020-08-04 14:58:35 +02007888 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7889 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007890 psa_algorithm_t alg = alg_arg;
7891 psa_key_type_t derived_type = derived_type_arg;
7892 size_t derived_bits = derived_bits_arg;
7893 psa_key_usage_t derived_usage = derived_usage_arg;
7894 psa_algorithm_t derived_alg = derived_alg_arg;
7895 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007896 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007898 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007899
Gilles Peskine8817f612018-12-18 00:18:46 +01007900 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007901
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007902 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7903 psa_set_key_algorithm( &attributes, alg );
7904 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007905 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007906 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007907
7908 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007909 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7910 input1->x, input1->len,
7911 input2->x, input2->len,
7912 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007913 goto exit;
7914
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007915 psa_set_key_usage_flags( &attributes, derived_usage );
7916 psa_set_key_algorithm( &attributes, derived_alg );
7917 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007918 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007919 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007920 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007921
7922 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007923 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007924 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7925 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007926
7927 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007928 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007929 goto exit;
7930
7931exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007932 /*
7933 * Key attributes may have been returned by psa_get_key_attributes()
7934 * thus reset them as required.
7935 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007936 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007937
7938 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007939 psa_destroy_key( base_key );
7940 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007941 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007942}
7943/* END_CASE */
7944
Janos Follath42fd8882019-07-03 14:17:09 +01007945/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007946void derive_key_export( int alg_arg,
7947 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007948 data_t *input1,
7949 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007950 int bytes1_arg,
7951 int bytes2_arg )
7952{
Ronald Cron5425a212020-08-04 14:58:35 +02007953 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7954 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007955 psa_algorithm_t alg = alg_arg;
7956 size_t bytes1 = bytes1_arg;
7957 size_t bytes2 = bytes2_arg;
7958 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007959 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007960 uint8_t *output_buffer = NULL;
7961 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007962 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7963 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007964 size_t length;
7965
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007966 ASSERT_ALLOC( output_buffer, capacity );
7967 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007968 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007969
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007970 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7971 psa_set_key_algorithm( &base_attributes, alg );
7972 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007973 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007974 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007975
7976 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007977 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7978 input1->x, input1->len,
7979 input2->x, input2->len,
7980 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007981 goto exit;
7982
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007983 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007984 output_buffer,
7985 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007986 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007987
7988 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007989 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7990 input1->x, input1->len,
7991 input2->x, input2->len,
7992 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007993 goto exit;
7994
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007995 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7996 psa_set_key_algorithm( &derived_attributes, 0 );
7997 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007998 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007999 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008000 &derived_key ) );
8001 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008002 export_buffer, bytes1,
8003 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008004 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02008005 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008006 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008007 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008008 &derived_key ) );
8009 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008010 export_buffer + bytes1, bytes2,
8011 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008012 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008013
8014 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008015 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8016 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008017
8018exit:
8019 mbedtls_free( output_buffer );
8020 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008021 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008022 psa_destroy_key( base_key );
8023 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008024 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008025}
8026/* END_CASE */
8027
8028/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008029void derive_key_type( int alg_arg,
8030 data_t *key_data,
8031 data_t *input1,
8032 data_t *input2,
8033 int key_type_arg, int bits_arg,
8034 data_t *expected_export )
8035{
8036 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8037 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8038 const psa_algorithm_t alg = alg_arg;
8039 const psa_key_type_t key_type = key_type_arg;
8040 const size_t bits = bits_arg;
8041 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8042 const size_t export_buffer_size =
8043 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8044 uint8_t *export_buffer = NULL;
8045 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8046 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8047 size_t export_length;
8048
8049 ASSERT_ALLOC( export_buffer, export_buffer_size );
8050 PSA_ASSERT( psa_crypto_init( ) );
8051
8052 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8053 psa_set_key_algorithm( &base_attributes, alg );
8054 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8055 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8056 &base_key ) );
8057
Przemek Stekielc85f0912022-03-08 11:37:54 +01008058 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008059 &operation, base_key, alg,
8060 input1->x, input1->len,
8061 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008062 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008063 goto exit;
8064
8065 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8066 psa_set_key_algorithm( &derived_attributes, 0 );
8067 psa_set_key_type( &derived_attributes, key_type );
8068 psa_set_key_bits( &derived_attributes, bits );
8069 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8070 &derived_key ) );
8071
8072 PSA_ASSERT( psa_export_key( derived_key,
8073 export_buffer, export_buffer_size,
8074 &export_length ) );
8075 ASSERT_COMPARE( export_buffer, export_length,
8076 expected_export->x, expected_export->len );
8077
8078exit:
8079 mbedtls_free( export_buffer );
8080 psa_key_derivation_abort( &operation );
8081 psa_destroy_key( base_key );
8082 psa_destroy_key( derived_key );
8083 PSA_DONE( );
8084}
8085/* END_CASE */
8086
8087/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008088void derive_key( int alg_arg,
8089 data_t *key_data, data_t *input1, data_t *input2,
8090 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008091 int expected_status_arg,
8092 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008093{
Ronald Cron5425a212020-08-04 14:58:35 +02008094 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8095 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008096 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008097 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008098 size_t bits = bits_arg;
8099 psa_status_t expected_status = expected_status_arg;
8100 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8101 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8102 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8103
8104 PSA_ASSERT( psa_crypto_init( ) );
8105
8106 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8107 psa_set_key_algorithm( &base_attributes, alg );
8108 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8109 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008110 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008111
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008112 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8113 input1->x, input1->len,
8114 input2->x, input2->len,
8115 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008116 goto exit;
8117
8118 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8119 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008120 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008121 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008122
8123 psa_status_t status =
8124 psa_key_derivation_output_key( &derived_attributes,
8125 &operation,
8126 &derived_key );
8127 if( is_large_output > 0 )
8128 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8129 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008130
8131exit:
8132 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008133 psa_destroy_key( base_key );
8134 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008135 PSA_DONE( );
8136}
8137/* END_CASE */
8138
8139/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008140void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008141 int our_key_type_arg, int our_key_alg_arg,
8142 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008143 int expected_status_arg )
8144{
Ronald Cron5425a212020-08-04 14:58:35 +02008145 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008146 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008147 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008148 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008149 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008151 psa_status_t expected_status = expected_status_arg;
8152 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008153
Gilles Peskine8817f612018-12-18 00:18:46 +01008154 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008155
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008156 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008157 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008158 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008159 PSA_ASSERT( psa_import_key( &attributes,
8160 our_key_data->x, our_key_data->len,
8161 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008162
Gilles Peskine77f40d82019-04-11 21:27:06 +02008163 /* The tests currently include inputs that should fail at either step.
8164 * Test cases that fail at the setup step should be changed to call
8165 * key_derivation_setup instead, and this function should be renamed
8166 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008167 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008168 if( status == PSA_SUCCESS )
8169 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008170 TEST_EQUAL( psa_key_derivation_key_agreement(
8171 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8172 our_key,
8173 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008174 expected_status );
8175 }
8176 else
8177 {
8178 TEST_ASSERT( status == expected_status );
8179 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008180
8181exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008182 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008183 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008184 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008185}
8186/* END_CASE */
8187
8188/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008189void raw_key_agreement( int alg_arg,
8190 int our_key_type_arg, data_t *our_key_data,
8191 data_t *peer_key_data,
8192 data_t *expected_output )
8193{
Ronald Cron5425a212020-08-04 14:58:35 +02008194 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008195 psa_algorithm_t alg = alg_arg;
8196 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008198 unsigned char *output = NULL;
8199 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008200 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008201
Gilles Peskinef0cba732019-04-11 22:12:38 +02008202 PSA_ASSERT( psa_crypto_init( ) );
8203
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008204 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8205 psa_set_key_algorithm( &attributes, alg );
8206 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008207 PSA_ASSERT( psa_import_key( &attributes,
8208 our_key_data->x, our_key_data->len,
8209 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008210
gabor-mezei-armceface22021-01-21 12:26:17 +01008211 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8212 key_bits = psa_get_key_bits( &attributes );
8213
Gilles Peskine992bee82022-04-13 23:25:52 +02008214 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008215 TEST_LE_U( expected_output->len,
8216 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8217 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8218 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008219
8220 /* Good case with exact output size */
8221 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008222 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8223 peer_key_data->x, peer_key_data->len,
8224 output, expected_output->len,
8225 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008226 ASSERT_COMPARE( output, output_length,
8227 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008228 mbedtls_free( output );
8229 output = NULL;
8230 output_length = ~0;
8231
8232 /* Larger buffer */
8233 ASSERT_ALLOC( output, expected_output->len + 1 );
8234 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8235 peer_key_data->x, peer_key_data->len,
8236 output, expected_output->len + 1,
8237 &output_length ) );
8238 ASSERT_COMPARE( output, output_length,
8239 expected_output->x, expected_output->len );
8240 mbedtls_free( output );
8241 output = NULL;
8242 output_length = ~0;
8243
8244 /* Buffer too small */
8245 ASSERT_ALLOC( output, expected_output->len - 1 );
8246 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8247 peer_key_data->x, peer_key_data->len,
8248 output, expected_output->len - 1,
8249 &output_length ),
8250 PSA_ERROR_BUFFER_TOO_SMALL );
8251 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008252 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008253 mbedtls_free( output );
8254 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008255
8256exit:
8257 mbedtls_free( output );
8258 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008259 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008260}
8261/* END_CASE */
8262
8263/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008264void key_agreement_capacity( int alg_arg,
8265 int our_key_type_arg, data_t *our_key_data,
8266 data_t *peer_key_data,
8267 int expected_capacity_arg )
8268{
Ronald Cron5425a212020-08-04 14:58:35 +02008269 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008270 psa_algorithm_t alg = alg_arg;
8271 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008272 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008274 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008275 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008276
Gilles Peskine8817f612018-12-18 00:18:46 +01008277 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008278
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008279 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8280 psa_set_key_algorithm( &attributes, alg );
8281 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008282 PSA_ASSERT( psa_import_key( &attributes,
8283 our_key_data->x, our_key_data->len,
8284 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008286 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008287 PSA_ASSERT( psa_key_derivation_key_agreement(
8288 &operation,
8289 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8290 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008291 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8292 {
8293 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008294 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008295 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008296 NULL, 0 ) );
8297 }
Gilles Peskine59685592018-09-18 12:11:34 +02008298
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008299 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008300 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008301 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008302 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008303
Gilles Peskinebf491972018-10-25 22:36:12 +02008304 /* Test the actual capacity by reading the output. */
8305 while( actual_capacity > sizeof( output ) )
8306 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008307 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008308 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008309 actual_capacity -= sizeof( output );
8310 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008311 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008312 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008313 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008314 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008315
Gilles Peskine59685592018-09-18 12:11:34 +02008316exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008317 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008318 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008319 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008320}
8321/* END_CASE */
8322
8323/* BEGIN_CASE */
8324void key_agreement_output( int alg_arg,
8325 int our_key_type_arg, data_t *our_key_data,
8326 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008327 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008328{
Ronald Cron5425a212020-08-04 14:58:35 +02008329 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008330 psa_algorithm_t alg = alg_arg;
8331 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008332 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008334 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008335
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008336 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8337 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008338
Gilles Peskine8817f612018-12-18 00:18:46 +01008339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008340
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008341 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8342 psa_set_key_algorithm( &attributes, alg );
8343 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008344 PSA_ASSERT( psa_import_key( &attributes,
8345 our_key_data->x, our_key_data->len,
8346 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008347
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008348 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008349 PSA_ASSERT( psa_key_derivation_key_agreement(
8350 &operation,
8351 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8352 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008353 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8354 {
8355 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008356 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008357 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008358 NULL, 0 ) );
8359 }
Gilles Peskine59685592018-09-18 12:11:34 +02008360
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008361 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008362 actual_output,
8363 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008364 ASSERT_COMPARE( actual_output, expected_output1->len,
8365 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008366 if( expected_output2->len != 0 )
8367 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008368 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008369 actual_output,
8370 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008371 ASSERT_COMPARE( actual_output, expected_output2->len,
8372 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008373 }
Gilles Peskine59685592018-09-18 12:11:34 +02008374
8375exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008376 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008377 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008378 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008379 mbedtls_free( actual_output );
8380}
8381/* END_CASE */
8382
8383/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008384void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008385{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008386 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008387 unsigned char *output = NULL;
8388 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008389 size_t i;
8390 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008391
Simon Butcher49f8e312020-03-03 15:51:50 +00008392 TEST_ASSERT( bytes_arg >= 0 );
8393
Gilles Peskine91892022021-02-08 19:50:26 +01008394 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008395 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008396
Gilles Peskine8817f612018-12-18 00:18:46 +01008397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008398
Gilles Peskinea50d7392018-06-21 10:22:13 +02008399 /* Run several times, to ensure that every output byte will be
8400 * nonzero at least once with overwhelming probability
8401 * (2^(-8*number_of_runs)). */
8402 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008403 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008404 if( bytes != 0 )
8405 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008406 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008407
Gilles Peskinea50d7392018-06-21 10:22:13 +02008408 for( i = 0; i < bytes; i++ )
8409 {
8410 if( output[i] != 0 )
8411 ++changed[i];
8412 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008413 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008414
8415 /* Check that every byte was changed to nonzero at least once. This
8416 * validates that psa_generate_random is overwriting every byte of
8417 * the output buffer. */
8418 for( i = 0; i < bytes; i++ )
8419 {
8420 TEST_ASSERT( changed[i] != 0 );
8421 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008422
8423exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008424 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008425 mbedtls_free( output );
8426 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008427}
8428/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008429
8430/* BEGIN_CASE */
8431void generate_key( int type_arg,
8432 int bits_arg,
8433 int usage_arg,
8434 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008435 int expected_status_arg,
8436 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008437{
Ronald Cron5425a212020-08-04 14:58:35 +02008438 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008439 psa_key_type_t type = type_arg;
8440 psa_key_usage_t usage = usage_arg;
8441 size_t bits = bits_arg;
8442 psa_algorithm_t alg = alg_arg;
8443 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008445 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008446
Gilles Peskine8817f612018-12-18 00:18:46 +01008447 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008448
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008449 psa_set_key_usage_flags( &attributes, usage );
8450 psa_set_key_algorithm( &attributes, alg );
8451 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008452 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008453
8454 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008455 psa_status_t status = psa_generate_key( &attributes, &key );
8456
8457 if( is_large_key > 0 )
8458 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8459 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008460 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008461 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008462
8463 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008464 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008465 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8466 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008467
Gilles Peskine818ca122018-06-20 18:16:48 +02008468 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008469 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008470 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008471
8472exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008473 /*
8474 * Key attributes may have been returned by psa_get_key_attributes()
8475 * thus reset them as required.
8476 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008477 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008478
Ronald Cron5425a212020-08-04 14:58:35 +02008479 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008480 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008481}
8482/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008483
Ronald Cronee414c72021-03-18 18:50:08 +01008484/* 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 +02008485void generate_key_rsa( int bits_arg,
8486 data_t *e_arg,
8487 int expected_status_arg )
8488{
Ronald Cron5425a212020-08-04 14:58:35 +02008489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008490 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008491 size_t bits = bits_arg;
8492 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8493 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8494 psa_status_t expected_status = expected_status_arg;
8495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8496 uint8_t *exported = NULL;
8497 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008498 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008499 size_t exported_length = SIZE_MAX;
8500 uint8_t *e_read_buffer = NULL;
8501 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008502 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008503 size_t e_read_length = SIZE_MAX;
8504
8505 if( e_arg->len == 0 ||
8506 ( e_arg->len == 3 &&
8507 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8508 {
8509 is_default_public_exponent = 1;
8510 e_read_size = 0;
8511 }
8512 ASSERT_ALLOC( e_read_buffer, e_read_size );
8513 ASSERT_ALLOC( exported, exported_size );
8514
8515 PSA_ASSERT( psa_crypto_init( ) );
8516
8517 psa_set_key_usage_flags( &attributes, usage );
8518 psa_set_key_algorithm( &attributes, alg );
8519 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8520 e_arg->x, e_arg->len ) );
8521 psa_set_key_bits( &attributes, bits );
8522
8523 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008524 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008525 if( expected_status != PSA_SUCCESS )
8526 goto exit;
8527
8528 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008529 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008530 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8531 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8532 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8533 e_read_buffer, e_read_size,
8534 &e_read_length ) );
8535 if( is_default_public_exponent )
8536 TEST_EQUAL( e_read_length, 0 );
8537 else
8538 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8539
8540 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008541 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008542 goto exit;
8543
8544 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008545 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008546 exported, exported_size,
8547 &exported_length ) );
8548 {
8549 uint8_t *p = exported;
8550 uint8_t *end = exported + exported_length;
8551 size_t len;
8552 /* RSAPublicKey ::= SEQUENCE {
8553 * modulus INTEGER, -- n
8554 * publicExponent INTEGER } -- e
8555 */
8556 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008557 MBEDTLS_ASN1_SEQUENCE |
8558 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008559 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008560 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8561 MBEDTLS_ASN1_INTEGER ) );
8562 if( len >= 1 && p[0] == 0 )
8563 {
8564 ++p;
8565 --len;
8566 }
8567 if( e_arg->len == 0 )
8568 {
8569 TEST_EQUAL( len, 3 );
8570 TEST_EQUAL( p[0], 1 );
8571 TEST_EQUAL( p[1], 0 );
8572 TEST_EQUAL( p[2], 1 );
8573 }
8574 else
8575 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8576 }
8577
8578exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008579 /*
8580 * Key attributes may have been returned by psa_get_key_attributes() or
8581 * set by psa_set_key_domain_parameters() thus reset them as required.
8582 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008583 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008584
Ronald Cron5425a212020-08-04 14:58:35 +02008585 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008586 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008587 mbedtls_free( e_read_buffer );
8588 mbedtls_free( exported );
8589}
8590/* END_CASE */
8591
Darryl Greend49a4992018-06-18 17:27:26 +01008592/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008593void persistent_key_load_key_from_storage( data_t *data,
8594 int type_arg, int bits_arg,
8595 int usage_flags_arg, int alg_arg,
8596 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008597{
Ronald Cron71016a92020-08-28 19:01:50 +02008598 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8601 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008602 psa_key_type_t type = type_arg;
8603 size_t bits = bits_arg;
8604 psa_key_usage_t usage_flags = usage_flags_arg;
8605 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008606 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008607 unsigned char *first_export = NULL;
8608 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008609 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008610 size_t first_exported_length;
8611 size_t second_exported_length;
8612
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008613 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8614 {
8615 ASSERT_ALLOC( first_export, export_size );
8616 ASSERT_ALLOC( second_export, export_size );
8617 }
Darryl Greend49a4992018-06-18 17:27:26 +01008618
Gilles Peskine8817f612018-12-18 00:18:46 +01008619 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008620
Gilles Peskinec87af662019-05-15 16:12:22 +02008621 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008622 psa_set_key_usage_flags( &attributes, usage_flags );
8623 psa_set_key_algorithm( &attributes, alg );
8624 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008625 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008626
Darryl Green0c6575a2018-11-07 16:05:30 +00008627 switch( generation_method )
8628 {
8629 case IMPORT_KEY:
8630 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008631 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008632 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008633 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008634
Darryl Green0c6575a2018-11-07 16:05:30 +00008635 case GENERATE_KEY:
8636 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008637 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008638 break;
8639
8640 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008641#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008642 {
8643 /* Create base key */
8644 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8645 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8646 psa_set_key_usage_flags( &base_attributes,
8647 PSA_KEY_USAGE_DERIVE );
8648 psa_set_key_algorithm( &base_attributes, derive_alg );
8649 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008650 PSA_ASSERT( psa_import_key( &base_attributes,
8651 data->x, data->len,
8652 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008653 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008654 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008655 PSA_ASSERT( psa_key_derivation_input_key(
8656 &operation,
8657 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008658 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008659 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008660 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008661 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8662 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008663 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008664 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008665 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008666 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008667 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008668#else
8669 TEST_ASSUME( ! "KDF not supported in this configuration" );
8670#endif
8671 break;
8672
8673 default:
8674 TEST_ASSERT( ! "generation_method not implemented in test" );
8675 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008676 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008677 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008678
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008679 /* Export the key if permitted by the key policy. */
8680 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8681 {
Ronald Cron5425a212020-08-04 14:58:35 +02008682 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008683 first_export, export_size,
8684 &first_exported_length ) );
8685 if( generation_method == IMPORT_KEY )
8686 ASSERT_COMPARE( data->x, data->len,
8687 first_export, first_exported_length );
8688 }
Darryl Greend49a4992018-06-18 17:27:26 +01008689
8690 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008691 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008692 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008693 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008694
Darryl Greend49a4992018-06-18 17:27:26 +01008695 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008696 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008697 TEST_ASSERT( mbedtls_svc_key_id_equal(
8698 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008699 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8700 PSA_KEY_LIFETIME_PERSISTENT );
8701 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8702 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008703 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008704 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008705 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008706
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008707 /* Export the key again if permitted by the key policy. */
8708 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008709 {
Ronald Cron5425a212020-08-04 14:58:35 +02008710 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008711 second_export, export_size,
8712 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008713 ASSERT_COMPARE( first_export, first_exported_length,
8714 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008715 }
8716
8717 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008718 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008719 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008720
8721exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008722 /*
8723 * Key attributes may have been returned by psa_get_key_attributes()
8724 * thus reset them as required.
8725 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008726 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008727
Darryl Greend49a4992018-06-18 17:27:26 +01008728 mbedtls_free( first_export );
8729 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008730 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008731 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008732 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008733 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008734}
8735/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008736
Neil Armstronga557cb82022-06-10 08:58:32 +02008737/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008738void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8739 int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008740 int input_first, data_t *pw_data,
Neil Armstrong2a73f212022-09-06 11:34:54 +02008741 int expected_status_setup_arg,
8742 int expected_status_set_role_arg,
8743 int expected_status_set_password_key_arg,
8744 int expected_status_input_output_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008745{
8746 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8747 psa_pake_operation_t operation = psa_pake_operation_init();
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008748 psa_pake_operation_t op_copy = psa_pake_operation_init();
Neil Armstrongd597bc72022-05-25 11:28:39 +02008749 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008750 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008751 psa_key_type_t key_type_pw = key_type_pw_arg;
8752 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008753 psa_algorithm_t hash_alg = hash_arg;
8754 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008757 psa_status_t expected_status_setup = expected_status_setup_arg;
8758 psa_status_t expected_status_set_role = expected_status_set_role_arg;
8759 psa_status_t expected_status_set_password_key =
8760 expected_status_set_password_key_arg;
8761 psa_status_t expected_status_input_output =
8762 expected_status_input_output_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008763 unsigned char *output_buffer = NULL;
8764 size_t output_len = 0;
8765
8766 PSA_INIT( );
8767
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008768 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8769 PSA_PAKE_STEP_KEY_SHARE);
8770 ASSERT_ALLOC( output_buffer, buf_size );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008771
8772 if( pw_data->len > 0 )
8773 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008774 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008775 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008776 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008777 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8778 &key ) );
8779 }
8780
8781 psa_pake_cs_set_algorithm( &cipher_suite, alg );
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008782 psa_pake_cs_set_primitive( &cipher_suite, primitive );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008783 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8784
Neil Armstrong645cccd2022-06-08 17:36:23 +02008785 PSA_ASSERT( psa_pake_abort( &operation ) );
8786
8787 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8788 PSA_ERROR_BAD_STATE );
8789 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8790 PSA_ERROR_BAD_STATE );
8791 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8792 PSA_ERROR_BAD_STATE );
8793 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8794 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008795 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8796 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008797 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008798 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008799 PSA_ERROR_BAD_STATE );
8800
8801 PSA_ASSERT( psa_pake_abort( &operation ) );
8802
Neil Armstrong2a73f212022-09-06 11:34:54 +02008803 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8804 expected_status_setup );
8805 if( expected_status_setup != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008806 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008807
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008808 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8809 PSA_ERROR_BAD_STATE );
8810
Neil Armstrong2a73f212022-09-06 11:34:54 +02008811 TEST_EQUAL( psa_pake_set_role( &operation, role),
8812 expected_status_set_role );
8813 if( expected_status_set_role != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008814 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008815
8816 if( pw_data->len > 0 )
8817 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008818 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8819 expected_status_set_password_key );
8820 if( expected_status_set_password_key != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008821 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008822 }
8823
Neil Armstrong707d9572022-06-08 17:31:49 +02008824 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8825 PSA_ERROR_INVALID_ARGUMENT );
8826 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8827 PSA_ERROR_INVALID_ARGUMENT );
8828
8829 const uint8_t unsupported_id[] = "abcd";
8830
8831 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8832 PSA_ERROR_NOT_SUPPORTED );
8833 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8834 PSA_ERROR_NOT_SUPPORTED );
8835
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008836 const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive,
8837 PSA_PAKE_STEP_KEY_SHARE );
8838 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive,
8839 PSA_PAKE_STEP_ZK_PUBLIC );
8840 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive,
8841 PSA_PAKE_STEP_ZK_PROOF );
8842
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008843 /* First round */
8844 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008845 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008846 /* Invalid parameters (input) */
8847 op_copy = operation;
8848 TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008849 NULL, 0 ),
8850 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008851 /* Invalid parameters (step) */
8852 op_copy = operation;
8853 TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008854 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008855 PSA_ERROR_INVALID_ARGUMENT );
8856 /* Invalid first step */
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008857 op_copy = operation;
8858 TEST_EQUAL( psa_pake_input( &op_copy, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008859 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008860 PSA_ERROR_BAD_STATE );
8861
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008862 /* Possibly valid */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008863 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008864 output_buffer, size_key_share ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008865 expected_status_input_output);
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008866
Neil Armstrong2a73f212022-09-06 11:34:54 +02008867 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008868 {
8869 /* Buffer too large */
8870 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008871 output_buffer, size_zk_public + 1 ),
8872 PSA_ERROR_INVALID_ARGUMENT );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008873
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008874 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008875 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008876 output_buffer, size_zk_public ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008877 PSA_ERROR_BAD_STATE );
8878 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008879 }
8880 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008881 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008882 /* Invalid parameters (output) */
8883 op_copy = operation;
8884 TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008885 NULL, 0, NULL ),
8886 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008887 op_copy = operation;
8888 /* Invalid parameters (step) */
8889 TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008890 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008891 PSA_ERROR_INVALID_ARGUMENT );
8892 /* Invalid first step */
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008893 op_copy = operation;
8894 TEST_EQUAL( psa_pake_output( &op_copy, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008895 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008896 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008897
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008898 /* Possibly valid */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008899 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008900 output_buffer, buf_size, &output_len ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008901 expected_status_input_output );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008902
Neil Armstrong2a73f212022-09-06 11:34:54 +02008903 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008904 {
8905 TEST_ASSERT( output_len > 0 );
8906
8907 /* Buffer too small */
8908 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008909 output_buffer, size_zk_public - 1, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008910 PSA_ERROR_BUFFER_TOO_SMALL );
8911
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008912 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008913 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008914 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008915 PSA_ERROR_BAD_STATE );
8916 }
8917 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008918
8919exit:
8920 PSA_ASSERT( psa_destroy_key( key ) );
8921 PSA_ASSERT( psa_pake_abort( &operation ) );
8922 mbedtls_free( output_buffer );
8923 PSA_DONE( );
8924}
8925/* END_CASE */
8926
Neil Armstronga557cb82022-06-10 08:58:32 +02008927/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008928void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8929 int client_input_first, int inject_error,
8930 data_t *pw_data )
8931{
8932 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8933 psa_pake_operation_t server = psa_pake_operation_init();
8934 psa_pake_operation_t client = psa_pake_operation_init();
8935 psa_algorithm_t alg = alg_arg;
8936 psa_algorithm_t hash_alg = hash_arg;
8937 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8938 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8939
8940 PSA_INIT( );
8941
8942 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8943 psa_set_key_algorithm( &attributes, alg );
8944 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8945 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8946 &key ) );
8947
8948 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8949 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8950 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8951
8952
8953 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8954 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8955
8956 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8957 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8958
8959 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8960 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8961
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008962 ecjpake_do_round( alg, primitive_arg, &server, &client,
8963 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008964
8965 if( inject_error == 1 || inject_error == 2 )
8966 goto exit;
8967
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008968 ecjpake_do_round( alg, primitive_arg, &server, &client,
8969 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008970
8971exit:
8972 psa_destroy_key( key );
8973 psa_pake_abort( &server );
8974 psa_pake_abort( &client );
8975 PSA_DONE( );
8976}
8977/* END_CASE */
8978
8979/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008980void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008981 int derive_alg_arg, data_t *pw_data,
8982 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008983{
8984 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8985 psa_pake_operation_t server = psa_pake_operation_init();
8986 psa_pake_operation_t client = psa_pake_operation_init();
8987 psa_algorithm_t alg = alg_arg;
8988 psa_algorithm_t hash_alg = hash_arg;
8989 psa_algorithm_t derive_alg = derive_alg_arg;
8990 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8992 psa_key_derivation_operation_t server_derive =
8993 PSA_KEY_DERIVATION_OPERATION_INIT;
8994 psa_key_derivation_operation_t client_derive =
8995 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008996
8997 PSA_INIT( );
8998
Neil Armstrongd597bc72022-05-25 11:28:39 +02008999 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9000 psa_set_key_algorithm( &attributes, alg );
9001 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9002 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9003 &key ) );
9004
9005 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9006 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9007 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9008
Neil Armstrong1e855602022-06-15 11:32:11 +02009009 /* Get shared key */
9010 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
9011 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
9012
9013 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
9014 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
9015 {
9016 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
9017 PSA_KEY_DERIVATION_INPUT_SEED,
9018 (const uint8_t*) "", 0) );
9019 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
9020 PSA_KEY_DERIVATION_INPUT_SEED,
9021 (const uint8_t*) "", 0) );
9022 }
9023
Neil Armstrongd597bc72022-05-25 11:28:39 +02009024 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9025 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9026
9027 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9028 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9029
9030 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9031 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9032
Neil Armstrong1e855602022-06-15 11:32:11 +02009033 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9034 PSA_ERROR_BAD_STATE );
9035 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9036 PSA_ERROR_BAD_STATE );
9037
Neil Armstrongf983caf2022-06-15 15:27:48 +02009038 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009039 ecjpake_do_round( alg, primitive_arg, &server, &client,
9040 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009041
Neil Armstrong1e855602022-06-15 11:32:11 +02009042 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9043 PSA_ERROR_BAD_STATE );
9044 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9045 PSA_ERROR_BAD_STATE );
9046
Neil Armstrongf983caf2022-06-15 15:27:48 +02009047 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009048 ecjpake_do_round( alg, primitive_arg, &server, &client,
9049 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009050
Neil Armstrongd597bc72022-05-25 11:28:39 +02009051 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9052 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9053
9054exit:
9055 psa_key_derivation_abort( &server_derive );
9056 psa_key_derivation_abort( &client_derive );
9057 psa_destroy_key( key );
9058 psa_pake_abort( &server );
9059 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009060 PSA_DONE( );
9061}
9062/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009063
9064/* BEGIN_CASE */
9065void ecjpake_size_macros( )
9066{
9067 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9068 const size_t bits = 256;
9069 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9070 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9071 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9072 PSA_ECC_FAMILY_SECP_R1 );
9073
9074 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9075 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9076 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9077 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9078 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9079 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9080 /* The output for ZK_PROOF is the same bitsize as the curve */
9081 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9082 PSA_BITS_TO_BYTES( bits ) );
9083
9084 /* Input sizes are the same as output sizes */
9085 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9086 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9087 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9088 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9089 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9090 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9091
9092 /* These inequalities will always hold even when other PAKEs are added */
9093 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9094 PSA_PAKE_OUTPUT_MAX_SIZE );
9095 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9096 PSA_PAKE_OUTPUT_MAX_SIZE );
9097 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9098 PSA_PAKE_OUTPUT_MAX_SIZE );
9099 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9100 PSA_PAKE_INPUT_MAX_SIZE );
9101 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9102 PSA_PAKE_INPUT_MAX_SIZE );
9103 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9104 PSA_PAKE_INPUT_MAX_SIZE );
9105}
9106/* END_CASE */