blob: 6c95c2a7cad72a007e44177573b6186a74ffa2f8 [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;
721 size_t buffer0_off = 0;
722 size_t buffer1_off = 0;
723 size_t s_g1_len, s_g2_len, s_a_len;
724 size_t s_g1_off, s_g2_off, s_a_off;
725 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
726 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
727 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
728 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
729 size_t c_g1_len, c_g2_len, c_a_len;
730 size_t c_g1_off, c_g2_off, c_a_off;
731 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
732 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
733 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
734 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
735 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200736 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
738 ASSERT_ALLOC( buffer0, buffer_length );
739 ASSERT_ALLOC( buffer1, buffer_length );
740
741 switch( round )
742 {
743 case 1:
744 /* Server first round Output */
745 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
746 buffer0 + buffer0_off,
747 512 - buffer0_off, &s_g1_len ) );
748 s_g1_off = buffer0_off;
749 buffer0_off += s_g1_len;
750 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
751 buffer0 + buffer0_off,
752 512 - buffer0_off, &s_x1_pk_len ) );
753 s_x1_pk_off = buffer0_off;
754 buffer0_off += s_x1_pk_len;
755 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
756 buffer0 + buffer0_off,
757 512 - buffer0_off, &s_x1_pr_len ) );
758 s_x1_pr_off = buffer0_off;
759 buffer0_off += s_x1_pr_len;
760 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
761 buffer0 + buffer0_off,
762 512 - buffer0_off, &s_g2_len ) );
763 s_g2_off = buffer0_off;
764 buffer0_off += s_g2_len;
765 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
766 buffer0 + buffer0_off,
767 512 - buffer0_off, &s_x2_pk_len ) );
768 s_x2_pk_off = buffer0_off;
769 buffer0_off += s_x2_pk_len;
770 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
771 buffer0 + buffer0_off,
772 512 - buffer0_off, &s_x2_pr_len ) );
773 s_x2_pr_off = buffer0_off;
774 buffer0_off += s_x2_pr_len;
775
776 if( inject_error == 1 )
777 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +0200778 buffer0[s_x1_pk_off + 8] >>= 4;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200779 buffer0[s_x2_pk_off + 7] <<= 4;
780 expected_status = PSA_ERROR_DATA_INVALID;
781 }
782
Neil Armstrong51009d72022-09-05 17:59:54 +0200783 /*
784 * When injecting errors in inputs, the implementation is
785 * free to detect it right away of with a delay.
786 * This permits delaying the error until the end of the input
787 * sequence, if no error appears then, this will be treated
788 * as an error.
789 */
790
Neil Armstrongf983caf2022-06-15 15:27:48 +0200791 if( client_input_first == 1 )
792 {
793 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200794 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
795 buffer0 + s_g1_off, s_g1_len );
796 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200797 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200798 TEST_EQUAL( status, expected_status );
799 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200800 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200801 else
802 {
803 TEST_EQUAL( status, PSA_SUCCESS );
804 }
805
806 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
807 buffer0 + s_x1_pk_off,
808 s_x1_pk_len );
809 if( inject_error == 1 && status != PSA_SUCCESS )
810 {
811 TEST_EQUAL( status, expected_status );
812 break;
813 }
814 else
815 {
816 TEST_EQUAL( status, PSA_SUCCESS );
817 }
818
819 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
820 buffer0 + s_x1_pr_off,
821 s_x1_pr_len );
822 if( inject_error == 1 && status != PSA_SUCCESS )
823 {
824 TEST_EQUAL( status, expected_status );
825 break;
826 }
827 else
828 {
829 TEST_EQUAL( status, PSA_SUCCESS );
830 }
831
832 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
833 buffer0 + s_g2_off,
834 s_g2_len );
835 if( inject_error == 1 && status != PSA_SUCCESS )
836 {
837 TEST_EQUAL( status, expected_status );
838 break;
839 }
840 else
841 {
842 TEST_EQUAL( status, PSA_SUCCESS );
843 }
844
845 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
846 buffer0 + s_x2_pk_off,
847 s_x2_pk_len );
848 if( inject_error == 1 && status != PSA_SUCCESS )
849 {
850 TEST_EQUAL( status, expected_status );
851 break;
852 }
853 else
854 {
855 TEST_EQUAL( status, PSA_SUCCESS );
856 }
857
858 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
859 buffer0 + s_x2_pr_off,
860 s_x2_pr_len );
861 if( inject_error == 1 && status != PSA_SUCCESS )
862 {
863 TEST_EQUAL( status, expected_status );
864 break;
865 }
866 else
867 {
868 TEST_EQUAL( status, PSA_SUCCESS );
869 }
870
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200871 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200872 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200873 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200874 }
875
876 /* Client first round Output */
877 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
878 buffer1 + buffer1_off,
879 512 - buffer1_off, &c_g1_len ) );
880 c_g1_off = buffer1_off;
881 buffer1_off += c_g1_len;
882 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
883 buffer1 + buffer1_off,
884 512 - buffer1_off, &c_x1_pk_len ) );
885 c_x1_pk_off = buffer1_off;
886 buffer1_off += c_x1_pk_len;
887 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
888 buffer1 + buffer1_off,
889 512 - buffer1_off, &c_x1_pr_len ) );
890 c_x1_pr_off = buffer1_off;
891 buffer1_off += c_x1_pr_len;
892 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
893 buffer1 + buffer1_off,
894 512 - buffer1_off, &c_g2_len ) );
895 c_g2_off = buffer1_off;
896 buffer1_off += c_g2_len;
897 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
898 buffer1 + buffer1_off,
899 512 - buffer1_off, &c_x2_pk_len ) );
900 c_x2_pk_off = buffer1_off;
901 buffer1_off += c_x2_pk_len;
902 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
903 buffer1 + buffer1_off,
904 512 - buffer1_off, &c_x2_pr_len ) );
905 c_x2_pr_off = buffer1_off;
906 buffer1_off += c_x2_pr_len;
907
908 if( client_input_first == 0 )
909 {
910 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200911 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
912 buffer0 + s_g1_off, s_g1_len );
913 if( inject_error == 1 && status != PSA_SUCCESS )
914 {
915 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200916 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200917 }
918 else
919 {
920 TEST_EQUAL( status, PSA_SUCCESS );
921 }
922
923 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
924 buffer0 + s_x1_pk_off,
925 s_x1_pk_len );
926 if( inject_error == 1 && status != PSA_SUCCESS )
927 {
928 TEST_EQUAL( status, expected_status );
929 break;
930 }
931 else
932 {
933 TEST_EQUAL( status, PSA_SUCCESS );
934 }
935
936 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
937 buffer0 + s_x1_pr_off,
938 s_x1_pr_len );
939 if( inject_error == 1 && status != PSA_SUCCESS )
940 {
941 TEST_EQUAL( status, expected_status );
942 break;
943 }
944 else
945 {
946 TEST_EQUAL( status, PSA_SUCCESS );
947 }
948
949 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
950 buffer0 + s_g2_off,
951 s_g2_len );
952 if( inject_error == 1 && status != PSA_SUCCESS )
953 {
954 TEST_EQUAL( status, expected_status );
955 break;
956 }
957 else
958 {
959 TEST_EQUAL( status, PSA_SUCCESS );
960 }
961
962 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
963 buffer0 + s_x2_pk_off,
964 s_x2_pk_len );
965 if( inject_error == 1 && status != PSA_SUCCESS )
966 {
967 TEST_EQUAL( status, expected_status );
968 break;
969 }
970 else
971 {
972 TEST_EQUAL( status, PSA_SUCCESS );
973 }
974
975 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
976 buffer0 + s_x2_pr_off,
977 s_x2_pr_len );
978 if( inject_error == 1 && status != PSA_SUCCESS )
979 {
980 TEST_EQUAL( status, expected_status );
981 break;
982 }
983 else
984 {
985 TEST_EQUAL( status, PSA_SUCCESS );
986 }
987
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200988 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200989 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200990 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200991 }
992
993 if( inject_error == 2 )
994 {
995 buffer1[c_x1_pk_off + 12] >>= 4;
996 buffer1[c_x2_pk_off + 7] <<= 4;
997 expected_status = PSA_ERROR_DATA_INVALID;
998 }
999
1000 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001001 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1002 buffer1 + c_g1_off, c_g1_len );
1003 if( inject_error == 2 && status != PSA_SUCCESS )
1004 {
1005 TEST_EQUAL( status, expected_status );
1006 break;
1007 }
1008 else
1009 {
1010 TEST_EQUAL( status, PSA_SUCCESS );
1011 }
1012
1013 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1014 buffer1 + c_x1_pk_off, c_x1_pk_len );
1015 if( inject_error == 2 && status != PSA_SUCCESS )
1016 {
1017 TEST_EQUAL( status, expected_status );
1018 break;
1019 }
1020 else
1021 {
1022 TEST_EQUAL( status, PSA_SUCCESS );
1023 }
1024
1025 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1026 buffer1 + c_x1_pr_off, c_x1_pr_len );
1027 if( inject_error == 2 && status != PSA_SUCCESS )
1028 {
1029 TEST_EQUAL( status, expected_status );
1030 break;
1031 }
1032 else
1033 {
1034 TEST_EQUAL( status, PSA_SUCCESS );
1035 }
1036
1037 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1038 buffer1 + c_g2_off, c_g2_len );
1039 if( inject_error == 2 && status != PSA_SUCCESS )
1040 {
1041 TEST_EQUAL( status, expected_status );
1042 break;
1043 }
1044 else
1045 {
1046 TEST_EQUAL( status, PSA_SUCCESS );
1047 }
1048
1049 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1050 buffer1 + c_x2_pk_off, c_x2_pk_len );
1051 if( inject_error == 2 && status != PSA_SUCCESS )
1052 {
1053 TEST_EQUAL( status, expected_status );
1054 break;
1055 }
1056 else
1057 {
1058 TEST_EQUAL( status, PSA_SUCCESS );
1059 }
1060
1061 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1062 buffer1 + c_x2_pr_off, c_x2_pr_len );
1063 if( inject_error == 2 && status != PSA_SUCCESS )
1064 {
1065 TEST_EQUAL( status, expected_status );
1066 break;
1067 }
1068 else
1069 {
1070 TEST_EQUAL( status, PSA_SUCCESS );
1071 }
1072
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001073 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001074 if( inject_error == 2 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001075 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001076
1077 break;
1078
1079 case 2:
1080 /* Server second round Output */
1081 buffer0_off = 0;
1082
1083 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1084 buffer0 + buffer0_off,
1085 512 - buffer0_off, &s_a_len ) );
1086 s_a_off = buffer0_off;
1087 buffer0_off += s_a_len;
1088 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1089 buffer0 + buffer0_off,
1090 512 - buffer0_off, &s_x2s_pk_len ) );
1091 s_x2s_pk_off = buffer0_off;
1092 buffer0_off += s_x2s_pk_len;
1093 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1094 buffer0 + buffer0_off,
1095 512 - buffer0_off, &s_x2s_pr_len ) );
1096 s_x2s_pr_off = buffer0_off;
1097 buffer0_off += s_x2s_pr_len;
1098
1099 if( inject_error == 3 )
1100 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001101 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001102 expected_status = PSA_ERROR_DATA_INVALID;
1103 }
1104
1105 if( client_input_first == 1 )
1106 {
1107 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001108 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1109 buffer0 + s_a_off, s_a_len );
1110 if( inject_error == 3 && status != PSA_SUCCESS )
1111 {
1112 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001113 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001114 }
1115 else
1116 {
1117 TEST_EQUAL( status, PSA_SUCCESS );
1118 }
1119
1120 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1121 buffer0 + s_x2s_pk_off,
1122 s_x2s_pk_len );
1123 if( inject_error == 3 && status != PSA_SUCCESS )
1124 {
1125 TEST_EQUAL( status, expected_status );
1126 break;
1127 }
1128 else
1129 {
1130 TEST_EQUAL( status, PSA_SUCCESS );
1131 }
1132
1133 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1134 buffer0 + s_x2s_pr_off,
1135 s_x2s_pr_len );
1136 if( inject_error == 3 && status != PSA_SUCCESS )
1137 {
1138 TEST_EQUAL( status, expected_status );
1139 break;
1140 }
1141 else
1142 {
1143 TEST_EQUAL( status, PSA_SUCCESS );
1144 }
1145
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001146 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001147 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001148 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001149 }
1150
1151 /* Client second round Output */
1152 buffer1_off = 0;
1153
1154 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1155 buffer1 + buffer1_off,
1156 512 - buffer1_off, &c_a_len ) );
1157 c_a_off = buffer1_off;
1158 buffer1_off += c_a_len;
1159 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1160 buffer1 + buffer1_off,
1161 512 - buffer1_off, &c_x2s_pk_len ) );
1162 c_x2s_pk_off = buffer1_off;
1163 buffer1_off += c_x2s_pk_len;
1164 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1165 buffer1 + buffer1_off,
1166 512 - buffer1_off, &c_x2s_pr_len ) );
1167 c_x2s_pr_off = buffer1_off;
1168 buffer1_off += c_x2s_pr_len;
1169
1170 if( client_input_first == 0 )
1171 {
1172 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001173 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1174 buffer0 + s_a_off, s_a_len );
1175 if( inject_error == 3 && status != PSA_SUCCESS )
1176 {
1177 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001178 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001179 }
1180 else
1181 {
1182 TEST_EQUAL( status, PSA_SUCCESS );
1183 }
1184
1185 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1186 buffer0 + s_x2s_pk_off,
1187 s_x2s_pk_len );
1188 if( inject_error == 3 && status != PSA_SUCCESS )
1189 {
1190 TEST_EQUAL( status, expected_status );
1191 break;
1192 }
1193 else
1194 {
1195 TEST_EQUAL( status, PSA_SUCCESS );
1196 }
1197
1198 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1199 buffer0 + s_x2s_pr_off,
1200 s_x2s_pr_len );
1201 if( inject_error == 3 && status != PSA_SUCCESS )
1202 {
1203 TEST_EQUAL( status, expected_status );
1204 break;
1205 }
1206 else
1207 {
1208 TEST_EQUAL( status, PSA_SUCCESS );
1209 }
1210
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001211 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001212 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001213 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001214 }
1215
1216 if( inject_error == 4 )
1217 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001218 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001219 expected_status = PSA_ERROR_DATA_INVALID;
1220 }
1221
1222 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001223 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1224 buffer1 + c_a_off, c_a_len );
1225 if( inject_error == 4 && status != PSA_SUCCESS )
1226 {
1227 TEST_EQUAL( status, expected_status );
1228 break;
1229 }
1230 else
1231 {
1232 TEST_EQUAL( status, PSA_SUCCESS );
1233 }
1234
1235 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1236 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1237 if( inject_error == 4 && status != PSA_SUCCESS )
1238 {
1239 TEST_EQUAL( status, expected_status );
1240 break;
1241 }
1242 else
1243 {
1244 TEST_EQUAL( status, PSA_SUCCESS );
1245 }
1246
1247 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1248 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1249 if( inject_error == 4 && status != PSA_SUCCESS )
1250 {
1251 TEST_EQUAL( status, expected_status );
1252 break;
1253 }
1254 else
1255 {
1256 TEST_EQUAL( status, PSA_SUCCESS );
1257 }
1258
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001259 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001260 if( inject_error == 4 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001261 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001262
1263 break;
1264
1265 }
1266
Neil Armstrongf983caf2022-06-15 15:27:48 +02001267exit:
1268 mbedtls_free( buffer0 );
1269 mbedtls_free( buffer1 );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001270}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001271#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001272
Gilles Peskinee59236f2018-01-27 23:32:46 +01001273/* END_HEADER */
1274
1275/* BEGIN_DEPENDENCIES
1276 * depends_on:MBEDTLS_PSA_CRYPTO_C
1277 * END_DEPENDENCIES
1278 */
1279
1280/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001281void static_checks( )
1282{
1283 size_t max_truncated_mac_size =
1284 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1285
1286 /* Check that the length for a truncated MAC always fits in the algorithm
1287 * encoding. The shifted mask is the maximum truncated value. The
1288 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001289 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001290}
1291/* END_CASE */
1292
1293/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001294void import_with_policy( int type_arg,
1295 int usage_arg, int alg_arg,
1296 int expected_status_arg )
1297{
1298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1299 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001300 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001301 psa_key_type_t type = type_arg;
1302 psa_key_usage_t usage = usage_arg;
1303 psa_algorithm_t alg = alg_arg;
1304 psa_status_t expected_status = expected_status_arg;
1305 const uint8_t key_material[16] = {0};
1306 psa_status_t status;
1307
1308 PSA_ASSERT( psa_crypto_init( ) );
1309
1310 psa_set_key_type( &attributes, type );
1311 psa_set_key_usage_flags( &attributes, usage );
1312 psa_set_key_algorithm( &attributes, alg );
1313
1314 status = psa_import_key( &attributes,
1315 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001316 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001317 TEST_EQUAL( status, expected_status );
1318 if( status != PSA_SUCCESS )
1319 goto exit;
1320
Ronald Cron5425a212020-08-04 14:58:35 +02001321 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001322 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001323 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001324 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001325 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001326 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001327
Ronald Cron5425a212020-08-04 14:58:35 +02001328 PSA_ASSERT( psa_destroy_key( key ) );
1329 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001330
1331exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001332 /*
1333 * Key attributes may have been returned by psa_get_key_attributes()
1334 * thus reset them as required.
1335 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001336 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001337
1338 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001339 PSA_DONE( );
1340}
1341/* END_CASE */
1342
1343/* BEGIN_CASE */
1344void import_with_data( data_t *data, int type_arg,
1345 int attr_bits_arg,
1346 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001347{
1348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1349 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001350 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001351 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001352 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001353 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
Gilles Peskine8817f612018-12-18 00:18:46 +01001356 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001357
Gilles Peskine4747d192019-04-17 15:05:45 +02001358 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001359 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001360
Ronald Cron5425a212020-08-04 14:58:35 +02001361 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001362 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001363 if( status != PSA_SUCCESS )
1364 goto exit;
1365
Ronald Cron5425a212020-08-04 14:58:35 +02001366 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001367 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001368 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001369 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001370 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001371
Ronald Cron5425a212020-08-04 14:58:35 +02001372 PSA_ASSERT( psa_destroy_key( key ) );
1373 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001374
1375exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001376 /*
1377 * Key attributes may have been returned by psa_get_key_attributes()
1378 * thus reset them as required.
1379 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001380 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001381
1382 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001383 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384}
1385/* END_CASE */
1386
1387/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001388void import_large_key( int type_arg, int byte_size_arg,
1389 int expected_status_arg )
1390{
1391 psa_key_type_t type = type_arg;
1392 size_t byte_size = byte_size_arg;
1393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1394 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001396 psa_status_t status;
1397 uint8_t *buffer = NULL;
1398 size_t buffer_size = byte_size + 1;
1399 size_t n;
1400
Steven Cooreman69967ce2021-01-18 18:01:08 +01001401 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001402 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001403 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001404 memset( buffer, 'K', byte_size );
1405
1406 PSA_ASSERT( psa_crypto_init( ) );
1407
1408 /* Try importing the key */
1409 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1410 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001411 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001412 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001413 TEST_EQUAL( status, expected_status );
1414
1415 if( status == PSA_SUCCESS )
1416 {
Ronald Cron5425a212020-08-04 14:58:35 +02001417 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001418 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1419 TEST_EQUAL( psa_get_key_bits( &attributes ),
1420 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001421 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001422 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001423 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001424 for( n = 0; n < byte_size; n++ )
1425 TEST_EQUAL( buffer[n], 'K' );
1426 for( n = byte_size; n < buffer_size; n++ )
1427 TEST_EQUAL( buffer[n], 0 );
1428 }
1429
1430exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001431 /*
1432 * Key attributes may have been returned by psa_get_key_attributes()
1433 * thus reset them as required.
1434 */
1435 psa_reset_key_attributes( &attributes );
1436
Ronald Cron5425a212020-08-04 14:58:35 +02001437 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001438 PSA_DONE( );
1439 mbedtls_free( buffer );
1440}
1441/* END_CASE */
1442
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001443/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001444void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1445{
Ronald Cron5425a212020-08-04 14:58:35 +02001446 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001447 size_t bits = bits_arg;
1448 psa_status_t expected_status = expected_status_arg;
1449 psa_status_t status;
1450 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001451 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001452 size_t buffer_size = /* Slight overapproximations */
1453 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001454 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001455 unsigned char *p;
1456 int ret;
1457 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001458 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001459
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001461 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001462
1463 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1464 bits, keypair ) ) >= 0 );
1465 length = ret;
1466
1467 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001468 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001469 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001470 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001471
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001472 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001473 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001474
1475exit:
1476 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001477 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001482void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001483 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001484 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301485 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486 int expected_bits,
1487 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001488 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489 int canonical_input )
1490{
Ronald Cron5425a212020-08-04 14:58:35 +02001491 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001493 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001494 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001495 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301496 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001497 unsigned char *exported = NULL;
1498 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001500 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001501 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504
Moran Pekercb088e72018-07-17 17:36:59 +03001505 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001506 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001507 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001508 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001510
Archana4d7ae1d2021-07-07 02:50:22 +05301511 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001512 psa_set_key_usage_flags( &attributes, usage_arg );
1513 psa_set_key_algorithm( &attributes, alg );
1514 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001515
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001516 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001517 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518
1519 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001520 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001521 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1522 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001523 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524
1525 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001526 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001527 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001528
1529 /* The exported length must be set by psa_export_key() to a value between 0
1530 * and export_size. On errors, the exported length must be 0. */
1531 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1532 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001533 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001534
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001535 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001536 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001537 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001538 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001539 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001541 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542
Gilles Peskineea38a922021-02-13 00:05:16 +01001543 /* Run sanity checks on the exported key. For non-canonical inputs,
1544 * this validates the canonical representations. For canonical inputs,
1545 * this doesn't directly validate the implementation, but it still helps
1546 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301547 if( !psa_key_lifetime_is_external( lifetime ) )
1548 {
1549 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1550 goto exit;
1551 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001552
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001553 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001554 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555 else
1556 {
Ronald Cron5425a212020-08-04 14:58:35 +02001557 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001558 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001559 &key2 ) );
1560 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001561 reexported,
1562 export_size,
1563 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001564 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301565 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001566 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001568 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301569 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301570 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001571 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572
1573destroy:
1574 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001575 PSA_ASSERT( psa_destroy_key( key ) );
1576 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001577
1578exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001579 /*
1580 * Key attributes may have been returned by psa_get_key_attributes()
1581 * thus reset them as required.
1582 */
1583 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301584 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001585 mbedtls_free( exported );
1586 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001587 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001588}
1589/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001590
Moran Pekerf709f4a2018-06-06 17:26:04 +03001591/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001592void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001593 int type_arg,
1594 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301595 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001596 int export_size_delta,
1597 int expected_export_status_arg,
1598 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001599{
Ronald Cron5425a212020-08-04 14:58:35 +02001600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001601 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001602 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001603 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001604 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301605 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001606 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001607 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001608 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001609 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001610
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001612
Archana4d7ae1d2021-07-07 02:50:22 +05301613 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001614 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1615 psa_set_key_algorithm( &attributes, alg );
1616 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001617
1618 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001619 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001620
Gilles Peskine49c25912018-10-29 15:15:31 +01001621 /* Export the public key */
1622 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001623 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001624 exported, export_size,
1625 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001626 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001627 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001628 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001629 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001630 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001631 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001632 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001633 TEST_LE_U( expected_public_key->len,
1634 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1635 TEST_LE_U( expected_public_key->len,
1636 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1637 TEST_LE_U( expected_public_key->len,
1638 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001639 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1640 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001641 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001642exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001643 /*
1644 * Key attributes may have been returned by psa_get_key_attributes()
1645 * thus reset them as required.
1646 */
1647 psa_reset_key_attributes( &attributes );
1648
itayzafrir3e02b3b2018-06-12 17:06:52 +03001649 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001650 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001651 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001652}
1653/* END_CASE */
1654
Gilles Peskine20035e32018-02-03 22:44:14 +01001655/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001656void import_and_exercise_key( data_t *data,
1657 int type_arg,
1658 int bits_arg,
1659 int alg_arg )
1660{
Ronald Cron5425a212020-08-04 14:58:35 +02001661 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001662 psa_key_type_t type = type_arg;
1663 size_t bits = bits_arg;
1664 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001665 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001667 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001668
Gilles Peskine8817f612018-12-18 00:18:46 +01001669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001670
Gilles Peskine4747d192019-04-17 15:05:45 +02001671 psa_set_key_usage_flags( &attributes, usage );
1672 psa_set_key_algorithm( &attributes, alg );
1673 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001674
1675 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001676 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001677
1678 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001679 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001680 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1681 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001682
1683 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001684 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001685 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001686
Ronald Cron5425a212020-08-04 14:58:35 +02001687 PSA_ASSERT( psa_destroy_key( key ) );
1688 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001689
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001690exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001691 /*
1692 * Key attributes may have been returned by psa_get_key_attributes()
1693 * thus reset them as required.
1694 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001695 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001696
1697 psa_reset_key_attributes( &attributes );
1698 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001699 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001700}
1701/* END_CASE */
1702
1703/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001704void effective_key_attributes( int type_arg, int expected_type_arg,
1705 int bits_arg, int expected_bits_arg,
1706 int usage_arg, int expected_usage_arg,
1707 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001708{
Ronald Cron5425a212020-08-04 14:58:35 +02001709 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001710 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001711 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001712 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001713 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001714 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001715 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001716 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001717 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001718 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001719
Gilles Peskine8817f612018-12-18 00:18:46 +01001720 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001721
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001722 psa_set_key_usage_flags( &attributes, usage );
1723 psa_set_key_algorithm( &attributes, alg );
1724 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001725 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001726
Ronald Cron5425a212020-08-04 14:58:35 +02001727 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001728 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001729
Ronald Cron5425a212020-08-04 14:58:35 +02001730 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001731 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1732 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1733 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1734 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001735
1736exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001737 /*
1738 * Key attributes may have been returned by psa_get_key_attributes()
1739 * thus reset them as required.
1740 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001741 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001742
1743 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001744 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001745}
1746/* END_CASE */
1747
1748/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001749void check_key_policy( int type_arg, int bits_arg,
1750 int usage_arg, int alg_arg )
1751{
1752 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001753 usage_arg,
1754 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001755 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001756 goto exit;
1757}
1758/* END_CASE */
1759
1760/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001761void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001762{
1763 /* Test each valid way of initializing the object, except for `= {0}`, as
1764 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1765 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001766 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001767 psa_key_attributes_t func = psa_key_attributes_init( );
1768 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1769 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001770
1771 memset( &zero, 0, sizeof( zero ) );
1772
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001773 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1774 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1775 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001776
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001777 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1778 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1779 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1780
1781 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1782 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1783 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1784
1785 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1786 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1787 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1788
1789 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1790 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1791 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001792}
1793/* END_CASE */
1794
1795/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001796void mac_key_policy( int policy_usage_arg,
1797 int policy_alg_arg,
1798 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001799 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001800 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001801 int expected_status_sign_arg,
1802 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001803{
Ronald Cron5425a212020-08-04 14:58:35 +02001804 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001806 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001807 psa_key_type_t key_type = key_type_arg;
1808 psa_algorithm_t policy_alg = policy_alg_arg;
1809 psa_algorithm_t exercise_alg = exercise_alg_arg;
1810 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001811 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001812 psa_status_t expected_status_sign = expected_status_sign_arg;
1813 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001814 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001815
Gilles Peskine8817f612018-12-18 00:18:46 +01001816 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001817
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001818 psa_set_key_usage_flags( &attributes, policy_usage );
1819 psa_set_key_algorithm( &attributes, policy_alg );
1820 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001821
Gilles Peskine049c7532019-05-15 20:22:09 +02001822 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001823 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001824
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001825 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1826 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001827
Ronald Cron5425a212020-08-04 14:58:35 +02001828 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001829 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001830
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001831 /* Calculate the MAC, one-shot case. */
1832 uint8_t input[128] = {0};
1833 size_t mac_len;
1834 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1835 input, 128,
1836 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1837 expected_status_sign );
1838
Neil Armstrong3af9b972022-02-07 12:20:21 +01001839 /* Calculate the MAC, multi-part case. */
1840 PSA_ASSERT( psa_mac_abort( &operation ) );
1841 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1842 if( status == PSA_SUCCESS )
1843 {
1844 status = psa_mac_update( &operation, input, 128 );
1845 if( status == PSA_SUCCESS )
1846 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1847 &mac_len ),
1848 expected_status_sign );
1849 else
1850 TEST_EQUAL( status, expected_status_sign );
1851 }
1852 else
1853 {
1854 TEST_EQUAL( status, expected_status_sign );
1855 }
1856 PSA_ASSERT( psa_mac_abort( &operation ) );
1857
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001858 /* Verify correct MAC, one-shot case. */
1859 status = psa_mac_verify( key, exercise_alg, input, 128,
1860 mac, mac_len );
1861
1862 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1863 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001864 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001865 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001866
Neil Armstrong3af9b972022-02-07 12:20:21 +01001867 /* Verify correct MAC, multi-part case. */
1868 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1869 if( status == PSA_SUCCESS )
1870 {
1871 status = psa_mac_update( &operation, input, 128 );
1872 if( status == PSA_SUCCESS )
1873 {
1874 status = psa_mac_verify_finish( &operation, mac, mac_len );
1875 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1876 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1877 else
1878 TEST_EQUAL( status, expected_status_verify );
1879 }
1880 else
1881 {
1882 TEST_EQUAL( status, expected_status_verify );
1883 }
1884 }
1885 else
1886 {
1887 TEST_EQUAL( status, expected_status_verify );
1888 }
1889
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001890 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001891
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001892 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001893 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001894 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895
1896exit:
1897 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001898 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001899 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001900}
1901/* END_CASE */
1902
1903/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001904void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905 int policy_alg,
1906 int key_type,
1907 data_t *key_data,
1908 int exercise_alg )
1909{
Ronald Cron5425a212020-08-04 14:58:35 +02001910 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001912 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001913 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001914 size_t output_buffer_size = 0;
1915 size_t input_buffer_size = 0;
1916 size_t output_length = 0;
1917 uint8_t *output = NULL;
1918 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001919 psa_status_t status;
1920
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001921 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1922 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1923 input_buffer_size );
1924
1925 ASSERT_ALLOC( input, input_buffer_size );
1926 ASSERT_ALLOC( output, output_buffer_size );
1927
Gilles Peskine8817f612018-12-18 00:18:46 +01001928 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001929
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001930 psa_set_key_usage_flags( &attributes, policy_usage );
1931 psa_set_key_algorithm( &attributes, policy_alg );
1932 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933
Gilles Peskine049c7532019-05-15 20:22:09 +02001934 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001935 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001936
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001937 /* Check if no key usage flag implication is done */
1938 TEST_EQUAL( policy_usage,
1939 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001940
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001941 /* Encrypt check, one-shot */
1942 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1943 output, output_buffer_size,
1944 &output_length);
1945 if( policy_alg == exercise_alg &&
1946 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1947 PSA_ASSERT( status );
1948 else
1949 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1950
1951 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001952 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953 if( policy_alg == exercise_alg &&
1954 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001955 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001957 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958 psa_cipher_abort( &operation );
1959
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001960 /* Decrypt check, one-shot */
1961 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1962 input, input_buffer_size,
1963 &output_length);
1964 if( policy_alg == exercise_alg &&
1965 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1966 PSA_ASSERT( status );
1967 else
1968 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1969
1970 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001971 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001972 if( policy_alg == exercise_alg &&
1973 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001974 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001976 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
1978exit:
1979 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001980 mbedtls_free( input );
1981 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001982 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001983 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984}
1985/* END_CASE */
1986
1987/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001988void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989 int policy_alg,
1990 int key_type,
1991 data_t *key_data,
1992 int nonce_length_arg,
1993 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001994 int exercise_alg,
1995 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001996{
Ronald Cron5425a212020-08-04 14:58:35 +02001997 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001998 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001999 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002000 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002001 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002002 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003 unsigned char nonce[16] = {0};
2004 size_t nonce_length = nonce_length_arg;
2005 unsigned char tag[16];
2006 size_t tag_length = tag_length_arg;
2007 size_t output_length;
2008
Gilles Peskine7be11a72022-04-14 00:12:57 +02002009 TEST_LE_U( nonce_length, sizeof( nonce ) );
2010 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011
Gilles Peskine8817f612018-12-18 00:18:46 +01002012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002013
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002014 psa_set_key_usage_flags( &attributes, policy_usage );
2015 psa_set_key_algorithm( &attributes, policy_alg );
2016 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017
Gilles Peskine049c7532019-05-15 20:22:09 +02002018 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002019 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002021 /* Check if no key usage implication is done */
2022 TEST_EQUAL( policy_usage,
2023 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002024
Neil Armstrong752d8112022-02-07 14:51:11 +01002025 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002026 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027 nonce, nonce_length,
2028 NULL, 0,
2029 NULL, 0,
2030 tag, tag_length,
2031 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002032 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2033 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002035 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002036
Neil Armstrong752d8112022-02-07 14:51:11 +01002037 /* Encrypt check, multi-part */
2038 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2039 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2040 TEST_EQUAL( status, expected_status );
2041 else
2042 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2043
2044 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002046 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047 nonce, nonce_length,
2048 NULL, 0,
2049 tag, tag_length,
2050 NULL, 0,
2051 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002052 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2053 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2054 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002055 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002057 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002058
Neil Armstrong752d8112022-02-07 14:51:11 +01002059 /* Decrypt check, multi-part */
2060 PSA_ASSERT( psa_aead_abort( &operation ) );
2061 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2062 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2063 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2064 else
2065 TEST_EQUAL( status, expected_status );
2066
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002068 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002069 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002070 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002071}
2072/* END_CASE */
2073
2074/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002075void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002076 int policy_alg,
2077 int key_type,
2078 data_t *key_data,
2079 int exercise_alg )
2080{
Ronald Cron5425a212020-08-04 14:58:35 +02002081 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002082 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002083 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084 psa_status_t status;
2085 size_t key_bits;
2086 size_t buffer_length;
2087 unsigned char *buffer = NULL;
2088 size_t output_length;
2089
Gilles Peskine8817f612018-12-18 00:18:46 +01002090 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002091
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002092 psa_set_key_usage_flags( &attributes, policy_usage );
2093 psa_set_key_algorithm( &attributes, policy_alg );
2094 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095
Gilles Peskine049c7532019-05-15 20:22:09 +02002096 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002097 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002099 /* Check if no key usage implication is done */
2100 TEST_EQUAL( policy_usage,
2101 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002102
Ronald Cron5425a212020-08-04 14:58:35 +02002103 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002104 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2106 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002107 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108
Ronald Cron5425a212020-08-04 14:58:35 +02002109 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002110 NULL, 0,
2111 NULL, 0,
2112 buffer, buffer_length,
2113 &output_length );
2114 if( policy_alg == exercise_alg &&
2115 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002116 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002117 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002118 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002120 if( buffer_length != 0 )
2121 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002122 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123 buffer, buffer_length,
2124 NULL, 0,
2125 buffer, buffer_length,
2126 &output_length );
2127 if( policy_alg == exercise_alg &&
2128 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002129 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002130 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002131 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132
2133exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002134 /*
2135 * Key attributes may have been returned by psa_get_key_attributes()
2136 * thus reset them as required.
2137 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002138 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002139
2140 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002141 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002142 mbedtls_free( buffer );
2143}
2144/* END_CASE */
2145
2146/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002147void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148 int policy_alg,
2149 int key_type,
2150 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002151 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002152 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002153 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002154{
Ronald Cron5425a212020-08-04 14:58:35 +02002155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002157 psa_key_usage_t policy_usage = policy_usage_arg;
2158 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002159 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002160 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2161 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2162 * compatible with the policy and `payload_length_arg` is supposed to be
2163 * a valid input length to sign. If `payload_length_arg <= 0`,
2164 * `exercise_alg` is supposed to be forbidden by the policy. */
2165 int compatible_alg = payload_length_arg > 0;
2166 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002167 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002168 size_t signature_length;
2169
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002170 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002171 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002172 TEST_EQUAL( expected_usage,
2173 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002174
Gilles Peskine8817f612018-12-18 00:18:46 +01002175 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002176
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002177 psa_set_key_usage_flags( &attributes, policy_usage );
2178 psa_set_key_algorithm( &attributes, policy_alg );
2179 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180
Gilles Peskine049c7532019-05-15 20:22:09 +02002181 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002182 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002183
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002184 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2185
Ronald Cron5425a212020-08-04 14:58:35 +02002186 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002187 payload, payload_length,
2188 signature, sizeof( signature ),
2189 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002190 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002192 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002193 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002194
2195 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002196 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002197 payload, payload_length,
2198 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002199 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002200 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002201 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002202 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002203
Gilles Peskinef7b41372021-09-22 16:15:05 +02002204 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002205 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002206 {
2207 status = psa_sign_message( key, exercise_alg,
2208 payload, payload_length,
2209 signature, sizeof( signature ),
2210 &signature_length );
2211 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2212 PSA_ASSERT( status );
2213 else
2214 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2215
2216 memset( signature, 0, sizeof( signature ) );
2217 status = psa_verify_message( key, exercise_alg,
2218 payload, payload_length,
2219 signature, sizeof( signature ) );
2220 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2221 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2222 else
2223 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2224 }
2225
Gilles Peskined5b33222018-06-18 22:20:03 +02002226exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002227 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002228 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002229}
2230/* END_CASE */
2231
Janos Follathba3fab92019-06-11 14:50:16 +01002232/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002233void derive_key_policy( int policy_usage,
2234 int policy_alg,
2235 int key_type,
2236 data_t *key_data,
2237 int exercise_alg )
2238{
Ronald Cron5425a212020-08-04 14:58:35 +02002239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002242 psa_status_t status;
2243
Gilles Peskine8817f612018-12-18 00:18:46 +01002244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002246 psa_set_key_usage_flags( &attributes, policy_usage );
2247 psa_set_key_algorithm( &attributes, policy_alg );
2248 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002249
Gilles Peskine049c7532019-05-15 20:22:09 +02002250 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002251 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002252
Janos Follathba3fab92019-06-11 14:50:16 +01002253 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2254
2255 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2256 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002257 {
Janos Follathba3fab92019-06-11 14:50:16 +01002258 PSA_ASSERT( psa_key_derivation_input_bytes(
2259 &operation,
2260 PSA_KEY_DERIVATION_INPUT_SEED,
2261 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002262 }
Janos Follathba3fab92019-06-11 14:50:16 +01002263
2264 status = psa_key_derivation_input_key( &operation,
2265 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002266 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002267
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268 if( policy_alg == exercise_alg &&
2269 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002270 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002271 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002272 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002273
2274exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002275 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002276 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002277 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002278}
2279/* END_CASE */
2280
2281/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002282void agreement_key_policy( int policy_usage,
2283 int policy_alg,
2284 int key_type_arg,
2285 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002286 int exercise_alg,
2287 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002288{
Ronald Cron5425a212020-08-04 14:58:35 +02002289 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002291 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002292 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002293 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002294 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002295
Gilles Peskine8817f612018-12-18 00:18:46 +01002296 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002297
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002298 psa_set_key_usage_flags( &attributes, policy_usage );
2299 psa_set_key_algorithm( &attributes, policy_alg );
2300 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002301
Gilles Peskine049c7532019-05-15 20:22:09 +02002302 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002303 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002305 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002306 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002307
Steven Cooremance48e852020-10-05 16:02:45 +02002308 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002309
2310exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002311 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002312 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002313 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002314}
2315/* END_CASE */
2316
2317/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002318void key_policy_alg2( int key_type_arg, data_t *key_data,
2319 int usage_arg, int alg_arg, int alg2_arg )
2320{
Ronald Cron5425a212020-08-04 14:58:35 +02002321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002322 psa_key_type_t key_type = key_type_arg;
2323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2324 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2325 psa_key_usage_t usage = usage_arg;
2326 psa_algorithm_t alg = alg_arg;
2327 psa_algorithm_t alg2 = alg2_arg;
2328
2329 PSA_ASSERT( psa_crypto_init( ) );
2330
2331 psa_set_key_usage_flags( &attributes, usage );
2332 psa_set_key_algorithm( &attributes, alg );
2333 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2334 psa_set_key_type( &attributes, key_type );
2335 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002336 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002337
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002338 /* Update the usage flags to obtain implicit usage flags */
2339 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002340 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002341 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2342 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2343 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2344
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002345 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002346 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002347 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002348 goto exit;
2349
2350exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002351 /*
2352 * Key attributes may have been returned by psa_get_key_attributes()
2353 * thus reset them as required.
2354 */
2355 psa_reset_key_attributes( &got_attributes );
2356
Ronald Cron5425a212020-08-04 14:58:35 +02002357 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002358 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002359}
2360/* END_CASE */
2361
2362/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002363void raw_agreement_key_policy( int policy_usage,
2364 int policy_alg,
2365 int key_type_arg,
2366 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002367 int exercise_alg,
2368 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002369{
Ronald Cron5425a212020-08-04 14:58:35 +02002370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002372 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002373 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002374 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002375 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002376
2377 PSA_ASSERT( psa_crypto_init( ) );
2378
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002379 psa_set_key_usage_flags( &attributes, policy_usage );
2380 psa_set_key_algorithm( &attributes, policy_alg );
2381 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002382
Gilles Peskine049c7532019-05-15 20:22:09 +02002383 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002384 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002385
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002386 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002387
Steven Cooremance48e852020-10-05 16:02:45 +02002388 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002389
2390exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002391 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002392 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002393 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002394}
2395/* END_CASE */
2396
2397/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002398void copy_success( int source_usage_arg,
2399 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302400 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002401 int type_arg, data_t *material,
2402 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002403 int target_usage_arg,
2404 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302405 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002406 int expected_usage_arg,
2407 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002408{
Gilles Peskineca25db92019-04-19 11:43:08 +02002409 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2410 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002411 psa_key_usage_t expected_usage = expected_usage_arg;
2412 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002413 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302414 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2415 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002416 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2417 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002418 uint8_t *export_buffer = NULL;
2419
Gilles Peskine57ab7212019-01-28 13:03:09 +01002420 PSA_ASSERT( psa_crypto_init( ) );
2421
Gilles Peskineca25db92019-04-19 11:43:08 +02002422 /* Prepare the source key. */
2423 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2424 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002425 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002426 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302427 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002428 PSA_ASSERT( psa_import_key( &source_attributes,
2429 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002430 &source_key ) );
2431 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002432
Gilles Peskineca25db92019-04-19 11:43:08 +02002433 /* Prepare the target attributes. */
2434 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002435 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002436 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002437 }
Archana8a180362021-07-05 02:18:48 +05302438 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002439
Gilles Peskineca25db92019-04-19 11:43:08 +02002440 if( target_usage_arg != -1 )
2441 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2442 if( target_alg_arg != -1 )
2443 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002444 if( target_alg2_arg != -1 )
2445 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002446
Archana8a180362021-07-05 02:18:48 +05302447
Gilles Peskine57ab7212019-01-28 13:03:09 +01002448 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002449 PSA_ASSERT( psa_copy_key( source_key,
2450 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002451
2452 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002453 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002454
2455 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002457 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2458 psa_get_key_type( &target_attributes ) );
2459 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2460 psa_get_key_bits( &target_attributes ) );
2461 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2462 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002463 TEST_EQUAL( expected_alg2,
2464 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002465 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2466 {
2467 size_t length;
2468 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002469 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002470 material->len, &length ) );
2471 ASSERT_COMPARE( material->x, material->len,
2472 export_buffer, length );
2473 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002474
Archana8a180362021-07-05 02:18:48 +05302475 if( !psa_key_lifetime_is_external( target_lifetime ) )
2476 {
2477 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2478 goto exit;
2479 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2480 goto exit;
2481 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002482
Ronald Cron5425a212020-08-04 14:58:35 +02002483 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002484
2485exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002486 /*
2487 * Source and target key attributes may have been returned by
2488 * psa_get_key_attributes() thus reset them as required.
2489 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002490 psa_reset_key_attributes( &source_attributes );
2491 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002492
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002493 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002494 mbedtls_free( export_buffer );
2495}
2496/* END_CASE */
2497
2498/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002499void copy_fail( int source_usage_arg,
2500 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302501 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002502 int type_arg, data_t *material,
2503 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002504 int target_usage_arg,
2505 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002506 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002507 int expected_status_arg )
2508{
2509 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2510 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002511 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2512 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002513 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002514
2515 PSA_ASSERT( psa_crypto_init( ) );
2516
2517 /* Prepare the source key. */
2518 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2519 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002520 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002521 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302522 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002523 PSA_ASSERT( psa_import_key( &source_attributes,
2524 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002525 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002526
2527 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002528 psa_set_key_id( &target_attributes, key_id );
2529 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002530 psa_set_key_type( &target_attributes, target_type_arg );
2531 psa_set_key_bits( &target_attributes, target_bits_arg );
2532 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2533 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002534 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002535
2536 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002537 TEST_EQUAL( psa_copy_key( source_key,
2538 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002539 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002540
Ronald Cron5425a212020-08-04 14:58:35 +02002541 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002542
Gilles Peskine4a644642019-05-03 17:14:08 +02002543exit:
2544 psa_reset_key_attributes( &source_attributes );
2545 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002546 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002547}
2548/* END_CASE */
2549
2550/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002551void hash_operation_init( )
2552{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002553 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002554 /* Test each valid way of initializing the object, except for `= {0}`, as
2555 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2556 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002557 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002558 psa_hash_operation_t func = psa_hash_operation_init( );
2559 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2560 psa_hash_operation_t zero;
2561
2562 memset( &zero, 0, sizeof( zero ) );
2563
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002564 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002565 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2566 PSA_ERROR_BAD_STATE );
2567 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2568 PSA_ERROR_BAD_STATE );
2569 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2570 PSA_ERROR_BAD_STATE );
2571
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002572 /* A default hash operation should be abortable without error. */
2573 PSA_ASSERT( psa_hash_abort( &func ) );
2574 PSA_ASSERT( psa_hash_abort( &init ) );
2575 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002576}
2577/* END_CASE */
2578
2579/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002580void hash_setup( int alg_arg,
2581 int expected_status_arg )
2582{
2583 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002584 uint8_t *output = NULL;
2585 size_t output_size = 0;
2586 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002587 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002588 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002589 psa_status_t status;
2590
Gilles Peskine8817f612018-12-18 00:18:46 +01002591 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002592
Neil Armstrongedb20862022-02-07 15:47:44 +01002593 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002594 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002595 ASSERT_ALLOC( output, output_size );
2596
2597 status = psa_hash_compute( alg, NULL, 0,
2598 output, output_size, &output_length );
2599 TEST_EQUAL( status, expected_status );
2600
2601 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002602 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002603 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002604
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002605 /* Whether setup succeeded or failed, abort must succeed. */
2606 PSA_ASSERT( psa_hash_abort( &operation ) );
2607
2608 /* If setup failed, reproduce the failure, so as to
2609 * test the resulting state of the operation object. */
2610 if( status != PSA_SUCCESS )
2611 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2612
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002613 /* Now the operation object should be reusable. */
2614#if defined(KNOWN_SUPPORTED_HASH_ALG)
2615 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2616 PSA_ASSERT( psa_hash_abort( &operation ) );
2617#endif
2618
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002620 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002621 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002622}
2623/* END_CASE */
2624
2625/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002626void hash_compute_fail( int alg_arg, data_t *input,
2627 int output_size_arg, int expected_status_arg )
2628{
2629 psa_algorithm_t alg = alg_arg;
2630 uint8_t *output = NULL;
2631 size_t output_size = output_size_arg;
2632 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002633 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002634 psa_status_t expected_status = expected_status_arg;
2635 psa_status_t status;
2636
2637 ASSERT_ALLOC( output, output_size );
2638
2639 PSA_ASSERT( psa_crypto_init( ) );
2640
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002641 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002642 status = psa_hash_compute( alg, input->x, input->len,
2643 output, output_size, &output_length );
2644 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002645 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002646
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002647 /* Hash Compute, multi-part */
2648 status = psa_hash_setup( &operation, alg );
2649 if( status == PSA_SUCCESS )
2650 {
2651 status = psa_hash_update( &operation, input->x, input->len );
2652 if( status == PSA_SUCCESS )
2653 {
2654 status = psa_hash_finish( &operation, output, output_size,
2655 &output_length );
2656 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002657 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002658 else
2659 TEST_EQUAL( status, expected_status );
2660 }
2661 else
2662 {
2663 TEST_EQUAL( status, expected_status );
2664 }
2665 }
2666 else
2667 {
2668 TEST_EQUAL( status, expected_status );
2669 }
2670
Gilles Peskine0a749c82019-11-28 19:33:58 +01002671exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002672 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002673 mbedtls_free( output );
2674 PSA_DONE( );
2675}
2676/* END_CASE */
2677
2678/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002679void hash_compare_fail( int alg_arg, data_t *input,
2680 data_t *reference_hash,
2681 int expected_status_arg )
2682{
2683 psa_algorithm_t alg = alg_arg;
2684 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002685 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002686 psa_status_t status;
2687
2688 PSA_ASSERT( psa_crypto_init( ) );
2689
Neil Armstrong55a1be12022-02-07 11:23:20 +01002690 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002691 status = psa_hash_compare( alg, input->x, input->len,
2692 reference_hash->x, reference_hash->len );
2693 TEST_EQUAL( status, expected_status );
2694
Neil Armstrong55a1be12022-02-07 11:23:20 +01002695 /* Hash Compare, multi-part */
2696 status = psa_hash_setup( &operation, alg );
2697 if( status == PSA_SUCCESS )
2698 {
2699 status = psa_hash_update( &operation, input->x, input->len );
2700 if( status == PSA_SUCCESS )
2701 {
2702 status = psa_hash_verify( &operation, reference_hash->x,
2703 reference_hash->len );
2704 TEST_EQUAL( status, expected_status );
2705 }
2706 else
2707 {
2708 TEST_EQUAL( status, expected_status );
2709 }
2710 }
2711 else
2712 {
2713 TEST_EQUAL( status, expected_status );
2714 }
2715
Gilles Peskine88e08462020-01-28 20:43:00 +01002716exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002717 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002718 PSA_DONE( );
2719}
2720/* END_CASE */
2721
2722/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002723void hash_compute_compare( int alg_arg, data_t *input,
2724 data_t *expected_output )
2725{
2726 psa_algorithm_t alg = alg_arg;
2727 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2728 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002729 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002730 size_t i;
2731
2732 PSA_ASSERT( psa_crypto_init( ) );
2733
Neil Armstrongca30a002022-02-07 11:40:23 +01002734 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002735 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002736 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002737 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002738 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002739 ASSERT_COMPARE( output, output_length,
2740 expected_output->x, expected_output->len );
2741
Neil Armstrongca30a002022-02-07 11:40:23 +01002742 /* Compute with tight buffer, multi-part */
2743 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2744 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2745 PSA_ASSERT( psa_hash_finish( &operation, output,
2746 PSA_HASH_LENGTH( alg ),
2747 &output_length ) );
2748 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2749 ASSERT_COMPARE( output, output_length,
2750 expected_output->x, expected_output->len );
2751
2752 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002753 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2754 output, sizeof( output ),
2755 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002756 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002757 ASSERT_COMPARE( output, output_length,
2758 expected_output->x, expected_output->len );
2759
Neil Armstrongca30a002022-02-07 11:40:23 +01002760 /* Compute with larger buffer, multi-part */
2761 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2762 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2763 PSA_ASSERT( psa_hash_finish( &operation, output,
2764 sizeof( output ), &output_length ) );
2765 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2766 ASSERT_COMPARE( output, output_length,
2767 expected_output->x, expected_output->len );
2768
2769 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002770 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2771 output, output_length ) );
2772
Neil Armstrongca30a002022-02-07 11:40:23 +01002773 /* Compare with correct hash, multi-part */
2774 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2775 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2776 PSA_ASSERT( psa_hash_verify( &operation, output,
2777 output_length ) );
2778
2779 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002780 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2781 output, output_length + 1 ),
2782 PSA_ERROR_INVALID_SIGNATURE );
2783
Neil Armstrongca30a002022-02-07 11:40:23 +01002784 /* Compare with trailing garbage, multi-part */
2785 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2786 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2787 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2788 PSA_ERROR_INVALID_SIGNATURE );
2789
2790 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002791 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2792 output, output_length - 1 ),
2793 PSA_ERROR_INVALID_SIGNATURE );
2794
Neil Armstrongca30a002022-02-07 11:40:23 +01002795 /* Compare with truncated hash, multi-part */
2796 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2797 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2798 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2799 PSA_ERROR_INVALID_SIGNATURE );
2800
Gilles Peskine0a749c82019-11-28 19:33:58 +01002801 /* Compare with corrupted value */
2802 for( i = 0; i < output_length; i++ )
2803 {
Chris Jones9634bb12021-01-20 15:56:42 +00002804 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002805 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002806
2807 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002808 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2809 output, output_length ),
2810 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002811
2812 /* Multi-Part */
2813 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2814 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2815 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2816 PSA_ERROR_INVALID_SIGNATURE );
2817
Gilles Peskine0a749c82019-11-28 19:33:58 +01002818 output[i] ^= 1;
2819 }
2820
2821exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002822 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002823 PSA_DONE( );
2824}
2825/* END_CASE */
2826
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002827/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002828void hash_bad_order( )
2829{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002830 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002831 unsigned char input[] = "";
2832 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002833 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002834 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2835 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2836 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002837 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002838 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002839 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002840
Gilles Peskine8817f612018-12-18 00:18:46 +01002841 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002842
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002843 /* Call setup twice in a row. */
2844 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002845 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002846 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2847 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002848 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002849 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002850 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002851
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002852 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002853 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002854 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002855 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002856
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002857 /* Check that update calls abort on error. */
2858 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002859 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002860 ASSERT_OPERATION_IS_ACTIVE( operation );
2861 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2862 PSA_ERROR_BAD_STATE );
2863 ASSERT_OPERATION_IS_INACTIVE( operation );
2864 PSA_ASSERT( psa_hash_abort( &operation ) );
2865 ASSERT_OPERATION_IS_INACTIVE( operation );
2866
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002867 /* Call update after finish. */
2868 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2869 PSA_ASSERT( psa_hash_finish( &operation,
2870 hash, sizeof( hash ), &hash_len ) );
2871 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002872 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002873 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002874
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002875 /* Call verify without calling setup beforehand. */
2876 TEST_EQUAL( psa_hash_verify( &operation,
2877 valid_hash, sizeof( valid_hash ) ),
2878 PSA_ERROR_BAD_STATE );
2879 PSA_ASSERT( psa_hash_abort( &operation ) );
2880
2881 /* Call verify after finish. */
2882 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2883 PSA_ASSERT( psa_hash_finish( &operation,
2884 hash, sizeof( hash ), &hash_len ) );
2885 TEST_EQUAL( psa_hash_verify( &operation,
2886 valid_hash, sizeof( valid_hash ) ),
2887 PSA_ERROR_BAD_STATE );
2888 PSA_ASSERT( psa_hash_abort( &operation ) );
2889
2890 /* Call verify twice in a row. */
2891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002892 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002893 PSA_ASSERT( psa_hash_verify( &operation,
2894 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002895 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002896 TEST_EQUAL( psa_hash_verify( &operation,
2897 valid_hash, sizeof( valid_hash ) ),
2898 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002899 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002900 PSA_ASSERT( psa_hash_abort( &operation ) );
2901
2902 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002903 TEST_EQUAL( psa_hash_finish( &operation,
2904 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002905 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002906 PSA_ASSERT( psa_hash_abort( &operation ) );
2907
2908 /* Call finish twice in a row. */
2909 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2910 PSA_ASSERT( psa_hash_finish( &operation,
2911 hash, sizeof( hash ), &hash_len ) );
2912 TEST_EQUAL( psa_hash_finish( &operation,
2913 hash, sizeof( hash ), &hash_len ),
2914 PSA_ERROR_BAD_STATE );
2915 PSA_ASSERT( psa_hash_abort( &operation ) );
2916
2917 /* Call finish after calling verify. */
2918 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2919 PSA_ASSERT( psa_hash_verify( &operation,
2920 valid_hash, sizeof( valid_hash ) ) );
2921 TEST_EQUAL( psa_hash_finish( &operation,
2922 hash, sizeof( hash ), &hash_len ),
2923 PSA_ERROR_BAD_STATE );
2924 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002925
2926exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002927 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002928}
2929/* END_CASE */
2930
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002931/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002932void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002933{
2934 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002935 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2936 * appended to it */
2937 unsigned char hash[] = {
2938 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2939 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2940 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002941 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002942 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002943
Gilles Peskine8817f612018-12-18 00:18:46 +01002944 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002945
itayzafrir27e69452018-11-01 14:26:34 +02002946 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002947 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002948 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002949 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002950 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002951 ASSERT_OPERATION_IS_INACTIVE( operation );
2952 PSA_ASSERT( psa_hash_abort( &operation ) );
2953 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002954
itayzafrir27e69452018-11-01 14:26:34 +02002955 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002957 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002958 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002959
itayzafrir27e69452018-11-01 14:26:34 +02002960 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002962 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002963 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002964
itayzafrirec93d302018-10-18 18:01:10 +03002965exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002966 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002967}
2968/* END_CASE */
2969
Ronald Cronee414c72021-03-18 18:50:08 +01002970/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002971void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002972{
2973 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002974 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002975 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002976 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002977 size_t hash_len;
2978
Gilles Peskine8817f612018-12-18 00:18:46 +01002979 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002980
itayzafrir58028322018-10-25 10:22:01 +03002981 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002982 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002983 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002984 hash, expected_size - 1, &hash_len ),
2985 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002986
2987exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002988 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002989}
2990/* END_CASE */
2991
Ronald Cronee414c72021-03-18 18:50:08 +01002992/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002993void hash_clone_source_state( )
2994{
2995 psa_algorithm_t alg = PSA_ALG_SHA_256;
2996 unsigned char hash[PSA_HASH_MAX_SIZE];
2997 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2998 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2999 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3000 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3001 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3002 size_t hash_len;
3003
3004 PSA_ASSERT( psa_crypto_init( ) );
3005 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3006
3007 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3008 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3009 PSA_ASSERT( psa_hash_finish( &op_finished,
3010 hash, sizeof( hash ), &hash_len ) );
3011 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3012 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3013
3014 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3015 PSA_ERROR_BAD_STATE );
3016
3017 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3018 PSA_ASSERT( psa_hash_finish( &op_init,
3019 hash, sizeof( hash ), &hash_len ) );
3020 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3021 PSA_ASSERT( psa_hash_finish( &op_finished,
3022 hash, sizeof( hash ), &hash_len ) );
3023 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3024 PSA_ASSERT( psa_hash_finish( &op_aborted,
3025 hash, sizeof( hash ), &hash_len ) );
3026
3027exit:
3028 psa_hash_abort( &op_source );
3029 psa_hash_abort( &op_init );
3030 psa_hash_abort( &op_setup );
3031 psa_hash_abort( &op_finished );
3032 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003033 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003034}
3035/* END_CASE */
3036
Ronald Cronee414c72021-03-18 18:50:08 +01003037/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003038void hash_clone_target_state( )
3039{
3040 psa_algorithm_t alg = PSA_ALG_SHA_256;
3041 unsigned char hash[PSA_HASH_MAX_SIZE];
3042 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3043 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3044 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3045 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3046 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3047 size_t hash_len;
3048
3049 PSA_ASSERT( psa_crypto_init( ) );
3050
3051 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3052 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3053 PSA_ASSERT( psa_hash_finish( &op_finished,
3054 hash, sizeof( hash ), &hash_len ) );
3055 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3056 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3057
3058 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3059 PSA_ASSERT( psa_hash_finish( &op_target,
3060 hash, sizeof( hash ), &hash_len ) );
3061
3062 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3063 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3064 PSA_ERROR_BAD_STATE );
3065 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3066 PSA_ERROR_BAD_STATE );
3067
3068exit:
3069 psa_hash_abort( &op_target );
3070 psa_hash_abort( &op_init );
3071 psa_hash_abort( &op_setup );
3072 psa_hash_abort( &op_finished );
3073 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003074 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003075}
3076/* END_CASE */
3077
itayzafrir58028322018-10-25 10:22:01 +03003078/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003079void mac_operation_init( )
3080{
Jaeden Amero252ef282019-02-15 14:05:35 +00003081 const uint8_t input[1] = { 0 };
3082
Jaeden Amero769ce272019-01-04 11:48:03 +00003083 /* Test each valid way of initializing the object, except for `= {0}`, as
3084 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3085 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003086 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003087 psa_mac_operation_t func = psa_mac_operation_init( );
3088 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3089 psa_mac_operation_t zero;
3090
3091 memset( &zero, 0, sizeof( zero ) );
3092
Jaeden Amero252ef282019-02-15 14:05:35 +00003093 /* A freshly-initialized MAC operation should not be usable. */
3094 TEST_EQUAL( psa_mac_update( &func,
3095 input, sizeof( input ) ),
3096 PSA_ERROR_BAD_STATE );
3097 TEST_EQUAL( psa_mac_update( &init,
3098 input, sizeof( input ) ),
3099 PSA_ERROR_BAD_STATE );
3100 TEST_EQUAL( psa_mac_update( &zero,
3101 input, sizeof( input ) ),
3102 PSA_ERROR_BAD_STATE );
3103
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003104 /* A default MAC operation should be abortable without error. */
3105 PSA_ASSERT( psa_mac_abort( &func ) );
3106 PSA_ASSERT( psa_mac_abort( &init ) );
3107 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003108}
3109/* END_CASE */
3110
3111/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003112void mac_setup( int key_type_arg,
3113 data_t *key,
3114 int alg_arg,
3115 int expected_status_arg )
3116{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003117 psa_key_type_t key_type = key_type_arg;
3118 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003119 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003121 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3122#if defined(KNOWN_SUPPORTED_MAC_ALG)
3123 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3124#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003125
Gilles Peskine8817f612018-12-18 00:18:46 +01003126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003127
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003128 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3129 &operation, &status ) )
3130 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003131 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003132
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003133 /* The operation object should be reusable. */
3134#if defined(KNOWN_SUPPORTED_MAC_ALG)
3135 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3136 smoke_test_key_data,
3137 sizeof( smoke_test_key_data ),
3138 KNOWN_SUPPORTED_MAC_ALG,
3139 &operation, &status ) )
3140 goto exit;
3141 TEST_EQUAL( status, PSA_SUCCESS );
3142#endif
3143
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003144exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003145 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003146}
3147/* END_CASE */
3148
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003149/* 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 +00003150void mac_bad_order( )
3151{
Ronald Cron5425a212020-08-04 14:58:35 +02003152 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003153 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3154 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003155 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003156 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3157 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3158 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003160 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3161 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3162 size_t sign_mac_length = 0;
3163 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3164 const uint8_t verify_mac[] = {
3165 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3166 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3167 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3168
3169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003170 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003171 psa_set_key_algorithm( &attributes, alg );
3172 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173
Ronald Cron5425a212020-08-04 14:58:35 +02003174 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3175 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003176
Jaeden Amero252ef282019-02-15 14:05:35 +00003177 /* Call update without calling setup beforehand. */
3178 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3179 PSA_ERROR_BAD_STATE );
3180 PSA_ASSERT( psa_mac_abort( &operation ) );
3181
3182 /* Call sign finish without calling setup beforehand. */
3183 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3184 &sign_mac_length),
3185 PSA_ERROR_BAD_STATE );
3186 PSA_ASSERT( psa_mac_abort( &operation ) );
3187
3188 /* Call verify finish without calling setup beforehand. */
3189 TEST_EQUAL( psa_mac_verify_finish( &operation,
3190 verify_mac, sizeof( verify_mac ) ),
3191 PSA_ERROR_BAD_STATE );
3192 PSA_ASSERT( psa_mac_abort( &operation ) );
3193
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003194 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003195 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003196 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003197 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003198 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003199 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003200 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003201 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003202
Jaeden Amero252ef282019-02-15 14:05:35 +00003203 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003204 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003205 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3206 PSA_ASSERT( psa_mac_sign_finish( &operation,
3207 sign_mac, sizeof( sign_mac ),
3208 &sign_mac_length ) );
3209 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3210 PSA_ERROR_BAD_STATE );
3211 PSA_ASSERT( psa_mac_abort( &operation ) );
3212
3213 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003214 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003215 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3216 PSA_ASSERT( psa_mac_verify_finish( &operation,
3217 verify_mac, sizeof( verify_mac ) ) );
3218 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3219 PSA_ERROR_BAD_STATE );
3220 PSA_ASSERT( psa_mac_abort( &operation ) );
3221
3222 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003223 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003224 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3225 PSA_ASSERT( psa_mac_sign_finish( &operation,
3226 sign_mac, sizeof( sign_mac ),
3227 &sign_mac_length ) );
3228 TEST_EQUAL( psa_mac_sign_finish( &operation,
3229 sign_mac, sizeof( sign_mac ),
3230 &sign_mac_length ),
3231 PSA_ERROR_BAD_STATE );
3232 PSA_ASSERT( psa_mac_abort( &operation ) );
3233
3234 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003235 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003236 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3237 PSA_ASSERT( psa_mac_verify_finish( &operation,
3238 verify_mac, sizeof( verify_mac ) ) );
3239 TEST_EQUAL( psa_mac_verify_finish( &operation,
3240 verify_mac, sizeof( verify_mac ) ),
3241 PSA_ERROR_BAD_STATE );
3242 PSA_ASSERT( psa_mac_abort( &operation ) );
3243
3244 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003245 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003246 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003247 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003248 TEST_EQUAL( psa_mac_verify_finish( &operation,
3249 verify_mac, sizeof( verify_mac ) ),
3250 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003251 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003252 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003253 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003254
3255 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003256 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003257 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003258 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003259 TEST_EQUAL( psa_mac_sign_finish( &operation,
3260 sign_mac, sizeof( sign_mac ),
3261 &sign_mac_length ),
3262 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003263 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003264 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003265 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003266
Ronald Cron5425a212020-08-04 14:58:35 +02003267 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003268
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003269exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003270 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003271}
3272/* END_CASE */
3273
3274/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003275void mac_sign_verify_multi( int key_type_arg,
3276 data_t *key_data,
3277 int alg_arg,
3278 data_t *input,
3279 int is_verify,
3280 data_t *expected_mac )
3281{
3282 size_t data_part_len = 0;
3283
3284 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3285 {
3286 /* Split data into length(data_part_len) parts. */
3287 mbedtls_test_set_step( 2000 + data_part_len );
3288
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003289 if( mac_multipart_internal_func( key_type_arg, key_data,
3290 alg_arg,
3291 input, data_part_len,
3292 expected_mac,
3293 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003294 break;
3295
3296 /* length(0) part, length(data_part_len) part, length(0) part... */
3297 mbedtls_test_set_step( 3000 + data_part_len );
3298
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003299 if( mac_multipart_internal_func( key_type_arg, key_data,
3300 alg_arg,
3301 input, data_part_len,
3302 expected_mac,
3303 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003304 break;
3305 }
3306
3307 /* Goto is required to silence warnings about unused labels, as we
3308 * don't actually do any test assertions in this function. */
3309 goto exit;
3310}
3311/* END_CASE */
3312
3313/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003314void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003315 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003316 int alg_arg,
3317 data_t *input,
3318 data_t *expected_mac )
3319{
Ronald Cron5425a212020-08-04 14:58:35 +02003320 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003321 psa_key_type_t key_type = key_type_arg;
3322 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003323 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003325 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003326 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003327 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003328 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003329 const size_t output_sizes_to_test[] = {
3330 0,
3331 1,
3332 expected_mac->len - 1,
3333 expected_mac->len,
3334 expected_mac->len + 1,
3335 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003336
Gilles Peskine7be11a72022-04-14 00:12:57 +02003337 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003338 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003339 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003340
Gilles Peskine8817f612018-12-18 00:18:46 +01003341 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003342
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003343 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003344 psa_set_key_algorithm( &attributes, alg );
3345 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003346
Ronald Cron5425a212020-08-04 14:58:35 +02003347 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3348 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003349
Gilles Peskine8b356b52020-08-25 23:44:59 +02003350 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3351 {
3352 const size_t output_size = output_sizes_to_test[i];
3353 psa_status_t expected_status =
3354 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3355 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003356
Chris Jones9634bb12021-01-20 15:56:42 +00003357 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003358 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003359
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003360 /* Calculate the MAC, one-shot case. */
3361 TEST_EQUAL( psa_mac_compute( key, alg,
3362 input->x, input->len,
3363 actual_mac, output_size, &mac_length ),
3364 expected_status );
3365 if( expected_status == PSA_SUCCESS )
3366 {
3367 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3368 actual_mac, mac_length );
3369 }
3370
3371 if( output_size > 0 )
3372 memset( actual_mac, 0, output_size );
3373
3374 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003375 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003376 PSA_ASSERT( psa_mac_update( &operation,
3377 input->x, input->len ) );
3378 TEST_EQUAL( psa_mac_sign_finish( &operation,
3379 actual_mac, output_size,
3380 &mac_length ),
3381 expected_status );
3382 PSA_ASSERT( psa_mac_abort( &operation ) );
3383
3384 if( expected_status == PSA_SUCCESS )
3385 {
3386 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3387 actual_mac, mac_length );
3388 }
3389 mbedtls_free( actual_mac );
3390 actual_mac = NULL;
3391 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003392
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003393exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003394 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003395 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003396 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003397 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003398}
3399/* END_CASE */
3400
3401/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003402void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003403 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003404 int alg_arg,
3405 data_t *input,
3406 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003407{
Ronald Cron5425a212020-08-04 14:58:35 +02003408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003409 psa_key_type_t key_type = key_type_arg;
3410 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003411 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003413 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003414
Gilles Peskine7be11a72022-04-14 00:12:57 +02003415 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003416
Gilles Peskine8817f612018-12-18 00:18:46 +01003417 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003418
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003419 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003420 psa_set_key_algorithm( &attributes, alg );
3421 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003422
Ronald Cron5425a212020-08-04 14:58:35 +02003423 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3424 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003425
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003426 /* Verify correct MAC, one-shot case. */
3427 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3428 expected_mac->x, expected_mac->len ) );
3429
3430 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003431 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003432 PSA_ASSERT( psa_mac_update( &operation,
3433 input->x, input->len ) );
3434 PSA_ASSERT( psa_mac_verify_finish( &operation,
3435 expected_mac->x,
3436 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003437
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003438 /* Test a MAC that's too short, one-shot case. */
3439 TEST_EQUAL( psa_mac_verify( key, alg,
3440 input->x, input->len,
3441 expected_mac->x,
3442 expected_mac->len - 1 ),
3443 PSA_ERROR_INVALID_SIGNATURE );
3444
3445 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003446 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003447 PSA_ASSERT( psa_mac_update( &operation,
3448 input->x, input->len ) );
3449 TEST_EQUAL( psa_mac_verify_finish( &operation,
3450 expected_mac->x,
3451 expected_mac->len - 1 ),
3452 PSA_ERROR_INVALID_SIGNATURE );
3453
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003454 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003455 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3456 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003457 TEST_EQUAL( psa_mac_verify( key, alg,
3458 input->x, input->len,
3459 perturbed_mac, expected_mac->len + 1 ),
3460 PSA_ERROR_INVALID_SIGNATURE );
3461
3462 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003463 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003464 PSA_ASSERT( psa_mac_update( &operation,
3465 input->x, input->len ) );
3466 TEST_EQUAL( psa_mac_verify_finish( &operation,
3467 perturbed_mac,
3468 expected_mac->len + 1 ),
3469 PSA_ERROR_INVALID_SIGNATURE );
3470
3471 /* Test changing one byte. */
3472 for( size_t i = 0; i < expected_mac->len; i++ )
3473 {
Chris Jones9634bb12021-01-20 15:56:42 +00003474 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003475 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003476
3477 TEST_EQUAL( psa_mac_verify( key, alg,
3478 input->x, input->len,
3479 perturbed_mac, expected_mac->len ),
3480 PSA_ERROR_INVALID_SIGNATURE );
3481
Ronald Cron5425a212020-08-04 14:58:35 +02003482 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003483 PSA_ASSERT( psa_mac_update( &operation,
3484 input->x, input->len ) );
3485 TEST_EQUAL( psa_mac_verify_finish( &operation,
3486 perturbed_mac,
3487 expected_mac->len ),
3488 PSA_ERROR_INVALID_SIGNATURE );
3489 perturbed_mac[i] ^= 1;
3490 }
3491
Gilles Peskine8c9def32018-02-08 10:02:12 +01003492exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003493 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003494 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003495 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003496 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003497}
3498/* END_CASE */
3499
3500/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003501void cipher_operation_init( )
3502{
Jaeden Ameroab439972019-02-15 14:12:05 +00003503 const uint8_t input[1] = { 0 };
3504 unsigned char output[1] = { 0 };
3505 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003506 /* Test each valid way of initializing the object, except for `= {0}`, as
3507 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3508 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003509 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003510 psa_cipher_operation_t func = psa_cipher_operation_init( );
3511 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3512 psa_cipher_operation_t zero;
3513
3514 memset( &zero, 0, sizeof( zero ) );
3515
Jaeden Ameroab439972019-02-15 14:12:05 +00003516 /* A freshly-initialized cipher operation should not be usable. */
3517 TEST_EQUAL( psa_cipher_update( &func,
3518 input, sizeof( input ),
3519 output, sizeof( output ),
3520 &output_length ),
3521 PSA_ERROR_BAD_STATE );
3522 TEST_EQUAL( psa_cipher_update( &init,
3523 input, sizeof( input ),
3524 output, sizeof( output ),
3525 &output_length ),
3526 PSA_ERROR_BAD_STATE );
3527 TEST_EQUAL( psa_cipher_update( &zero,
3528 input, sizeof( input ),
3529 output, sizeof( output ),
3530 &output_length ),
3531 PSA_ERROR_BAD_STATE );
3532
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003533 /* A default cipher operation should be abortable without error. */
3534 PSA_ASSERT( psa_cipher_abort( &func ) );
3535 PSA_ASSERT( psa_cipher_abort( &init ) );
3536 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003537}
3538/* END_CASE */
3539
3540/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003541void cipher_setup( int key_type_arg,
3542 data_t *key,
3543 int alg_arg,
3544 int expected_status_arg )
3545{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003546 psa_key_type_t key_type = key_type_arg;
3547 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003548 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003549 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003550 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003551#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003552 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3553#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003554
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003556
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003557 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3558 &operation, &status ) )
3559 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003560 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003561
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003562 /* The operation object should be reusable. */
3563#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3564 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3565 smoke_test_key_data,
3566 sizeof( smoke_test_key_data ),
3567 KNOWN_SUPPORTED_CIPHER_ALG,
3568 &operation, &status ) )
3569 goto exit;
3570 TEST_EQUAL( status, PSA_SUCCESS );
3571#endif
3572
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003573exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003574 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003575 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003576}
3577/* END_CASE */
3578
Ronald Cronee414c72021-03-18 18:50:08 +01003579/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003580void cipher_bad_order( )
3581{
Ronald Cron5425a212020-08-04 14:58:35 +02003582 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003583 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3584 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003586 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003587 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003588 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003589 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3590 0xaa, 0xaa, 0xaa, 0xaa };
3591 const uint8_t text[] = {
3592 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3593 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003594 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003595 size_t length = 0;
3596
3597 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003598 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3599 psa_set_key_algorithm( &attributes, alg );
3600 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003601 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3602 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003603
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003604 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003605 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003606 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003607 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003608 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003609 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003610 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003611 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003612
3613 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003614 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003615 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003616 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003617 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003618 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003619 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003620 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003621
Jaeden Ameroab439972019-02-15 14:12:05 +00003622 /* Generate an IV without calling setup beforehand. */
3623 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3624 buffer, sizeof( buffer ),
3625 &length ),
3626 PSA_ERROR_BAD_STATE );
3627 PSA_ASSERT( psa_cipher_abort( &operation ) );
3628
3629 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003630 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003631 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3632 buffer, sizeof( buffer ),
3633 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003634 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003635 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3636 buffer, sizeof( buffer ),
3637 &length ),
3638 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003639 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003640 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003641 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003642
3643 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003644 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003645 PSA_ASSERT( psa_cipher_set_iv( &operation,
3646 iv, sizeof( iv ) ) );
3647 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3648 buffer, sizeof( buffer ),
3649 &length ),
3650 PSA_ERROR_BAD_STATE );
3651 PSA_ASSERT( psa_cipher_abort( &operation ) );
3652
3653 /* Set an IV without calling setup beforehand. */
3654 TEST_EQUAL( psa_cipher_set_iv( &operation,
3655 iv, sizeof( iv ) ),
3656 PSA_ERROR_BAD_STATE );
3657 PSA_ASSERT( psa_cipher_abort( &operation ) );
3658
3659 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003660 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003661 PSA_ASSERT( psa_cipher_set_iv( &operation,
3662 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003663 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003664 TEST_EQUAL( psa_cipher_set_iv( &operation,
3665 iv, sizeof( iv ) ),
3666 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003667 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003668 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003669 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003670
3671 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003672 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003673 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3674 buffer, sizeof( buffer ),
3675 &length ) );
3676 TEST_EQUAL( psa_cipher_set_iv( &operation,
3677 iv, sizeof( iv ) ),
3678 PSA_ERROR_BAD_STATE );
3679 PSA_ASSERT( psa_cipher_abort( &operation ) );
3680
3681 /* Call update without calling setup beforehand. */
3682 TEST_EQUAL( psa_cipher_update( &operation,
3683 text, sizeof( text ),
3684 buffer, sizeof( buffer ),
3685 &length ),
3686 PSA_ERROR_BAD_STATE );
3687 PSA_ASSERT( psa_cipher_abort( &operation ) );
3688
3689 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003690 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003691 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003692 TEST_EQUAL( psa_cipher_update( &operation,
3693 text, sizeof( text ),
3694 buffer, sizeof( buffer ),
3695 &length ),
3696 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003697 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003698 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003699 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003700
3701 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003702 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003703 PSA_ASSERT( psa_cipher_set_iv( &operation,
3704 iv, sizeof( iv ) ) );
3705 PSA_ASSERT( psa_cipher_finish( &operation,
3706 buffer, sizeof( buffer ), &length ) );
3707 TEST_EQUAL( psa_cipher_update( &operation,
3708 text, sizeof( text ),
3709 buffer, sizeof( buffer ),
3710 &length ),
3711 PSA_ERROR_BAD_STATE );
3712 PSA_ASSERT( psa_cipher_abort( &operation ) );
3713
3714 /* Call finish without calling setup beforehand. */
3715 TEST_EQUAL( psa_cipher_finish( &operation,
3716 buffer, sizeof( buffer ), &length ),
3717 PSA_ERROR_BAD_STATE );
3718 PSA_ASSERT( psa_cipher_abort( &operation ) );
3719
3720 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003721 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003722 /* Not calling update means we are encrypting an empty buffer, which is OK
3723 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003724 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003725 TEST_EQUAL( psa_cipher_finish( &operation,
3726 buffer, sizeof( buffer ), &length ),
3727 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003728 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003729 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003730 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003731
3732 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003733 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003734 PSA_ASSERT( psa_cipher_set_iv( &operation,
3735 iv, sizeof( iv ) ) );
3736 PSA_ASSERT( psa_cipher_finish( &operation,
3737 buffer, sizeof( buffer ), &length ) );
3738 TEST_EQUAL( psa_cipher_finish( &operation,
3739 buffer, sizeof( buffer ), &length ),
3740 PSA_ERROR_BAD_STATE );
3741 PSA_ASSERT( psa_cipher_abort( &operation ) );
3742
Ronald Cron5425a212020-08-04 14:58:35 +02003743 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003744
Jaeden Ameroab439972019-02-15 14:12:05 +00003745exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003746 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003747 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003748}
3749/* END_CASE */
3750
3751/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003752void cipher_encrypt_fail( int alg_arg,
3753 int key_type_arg,
3754 data_t *key_data,
3755 data_t *input,
3756 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003757{
Ronald Cron5425a212020-08-04 14:58:35 +02003758 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003759 psa_status_t status;
3760 psa_key_type_t key_type = key_type_arg;
3761 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003762 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003763 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3764 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3765 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003766 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003767 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003768 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003769 size_t function_output_length;
3770 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3772
3773 if ( PSA_ERROR_BAD_STATE != expected_status )
3774 {
3775 PSA_ASSERT( psa_crypto_init( ) );
3776
3777 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3778 psa_set_key_algorithm( &attributes, alg );
3779 psa_set_key_type( &attributes, key_type );
3780
3781 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3782 input->len );
3783 ASSERT_ALLOC( output, output_buffer_size );
3784
3785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3786 &key ) );
3787 }
3788
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003789 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003790 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3791 output_buffer_size, &output_length );
3792
3793 TEST_EQUAL( status, expected_status );
3794
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003795 /* Encrypt, multi-part */
3796 status = psa_cipher_encrypt_setup( &operation, key, alg );
3797 if( status == PSA_SUCCESS )
3798 {
3799 if( alg != PSA_ALG_ECB_NO_PADDING )
3800 {
3801 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3802 iv, iv_size,
3803 &iv_length ) );
3804 }
3805
3806 status = psa_cipher_update( &operation, input->x, input->len,
3807 output, output_buffer_size,
3808 &function_output_length );
3809 if( status == PSA_SUCCESS )
3810 {
3811 output_length += function_output_length;
3812
3813 status = psa_cipher_finish( &operation, output + output_length,
3814 output_buffer_size - output_length,
3815 &function_output_length );
3816
3817 TEST_EQUAL( status, expected_status );
3818 }
3819 else
3820 {
3821 TEST_EQUAL( status, expected_status );
3822 }
3823 }
3824 else
3825 {
3826 TEST_EQUAL( status, expected_status );
3827 }
3828
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003829exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003830 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003831 mbedtls_free( output );
3832 psa_destroy_key( key );
3833 PSA_DONE( );
3834}
3835/* END_CASE */
3836
3837/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003838void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3839 data_t *input, int iv_length,
3840 int expected_result )
3841{
3842 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3843 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3845 size_t output_buffer_size = 0;
3846 unsigned char *output = NULL;
3847
3848 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3849 ASSERT_ALLOC( output, output_buffer_size );
3850
3851 PSA_ASSERT( psa_crypto_init( ) );
3852
3853 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3854 psa_set_key_algorithm( &attributes, alg );
3855 psa_set_key_type( &attributes, key_type );
3856
3857 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3858 &key ) );
3859 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3860 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3861 iv_length ) );
3862
3863exit:
3864 psa_cipher_abort( &operation );
3865 mbedtls_free( output );
3866 psa_destroy_key( key );
3867 PSA_DONE( );
3868}
3869/* END_CASE */
3870
3871/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003872void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3873 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003874{
3875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3876 psa_key_type_t key_type = key_type_arg;
3877 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003878 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3879 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003880 unsigned char *output = NULL;
3881 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003882 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3884
3885 PSA_ASSERT( psa_crypto_init( ) );
3886
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003887 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003888 TEST_LE_U( ciphertext->len,
3889 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3890 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003891 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003892 TEST_LE_U( plaintext->len,
3893 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3894 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3895 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003896
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003897
3898 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003899 psa_set_key_usage_flags( &attributes,
3900 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003901 psa_set_key_algorithm( &attributes, alg );
3902 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3904 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003905 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3906 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003907 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003908
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003909 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003910 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3911 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3912 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003913 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3914 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003915 PSA_ERROR_BAD_STATE );
3916
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003917 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003918 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3919 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003920 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003921 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003922 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3923 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003924 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003925 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003926
Gilles Peskine286c3142022-04-20 17:09:38 +02003927 /* Multipart encryption */
3928 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3929 output_length = 0;
3930 length = ~0;
3931 PSA_ASSERT( psa_cipher_update( &operation,
3932 plaintext->x, plaintext->len,
3933 output, output_buffer_size,
3934 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003935 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003936 output_length += length;
3937 PSA_ASSERT( psa_cipher_finish( &operation,
3938 output + output_length,
3939 output_buffer_size - output_length,
3940 &length ) );
3941 output_length += length;
3942 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003943 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003944
Gilles Peskine286c3142022-04-20 17:09:38 +02003945 /* Multipart encryption */
3946 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3947 output_length = 0;
3948 length = ~0;
3949 PSA_ASSERT( psa_cipher_update( &operation,
3950 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003951 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003952 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003953 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003954 output_length += length;
3955 PSA_ASSERT( psa_cipher_finish( &operation,
3956 output + output_length,
3957 output_buffer_size - output_length,
3958 &length ) );
3959 output_length += length;
3960 ASSERT_COMPARE( plaintext->x, plaintext->len,
3961 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003962
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003963 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003964 output_length = ~0;
3965 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3966 output, output_buffer_size,
3967 &output_length ) );
3968 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3969 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003970
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003971 /* One-shot decryption */
3972 output_length = ~0;
3973 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
3974 output, output_buffer_size,
3975 &output_length ) );
3976 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003977 output, output_length );
3978
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003979exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003980 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003981 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003982 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003983 psa_destroy_key( key );
3984 PSA_DONE( );
3985}
3986/* END_CASE */
3987
3988/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003989void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3990{
3991 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3992 psa_algorithm_t alg = alg_arg;
3993 psa_key_type_t key_type = key_type_arg;
3994 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3995 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3996 psa_status_t status;
3997
3998 PSA_ASSERT( psa_crypto_init( ) );
3999
4000 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4001 psa_set_key_algorithm( &attributes, alg );
4002 psa_set_key_type( &attributes, key_type );
4003
4004 /* Usage of either of these two size macros would cause divide by zero
4005 * with incorrect key types previously. Input length should be irrelevant
4006 * here. */
4007 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4008 0 );
4009 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4010
4011
4012 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4013 &key ) );
4014
4015 /* Should fail due to invalid alg type (to support invalid key type).
4016 * Encrypt or decrypt will end up in the same place. */
4017 status = psa_cipher_encrypt_setup( &operation, key, alg );
4018
4019 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4020
4021exit:
4022 psa_cipher_abort( &operation );
4023 psa_destroy_key( key );
4024 PSA_DONE( );
4025}
4026/* END_CASE */
4027
4028/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004029void cipher_encrypt_validation( int alg_arg,
4030 int key_type_arg,
4031 data_t *key_data,
4032 data_t *input )
4033{
4034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4035 psa_key_type_t key_type = key_type_arg;
4036 psa_algorithm_t alg = alg_arg;
4037 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4038 unsigned char *output1 = NULL;
4039 size_t output1_buffer_size = 0;
4040 size_t output1_length = 0;
4041 unsigned char *output2 = NULL;
4042 size_t output2_buffer_size = 0;
4043 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004044 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004045 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004047
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004049
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004050 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4051 psa_set_key_algorithm( &attributes, alg );
4052 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004053
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004054 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4055 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4056 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4057 ASSERT_ALLOC( output1, output1_buffer_size );
4058 ASSERT_ALLOC( output2, output2_buffer_size );
4059
Ronald Cron5425a212020-08-04 14:58:35 +02004060 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4061 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004062
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004063 /* The one-shot cipher encryption uses generated iv so validating
4064 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004065 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4066 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004067 TEST_LE_U( output1_length,
4068 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4069 TEST_LE_U( output1_length,
4070 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004071
4072 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4073 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004074
Gilles Peskine8817f612018-12-18 00:18:46 +01004075 PSA_ASSERT( psa_cipher_update( &operation,
4076 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004077 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004079 TEST_LE_U( function_output_length,
4080 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4081 TEST_LE_U( function_output_length,
4082 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004083 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004084
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004085 PSA_ASSERT( psa_cipher_finish( &operation,
4086 output2 + output2_length,
4087 output2_buffer_size - output2_length,
4088 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004089 TEST_LE_U( function_output_length,
4090 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4091 TEST_LE_U( function_output_length,
4092 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004093 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004094
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004095 PSA_ASSERT( psa_cipher_abort( &operation ) );
4096 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4097 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004098
Gilles Peskine50e586b2018-06-08 14:28:46 +02004099exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004100 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004101 mbedtls_free( output1 );
4102 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004103 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004104 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004105}
4106/* END_CASE */
4107
4108/* BEGIN_CASE */
4109void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004110 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004111 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004112 int first_part_size_arg,
4113 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004114 data_t *expected_output,
4115 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004116{
Ronald Cron5425a212020-08-04 14:58:35 +02004117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004118 psa_key_type_t key_type = key_type_arg;
4119 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004120 psa_status_t status;
4121 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004122 size_t first_part_size = first_part_size_arg;
4123 size_t output1_length = output1_length_arg;
4124 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004125 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004126 size_t output_buffer_size = 0;
4127 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004128 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004129 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004130 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004131
Gilles Peskine8817f612018-12-18 00:18:46 +01004132 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004133
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004134 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4135 psa_set_key_algorithm( &attributes, alg );
4136 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004137
Ronald Cron5425a212020-08-04 14:58:35 +02004138 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4139 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004140
Ronald Cron5425a212020-08-04 14:58:35 +02004141 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004142
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004143 if( iv->len > 0 )
4144 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004145 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004146 }
4147
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004148 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4149 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004150 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004151
Gilles Peskine7be11a72022-04-14 00:12:57 +02004152 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004153 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4154 output, output_buffer_size,
4155 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004156 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004157 TEST_LE_U( function_output_length,
4158 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4159 TEST_LE_U( function_output_length,
4160 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004161 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004162
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004163 if( first_part_size < input->len )
4164 {
4165 PSA_ASSERT( psa_cipher_update( &operation,
4166 input->x + first_part_size,
4167 input->len - first_part_size,
4168 ( output_buffer_size == 0 ? NULL :
4169 output + total_output_length ),
4170 output_buffer_size - total_output_length,
4171 &function_output_length ) );
4172 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004173 TEST_LE_U( function_output_length,
4174 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4175 alg,
4176 input->len - first_part_size ) );
4177 TEST_LE_U( function_output_length,
4178 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004179 total_output_length += function_output_length;
4180 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004181
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004182 status = psa_cipher_finish( &operation,
4183 ( output_buffer_size == 0 ? NULL :
4184 output + total_output_length ),
4185 output_buffer_size - total_output_length,
4186 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004187 TEST_LE_U( function_output_length,
4188 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4189 TEST_LE_U( function_output_length,
4190 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004191 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004192 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004193
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004194 if( expected_status == PSA_SUCCESS )
4195 {
4196 PSA_ASSERT( psa_cipher_abort( &operation ) );
4197
4198 ASSERT_COMPARE( expected_output->x, expected_output->len,
4199 output, total_output_length );
4200 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004201
4202exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004203 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004204 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004205 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004206 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004207}
4208/* END_CASE */
4209
4210/* BEGIN_CASE */
4211void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004212 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004213 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004214 int first_part_size_arg,
4215 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004216 data_t *expected_output,
4217 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004218{
Ronald Cron5425a212020-08-04 14:58:35 +02004219 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004220 psa_key_type_t key_type = key_type_arg;
4221 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004222 psa_status_t status;
4223 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004224 size_t first_part_size = first_part_size_arg;
4225 size_t output1_length = output1_length_arg;
4226 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004227 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004228 size_t output_buffer_size = 0;
4229 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004230 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004231 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233
Gilles Peskine8817f612018-12-18 00:18:46 +01004234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004235
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004236 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4237 psa_set_key_algorithm( &attributes, alg );
4238 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004239
Ronald Cron5425a212020-08-04 14:58:35 +02004240 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4241 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004242
Ronald Cron5425a212020-08-04 14:58:35 +02004243 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004244
Steven Cooreman177deba2020-09-07 17:14:14 +02004245 if( iv->len > 0 )
4246 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004247 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004248 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004249
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004250 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4251 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004252 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004253
Gilles Peskine7be11a72022-04-14 00:12:57 +02004254 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004255 PSA_ASSERT( psa_cipher_update( &operation,
4256 input->x, first_part_size,
4257 output, output_buffer_size,
4258 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004259 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004260 TEST_LE_U( function_output_length,
4261 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4262 TEST_LE_U( function_output_length,
4263 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004264 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004265
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004266 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004267 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004268 PSA_ASSERT( psa_cipher_update( &operation,
4269 input->x + first_part_size,
4270 input->len - first_part_size,
4271 ( output_buffer_size == 0 ? NULL :
4272 output + total_output_length ),
4273 output_buffer_size - total_output_length,
4274 &function_output_length ) );
4275 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004276 TEST_LE_U( function_output_length,
4277 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4278 alg,
4279 input->len - first_part_size ) );
4280 TEST_LE_U( function_output_length,
4281 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004282 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004283 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004284
Gilles Peskine50e586b2018-06-08 14:28:46 +02004285 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004286 ( output_buffer_size == 0 ? NULL :
4287 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004288 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004289 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004290 TEST_LE_U( function_output_length,
4291 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4292 TEST_LE_U( function_output_length,
4293 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004294 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004295 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004296
4297 if( expected_status == PSA_SUCCESS )
4298 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004299 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004300
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004301 ASSERT_COMPARE( expected_output->x, expected_output->len,
4302 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004303 }
4304
Gilles Peskine50e586b2018-06-08 14:28:46 +02004305exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004306 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004307 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004308 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004309 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004310}
4311/* END_CASE */
4312
Gilles Peskine50e586b2018-06-08 14:28:46 +02004313/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004314void cipher_decrypt_fail( int alg_arg,
4315 int key_type_arg,
4316 data_t *key_data,
4317 data_t *iv,
4318 data_t *input_arg,
4319 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004320{
4321 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4322 psa_status_t status;
4323 psa_key_type_t key_type = key_type_arg;
4324 psa_algorithm_t alg = alg_arg;
4325 psa_status_t expected_status = expected_status_arg;
4326 unsigned char *input = NULL;
4327 size_t input_buffer_size = 0;
4328 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004329 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004330 size_t output_buffer_size = 0;
4331 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004332 size_t function_output_length;
4333 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4335
4336 if ( PSA_ERROR_BAD_STATE != expected_status )
4337 {
4338 PSA_ASSERT( psa_crypto_init( ) );
4339
4340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4341 psa_set_key_algorithm( &attributes, alg );
4342 psa_set_key_type( &attributes, key_type );
4343
4344 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4345 &key ) );
4346 }
4347
4348 /* Allocate input buffer and copy the iv and the plaintext */
4349 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4350 if ( input_buffer_size > 0 )
4351 {
4352 ASSERT_ALLOC( input, input_buffer_size );
4353 memcpy( input, iv->x, iv->len );
4354 memcpy( input + iv->len, input_arg->x, input_arg->len );
4355 }
4356
4357 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4358 ASSERT_ALLOC( output, output_buffer_size );
4359
Neil Armstrong66a479f2022-02-07 15:41:19 +01004360 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004361 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4362 output_buffer_size, &output_length );
4363 TEST_EQUAL( status, expected_status );
4364
Neil Armstrong66a479f2022-02-07 15:41:19 +01004365 /* Decrypt, multi-part */
4366 status = psa_cipher_decrypt_setup( &operation, key, alg );
4367 if( status == PSA_SUCCESS )
4368 {
4369 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4370 input_arg->len ) +
4371 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4372 ASSERT_ALLOC( output_multi, output_buffer_size );
4373
4374 if( iv->len > 0 )
4375 {
4376 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4377
4378 if( status != PSA_SUCCESS )
4379 TEST_EQUAL( status, expected_status );
4380 }
4381
4382 if( status == PSA_SUCCESS )
4383 {
4384 status = psa_cipher_update( &operation,
4385 input_arg->x, input_arg->len,
4386 output_multi, output_buffer_size,
4387 &function_output_length );
4388 if( status == PSA_SUCCESS )
4389 {
4390 output_length = function_output_length;
4391
4392 status = psa_cipher_finish( &operation,
4393 output_multi + output_length,
4394 output_buffer_size - output_length,
4395 &function_output_length );
4396
4397 TEST_EQUAL( status, expected_status );
4398 }
4399 else
4400 {
4401 TEST_EQUAL( status, expected_status );
4402 }
4403 }
4404 else
4405 {
4406 TEST_EQUAL( status, expected_status );
4407 }
4408 }
4409 else
4410 {
4411 TEST_EQUAL( status, expected_status );
4412 }
4413
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004414exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004415 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004416 mbedtls_free( input );
4417 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004418 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004419 psa_destroy_key( key );
4420 PSA_DONE( );
4421}
4422/* END_CASE */
4423
4424/* BEGIN_CASE */
4425void cipher_decrypt( int alg_arg,
4426 int key_type_arg,
4427 data_t *key_data,
4428 data_t *iv,
4429 data_t *input_arg,
4430 data_t *expected_output )
4431{
4432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4433 psa_key_type_t key_type = key_type_arg;
4434 psa_algorithm_t alg = alg_arg;
4435 unsigned char *input = NULL;
4436 size_t input_buffer_size = 0;
4437 unsigned char *output = NULL;
4438 size_t output_buffer_size = 0;
4439 size_t output_length = 0;
4440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4441
4442 PSA_ASSERT( psa_crypto_init( ) );
4443
4444 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4445 psa_set_key_algorithm( &attributes, alg );
4446 psa_set_key_type( &attributes, key_type );
4447
4448 /* Allocate input buffer and copy the iv and the plaintext */
4449 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4450 if ( input_buffer_size > 0 )
4451 {
4452 ASSERT_ALLOC( input, input_buffer_size );
4453 memcpy( input, iv->x, iv->len );
4454 memcpy( input + iv->len, input_arg->x, input_arg->len );
4455 }
4456
4457 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4458 ASSERT_ALLOC( output, output_buffer_size );
4459
4460 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4461 &key ) );
4462
4463 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4464 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004465 TEST_LE_U( output_length,
4466 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4467 TEST_LE_U( output_length,
4468 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004469
4470 ASSERT_COMPARE( expected_output->x, expected_output->len,
4471 output, output_length );
4472exit:
4473 mbedtls_free( input );
4474 mbedtls_free( output );
4475 psa_destroy_key( key );
4476 PSA_DONE( );
4477}
4478/* END_CASE */
4479
4480/* BEGIN_CASE */
4481void cipher_verify_output( int alg_arg,
4482 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004483 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004484 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004485{
Ronald Cron5425a212020-08-04 14:58:35 +02004486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004487 psa_key_type_t key_type = key_type_arg;
4488 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004489 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004490 size_t output1_size = 0;
4491 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004492 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004493 size_t output2_size = 0;
4494 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004496
Gilles Peskine8817f612018-12-18 00:18:46 +01004497 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004498
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004499 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4500 psa_set_key_algorithm( &attributes, alg );
4501 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004502
Ronald Cron5425a212020-08-04 14:58:35 +02004503 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4504 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004505 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004506 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004507
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004508 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4509 output1, output1_size,
4510 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004511 TEST_LE_U( output1_length,
4512 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4513 TEST_LE_U( output1_length,
4514 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004515
4516 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004517 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004518
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004519 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4520 output2, output2_size,
4521 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004522 TEST_LE_U( output2_length,
4523 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4524 TEST_LE_U( output2_length,
4525 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004526
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004527 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004528
4529exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004530 mbedtls_free( output1 );
4531 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004532 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004533 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004534}
4535/* END_CASE */
4536
4537/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004538void cipher_verify_output_multipart( int alg_arg,
4539 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004540 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004541 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004542 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004543{
Ronald Cron5425a212020-08-04 14:58:35 +02004544 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004545 psa_key_type_t key_type = key_type_arg;
4546 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004547 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004548 unsigned char iv[16] = {0};
4549 size_t iv_size = 16;
4550 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004551 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004552 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004553 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004554 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004555 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004556 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004557 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004558 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4559 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004560 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004561
Gilles Peskine8817f612018-12-18 00:18:46 +01004562 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004563
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004564 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4565 psa_set_key_algorithm( &attributes, alg );
4566 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004567
Ronald Cron5425a212020-08-04 14:58:35 +02004568 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4569 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004570
Ronald Cron5425a212020-08-04 14:58:35 +02004571 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4572 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004573
Steven Cooreman177deba2020-09-07 17:14:14 +02004574 if( alg != PSA_ALG_ECB_NO_PADDING )
4575 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004576 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4577 iv, iv_size,
4578 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004579 }
4580
gabor-mezei-armceface22021-01-21 12:26:17 +01004581 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004582 TEST_LE_U( output1_buffer_size,
4583 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004584 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004585
Gilles Peskine7be11a72022-04-14 00:12:57 +02004586 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004587
Gilles Peskine8817f612018-12-18 00:18:46 +01004588 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4589 output1, output1_buffer_size,
4590 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004591 TEST_LE_U( function_output_length,
4592 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4593 TEST_LE_U( function_output_length,
4594 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004595 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004596
Gilles Peskine8817f612018-12-18 00:18:46 +01004597 PSA_ASSERT( psa_cipher_update( &operation1,
4598 input->x + first_part_size,
4599 input->len - first_part_size,
4600 output1, output1_buffer_size,
4601 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004602 TEST_LE_U( function_output_length,
4603 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4604 alg,
4605 input->len - first_part_size ) );
4606 TEST_LE_U( function_output_length,
4607 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004608 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004609
Gilles Peskine8817f612018-12-18 00:18:46 +01004610 PSA_ASSERT( psa_cipher_finish( &operation1,
4611 output1 + output1_length,
4612 output1_buffer_size - output1_length,
4613 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004614 TEST_LE_U( function_output_length,
4615 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4616 TEST_LE_U( function_output_length,
4617 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004618 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004619
Gilles Peskine8817f612018-12-18 00:18:46 +01004620 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004621
Gilles Peskine048b7f02018-06-08 14:20:49 +02004622 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004623 TEST_LE_U( output2_buffer_size,
4624 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4625 TEST_LE_U( output2_buffer_size,
4626 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004627 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004628
Steven Cooreman177deba2020-09-07 17:14:14 +02004629 if( iv_length > 0 )
4630 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004631 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4632 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004633 }
Moran Pekerded84402018-06-06 16:36:50 +03004634
Gilles Peskine8817f612018-12-18 00:18:46 +01004635 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4636 output2, output2_buffer_size,
4637 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004638 TEST_LE_U( function_output_length,
4639 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4640 TEST_LE_U( function_output_length,
4641 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004642 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004643
Gilles Peskine8817f612018-12-18 00:18:46 +01004644 PSA_ASSERT( psa_cipher_update( &operation2,
4645 output1 + first_part_size,
4646 output1_length - first_part_size,
4647 output2, output2_buffer_size,
4648 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004649 TEST_LE_U( function_output_length,
4650 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4651 alg,
4652 output1_length - first_part_size ) );
4653 TEST_LE_U( function_output_length,
4654 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004655 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004656
Gilles Peskine8817f612018-12-18 00:18:46 +01004657 PSA_ASSERT( psa_cipher_finish( &operation2,
4658 output2 + output2_length,
4659 output2_buffer_size - output2_length,
4660 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004661 TEST_LE_U( function_output_length,
4662 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4663 TEST_LE_U( function_output_length,
4664 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004665 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004666
Gilles Peskine8817f612018-12-18 00:18:46 +01004667 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004668
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004669 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004670
4671exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004672 psa_cipher_abort( &operation1 );
4673 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004674 mbedtls_free( output1 );
4675 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004676 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004677 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004678}
4679/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004680
Gilles Peskine20035e32018-02-03 22:44:14 +01004681/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004682void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004683 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004684 data_t *nonce,
4685 data_t *additional_data,
4686 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004687 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004688{
Ronald Cron5425a212020-08-04 14:58:35 +02004689 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004690 psa_key_type_t key_type = key_type_arg;
4691 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004692 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004693 unsigned char *output_data = NULL;
4694 size_t output_size = 0;
4695 size_t output_length = 0;
4696 unsigned char *output_data2 = NULL;
4697 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004698 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004699 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004701
Gilles Peskine8817f612018-12-18 00:18:46 +01004702 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004703
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004704 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4705 psa_set_key_algorithm( &attributes, alg );
4706 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004707
Gilles Peskine049c7532019-05-15 20:22:09 +02004708 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004709 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004710 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4711 key_bits = psa_get_key_bits( &attributes );
4712
4713 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4714 alg );
4715 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4716 * should be exact. */
4717 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4718 expected_result != PSA_ERROR_NOT_SUPPORTED )
4719 {
4720 TEST_EQUAL( output_size,
4721 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004722 TEST_LE_U( output_size,
4723 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004724 }
4725 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004726
Steven Cooremanf49478b2021-02-15 15:19:25 +01004727 status = psa_aead_encrypt( key, alg,
4728 nonce->x, nonce->len,
4729 additional_data->x,
4730 additional_data->len,
4731 input_data->x, input_data->len,
4732 output_data, output_size,
4733 &output_length );
4734
4735 /* If the operation is not supported, just skip and not fail in case the
4736 * encryption involves a common limitation of cryptography hardwares and
4737 * an alternative implementation. */
4738 if( status == PSA_ERROR_NOT_SUPPORTED )
4739 {
4740 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4741 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4742 }
4743
4744 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004745
4746 if( PSA_SUCCESS == expected_result )
4747 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004748 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004749
Gilles Peskine003a4a92019-05-14 16:09:40 +02004750 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4751 * should be exact. */
4752 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004753 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004754
Gilles Peskine7be11a72022-04-14 00:12:57 +02004755 TEST_LE_U( input_data->len,
4756 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004757
Ronald Cron5425a212020-08-04 14:58:35 +02004758 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004759 nonce->x, nonce->len,
4760 additional_data->x,
4761 additional_data->len,
4762 output_data, output_length,
4763 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004764 &output_length2 ),
4765 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004766
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004767 ASSERT_COMPARE( input_data->x, input_data->len,
4768 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004769 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004770
Gilles Peskinea1cac842018-06-11 19:33:02 +02004771exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004772 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004773 mbedtls_free( output_data );
4774 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004775 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004776}
4777/* END_CASE */
4778
4779/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004780void aead_encrypt( int key_type_arg, data_t *key_data,
4781 int alg_arg,
4782 data_t *nonce,
4783 data_t *additional_data,
4784 data_t *input_data,
4785 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004786{
Ronald Cron5425a212020-08-04 14:58:35 +02004787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004788 psa_key_type_t key_type = key_type_arg;
4789 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004790 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791 unsigned char *output_data = NULL;
4792 size_t output_size = 0;
4793 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004794 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004795 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004796
Gilles Peskine8817f612018-12-18 00:18:46 +01004797 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004798
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004799 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4800 psa_set_key_algorithm( &attributes, alg );
4801 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802
Gilles Peskine049c7532019-05-15 20:22:09 +02004803 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004804 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004805 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4806 key_bits = psa_get_key_bits( &attributes );
4807
4808 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4809 alg );
4810 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4811 * should be exact. */
4812 TEST_EQUAL( output_size,
4813 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004814 TEST_LE_U( output_size,
4815 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004816 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817
Steven Cooremand588ea12021-01-11 19:36:04 +01004818 status = psa_aead_encrypt( key, alg,
4819 nonce->x, nonce->len,
4820 additional_data->x, additional_data->len,
4821 input_data->x, input_data->len,
4822 output_data, output_size,
4823 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004824
Ronald Cron28a45ed2021-02-09 20:35:42 +01004825 /* If the operation is not supported, just skip and not fail in case the
4826 * encryption involves a common limitation of cryptography hardwares and
4827 * an alternative implementation. */
4828 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004829 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004830 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4831 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004832 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004833
4834 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004835 ASSERT_COMPARE( expected_result->x, expected_result->len,
4836 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004837
Gilles Peskinea1cac842018-06-11 19:33:02 +02004838exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004839 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004840 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004841 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004842}
4843/* END_CASE */
4844
4845/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004846void aead_decrypt( int key_type_arg, data_t *key_data,
4847 int alg_arg,
4848 data_t *nonce,
4849 data_t *additional_data,
4850 data_t *input_data,
4851 data_t *expected_data,
4852 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004853{
Ronald Cron5425a212020-08-04 14:58:35 +02004854 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004855 psa_key_type_t key_type = key_type_arg;
4856 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004857 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004858 unsigned char *output_data = NULL;
4859 size_t output_size = 0;
4860 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004861 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004862 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004863 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004864
Gilles Peskine8817f612018-12-18 00:18:46 +01004865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004866
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004867 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4868 psa_set_key_algorithm( &attributes, alg );
4869 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004870
Gilles Peskine049c7532019-05-15 20:22:09 +02004871 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004872 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004873 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4874 key_bits = psa_get_key_bits( &attributes );
4875
4876 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4877 alg );
4878 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4879 expected_result != PSA_ERROR_NOT_SUPPORTED )
4880 {
4881 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4882 * should be exact. */
4883 TEST_EQUAL( output_size,
4884 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004885 TEST_LE_U( output_size,
4886 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004887 }
4888 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004889
Steven Cooremand588ea12021-01-11 19:36:04 +01004890 status = psa_aead_decrypt( key, alg,
4891 nonce->x, nonce->len,
4892 additional_data->x,
4893 additional_data->len,
4894 input_data->x, input_data->len,
4895 output_data, output_size,
4896 &output_length );
4897
Ronald Cron28a45ed2021-02-09 20:35:42 +01004898 /* If the operation is not supported, just skip and not fail in case the
4899 * decryption involves a common limitation of cryptography hardwares and
4900 * an alternative implementation. */
4901 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004902 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004903 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4904 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004905 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004906
4907 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004908
Gilles Peskine2d277862018-06-18 15:41:12 +02004909 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004910 ASSERT_COMPARE( expected_data->x, expected_data->len,
4911 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004912
Gilles Peskinea1cac842018-06-11 19:33:02 +02004913exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004914 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004915 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004916 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004917}
4918/* END_CASE */
4919
4920/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004921void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4922 int alg_arg,
4923 data_t *nonce,
4924 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004925 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004926 int do_set_lengths,
4927 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004928{
Paul Elliottd3f82412021-06-16 16:52:21 +01004929 size_t ad_part_len = 0;
4930 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004931 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004932
Paul Elliott32f46ba2021-09-23 18:24:36 +01004933 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004934 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004935 mbedtls_test_set_step( ad_part_len );
4936
4937 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004938 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004939 if( ad_part_len & 0x01 )
4940 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4941 else
4942 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004943 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004944
4945 /* Split ad into length(ad_part_len) parts. */
4946 if( !aead_multipart_internal_func( key_type_arg, key_data,
4947 alg_arg, nonce,
4948 additional_data,
4949 ad_part_len,
4950 input_data, -1,
4951 set_lengths_method,
4952 expected_output,
4953 1, 0 ) )
4954 break;
4955
4956 /* length(0) part, length(ad_part_len) part, length(0) part... */
4957 mbedtls_test_set_step( 1000 + ad_part_len );
4958
4959 if( !aead_multipart_internal_func( key_type_arg, key_data,
4960 alg_arg, nonce,
4961 additional_data,
4962 ad_part_len,
4963 input_data, -1,
4964 set_lengths_method,
4965 expected_output,
4966 1, 1 ) )
4967 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004968 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004969
Paul Elliott32f46ba2021-09-23 18:24:36 +01004970 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004972 /* Split data into length(data_part_len) parts. */
4973 mbedtls_test_set_step( 2000 + data_part_len );
4974
4975 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004976 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004977 if( data_part_len & 0x01 )
4978 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4979 else
4980 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004981 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004982
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983 if( !aead_multipart_internal_func( key_type_arg, key_data,
4984 alg_arg, nonce,
4985 additional_data, -1,
4986 input_data, data_part_len,
4987 set_lengths_method,
4988 expected_output,
4989 1, 0 ) )
4990 break;
4991
4992 /* length(0) part, length(data_part_len) part, length(0) part... */
4993 mbedtls_test_set_step( 3000 + data_part_len );
4994
4995 if( !aead_multipart_internal_func( key_type_arg, key_data,
4996 alg_arg, nonce,
4997 additional_data, -1,
4998 input_data, data_part_len,
4999 set_lengths_method,
5000 expected_output,
5001 1, 1 ) )
5002 break;
5003 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005004
Paul Elliott8fc45162021-06-23 16:06:01 +01005005 /* Goto is required to silence warnings about unused labels, as we
5006 * don't actually do any test assertions in this function. */
5007 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005008}
5009/* END_CASE */
5010
5011/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005012void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5013 int alg_arg,
5014 data_t *nonce,
5015 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005016 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005017 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005018 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005019{
Paul Elliottd3f82412021-06-16 16:52:21 +01005020 size_t ad_part_len = 0;
5021 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005022 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005023
Paul Elliott32f46ba2021-09-23 18:24:36 +01005024 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005025 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005026 /* Split ad into length(ad_part_len) parts. */
5027 mbedtls_test_set_step( ad_part_len );
5028
5029 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005030 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031 if( ad_part_len & 0x01 )
5032 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5033 else
5034 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005035 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005036
5037 if( !aead_multipart_internal_func( key_type_arg, key_data,
5038 alg_arg, nonce,
5039 additional_data,
5040 ad_part_len,
5041 input_data, -1,
5042 set_lengths_method,
5043 expected_output,
5044 0, 0 ) )
5045 break;
5046
5047 /* length(0) part, length(ad_part_len) part, length(0) part... */
5048 mbedtls_test_set_step( 1000 + ad_part_len );
5049
5050 if( !aead_multipart_internal_func( key_type_arg, key_data,
5051 alg_arg, nonce,
5052 additional_data,
5053 ad_part_len,
5054 input_data, -1,
5055 set_lengths_method,
5056 expected_output,
5057 0, 1 ) )
5058 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005059 }
5060
Paul Elliott32f46ba2021-09-23 18:24:36 +01005061 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005062 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063 /* Split data into length(data_part_len) parts. */
5064 mbedtls_test_set_step( 2000 + data_part_len );
5065
5066 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005067 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005068 if( data_part_len & 0x01 )
5069 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5070 else
5071 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005072 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005073
5074 if( !aead_multipart_internal_func( key_type_arg, key_data,
5075 alg_arg, nonce,
5076 additional_data, -1,
5077 input_data, data_part_len,
5078 set_lengths_method,
5079 expected_output,
5080 0, 0 ) )
5081 break;
5082
5083 /* length(0) part, length(data_part_len) part, length(0) part... */
5084 mbedtls_test_set_step( 3000 + data_part_len );
5085
5086 if( !aead_multipart_internal_func( key_type_arg, key_data,
5087 alg_arg, nonce,
5088 additional_data, -1,
5089 input_data, data_part_len,
5090 set_lengths_method,
5091 expected_output,
5092 0, 1 ) )
5093 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005094 }
5095
Paul Elliott8fc45162021-06-23 16:06:01 +01005096 /* Goto is required to silence warnings about unused labels, as we
5097 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005098 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005099}
5100/* END_CASE */
5101
5102/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005103void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5104 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005105 int nonce_length,
5106 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005107 data_t *additional_data,
5108 data_t *input_data,
5109 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005110{
5111
5112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5113 psa_key_type_t key_type = key_type_arg;
5114 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005115 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005116 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5118 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005119 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005120 size_t actual_nonce_length = 0;
5121 size_t expected_nonce_length = expected_nonce_length_arg;
5122 unsigned char *output = NULL;
5123 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005124 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005125 size_t ciphertext_size = 0;
5126 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005127 size_t tag_length = 0;
5128 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005129
5130 PSA_ASSERT( psa_crypto_init( ) );
5131
5132 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5133 psa_set_key_algorithm( & attributes, alg );
5134 psa_set_key_type( & attributes, key_type );
5135
5136 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5137 &key ) );
5138
5139 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5140
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005141 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5142
Paul Elliottf1277632021-08-24 18:11:37 +01005143 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005144
Paul Elliottf1277632021-08-24 18:11:37 +01005145 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005146
Gilles Peskine7be11a72022-04-14 00:12:57 +02005147 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005148
Paul Elliottf1277632021-08-24 18:11:37 +01005149 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005151 status = psa_aead_encrypt_setup( &operation, key, alg );
5152
5153 /* If the operation is not supported, just skip and not fail in case the
5154 * encryption involves a common limitation of cryptography hardwares and
5155 * an alternative implementation. */
5156 if( status == PSA_ERROR_NOT_SUPPORTED )
5157 {
5158 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005159 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005160 }
5161
5162 PSA_ASSERT( status );
5163
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005164 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005165 nonce_length,
5166 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005167
Paul Elliott693bf312021-07-23 17:40:41 +01005168 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005169
Paul Elliottf1277632021-08-24 18:11:37 +01005170 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005171
Paul Elliott88ecbe12021-09-22 17:23:03 +01005172 if( expected_status == PSA_SUCCESS )
5173 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5174 alg ) );
5175
Gilles Peskine7be11a72022-04-14 00:12:57 +02005176 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005177
Paul Elliott693bf312021-07-23 17:40:41 +01005178 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005179 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005180 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005181 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5182 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005183
5184 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5185 additional_data->len ) );
5186
5187 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005188 output, output_size,
5189 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005190
Paul Elliottf1277632021-08-24 18:11:37 +01005191 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5192 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005193 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5194 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005195
5196exit:
5197 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005198 mbedtls_free( output );
5199 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005200 psa_aead_abort( &operation );
5201 PSA_DONE( );
5202}
5203/* END_CASE */
5204
5205/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005206void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5207 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005208 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005209 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005210 data_t *additional_data,
5211 data_t *input_data,
5212 int expected_status_arg )
5213{
5214
5215 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5216 psa_key_type_t key_type = key_type_arg;
5217 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005218 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005219 uint8_t *nonce_buffer = NULL;
5220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5221 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5222 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005223 unsigned char *output = NULL;
5224 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005225 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005226 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005227 size_t ciphertext_size = 0;
5228 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005229 size_t tag_length = 0;
5230 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005231 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005232 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005233
5234 PSA_ASSERT( psa_crypto_init( ) );
5235
5236 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5237 psa_set_key_algorithm( &attributes, alg );
5238 psa_set_key_type( &attributes, key_type );
5239
5240 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5241 &key ) );
5242
5243 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5244
5245 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5246
Paul Elliott6f0e7202021-08-25 12:57:18 +01005247 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005248
Paul Elliott6f0e7202021-08-25 12:57:18 +01005249 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005250
Gilles Peskine7be11a72022-04-14 00:12:57 +02005251 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005252
Paul Elliott6f0e7202021-08-25 12:57:18 +01005253 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005254
Paul Elliott863864a2021-07-23 17:28:31 +01005255 status = psa_aead_encrypt_setup( &operation, key, alg );
5256
5257 /* If the operation is not supported, just skip and not fail in case the
5258 * encryption involves a common limitation of cryptography hardwares and
5259 * an alternative implementation. */
5260 if( status == PSA_ERROR_NOT_SUPPORTED )
5261 {
5262 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005263 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005264 }
5265
5266 PSA_ASSERT( status );
5267
Paul Elliott4023ffd2021-09-10 16:21:22 +01005268 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5269 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005270 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005271 /* Arbitrary size buffer, to test zero length valid buffer. */
5272 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005273 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005274 }
5275 else
5276 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005277 /* If length is zero, then this will return NULL. */
5278 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005279 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005280
Paul Elliott4023ffd2021-09-10 16:21:22 +01005281 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005282 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005283 for( index = 0; index < nonce_length - 1; ++index )
5284 {
5285 nonce_buffer[index] = 'a' + index;
5286 }
Paul Elliott66696b52021-08-16 18:42:41 +01005287 }
Paul Elliott863864a2021-07-23 17:28:31 +01005288 }
5289
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005290 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5291 {
5292 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5293 input_data->len ) );
5294 }
5295
Paul Elliott6f0e7202021-08-25 12:57:18 +01005296 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005297
Paul Elliott693bf312021-07-23 17:40:41 +01005298 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005299
5300 if( expected_status == PSA_SUCCESS )
5301 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005302 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5303 {
5304 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5305 input_data->len ) );
5306 }
5307 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5308 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005309
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005310 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5311 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5312 additional_data->len ),
5313 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005314
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005315 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005316 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005317 &ciphertext_length ),
5318 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005319
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005320 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005321 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005322 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5323 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005324 }
5325
5326exit:
5327 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005328 mbedtls_free( output );
5329 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005330 mbedtls_free( nonce_buffer );
5331 psa_aead_abort( &operation );
5332 PSA_DONE( );
5333}
5334/* END_CASE */
5335
5336/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005337void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5338 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005339 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005340 data_t *nonce,
5341 data_t *additional_data,
5342 data_t *input_data,
5343 int expected_status_arg )
5344{
5345
5346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5347 psa_key_type_t key_type = key_type_arg;
5348 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005349 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5351 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5352 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005353 unsigned char *output = NULL;
5354 unsigned char *ciphertext = NULL;
5355 size_t output_size = output_size_arg;
5356 size_t ciphertext_size = 0;
5357 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005358 size_t tag_length = 0;
5359 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5360
5361 PSA_ASSERT( psa_crypto_init( ) );
5362
5363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5364 psa_set_key_algorithm( &attributes, alg );
5365 psa_set_key_type( &attributes, key_type );
5366
5367 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5368 &key ) );
5369
5370 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5371
Paul Elliottc6d11d02021-09-01 12:04:23 +01005372 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005373
Paul Elliottc6d11d02021-09-01 12:04:23 +01005374 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005375
Paul Elliottc6d11d02021-09-01 12:04:23 +01005376 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005377
Paul Elliott43fbda62021-07-23 18:30:59 +01005378 status = psa_aead_encrypt_setup( &operation, key, alg );
5379
5380 /* If the operation is not supported, just skip and not fail in case the
5381 * encryption involves a common limitation of cryptography hardwares and
5382 * an alternative implementation. */
5383 if( status == PSA_ERROR_NOT_SUPPORTED )
5384 {
5385 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5386 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5387 }
5388
5389 PSA_ASSERT( status );
5390
Paul Elliott47b9a142021-10-07 15:04:57 +01005391 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5392 input_data->len ) );
5393
Paul Elliott43fbda62021-07-23 18:30:59 +01005394 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5395
5396 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5397 additional_data->len ) );
5398
5399 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005400 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005401
5402 TEST_EQUAL( status, expected_status );
5403
5404 if( expected_status == PSA_SUCCESS )
5405 {
5406 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005407 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5408 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005409 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5410 }
5411
5412exit:
5413 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005414 mbedtls_free( output );
5415 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005416 psa_aead_abort( &operation );
5417 PSA_DONE( );
5418}
5419/* END_CASE */
5420
Paul Elliott91b021e2021-07-23 18:52:31 +01005421/* BEGIN_CASE */
5422void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5423 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005424 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005425 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005426 data_t *nonce,
5427 data_t *additional_data,
5428 data_t *input_data,
5429 int expected_status_arg )
5430{
5431
5432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5433 psa_key_type_t key_type = key_type_arg;
5434 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005435 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5437 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5438 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005439 unsigned char *ciphertext = NULL;
5440 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005441 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005442 size_t ciphertext_size = 0;
5443 size_t ciphertext_length = 0;
5444 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005445 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005446 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005447
5448 PSA_ASSERT( psa_crypto_init( ) );
5449
5450 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5451 psa_set_key_algorithm( &attributes, alg );
5452 psa_set_key_type( &attributes, key_type );
5453
5454 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5455 &key ) );
5456
5457 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5458
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005459 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005460
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005461 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005462
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005463 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005464
Paul Elliott719c1322021-09-13 18:27:22 +01005465 ASSERT_ALLOC( tag_buffer, tag_size );
5466
Paul Elliott91b021e2021-07-23 18:52:31 +01005467 status = psa_aead_encrypt_setup( &operation, key, alg );
5468
5469 /* If the operation is not supported, just skip and not fail in case the
5470 * encryption involves a common limitation of cryptography hardwares and
5471 * an alternative implementation. */
5472 if( status == PSA_ERROR_NOT_SUPPORTED )
5473 {
5474 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5475 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5476 }
5477
5478 PSA_ASSERT( status );
5479
5480 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5481
Paul Elliott76bda482021-10-07 17:07:23 +01005482 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5483 input_data->len ) );
5484
Paul Elliott91b021e2021-07-23 18:52:31 +01005485 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5486 additional_data->len ) );
5487
5488 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005489 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005490
5491 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005492 status = psa_aead_finish( &operation, finish_ciphertext,
5493 finish_ciphertext_size,
5494 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005495 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005496
5497 TEST_EQUAL( status, expected_status );
5498
5499exit:
5500 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005501 mbedtls_free( ciphertext );
5502 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005503 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005504 psa_aead_abort( &operation );
5505 PSA_DONE( );
5506}
5507/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005508
5509/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005510void aead_multipart_verify( int key_type_arg, data_t *key_data,
5511 int alg_arg,
5512 data_t *nonce,
5513 data_t *additional_data,
5514 data_t *input_data,
5515 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005516 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005517 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005518 int expected_status_arg )
5519{
5520 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5521 psa_key_type_t key_type = key_type_arg;
5522 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005523 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5525 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5526 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005527 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005528 unsigned char *plaintext = NULL;
5529 unsigned char *finish_plaintext = NULL;
5530 size_t plaintext_size = 0;
5531 size_t plaintext_length = 0;
5532 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005533 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005534 unsigned char *tag_buffer = NULL;
5535 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005536
5537 PSA_ASSERT( psa_crypto_init( ) );
5538
5539 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5540 psa_set_key_algorithm( &attributes, alg );
5541 psa_set_key_type( &attributes, key_type );
5542
5543 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5544 &key ) );
5545
5546 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5547
5548 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5549 input_data->len );
5550
5551 ASSERT_ALLOC( plaintext, plaintext_size );
5552
5553 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5554
5555 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5556
Paul Elliott9961a662021-09-17 19:19:02 +01005557 status = psa_aead_decrypt_setup( &operation, key, alg );
5558
5559 /* If the operation is not supported, just skip and not fail in case the
5560 * encryption involves a common limitation of cryptography hardwares and
5561 * an alternative implementation. */
5562 if( status == PSA_ERROR_NOT_SUPPORTED )
5563 {
5564 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5565 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5566 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005567 TEST_EQUAL( status, expected_setup_status );
5568
5569 if( status != PSA_SUCCESS )
5570 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005571
5572 PSA_ASSERT( status );
5573
5574 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5575
Paul Elliottfec6f372021-10-06 17:15:02 +01005576 status = psa_aead_set_lengths( &operation, additional_data->len,
5577 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005578 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005579
Paul Elliott9961a662021-09-17 19:19:02 +01005580 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5581 additional_data->len ) );
5582
5583 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5584 input_data->len,
5585 plaintext, plaintext_size,
5586 &plaintext_length ) );
5587
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005588 if( tag_usage == USE_GIVEN_TAG )
5589 {
5590 tag_buffer = tag->x;
5591 tag_size = tag->len;
5592 }
5593
Paul Elliott9961a662021-09-17 19:19:02 +01005594 status = psa_aead_verify( &operation, finish_plaintext,
5595 verify_plaintext_size,
5596 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005597 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005598
5599 TEST_EQUAL( status, expected_status );
5600
5601exit:
5602 psa_destroy_key( key );
5603 mbedtls_free( plaintext );
5604 mbedtls_free( finish_plaintext );
5605 psa_aead_abort( &operation );
5606 PSA_DONE( );
5607}
5608/* END_CASE */
5609
Paul Elliott9961a662021-09-17 19:19:02 +01005610/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005611void aead_multipart_setup( int key_type_arg, data_t *key_data,
5612 int alg_arg, int expected_status_arg )
5613{
5614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5615 psa_key_type_t key_type = key_type_arg;
5616 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005617 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5619 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5620 psa_status_t expected_status = expected_status_arg;
5621
5622 PSA_ASSERT( psa_crypto_init( ) );
5623
5624 psa_set_key_usage_flags( &attributes,
5625 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5626 psa_set_key_algorithm( &attributes, alg );
5627 psa_set_key_type( &attributes, key_type );
5628
5629 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5630 &key ) );
5631
Paul Elliott5221ef62021-09-19 17:33:03 +01005632 status = psa_aead_encrypt_setup( &operation, key, alg );
5633
5634 TEST_EQUAL( status, expected_status );
5635
5636 psa_aead_abort( &operation );
5637
Paul Elliott5221ef62021-09-19 17:33:03 +01005638 status = psa_aead_decrypt_setup( &operation, key, alg );
5639
5640 TEST_EQUAL(status, expected_status );
5641
5642exit:
5643 psa_destroy_key( key );
5644 psa_aead_abort( &operation );
5645 PSA_DONE( );
5646}
5647/* END_CASE */
5648
5649/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005650void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5651 int alg_arg,
5652 data_t *nonce,
5653 data_t *additional_data,
5654 data_t *input_data )
5655{
5656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5657 psa_key_type_t key_type = key_type_arg;
5658 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005659 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005660 unsigned char *output_data = NULL;
5661 unsigned char *final_data = NULL;
5662 size_t output_size = 0;
5663 size_t finish_output_size = 0;
5664 size_t output_length = 0;
5665 size_t key_bits = 0;
5666 size_t tag_length = 0;
5667 size_t tag_size = 0;
5668 size_t nonce_length = 0;
5669 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5670 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5671 size_t output_part_length = 0;
5672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5673
5674 PSA_ASSERT( psa_crypto_init( ) );
5675
5676 psa_set_key_usage_flags( & attributes,
5677 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5678 psa_set_key_algorithm( & attributes, alg );
5679 psa_set_key_type( & attributes, key_type );
5680
5681 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5682 &key ) );
5683
5684 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5685 key_bits = psa_get_key_bits( &attributes );
5686
5687 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5688
Gilles Peskine7be11a72022-04-14 00:12:57 +02005689 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005690
5691 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5692
5693 ASSERT_ALLOC( output_data, output_size );
5694
5695 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5696
Gilles Peskine7be11a72022-04-14 00:12:57 +02005697 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698
5699 ASSERT_ALLOC( final_data, finish_output_size );
5700
5701 /* Test all operations error without calling setup first. */
5702
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5704 PSA_ERROR_BAD_STATE );
5705
5706 psa_aead_abort( &operation );
5707
Paul Elliottc23a9a02021-06-21 18:32:46 +01005708 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5709 PSA_AEAD_NONCE_MAX_SIZE,
5710 &nonce_length ),
5711 PSA_ERROR_BAD_STATE );
5712
5713 psa_aead_abort( &operation );
5714
Paul Elliott481be342021-07-16 17:38:47 +01005715 /* ------------------------------------------------------- */
5716
Paul Elliottc23a9a02021-06-21 18:32:46 +01005717 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5718 input_data->len ),
5719 PSA_ERROR_BAD_STATE );
5720
5721 psa_aead_abort( &operation );
5722
Paul Elliott481be342021-07-16 17:38:47 +01005723 /* ------------------------------------------------------- */
5724
Paul Elliottc23a9a02021-06-21 18:32:46 +01005725 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5726 additional_data->len ),
5727 PSA_ERROR_BAD_STATE );
5728
5729 psa_aead_abort( &operation );
5730
Paul Elliott481be342021-07-16 17:38:47 +01005731 /* ------------------------------------------------------- */
5732
Paul Elliottc23a9a02021-06-21 18:32:46 +01005733 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5734 input_data->len, output_data,
5735 output_size, &output_length ),
5736 PSA_ERROR_BAD_STATE );
5737
5738 psa_aead_abort( &operation );
5739
Paul Elliott481be342021-07-16 17:38:47 +01005740 /* ------------------------------------------------------- */
5741
Paul Elliottc23a9a02021-06-21 18:32:46 +01005742 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5743 finish_output_size,
5744 &output_part_length,
5745 tag_buffer, tag_length,
5746 &tag_size ),
5747 PSA_ERROR_BAD_STATE );
5748
5749 psa_aead_abort( &operation );
5750
Paul Elliott481be342021-07-16 17:38:47 +01005751 /* ------------------------------------------------------- */
5752
Paul Elliottc23a9a02021-06-21 18:32:46 +01005753 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5754 finish_output_size,
5755 &output_part_length,
5756 tag_buffer,
5757 tag_length ),
5758 PSA_ERROR_BAD_STATE );
5759
5760 psa_aead_abort( &operation );
5761
5762 /* Test for double setups. */
5763
Paul Elliottc23a9a02021-06-21 18:32:46 +01005764 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5765
5766 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5767 PSA_ERROR_BAD_STATE );
5768
5769 psa_aead_abort( &operation );
5770
Paul Elliott481be342021-07-16 17:38:47 +01005771 /* ------------------------------------------------------- */
5772
Paul Elliottc23a9a02021-06-21 18:32:46 +01005773 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5774
5775 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5776 PSA_ERROR_BAD_STATE );
5777
5778 psa_aead_abort( &operation );
5779
Paul Elliott374a2be2021-07-16 17:53:40 +01005780 /* ------------------------------------------------------- */
5781
Paul Elliott374a2be2021-07-16 17:53:40 +01005782 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5783
5784 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5785 PSA_ERROR_BAD_STATE );
5786
5787 psa_aead_abort( &operation );
5788
5789 /* ------------------------------------------------------- */
5790
Paul Elliott374a2be2021-07-16 17:53:40 +01005791 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5792
5793 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5794 PSA_ERROR_BAD_STATE );
5795
5796 psa_aead_abort( &operation );
5797
Paul Elliottc23a9a02021-06-21 18:32:46 +01005798 /* Test for not setting a nonce. */
5799
Paul Elliottc23a9a02021-06-21 18:32:46 +01005800 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5801
5802 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5803 additional_data->len ),
5804 PSA_ERROR_BAD_STATE );
5805
5806 psa_aead_abort( &operation );
5807
Paul Elliott7f628422021-09-01 12:08:29 +01005808 /* ------------------------------------------------------- */
5809
Paul Elliott7f628422021-09-01 12:08:29 +01005810 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5811
5812 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5813 input_data->len, output_data,
5814 output_size, &output_length ),
5815 PSA_ERROR_BAD_STATE );
5816
5817 psa_aead_abort( &operation );
5818
Paul Elliottbdc2c682021-09-21 18:37:10 +01005819 /* ------------------------------------------------------- */
5820
Paul Elliottbdc2c682021-09-21 18:37:10 +01005821 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5822
5823 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5824 finish_output_size,
5825 &output_part_length,
5826 tag_buffer, tag_length,
5827 &tag_size ),
5828 PSA_ERROR_BAD_STATE );
5829
5830 psa_aead_abort( &operation );
5831
5832 /* ------------------------------------------------------- */
5833
Paul Elliottbdc2c682021-09-21 18:37:10 +01005834 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5835
5836 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5837 finish_output_size,
5838 &output_part_length,
5839 tag_buffer,
5840 tag_length ),
5841 PSA_ERROR_BAD_STATE );
5842
5843 psa_aead_abort( &operation );
5844
Paul Elliottc23a9a02021-06-21 18:32:46 +01005845 /* Test for double setting nonce. */
5846
Paul Elliottc23a9a02021-06-21 18:32:46 +01005847 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5848
5849 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5850
5851 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5852 PSA_ERROR_BAD_STATE );
5853
5854 psa_aead_abort( &operation );
5855
Paul Elliott374a2be2021-07-16 17:53:40 +01005856 /* Test for double generating nonce. */
5857
Paul Elliott374a2be2021-07-16 17:53:40 +01005858 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5859
5860 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5861 PSA_AEAD_NONCE_MAX_SIZE,
5862 &nonce_length ) );
5863
5864 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5865 PSA_AEAD_NONCE_MAX_SIZE,
5866 &nonce_length ),
5867 PSA_ERROR_BAD_STATE );
5868
5869
5870 psa_aead_abort( &operation );
5871
5872 /* Test for generate nonce then set and vice versa */
5873
Paul Elliott374a2be2021-07-16 17:53:40 +01005874 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5875
5876 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5877 PSA_AEAD_NONCE_MAX_SIZE,
5878 &nonce_length ) );
5879
5880 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5881 PSA_ERROR_BAD_STATE );
5882
5883 psa_aead_abort( &operation );
5884
Andrzej Kurekad837522021-12-15 15:28:49 +01005885 /* Test for generating nonce after calling set lengths */
5886
5887 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5888
5889 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5890 input_data->len ) );
5891
5892 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5893 PSA_AEAD_NONCE_MAX_SIZE,
5894 &nonce_length ) );
5895
5896 psa_aead_abort( &operation );
5897
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005898 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005899
5900 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5901
5902 if( operation.alg == PSA_ALG_CCM )
5903 {
5904 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5905 input_data->len ),
5906 PSA_ERROR_INVALID_ARGUMENT );
5907 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5908 PSA_AEAD_NONCE_MAX_SIZE,
5909 &nonce_length ),
5910 PSA_ERROR_BAD_STATE );
5911 }
5912 else
5913 {
5914 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5915 input_data->len ) );
5916 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5917 PSA_AEAD_NONCE_MAX_SIZE,
5918 &nonce_length ) );
5919 }
5920
5921 psa_aead_abort( &operation );
5922
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005923 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005924#if SIZE_MAX > UINT32_MAX
5925 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5926
5927 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5928 {
5929 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5930 input_data->len ),
5931 PSA_ERROR_INVALID_ARGUMENT );
5932 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5933 PSA_AEAD_NONCE_MAX_SIZE,
5934 &nonce_length ),
5935 PSA_ERROR_BAD_STATE );
5936 }
5937 else
5938 {
5939 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5940 input_data->len ) );
5941 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5942 PSA_AEAD_NONCE_MAX_SIZE,
5943 &nonce_length ) );
5944 }
5945
5946 psa_aead_abort( &operation );
5947#endif
5948
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005949 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005950
5951 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5952
5953 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5954 PSA_AEAD_NONCE_MAX_SIZE,
5955 &nonce_length ) );
5956
5957 if( operation.alg == PSA_ALG_CCM )
5958 {
5959 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5960 input_data->len ),
5961 PSA_ERROR_INVALID_ARGUMENT );
5962 }
5963 else
5964 {
5965 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5966 input_data->len ) );
5967 }
5968
5969 psa_aead_abort( &operation );
5970
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005971 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005972 /* Test for setting nonce after calling set lengths */
5973
5974 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5975
5976 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5977 input_data->len ) );
5978
5979 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5980
5981 psa_aead_abort( &operation );
5982
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005983 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005984
5985 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5986
5987 if( operation.alg == PSA_ALG_CCM )
5988 {
5989 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5990 input_data->len ),
5991 PSA_ERROR_INVALID_ARGUMENT );
5992 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5993 PSA_ERROR_BAD_STATE );
5994 }
5995 else
5996 {
5997 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5998 input_data->len ) );
5999 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6000 }
6001
6002 psa_aead_abort( &operation );
6003
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006004 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006005#if SIZE_MAX > UINT32_MAX
6006 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6007
6008 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6009 {
6010 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6011 input_data->len ),
6012 PSA_ERROR_INVALID_ARGUMENT );
6013 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6014 PSA_ERROR_BAD_STATE );
6015 }
6016 else
6017 {
6018 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6019 input_data->len ) );
6020 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6021 }
6022
6023 psa_aead_abort( &operation );
6024#endif
6025
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006026 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006027
6028 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6029
6030 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6031
6032 if( operation.alg == PSA_ALG_CCM )
6033 {
6034 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6035 input_data->len ),
6036 PSA_ERROR_INVALID_ARGUMENT );
6037 }
6038 else
6039 {
6040 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6041 input_data->len ) );
6042 }
6043
6044 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006045
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006046 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006047#if SIZE_MAX > UINT32_MAX
6048 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6049
6050 if( operation.alg == PSA_ALG_GCM )
6051 {
6052 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6053 SIZE_MAX ),
6054 PSA_ERROR_INVALID_ARGUMENT );
6055 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6056 PSA_ERROR_BAD_STATE );
6057 }
6058 else if ( operation.alg != PSA_ALG_CCM )
6059 {
6060 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6061 SIZE_MAX ) );
6062 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6063 }
6064
6065 psa_aead_abort( &operation );
6066
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006067 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006068 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6069
6070 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6071
6072 if( operation.alg == PSA_ALG_GCM )
6073 {
6074 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6075 SIZE_MAX ),
6076 PSA_ERROR_INVALID_ARGUMENT );
6077 }
6078 else if ( operation.alg != PSA_ALG_CCM )
6079 {
6080 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6081 SIZE_MAX ) );
6082 }
6083
6084 psa_aead_abort( &operation );
6085#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006086
6087 /* ------------------------------------------------------- */
6088
Paul Elliott374a2be2021-07-16 17:53:40 +01006089 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6090
6091 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6092
6093 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6094 PSA_AEAD_NONCE_MAX_SIZE,
6095 &nonce_length ),
6096 PSA_ERROR_BAD_STATE );
6097
6098 psa_aead_abort( &operation );
6099
Paul Elliott7220cae2021-06-22 17:25:57 +01006100 /* Test for generating nonce in decrypt setup. */
6101
Paul Elliott7220cae2021-06-22 17:25:57 +01006102 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6103
6104 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6105 PSA_AEAD_NONCE_MAX_SIZE,
6106 &nonce_length ),
6107 PSA_ERROR_BAD_STATE );
6108
6109 psa_aead_abort( &operation );
6110
Paul Elliottc23a9a02021-06-21 18:32:46 +01006111 /* Test for setting lengths twice. */
6112
Paul Elliottc23a9a02021-06-21 18:32:46 +01006113 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6114
6115 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6116
6117 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6118 input_data->len ) );
6119
6120 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6121 input_data->len ),
6122 PSA_ERROR_BAD_STATE );
6123
6124 psa_aead_abort( &operation );
6125
Andrzej Kurekad837522021-12-15 15:28:49 +01006126 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006127
Paul Elliottc23a9a02021-06-21 18:32:46 +01006128 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6129
6130 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6131
Andrzej Kurekad837522021-12-15 15:28:49 +01006132 if( operation.alg == PSA_ALG_CCM )
6133 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006134
Andrzej Kurekad837522021-12-15 15:28:49 +01006135 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6136 additional_data->len ),
6137 PSA_ERROR_BAD_STATE );
6138 }
6139 else
6140 {
6141 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6142 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006143
Andrzej Kurekad837522021-12-15 15:28:49 +01006144 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6145 input_data->len ),
6146 PSA_ERROR_BAD_STATE );
6147 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006148 psa_aead_abort( &operation );
6149
6150 /* ------------------------------------------------------- */
6151
Paul Elliottf94bd992021-09-19 18:15:59 +01006152 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6153
6154 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6155
Andrzej Kurekad837522021-12-15 15:28:49 +01006156 if( operation.alg == PSA_ALG_CCM )
6157 {
6158 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6159 input_data->len, output_data,
6160 output_size, &output_length ),
6161 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006162
Andrzej Kurekad837522021-12-15 15:28:49 +01006163 }
6164 else
6165 {
6166 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6167 input_data->len, output_data,
6168 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006169
Andrzej Kurekad837522021-12-15 15:28:49 +01006170 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6171 input_data->len ),
6172 PSA_ERROR_BAD_STATE );
6173 }
6174 psa_aead_abort( &operation );
6175
6176 /* ------------------------------------------------------- */
6177
6178 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6179
6180 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6181
6182 if( operation.alg == PSA_ALG_CCM )
6183 {
6184 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6185 finish_output_size,
6186 &output_part_length,
6187 tag_buffer, tag_length,
6188 &tag_size ) );
6189 }
6190 else
6191 {
6192 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6193 finish_output_size,
6194 &output_part_length,
6195 tag_buffer, tag_length,
6196 &tag_size ) );
6197
6198 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6199 input_data->len ),
6200 PSA_ERROR_BAD_STATE );
6201 }
6202 psa_aead_abort( &operation );
6203
6204 /* Test for setting lengths after generating nonce + already starting data. */
6205
6206 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6207
6208 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6209 PSA_AEAD_NONCE_MAX_SIZE,
6210 &nonce_length ) );
6211 if( operation.alg == PSA_ALG_CCM )
6212 {
6213
6214 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6215 additional_data->len ),
6216 PSA_ERROR_BAD_STATE );
6217 }
6218 else
6219 {
6220 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6221 additional_data->len ) );
6222
6223 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6224 input_data->len ),
6225 PSA_ERROR_BAD_STATE );
6226 }
6227 psa_aead_abort( &operation );
6228
6229 /* ------------------------------------------------------- */
6230
6231 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6232
6233 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6234 PSA_AEAD_NONCE_MAX_SIZE,
6235 &nonce_length ) );
6236 if( operation.alg == PSA_ALG_CCM )
6237 {
6238 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6239 input_data->len, output_data,
6240 output_size, &output_length ),
6241 PSA_ERROR_BAD_STATE );
6242
6243 }
6244 else
6245 {
6246 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6247 input_data->len, output_data,
6248 output_size, &output_length ) );
6249
6250 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6251 input_data->len ),
6252 PSA_ERROR_BAD_STATE );
6253 }
6254 psa_aead_abort( &operation );
6255
6256 /* ------------------------------------------------------- */
6257
6258 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6259
6260 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6261 PSA_AEAD_NONCE_MAX_SIZE,
6262 &nonce_length ) );
6263 if( operation.alg == PSA_ALG_CCM )
6264 {
6265 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6266 finish_output_size,
6267 &output_part_length,
6268 tag_buffer, tag_length,
6269 &tag_size ) );
6270 }
6271 else
6272 {
6273 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6274 finish_output_size,
6275 &output_part_length,
6276 tag_buffer, tag_length,
6277 &tag_size ) );
6278
6279 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6280 input_data->len ),
6281 PSA_ERROR_BAD_STATE );
6282 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006283 psa_aead_abort( &operation );
6284
Paul Elliott243080c2021-07-21 19:01:17 +01006285 /* Test for not sending any additional data or data after setting non zero
6286 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006287
Paul Elliottc23a9a02021-06-21 18:32:46 +01006288 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6289
6290 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6291
6292 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6293 input_data->len ) );
6294
6295 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6296 finish_output_size,
6297 &output_part_length,
6298 tag_buffer, tag_length,
6299 &tag_size ),
6300 PSA_ERROR_INVALID_ARGUMENT );
6301
6302 psa_aead_abort( &operation );
6303
Paul Elliott243080c2021-07-21 19:01:17 +01006304 /* Test for not sending any additional data or data after setting non-zero
6305 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006306
Paul Elliottc23a9a02021-06-21 18:32:46 +01006307 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6308
6309 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6310
6311 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6312 input_data->len ) );
6313
6314 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6315 finish_output_size,
6316 &output_part_length,
6317 tag_buffer,
6318 tag_length ),
6319 PSA_ERROR_INVALID_ARGUMENT );
6320
6321 psa_aead_abort( &operation );
6322
Paul Elliott243080c2021-07-21 19:01:17 +01006323 /* Test for not sending any additional data after setting a non-zero length
6324 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006325
Paul Elliottc23a9a02021-06-21 18:32:46 +01006326 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6327
6328 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6329
6330 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6331 input_data->len ) );
6332
6333 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6334 input_data->len, output_data,
6335 output_size, &output_length ),
6336 PSA_ERROR_INVALID_ARGUMENT );
6337
6338 psa_aead_abort( &operation );
6339
Paul Elliottf94bd992021-09-19 18:15:59 +01006340 /* Test for not sending any data after setting a non-zero length for it.*/
6341
Paul Elliottf94bd992021-09-19 18:15:59 +01006342 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6343
6344 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6345
6346 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6347 input_data->len ) );
6348
6349 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6350 additional_data->len ) );
6351
6352 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6353 finish_output_size,
6354 &output_part_length,
6355 tag_buffer, tag_length,
6356 &tag_size ),
6357 PSA_ERROR_INVALID_ARGUMENT );
6358
6359 psa_aead_abort( &operation );
6360
Paul Elliottb0450fe2021-09-01 15:06:26 +01006361 /* Test for sending too much additional data after setting lengths. */
6362
Paul Elliottb0450fe2021-09-01 15:06:26 +01006363 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6364
6365 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6366
6367 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6368
6369
6370 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6371 additional_data->len ),
6372 PSA_ERROR_INVALID_ARGUMENT );
6373
6374 psa_aead_abort( &operation );
6375
Paul Elliotta2a09b02021-09-22 14:56:40 +01006376 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006377
6378 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6379
6380 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6381
6382 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6383 input_data->len ) );
6384
6385 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6386 additional_data->len ) );
6387
6388 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6389 1 ),
6390 PSA_ERROR_INVALID_ARGUMENT );
6391
6392 psa_aead_abort( &operation );
6393
Paul Elliottb0450fe2021-09-01 15:06:26 +01006394 /* Test for sending too much data after setting lengths. */
6395
Paul Elliottb0450fe2021-09-01 15:06:26 +01006396 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6397
6398 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6399
6400 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6401
6402 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6403 input_data->len, output_data,
6404 output_size, &output_length ),
6405 PSA_ERROR_INVALID_ARGUMENT );
6406
6407 psa_aead_abort( &operation );
6408
Paul Elliotta2a09b02021-09-22 14:56:40 +01006409 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006410
6411 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6412
6413 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6414
6415 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6416 input_data->len ) );
6417
6418 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6419 additional_data->len ) );
6420
6421 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6422 input_data->len, output_data,
6423 output_size, &output_length ) );
6424
6425 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6426 1, output_data,
6427 output_size, &output_length ),
6428 PSA_ERROR_INVALID_ARGUMENT );
6429
6430 psa_aead_abort( &operation );
6431
Paul Elliottc23a9a02021-06-21 18:32:46 +01006432 /* Test sending additional data after data. */
6433
Paul Elliottc23a9a02021-06-21 18:32:46 +01006434 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6435
6436 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6437
Andrzej Kurekad837522021-12-15 15:28:49 +01006438 if( operation.alg != PSA_ALG_CCM )
6439 {
6440 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6441 input_data->len, output_data,
6442 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006443
Andrzej Kurekad837522021-12-15 15:28:49 +01006444 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6445 additional_data->len ),
6446 PSA_ERROR_BAD_STATE );
6447 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006448 psa_aead_abort( &operation );
6449
Paul Elliott534d0b42021-06-22 19:15:20 +01006450 /* Test calling finish on decryption. */
6451
Paul Elliott534d0b42021-06-22 19:15:20 +01006452 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6453
6454 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6455
6456 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6457 finish_output_size,
6458 &output_part_length,
6459 tag_buffer, tag_length,
6460 &tag_size ),
6461 PSA_ERROR_BAD_STATE );
6462
6463 psa_aead_abort( &operation );
6464
6465 /* Test calling verify on encryption. */
6466
Paul Elliott534d0b42021-06-22 19:15:20 +01006467 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6468
6469 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6470
6471 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6472 finish_output_size,
6473 &output_part_length,
6474 tag_buffer,
6475 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006476 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006477
6478 psa_aead_abort( &operation );
6479
6480
Paul Elliottc23a9a02021-06-21 18:32:46 +01006481exit:
6482 psa_destroy_key( key );
6483 psa_aead_abort( &operation );
6484 mbedtls_free( output_data );
6485 mbedtls_free( final_data );
6486 PSA_DONE( );
6487}
6488/* END_CASE */
6489
6490/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006491void signature_size( int type_arg,
6492 int bits,
6493 int alg_arg,
6494 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006495{
6496 psa_key_type_t type = type_arg;
6497 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006498 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006499
Gilles Peskinefe11b722018-12-18 00:24:04 +01006500 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006501
Gilles Peskinee59236f2018-01-27 23:32:46 +01006502exit:
6503 ;
6504}
6505/* END_CASE */
6506
6507/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006508void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6509 int alg_arg, data_t *input_data,
6510 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006511{
Ronald Cron5425a212020-08-04 14:58:35 +02006512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006513 psa_key_type_t key_type = key_type_arg;
6514 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006515 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006516 unsigned char *signature = NULL;
6517 size_t signature_size;
6518 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006520
Gilles Peskine8817f612018-12-18 00:18:46 +01006521 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006522
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006524 psa_set_key_algorithm( &attributes, alg );
6525 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006526
Gilles Peskine049c7532019-05-15 20:22:09 +02006527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006528 &key ) );
6529 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006530 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006531
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006532 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006533 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006534 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006535 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006536 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006537 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006538 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006539
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006540 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006541 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006542 input_data->x, input_data->len,
6543 signature, signature_size,
6544 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006545 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006546 ASSERT_COMPARE( output_data->x, output_data->len,
6547 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006548
6549exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006550 /*
6551 * Key attributes may have been returned by psa_get_key_attributes()
6552 * thus reset them as required.
6553 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006554 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006555
Ronald Cron5425a212020-08-04 14:58:35 +02006556 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006557 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006558 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006559}
6560/* END_CASE */
6561
6562/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006563void sign_hash_fail( int key_type_arg, data_t *key_data,
6564 int alg_arg, data_t *input_data,
6565 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006566{
Ronald Cron5425a212020-08-04 14:58:35 +02006567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006568 psa_key_type_t key_type = key_type_arg;
6569 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006570 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006571 psa_status_t actual_status;
6572 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006573 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006574 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006576
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006577 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006578
Gilles Peskine8817f612018-12-18 00:18:46 +01006579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006580
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006581 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006582 psa_set_key_algorithm( &attributes, alg );
6583 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006584
Gilles Peskine049c7532019-05-15 20:22:09 +02006585 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006586 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006587
Ronald Cron5425a212020-08-04 14:58:35 +02006588 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006589 input_data->x, input_data->len,
6590 signature, signature_size,
6591 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006592 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006593 /* The value of *signature_length is unspecified on error, but
6594 * whatever it is, it should be less than signature_size, so that
6595 * if the caller tries to read *signature_length bytes without
6596 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006597 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006598
6599exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006600 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006601 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006602 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006603 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006604}
6605/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006606
6607/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006608void sign_verify_hash( int key_type_arg, data_t *key_data,
6609 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006610{
Ronald Cron5425a212020-08-04 14:58:35 +02006611 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006612 psa_key_type_t key_type = key_type_arg;
6613 psa_algorithm_t alg = alg_arg;
6614 size_t key_bits;
6615 unsigned char *signature = NULL;
6616 size_t signature_size;
6617 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006619
Gilles Peskine8817f612018-12-18 00:18:46 +01006620 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006621
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006622 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006623 psa_set_key_algorithm( &attributes, alg );
6624 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006625
Gilles Peskine049c7532019-05-15 20:22:09 +02006626 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006627 &key ) );
6628 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006629 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006630
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006631 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006632 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006633 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006634 key_bits, alg );
6635 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006636 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006637 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006638
6639 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006640 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006641 input_data->x, input_data->len,
6642 signature, signature_size,
6643 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006644 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006645 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006646 TEST_ASSERT( signature_length > 0 );
6647
6648 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006649 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006650 input_data->x, input_data->len,
6651 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006652
6653 if( input_data->len != 0 )
6654 {
6655 /* Flip a bit in the input and verify that the signature is now
6656 * detected as invalid. Flip a bit at the beginning, not at the end,
6657 * because ECDSA may ignore the last few bits of the input. */
6658 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006659 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006660 input_data->x, input_data->len,
6661 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006662 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006663 }
6664
6665exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006666 /*
6667 * Key attributes may have been returned by psa_get_key_attributes()
6668 * thus reset them as required.
6669 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006670 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006671
Ronald Cron5425a212020-08-04 14:58:35 +02006672 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006673 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006674 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006675}
6676/* END_CASE */
6677
6678/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006679void verify_hash( int key_type_arg, data_t *key_data,
6680 int alg_arg, data_t *hash_data,
6681 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006682{
Ronald Cron5425a212020-08-04 14:58:35 +02006683 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006684 psa_key_type_t key_type = key_type_arg;
6685 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006686 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006687
Gilles Peskine7be11a72022-04-14 00:12:57 +02006688 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006689
Gilles Peskine8817f612018-12-18 00:18:46 +01006690 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006691
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006692 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006693 psa_set_key_algorithm( &attributes, alg );
6694 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006695
Gilles Peskine049c7532019-05-15 20:22:09 +02006696 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006697 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006698
Ronald Cron5425a212020-08-04 14:58:35 +02006699 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006700 hash_data->x, hash_data->len,
6701 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006702
itayzafrir5c753392018-05-08 11:18:38 +03006703exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006704 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006705 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006706 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006707}
6708/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006709
6710/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006711void verify_hash_fail( int key_type_arg, data_t *key_data,
6712 int alg_arg, data_t *hash_data,
6713 data_t *signature_data,
6714 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006715{
Ronald Cron5425a212020-08-04 14:58:35 +02006716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006717 psa_key_type_t key_type = key_type_arg;
6718 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006719 psa_status_t actual_status;
6720 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006722
Gilles Peskine8817f612018-12-18 00:18:46 +01006723 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006724
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006725 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006726 psa_set_key_algorithm( &attributes, alg );
6727 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006728
Gilles Peskine049c7532019-05-15 20:22:09 +02006729 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006730 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006731
Ronald Cron5425a212020-08-04 14:58:35 +02006732 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006733 hash_data->x, hash_data->len,
6734 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006735 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006736
6737exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006738 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006739 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006740 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006741}
6742/* END_CASE */
6743
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006744/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006745void sign_message_deterministic( int key_type_arg,
6746 data_t *key_data,
6747 int alg_arg,
6748 data_t *input_data,
6749 data_t *output_data )
6750{
6751 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6752 psa_key_type_t key_type = key_type_arg;
6753 psa_algorithm_t alg = alg_arg;
6754 size_t key_bits;
6755 unsigned char *signature = NULL;
6756 size_t signature_size;
6757 size_t signature_length = 0xdeadbeef;
6758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6759
6760 PSA_ASSERT( psa_crypto_init( ) );
6761
6762 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6763 psa_set_key_algorithm( &attributes, alg );
6764 psa_set_key_type( &attributes, key_type );
6765
6766 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6767 &key ) );
6768 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6769 key_bits = psa_get_key_bits( &attributes );
6770
6771 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6772 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006773 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006774 ASSERT_ALLOC( signature, signature_size );
6775
6776 PSA_ASSERT( psa_sign_message( key, alg,
6777 input_data->x, input_data->len,
6778 signature, signature_size,
6779 &signature_length ) );
6780
6781 ASSERT_COMPARE( output_data->x, output_data->len,
6782 signature, signature_length );
6783
6784exit:
6785 psa_reset_key_attributes( &attributes );
6786
6787 psa_destroy_key( key );
6788 mbedtls_free( signature );
6789 PSA_DONE( );
6790
6791}
6792/* END_CASE */
6793
6794/* BEGIN_CASE */
6795void sign_message_fail( int key_type_arg,
6796 data_t *key_data,
6797 int alg_arg,
6798 data_t *input_data,
6799 int signature_size_arg,
6800 int expected_status_arg )
6801{
6802 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6803 psa_key_type_t key_type = key_type_arg;
6804 psa_algorithm_t alg = alg_arg;
6805 size_t signature_size = signature_size_arg;
6806 psa_status_t actual_status;
6807 psa_status_t expected_status = expected_status_arg;
6808 unsigned char *signature = NULL;
6809 size_t signature_length = 0xdeadbeef;
6810 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6811
6812 ASSERT_ALLOC( signature, signature_size );
6813
6814 PSA_ASSERT( psa_crypto_init( ) );
6815
6816 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6817 psa_set_key_algorithm( &attributes, alg );
6818 psa_set_key_type( &attributes, key_type );
6819
6820 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6821 &key ) );
6822
6823 actual_status = psa_sign_message( key, alg,
6824 input_data->x, input_data->len,
6825 signature, signature_size,
6826 &signature_length );
6827 TEST_EQUAL( actual_status, expected_status );
6828 /* The value of *signature_length is unspecified on error, but
6829 * whatever it is, it should be less than signature_size, so that
6830 * if the caller tries to read *signature_length bytes without
6831 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006832 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006833
6834exit:
6835 psa_reset_key_attributes( &attributes );
6836 psa_destroy_key( key );
6837 mbedtls_free( signature );
6838 PSA_DONE( );
6839}
6840/* END_CASE */
6841
6842/* BEGIN_CASE */
6843void sign_verify_message( int key_type_arg,
6844 data_t *key_data,
6845 int alg_arg,
6846 data_t *input_data )
6847{
6848 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6849 psa_key_type_t key_type = key_type_arg;
6850 psa_algorithm_t alg = alg_arg;
6851 size_t key_bits;
6852 unsigned char *signature = NULL;
6853 size_t signature_size;
6854 size_t signature_length = 0xdeadbeef;
6855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6856
6857 PSA_ASSERT( psa_crypto_init( ) );
6858
6859 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6860 PSA_KEY_USAGE_VERIFY_MESSAGE );
6861 psa_set_key_algorithm( &attributes, alg );
6862 psa_set_key_type( &attributes, key_type );
6863
6864 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6865 &key ) );
6866 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6867 key_bits = psa_get_key_bits( &attributes );
6868
6869 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6870 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006871 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006872 ASSERT_ALLOC( signature, signature_size );
6873
6874 PSA_ASSERT( psa_sign_message( key, alg,
6875 input_data->x, input_data->len,
6876 signature, signature_size,
6877 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006878 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006879 TEST_ASSERT( signature_length > 0 );
6880
6881 PSA_ASSERT( psa_verify_message( key, alg,
6882 input_data->x, input_data->len,
6883 signature, signature_length ) );
6884
6885 if( input_data->len != 0 )
6886 {
6887 /* Flip a bit in the input and verify that the signature is now
6888 * detected as invalid. Flip a bit at the beginning, not at the end,
6889 * because ECDSA may ignore the last few bits of the input. */
6890 input_data->x[0] ^= 1;
6891 TEST_EQUAL( psa_verify_message( key, alg,
6892 input_data->x, input_data->len,
6893 signature, signature_length ),
6894 PSA_ERROR_INVALID_SIGNATURE );
6895 }
6896
6897exit:
6898 psa_reset_key_attributes( &attributes );
6899
6900 psa_destroy_key( key );
6901 mbedtls_free( signature );
6902 PSA_DONE( );
6903}
6904/* END_CASE */
6905
6906/* BEGIN_CASE */
6907void verify_message( int key_type_arg,
6908 data_t *key_data,
6909 int alg_arg,
6910 data_t *input_data,
6911 data_t *signature_data )
6912{
6913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6914 psa_key_type_t key_type = key_type_arg;
6915 psa_algorithm_t alg = alg_arg;
6916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6917
Gilles Peskine7be11a72022-04-14 00:12:57 +02006918 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006919
6920 PSA_ASSERT( psa_crypto_init( ) );
6921
6922 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6923 psa_set_key_algorithm( &attributes, alg );
6924 psa_set_key_type( &attributes, key_type );
6925
6926 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6927 &key ) );
6928
6929 PSA_ASSERT( psa_verify_message( key, alg,
6930 input_data->x, input_data->len,
6931 signature_data->x, signature_data->len ) );
6932
6933exit:
6934 psa_reset_key_attributes( &attributes );
6935 psa_destroy_key( key );
6936 PSA_DONE( );
6937}
6938/* END_CASE */
6939
6940/* BEGIN_CASE */
6941void verify_message_fail( int key_type_arg,
6942 data_t *key_data,
6943 int alg_arg,
6944 data_t *hash_data,
6945 data_t *signature_data,
6946 int expected_status_arg )
6947{
6948 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6949 psa_key_type_t key_type = key_type_arg;
6950 psa_algorithm_t alg = alg_arg;
6951 psa_status_t actual_status;
6952 psa_status_t expected_status = expected_status_arg;
6953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6954
6955 PSA_ASSERT( psa_crypto_init( ) );
6956
6957 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6958 psa_set_key_algorithm( &attributes, alg );
6959 psa_set_key_type( &attributes, key_type );
6960
6961 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6962 &key ) );
6963
6964 actual_status = psa_verify_message( key, alg,
6965 hash_data->x, hash_data->len,
6966 signature_data->x,
6967 signature_data->len );
6968 TEST_EQUAL( actual_status, expected_status );
6969
6970exit:
6971 psa_reset_key_attributes( &attributes );
6972 psa_destroy_key( key );
6973 PSA_DONE( );
6974}
6975/* END_CASE */
6976
6977/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006978void asymmetric_encrypt( int key_type_arg,
6979 data_t *key_data,
6980 int alg_arg,
6981 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006982 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006983 int expected_output_length_arg,
6984 int expected_status_arg )
6985{
Ronald Cron5425a212020-08-04 14:58:35 +02006986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006987 psa_key_type_t key_type = key_type_arg;
6988 psa_algorithm_t alg = alg_arg;
6989 size_t expected_output_length = expected_output_length_arg;
6990 size_t key_bits;
6991 unsigned char *output = NULL;
6992 size_t output_size;
6993 size_t output_length = ~0;
6994 psa_status_t actual_status;
6995 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006997
Gilles Peskine8817f612018-12-18 00:18:46 +01006998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006999
Gilles Peskine656896e2018-06-29 19:12:28 +02007000 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007001 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7002 psa_set_key_algorithm( &attributes, alg );
7003 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007004 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007005 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007006
7007 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007008 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007009 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007010
Gilles Peskine656896e2018-06-29 19:12:28 +02007011 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007012 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007013 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007014
7015 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007016 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007017 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007018 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007019 output, output_size,
7020 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007021 TEST_EQUAL( actual_status, expected_status );
7022 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007023
Gilles Peskine68428122018-06-30 18:42:41 +02007024 /* If the label is empty, the test framework puts a non-null pointer
7025 * in label->x. Test that a null pointer works as well. */
7026 if( label->len == 0 )
7027 {
7028 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007029 if( output_size != 0 )
7030 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007031 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007032 input_data->x, input_data->len,
7033 NULL, label->len,
7034 output, output_size,
7035 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007036 TEST_EQUAL( actual_status, expected_status );
7037 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007038 }
7039
Gilles Peskine656896e2018-06-29 19:12:28 +02007040exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007041 /*
7042 * Key attributes may have been returned by psa_get_key_attributes()
7043 * thus reset them as required.
7044 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007045 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007046
Ronald Cron5425a212020-08-04 14:58:35 +02007047 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007048 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007049 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007050}
7051/* END_CASE */
7052
7053/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007054void asymmetric_encrypt_decrypt( int key_type_arg,
7055 data_t *key_data,
7056 int alg_arg,
7057 data_t *input_data,
7058 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007059{
Ronald Cron5425a212020-08-04 14:58:35 +02007060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007061 psa_key_type_t key_type = key_type_arg;
7062 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007063 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007064 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007065 size_t output_size;
7066 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007067 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007068 size_t output2_size;
7069 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007070 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007071
Gilles Peskine8817f612018-12-18 00:18:46 +01007072 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007074 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7075 psa_set_key_algorithm( &attributes, alg );
7076 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007077
Gilles Peskine049c7532019-05-15 20:22:09 +02007078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007079 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007080
7081 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007082 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007083 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007084
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007085 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007086 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007087 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007088
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007089 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007090 TEST_LE_U( output2_size,
7091 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7092 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007093 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007094
Gilles Peskineeebd7382018-06-08 18:11:54 +02007095 /* We test encryption by checking that encrypt-then-decrypt gives back
7096 * the original plaintext because of the non-optional random
7097 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007098 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007099 input_data->x, input_data->len,
7100 label->x, label->len,
7101 output, output_size,
7102 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007103 /* We don't know what ciphertext length to expect, but check that
7104 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007105 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007106
Ronald Cron5425a212020-08-04 14:58:35 +02007107 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007108 output, output_length,
7109 label->x, label->len,
7110 output2, output2_size,
7111 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007112 ASSERT_COMPARE( input_data->x, input_data->len,
7113 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007114
7115exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007116 /*
7117 * Key attributes may have been returned by psa_get_key_attributes()
7118 * thus reset them as required.
7119 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007120 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007121
Ronald Cron5425a212020-08-04 14:58:35 +02007122 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007123 mbedtls_free( output );
7124 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007125 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007126}
7127/* END_CASE */
7128
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007129/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007130void asymmetric_decrypt( int key_type_arg,
7131 data_t *key_data,
7132 int alg_arg,
7133 data_t *input_data,
7134 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007135 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007136{
Ronald Cron5425a212020-08-04 14:58:35 +02007137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007138 psa_key_type_t key_type = key_type_arg;
7139 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007140 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007141 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007142 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007143 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007145
Gilles Peskine8817f612018-12-18 00:18:46 +01007146 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007147
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007148 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7149 psa_set_key_algorithm( &attributes, alg );
7150 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007151
Gilles Peskine049c7532019-05-15 20:22:09 +02007152 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007153 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007154
gabor-mezei-armceface22021-01-21 12:26:17 +01007155 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7156 key_bits = psa_get_key_bits( &attributes );
7157
7158 /* Determine the maximum ciphertext length */
7159 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007160 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007161 ASSERT_ALLOC( output, output_size );
7162
Ronald Cron5425a212020-08-04 14:58:35 +02007163 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007164 input_data->x, input_data->len,
7165 label->x, label->len,
7166 output,
7167 output_size,
7168 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007169 ASSERT_COMPARE( expected_data->x, expected_data->len,
7170 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007171
Gilles Peskine68428122018-06-30 18:42:41 +02007172 /* If the label is empty, the test framework puts a non-null pointer
7173 * in label->x. Test that a null pointer works as well. */
7174 if( label->len == 0 )
7175 {
7176 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007177 if( output_size != 0 )
7178 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007179 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007180 input_data->x, input_data->len,
7181 NULL, label->len,
7182 output,
7183 output_size,
7184 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007185 ASSERT_COMPARE( expected_data->x, expected_data->len,
7186 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007187 }
7188
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007189exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007190 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007191 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007192 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007193 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007194}
7195/* END_CASE */
7196
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007197/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007198void asymmetric_decrypt_fail( int key_type_arg,
7199 data_t *key_data,
7200 int alg_arg,
7201 data_t *input_data,
7202 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007203 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007204 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007205{
Ronald Cron5425a212020-08-04 14:58:35 +02007206 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007207 psa_key_type_t key_type = key_type_arg;
7208 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007209 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007210 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007211 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007212 psa_status_t actual_status;
7213 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007215
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007216 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007217
Gilles Peskine8817f612018-12-18 00:18:46 +01007218 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007219
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7221 psa_set_key_algorithm( &attributes, alg );
7222 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007223
Gilles Peskine049c7532019-05-15 20:22:09 +02007224 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007225 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007226
Ronald Cron5425a212020-08-04 14:58:35 +02007227 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007228 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007229 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007230 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007231 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007232 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007233 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007234
Gilles Peskine68428122018-06-30 18:42:41 +02007235 /* If the label is empty, the test framework puts a non-null pointer
7236 * in label->x. Test that a null pointer works as well. */
7237 if( label->len == 0 )
7238 {
7239 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007240 if( output_size != 0 )
7241 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007242 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007243 input_data->x, input_data->len,
7244 NULL, label->len,
7245 output, output_size,
7246 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007247 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007248 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007249 }
7250
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007251exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007252 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007253 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007254 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007255 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007256}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007257/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007258
7259/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007260void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007261{
7262 /* Test each valid way of initializing the object, except for `= {0}`, as
7263 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7264 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007265 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007266 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007267 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7268 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7269 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007270
7271 memset( &zero, 0, sizeof( zero ) );
7272
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007273 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007274 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007275 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007276 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007277 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007278 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007279 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007280
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007281 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007282 PSA_ASSERT( psa_key_derivation_abort(&func) );
7283 PSA_ASSERT( psa_key_derivation_abort(&init) );
7284 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007285}
7286/* END_CASE */
7287
Janos Follath16de4a42019-06-13 16:32:24 +01007288/* BEGIN_CASE */
7289void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007290{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007291 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007292 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007293 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007294
Gilles Peskine8817f612018-12-18 00:18:46 +01007295 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007296
Janos Follath16de4a42019-06-13 16:32:24 +01007297 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007298 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007299
7300exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007301 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007302 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007303}
7304/* END_CASE */
7305
Janos Follathaf3c2a02019-06-12 12:34:34 +01007306/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007307void derive_set_capacity( int alg_arg, int capacity_arg,
7308 int expected_status_arg )
7309{
7310 psa_algorithm_t alg = alg_arg;
7311 size_t capacity = capacity_arg;
7312 psa_status_t expected_status = expected_status_arg;
7313 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7314
7315 PSA_ASSERT( psa_crypto_init( ) );
7316
7317 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7318
7319 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7320 expected_status );
7321
7322exit:
7323 psa_key_derivation_abort( &operation );
7324 PSA_DONE( );
7325}
7326/* END_CASE */
7327
7328/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007329void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007330 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007331 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007332 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007333 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007334 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007335 int expected_status_arg3,
7336 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007337{
7338 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007339 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7340 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007341 psa_status_t expected_statuses[] = {expected_status_arg1,
7342 expected_status_arg2,
7343 expected_status_arg3};
7344 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007345 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7346 MBEDTLS_SVC_KEY_ID_INIT,
7347 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007348 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7350 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007351 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007352 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007353 psa_status_t expected_output_status = expected_output_status_arg;
7354 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007355
7356 PSA_ASSERT( psa_crypto_init( ) );
7357
7358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7359 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007360
7361 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7362
7363 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7364 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007365 mbedtls_test_set_step( i );
7366 if( steps[i] == 0 )
7367 {
7368 /* Skip this step */
7369 }
7370 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007371 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007372 psa_set_key_type( &attributes, key_types[i] );
7373 PSA_ASSERT( psa_import_key( &attributes,
7374 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007375 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007376 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7377 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7378 {
7379 // When taking a private key as secret input, use key agreement
7380 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007381 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7382 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007383 expected_statuses[i] );
7384 }
7385 else
7386 {
7387 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007388 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007389 expected_statuses[i] );
7390 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007391 }
7392 else
7393 {
7394 TEST_EQUAL( psa_key_derivation_input_bytes(
7395 &operation, steps[i],
7396 inputs[i]->x, inputs[i]->len ),
7397 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007398 }
7399 }
7400
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007401 if( output_key_type != PSA_KEY_TYPE_NONE )
7402 {
7403 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007404 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007405 psa_set_key_bits( &attributes, 8 );
7406 actual_output_status =
7407 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007408 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007409 }
7410 else
7411 {
7412 uint8_t buffer[1];
7413 actual_output_status =
7414 psa_key_derivation_output_bytes( &operation,
7415 buffer, sizeof( buffer ) );
7416 }
7417 TEST_EQUAL( actual_output_status, expected_output_status );
7418
Janos Follathaf3c2a02019-06-12 12:34:34 +01007419exit:
7420 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007421 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7422 psa_destroy_key( keys[i] );
7423 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007424 PSA_DONE( );
7425}
7426/* END_CASE */
7427
Janos Follathd958bb72019-07-03 15:02:16 +01007428/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007429void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007430{
Janos Follathd958bb72019-07-03 15:02:16 +01007431 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007433 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007434 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007435 unsigned char input1[] = "Input 1";
7436 size_t input1_length = sizeof( input1 );
7437 unsigned char input2[] = "Input 2";
7438 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007439 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007440 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007441 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7442 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7443 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007445
Gilles Peskine8817f612018-12-18 00:18:46 +01007446 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007447
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007448 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7449 psa_set_key_algorithm( &attributes, alg );
7450 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007451
Gilles Peskine73676cb2019-05-15 20:15:10 +02007452 PSA_ASSERT( psa_import_key( &attributes,
7453 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007454 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007455
7456 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007457 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7458 input1, input1_length,
7459 input2, input2_length,
7460 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007461 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007462
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007463 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007464 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007465 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007466
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007467 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007468
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007469 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007470 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007471
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007472exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007473 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007474 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007475 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007476}
7477/* END_CASE */
7478
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007479/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007480void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007481{
7482 uint8_t output_buffer[16];
7483 size_t buffer_size = 16;
7484 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007485 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007486
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007487 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7488 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007489 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007490
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007491 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007492 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007493
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007494 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007495
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007496 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7497 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007498 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007499
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007500 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007501 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007502
7503exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007504 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007505}
7506/* END_CASE */
7507
7508/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007509void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007510 int step1_arg, data_t *input1, int expected_status_arg1,
7511 int step2_arg, data_t *input2, int expected_status_arg2,
7512 int step3_arg, data_t *input3, int expected_status_arg3,
7513 int step4_arg, data_t *input4, int expected_status_arg4,
7514 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007515 int requested_capacity_arg,
7516 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007517 data_t *expected_output2,
7518 int other_key_input_type,
7519 int key_input_type,
7520 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007521{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007522 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007523 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7524 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007525 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7526 MBEDTLS_SVC_KEY_ID_INIT,
7527 MBEDTLS_SVC_KEY_ID_INIT,
7528 MBEDTLS_SVC_KEY_ID_INIT};
7529 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7530 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007531 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007532 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007533 uint8_t *expected_outputs[2] =
7534 {expected_output1->x, expected_output2->x};
7535 size_t output_sizes[2] =
7536 {expected_output1->len, expected_output2->len};
7537 size_t output_buffer_size = 0;
7538 uint8_t *output_buffer = NULL;
7539 size_t expected_capacity;
7540 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007541 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7542 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7543 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7544 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007545 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007546 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007547 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007548
7549 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7550 {
7551 if( output_sizes[i] > output_buffer_size )
7552 output_buffer_size = output_sizes[i];
7553 if( output_sizes[i] == 0 )
7554 expected_outputs[i] = NULL;
7555 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007556 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007558
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007559 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007560 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7561 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7562 requested_capacity ) );
7563 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007564 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007565 switch( steps[i] )
7566 {
7567 case 0:
7568 break;
7569 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007570 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007571 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007572 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007573 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007574 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007575 inputs[i]->x, inputs[i]->len ),
7576 statuses[i] );
7577
7578 if( statuses[i] != PSA_SUCCESS )
7579 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007580 break;
7581 case 1: // input key
7582 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7583 psa_set_key_algorithm( &attributes1, alg );
7584 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7585
7586 PSA_ASSERT( psa_import_key( &attributes1,
7587 inputs[i]->x, inputs[i]->len,
7588 &keys[i] ) );
7589
Przemek Stekiel38647de2022-04-19 13:27:47 +02007590 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007591 {
7592 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007593 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7594 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007595 }
7596
Przemek Stekiel38647de2022-04-19 13:27:47 +02007597 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7598 steps[i],
7599 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007600 break;
7601 default:
7602 TEST_ASSERT( ! "default case not supported" );
7603 break;
7604 }
7605 break;
7606 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007607 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007608 {
7609 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007610 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7611 steps[i],
7612 inputs[i]->x,
7613 inputs[i]->len ),
7614 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007615 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007616 case 1: // input key, type DERIVE
7617 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007618 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7619 psa_set_key_algorithm( &attributes2, alg );
7620 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7621
7622 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007623 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007624 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7625
7626 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007627 inputs[i]->x, inputs[i]->len,
7628 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007629
Przemek Stekiel38647de2022-04-19 13:27:47 +02007630 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7631 steps[i],
7632 keys[i] ),
7633 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007634 break;
7635 case 2: // key agreement
7636 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7637 psa_set_key_algorithm( &attributes3, alg );
7638 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7639
7640 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007641 inputs[i]->x, inputs[i]->len,
7642 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007643
7644 TEST_EQUAL( psa_key_derivation_key_agreement(
7645 &operation,
7646 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7647 keys[i], key_agreement_peer_key->x,
7648 key_agreement_peer_key->len ), statuses[i] );
7649 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007650 default:
7651 TEST_ASSERT( ! "default case not supported" );
7652 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007653 }
7654
Przemek Stekiel38647de2022-04-19 13:27:47 +02007655 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007656 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007657 break;
7658 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007659 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007660 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007661 inputs[i]->x, inputs[i]->len ), statuses[i] );
7662
7663 if( statuses[i] != PSA_SUCCESS )
7664 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007665 break;
7666 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007667 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007668
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007669 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007670 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007671 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007672 expected_capacity = requested_capacity;
7673
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007674 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007675 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007676 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7677
7678 /* For output key derivation secret must be provided using
7679 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007680 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007681 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007682
7683 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7684 psa_set_key_algorithm( &attributes4, alg );
7685 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007686 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007687
7688 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007689 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007690 }
7691 else // output bytes
7692 {
7693 /* Expansion phase. */
7694 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007695 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007696 /* Read some bytes. */
7697 status = psa_key_derivation_output_bytes( &operation,
7698 output_buffer, output_sizes[i] );
7699 if( expected_capacity == 0 && output_sizes[i] == 0 )
7700 {
7701 /* Reading 0 bytes when 0 bytes are available can go either way. */
7702 TEST_ASSERT( status == PSA_SUCCESS ||
7703 status == PSA_ERROR_INSUFFICIENT_DATA );
7704 continue;
7705 }
7706 else if( expected_capacity == 0 ||
7707 output_sizes[i] > expected_capacity )
7708 {
7709 /* Capacity exceeded. */
7710 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7711 expected_capacity = 0;
7712 continue;
7713 }
7714 /* Success. Check the read data. */
7715 PSA_ASSERT( status );
7716 if( output_sizes[i] != 0 )
7717 ASSERT_COMPARE( output_buffer, output_sizes[i],
7718 expected_outputs[i], output_sizes[i] );
7719 /* Check the operation status. */
7720 expected_capacity -= output_sizes[i];
7721 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7722 &current_capacity ) );
7723 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007724 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007725 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007726 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007727
7728exit:
7729 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007730 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007731 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7732 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007733 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007734 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007735}
7736/* END_CASE */
7737
7738/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007739void derive_full( int alg_arg,
7740 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007741 data_t *input1,
7742 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007743 int requested_capacity_arg )
7744{
Ronald Cron5425a212020-08-04 14:58:35 +02007745 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007746 psa_algorithm_t alg = alg_arg;
7747 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007748 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007749 unsigned char output_buffer[16];
7750 size_t expected_capacity = requested_capacity;
7751 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007752 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007753
Gilles Peskine8817f612018-12-18 00:18:46 +01007754 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007755
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007756 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7757 psa_set_key_algorithm( &attributes, alg );
7758 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007759
Gilles Peskine049c7532019-05-15 20:22:09 +02007760 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007761 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007762
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007763 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7764 input1->x, input1->len,
7765 input2->x, input2->len,
7766 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007767 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007768
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007769 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007770 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007771 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007772
7773 /* Expansion phase. */
7774 while( current_capacity > 0 )
7775 {
7776 size_t read_size = sizeof( output_buffer );
7777 if( read_size > current_capacity )
7778 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007779 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007780 output_buffer,
7781 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007782 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007783 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007784 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007785 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007786 }
7787
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007788 /* Check that the operation refuses to go over capacity. */
7789 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007790 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007791
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007792 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007793
7794exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007795 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007796 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007797 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007798}
7799/* END_CASE */
7800
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007801/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007802void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007803 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007804 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007805 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007806 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007807{
7808 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7809 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007810 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007811 uint8_t *output_buffer = NULL;
7812 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007813 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7814 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7815 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007816
7817 ASSERT_ALLOC( output_buffer, expected_output->len );
7818 PSA_ASSERT( psa_crypto_init() );
7819
7820 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007821 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007822 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007823
7824 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007825 step, input->x, input->len ),
7826 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007827
7828 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7829 goto exit;
7830
7831 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7832 expected_output->len );
7833
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007834 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007835 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7836 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7837 expected_output->len );
7838
7839exit:
7840 mbedtls_free( output_buffer );
7841 psa_key_derivation_abort( &operation );
7842 PSA_DONE();
7843}
7844/* END_CASE */
7845
Janos Follathe60c9052019-07-03 13:51:30 +01007846/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007847void derive_key_exercise( int alg_arg,
7848 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007849 data_t *input1,
7850 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007851 int derived_type_arg,
7852 int derived_bits_arg,
7853 int derived_usage_arg,
7854 int derived_alg_arg )
7855{
Ronald Cron5425a212020-08-04 14:58:35 +02007856 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7857 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007858 psa_algorithm_t alg = alg_arg;
7859 psa_key_type_t derived_type = derived_type_arg;
7860 size_t derived_bits = derived_bits_arg;
7861 psa_key_usage_t derived_usage = derived_usage_arg;
7862 psa_algorithm_t derived_alg = derived_alg_arg;
7863 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007864 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007866 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007867
Gilles Peskine8817f612018-12-18 00:18:46 +01007868 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007869
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007870 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7871 psa_set_key_algorithm( &attributes, alg );
7872 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007873 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007874 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007875
7876 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007877 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7878 input1->x, input1->len,
7879 input2->x, input2->len,
7880 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007881 goto exit;
7882
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007883 psa_set_key_usage_flags( &attributes, derived_usage );
7884 psa_set_key_algorithm( &attributes, derived_alg );
7885 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007886 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007887 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007888 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007889
7890 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007891 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007892 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7893 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007894
7895 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007896 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007897 goto exit;
7898
7899exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007900 /*
7901 * Key attributes may have been returned by psa_get_key_attributes()
7902 * thus reset them as required.
7903 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007904 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007905
7906 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007907 psa_destroy_key( base_key );
7908 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007909 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007910}
7911/* END_CASE */
7912
Janos Follath42fd8882019-07-03 14:17:09 +01007913/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007914void derive_key_export( int alg_arg,
7915 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007916 data_t *input1,
7917 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007918 int bytes1_arg,
7919 int bytes2_arg )
7920{
Ronald Cron5425a212020-08-04 14:58:35 +02007921 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7922 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007923 psa_algorithm_t alg = alg_arg;
7924 size_t bytes1 = bytes1_arg;
7925 size_t bytes2 = bytes2_arg;
7926 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007927 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007928 uint8_t *output_buffer = NULL;
7929 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007930 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7931 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007932 size_t length;
7933
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007934 ASSERT_ALLOC( output_buffer, capacity );
7935 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007937
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007938 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7939 psa_set_key_algorithm( &base_attributes, alg );
7940 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007941 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007942 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007943
7944 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007945 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7946 input1->x, input1->len,
7947 input2->x, input2->len,
7948 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007949 goto exit;
7950
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007951 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007952 output_buffer,
7953 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007954 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007955
7956 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007957 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7958 input1->x, input1->len,
7959 input2->x, input2->len,
7960 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007961 goto exit;
7962
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007963 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7964 psa_set_key_algorithm( &derived_attributes, 0 );
7965 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007966 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007967 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007968 &derived_key ) );
7969 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007970 export_buffer, bytes1,
7971 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007972 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007973 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007974 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007975 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007976 &derived_key ) );
7977 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007978 export_buffer + bytes1, bytes2,
7979 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007980 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007981
7982 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007983 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7984 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007985
7986exit:
7987 mbedtls_free( output_buffer );
7988 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007989 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007990 psa_destroy_key( base_key );
7991 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007992 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007993}
7994/* END_CASE */
7995
7996/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007997void derive_key_type( int alg_arg,
7998 data_t *key_data,
7999 data_t *input1,
8000 data_t *input2,
8001 int key_type_arg, int bits_arg,
8002 data_t *expected_export )
8003{
8004 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8005 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8006 const psa_algorithm_t alg = alg_arg;
8007 const psa_key_type_t key_type = key_type_arg;
8008 const size_t bits = bits_arg;
8009 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8010 const size_t export_buffer_size =
8011 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8012 uint8_t *export_buffer = NULL;
8013 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8014 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8015 size_t export_length;
8016
8017 ASSERT_ALLOC( export_buffer, export_buffer_size );
8018 PSA_ASSERT( psa_crypto_init( ) );
8019
8020 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8021 psa_set_key_algorithm( &base_attributes, alg );
8022 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8023 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8024 &base_key ) );
8025
Przemek Stekielc85f0912022-03-08 11:37:54 +01008026 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008027 &operation, base_key, alg,
8028 input1->x, input1->len,
8029 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008030 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008031 goto exit;
8032
8033 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8034 psa_set_key_algorithm( &derived_attributes, 0 );
8035 psa_set_key_type( &derived_attributes, key_type );
8036 psa_set_key_bits( &derived_attributes, bits );
8037 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8038 &derived_key ) );
8039
8040 PSA_ASSERT( psa_export_key( derived_key,
8041 export_buffer, export_buffer_size,
8042 &export_length ) );
8043 ASSERT_COMPARE( export_buffer, export_length,
8044 expected_export->x, expected_export->len );
8045
8046exit:
8047 mbedtls_free( export_buffer );
8048 psa_key_derivation_abort( &operation );
8049 psa_destroy_key( base_key );
8050 psa_destroy_key( derived_key );
8051 PSA_DONE( );
8052}
8053/* END_CASE */
8054
8055/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008056void derive_key( int alg_arg,
8057 data_t *key_data, data_t *input1, data_t *input2,
8058 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008059 int expected_status_arg,
8060 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008061{
Ronald Cron5425a212020-08-04 14:58:35 +02008062 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8063 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008064 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008065 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008066 size_t bits = bits_arg;
8067 psa_status_t expected_status = expected_status_arg;
8068 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8069 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8070 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8071
8072 PSA_ASSERT( psa_crypto_init( ) );
8073
8074 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8075 psa_set_key_algorithm( &base_attributes, alg );
8076 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8077 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008078 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008079
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008080 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8081 input1->x, input1->len,
8082 input2->x, input2->len,
8083 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008084 goto exit;
8085
8086 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8087 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008088 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008089 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008090
8091 psa_status_t status =
8092 psa_key_derivation_output_key( &derived_attributes,
8093 &operation,
8094 &derived_key );
8095 if( is_large_output > 0 )
8096 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8097 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008098
8099exit:
8100 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008101 psa_destroy_key( base_key );
8102 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008103 PSA_DONE( );
8104}
8105/* END_CASE */
8106
8107/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008108void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008109 int our_key_type_arg, int our_key_alg_arg,
8110 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008111 int expected_status_arg )
8112{
Ronald Cron5425a212020-08-04 14:58:35 +02008113 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008114 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008115 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008116 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008117 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008119 psa_status_t expected_status = expected_status_arg;
8120 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008121
Gilles Peskine8817f612018-12-18 00:18:46 +01008122 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008123
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008124 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008125 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008126 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008127 PSA_ASSERT( psa_import_key( &attributes,
8128 our_key_data->x, our_key_data->len,
8129 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008130
Gilles Peskine77f40d82019-04-11 21:27:06 +02008131 /* The tests currently include inputs that should fail at either step.
8132 * Test cases that fail at the setup step should be changed to call
8133 * key_derivation_setup instead, and this function should be renamed
8134 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008135 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008136 if( status == PSA_SUCCESS )
8137 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008138 TEST_EQUAL( psa_key_derivation_key_agreement(
8139 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8140 our_key,
8141 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008142 expected_status );
8143 }
8144 else
8145 {
8146 TEST_ASSERT( status == expected_status );
8147 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008148
8149exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008150 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008151 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008152 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008153}
8154/* END_CASE */
8155
8156/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008157void raw_key_agreement( int alg_arg,
8158 int our_key_type_arg, data_t *our_key_data,
8159 data_t *peer_key_data,
8160 data_t *expected_output )
8161{
Ronald Cron5425a212020-08-04 14:58:35 +02008162 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008163 psa_algorithm_t alg = alg_arg;
8164 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008166 unsigned char *output = NULL;
8167 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008168 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008169
Gilles Peskinef0cba732019-04-11 22:12:38 +02008170 PSA_ASSERT( psa_crypto_init( ) );
8171
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8173 psa_set_key_algorithm( &attributes, alg );
8174 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008175 PSA_ASSERT( psa_import_key( &attributes,
8176 our_key_data->x, our_key_data->len,
8177 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008178
gabor-mezei-armceface22021-01-21 12:26:17 +01008179 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8180 key_bits = psa_get_key_bits( &attributes );
8181
Gilles Peskine992bee82022-04-13 23:25:52 +02008182 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008183 TEST_LE_U( expected_output->len,
8184 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8185 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8186 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008187
8188 /* Good case with exact output size */
8189 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008190 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8191 peer_key_data->x, peer_key_data->len,
8192 output, expected_output->len,
8193 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008194 ASSERT_COMPARE( output, output_length,
8195 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008196 mbedtls_free( output );
8197 output = NULL;
8198 output_length = ~0;
8199
8200 /* Larger buffer */
8201 ASSERT_ALLOC( output, expected_output->len + 1 );
8202 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8203 peer_key_data->x, peer_key_data->len,
8204 output, expected_output->len + 1,
8205 &output_length ) );
8206 ASSERT_COMPARE( output, output_length,
8207 expected_output->x, expected_output->len );
8208 mbedtls_free( output );
8209 output = NULL;
8210 output_length = ~0;
8211
8212 /* Buffer too small */
8213 ASSERT_ALLOC( output, expected_output->len - 1 );
8214 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8215 peer_key_data->x, peer_key_data->len,
8216 output, expected_output->len - 1,
8217 &output_length ),
8218 PSA_ERROR_BUFFER_TOO_SMALL );
8219 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008220 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008221 mbedtls_free( output );
8222 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008223
8224exit:
8225 mbedtls_free( output );
8226 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008227 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008228}
8229/* END_CASE */
8230
8231/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008232void key_agreement_capacity( int alg_arg,
8233 int our_key_type_arg, data_t *our_key_data,
8234 data_t *peer_key_data,
8235 int expected_capacity_arg )
8236{
Ronald Cron5425a212020-08-04 14:58:35 +02008237 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008238 psa_algorithm_t alg = alg_arg;
8239 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008240 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008241 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008242 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008243 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008244
Gilles Peskine8817f612018-12-18 00:18:46 +01008245 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008246
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008247 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8248 psa_set_key_algorithm( &attributes, alg );
8249 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008250 PSA_ASSERT( psa_import_key( &attributes,
8251 our_key_data->x, our_key_data->len,
8252 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008253
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008254 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008255 PSA_ASSERT( psa_key_derivation_key_agreement(
8256 &operation,
8257 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8258 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008259 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8260 {
8261 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008262 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008263 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008264 NULL, 0 ) );
8265 }
Gilles Peskine59685592018-09-18 12:11:34 +02008266
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008267 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008268 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008269 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008270 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008271
Gilles Peskinebf491972018-10-25 22:36:12 +02008272 /* Test the actual capacity by reading the output. */
8273 while( actual_capacity > sizeof( output ) )
8274 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008275 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008276 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008277 actual_capacity -= sizeof( output );
8278 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008279 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008280 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008281 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008282 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008283
Gilles Peskine59685592018-09-18 12:11:34 +02008284exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008285 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008286 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008287 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008288}
8289/* END_CASE */
8290
8291/* BEGIN_CASE */
8292void key_agreement_output( int alg_arg,
8293 int our_key_type_arg, data_t *our_key_data,
8294 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008295 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008296{
Ronald Cron5425a212020-08-04 14:58:35 +02008297 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008298 psa_algorithm_t alg = alg_arg;
8299 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008300 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008302 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008303
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008304 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8305 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008306
Gilles Peskine8817f612018-12-18 00:18:46 +01008307 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008308
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008309 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8310 psa_set_key_algorithm( &attributes, alg );
8311 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008312 PSA_ASSERT( psa_import_key( &attributes,
8313 our_key_data->x, our_key_data->len,
8314 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008315
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008316 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008317 PSA_ASSERT( psa_key_derivation_key_agreement(
8318 &operation,
8319 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8320 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008321 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8322 {
8323 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008324 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008325 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008326 NULL, 0 ) );
8327 }
Gilles Peskine59685592018-09-18 12:11:34 +02008328
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008329 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008330 actual_output,
8331 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008332 ASSERT_COMPARE( actual_output, expected_output1->len,
8333 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008334 if( expected_output2->len != 0 )
8335 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008336 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008337 actual_output,
8338 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008339 ASSERT_COMPARE( actual_output, expected_output2->len,
8340 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008341 }
Gilles Peskine59685592018-09-18 12:11:34 +02008342
8343exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008344 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008345 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008346 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008347 mbedtls_free( actual_output );
8348}
8349/* END_CASE */
8350
8351/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008352void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008353{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008354 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008355 unsigned char *output = NULL;
8356 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008357 size_t i;
8358 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008359
Simon Butcher49f8e312020-03-03 15:51:50 +00008360 TEST_ASSERT( bytes_arg >= 0 );
8361
Gilles Peskine91892022021-02-08 19:50:26 +01008362 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008363 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008364
Gilles Peskine8817f612018-12-18 00:18:46 +01008365 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008366
Gilles Peskinea50d7392018-06-21 10:22:13 +02008367 /* Run several times, to ensure that every output byte will be
8368 * nonzero at least once with overwhelming probability
8369 * (2^(-8*number_of_runs)). */
8370 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008371 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008372 if( bytes != 0 )
8373 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008374 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008375
Gilles Peskinea50d7392018-06-21 10:22:13 +02008376 for( i = 0; i < bytes; i++ )
8377 {
8378 if( output[i] != 0 )
8379 ++changed[i];
8380 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008381 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008382
8383 /* Check that every byte was changed to nonzero at least once. This
8384 * validates that psa_generate_random is overwriting every byte of
8385 * the output buffer. */
8386 for( i = 0; i < bytes; i++ )
8387 {
8388 TEST_ASSERT( changed[i] != 0 );
8389 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008390
8391exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008392 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008393 mbedtls_free( output );
8394 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008395}
8396/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008397
8398/* BEGIN_CASE */
8399void generate_key( int type_arg,
8400 int bits_arg,
8401 int usage_arg,
8402 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008403 int expected_status_arg,
8404 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008405{
Ronald Cron5425a212020-08-04 14:58:35 +02008406 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008407 psa_key_type_t type = type_arg;
8408 psa_key_usage_t usage = usage_arg;
8409 size_t bits = bits_arg;
8410 psa_algorithm_t alg = alg_arg;
8411 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008413 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008414
Gilles Peskine8817f612018-12-18 00:18:46 +01008415 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008416
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008417 psa_set_key_usage_flags( &attributes, usage );
8418 psa_set_key_algorithm( &attributes, alg );
8419 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008420 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008421
8422 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008423 psa_status_t status = psa_generate_key( &attributes, &key );
8424
8425 if( is_large_key > 0 )
8426 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8427 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008428 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008429 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008430
8431 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008432 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008433 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8434 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008435
Gilles Peskine818ca122018-06-20 18:16:48 +02008436 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008437 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008438 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008439
8440exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008441 /*
8442 * Key attributes may have been returned by psa_get_key_attributes()
8443 * thus reset them as required.
8444 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008445 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008446
Ronald Cron5425a212020-08-04 14:58:35 +02008447 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008448 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008449}
8450/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008451
Ronald Cronee414c72021-03-18 18:50:08 +01008452/* 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 +02008453void generate_key_rsa( int bits_arg,
8454 data_t *e_arg,
8455 int expected_status_arg )
8456{
Ronald Cron5425a212020-08-04 14:58:35 +02008457 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008458 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008459 size_t bits = bits_arg;
8460 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8461 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8462 psa_status_t expected_status = expected_status_arg;
8463 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8464 uint8_t *exported = NULL;
8465 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008466 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008467 size_t exported_length = SIZE_MAX;
8468 uint8_t *e_read_buffer = NULL;
8469 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008470 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008471 size_t e_read_length = SIZE_MAX;
8472
8473 if( e_arg->len == 0 ||
8474 ( e_arg->len == 3 &&
8475 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8476 {
8477 is_default_public_exponent = 1;
8478 e_read_size = 0;
8479 }
8480 ASSERT_ALLOC( e_read_buffer, e_read_size );
8481 ASSERT_ALLOC( exported, exported_size );
8482
8483 PSA_ASSERT( psa_crypto_init( ) );
8484
8485 psa_set_key_usage_flags( &attributes, usage );
8486 psa_set_key_algorithm( &attributes, alg );
8487 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8488 e_arg->x, e_arg->len ) );
8489 psa_set_key_bits( &attributes, bits );
8490
8491 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008492 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008493 if( expected_status != PSA_SUCCESS )
8494 goto exit;
8495
8496 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008497 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008498 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8499 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8500 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8501 e_read_buffer, e_read_size,
8502 &e_read_length ) );
8503 if( is_default_public_exponent )
8504 TEST_EQUAL( e_read_length, 0 );
8505 else
8506 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8507
8508 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008509 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008510 goto exit;
8511
8512 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008513 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008514 exported, exported_size,
8515 &exported_length ) );
8516 {
8517 uint8_t *p = exported;
8518 uint8_t *end = exported + exported_length;
8519 size_t len;
8520 /* RSAPublicKey ::= SEQUENCE {
8521 * modulus INTEGER, -- n
8522 * publicExponent INTEGER } -- e
8523 */
8524 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008525 MBEDTLS_ASN1_SEQUENCE |
8526 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008527 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008528 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8529 MBEDTLS_ASN1_INTEGER ) );
8530 if( len >= 1 && p[0] == 0 )
8531 {
8532 ++p;
8533 --len;
8534 }
8535 if( e_arg->len == 0 )
8536 {
8537 TEST_EQUAL( len, 3 );
8538 TEST_EQUAL( p[0], 1 );
8539 TEST_EQUAL( p[1], 0 );
8540 TEST_EQUAL( p[2], 1 );
8541 }
8542 else
8543 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8544 }
8545
8546exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008547 /*
8548 * Key attributes may have been returned by psa_get_key_attributes() or
8549 * set by psa_set_key_domain_parameters() thus reset them as required.
8550 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008551 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008552
Ronald Cron5425a212020-08-04 14:58:35 +02008553 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008554 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008555 mbedtls_free( e_read_buffer );
8556 mbedtls_free( exported );
8557}
8558/* END_CASE */
8559
Darryl Greend49a4992018-06-18 17:27:26 +01008560/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008561void persistent_key_load_key_from_storage( data_t *data,
8562 int type_arg, int bits_arg,
8563 int usage_flags_arg, int alg_arg,
8564 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008565{
Ronald Cron71016a92020-08-28 19:01:50 +02008566 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8569 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008570 psa_key_type_t type = type_arg;
8571 size_t bits = bits_arg;
8572 psa_key_usage_t usage_flags = usage_flags_arg;
8573 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008574 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008575 unsigned char *first_export = NULL;
8576 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008577 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008578 size_t first_exported_length;
8579 size_t second_exported_length;
8580
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008581 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8582 {
8583 ASSERT_ALLOC( first_export, export_size );
8584 ASSERT_ALLOC( second_export, export_size );
8585 }
Darryl Greend49a4992018-06-18 17:27:26 +01008586
Gilles Peskine8817f612018-12-18 00:18:46 +01008587 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008588
Gilles Peskinec87af662019-05-15 16:12:22 +02008589 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008590 psa_set_key_usage_flags( &attributes, usage_flags );
8591 psa_set_key_algorithm( &attributes, alg );
8592 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008593 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008594
Darryl Green0c6575a2018-11-07 16:05:30 +00008595 switch( generation_method )
8596 {
8597 case IMPORT_KEY:
8598 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008599 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008600 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008601 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008602
Darryl Green0c6575a2018-11-07 16:05:30 +00008603 case GENERATE_KEY:
8604 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008605 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008606 break;
8607
8608 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008609#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008610 {
8611 /* Create base key */
8612 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8613 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8614 psa_set_key_usage_flags( &base_attributes,
8615 PSA_KEY_USAGE_DERIVE );
8616 psa_set_key_algorithm( &base_attributes, derive_alg );
8617 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008618 PSA_ASSERT( psa_import_key( &base_attributes,
8619 data->x, data->len,
8620 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008621 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008622 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008623 PSA_ASSERT( psa_key_derivation_input_key(
8624 &operation,
8625 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008626 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008627 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008628 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008629 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8630 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008631 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008632 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008633 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008634 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008635 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008636#else
8637 TEST_ASSUME( ! "KDF not supported in this configuration" );
8638#endif
8639 break;
8640
8641 default:
8642 TEST_ASSERT( ! "generation_method not implemented in test" );
8643 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008644 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008645 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008646
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008647 /* Export the key if permitted by the key policy. */
8648 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8649 {
Ronald Cron5425a212020-08-04 14:58:35 +02008650 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008651 first_export, export_size,
8652 &first_exported_length ) );
8653 if( generation_method == IMPORT_KEY )
8654 ASSERT_COMPARE( data->x, data->len,
8655 first_export, first_exported_length );
8656 }
Darryl Greend49a4992018-06-18 17:27:26 +01008657
8658 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008659 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008660 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008661 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008662
Darryl Greend49a4992018-06-18 17:27:26 +01008663 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008664 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008665 TEST_ASSERT( mbedtls_svc_key_id_equal(
8666 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008667 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8668 PSA_KEY_LIFETIME_PERSISTENT );
8669 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8670 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008671 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008672 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008673 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008674
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008675 /* Export the key again if permitted by the key policy. */
8676 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008677 {
Ronald Cron5425a212020-08-04 14:58:35 +02008678 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008679 second_export, export_size,
8680 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008681 ASSERT_COMPARE( first_export, first_exported_length,
8682 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008683 }
8684
8685 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008686 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008687 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008688
8689exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008690 /*
8691 * Key attributes may have been returned by psa_get_key_attributes()
8692 * thus reset them as required.
8693 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008694 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008695
Darryl Greend49a4992018-06-18 17:27:26 +01008696 mbedtls_free( first_export );
8697 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008698 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008699 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008701 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008702}
8703/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008704
Neil Armstronga557cb82022-06-10 08:58:32 +02008705/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008706void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8707 int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008708 int input_first, data_t *pw_data,
Neil Armstrong2a73f212022-09-06 11:34:54 +02008709 int expected_status_setup_arg,
8710 int expected_status_set_role_arg,
8711 int expected_status_set_password_key_arg,
8712 int expected_status_input_output_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008713{
8714 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8715 psa_pake_operation_t operation = psa_pake_operation_init();
8716 psa_algorithm_t alg = alg_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008717 psa_key_type_t key_type_pw = key_type_pw_arg;
8718 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008719 psa_algorithm_t hash_alg = hash_arg;
8720 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008721 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8722 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008723 psa_status_t expected_status_setup = expected_status_setup_arg;
8724 psa_status_t expected_status_set_role = expected_status_set_role_arg;
8725 psa_status_t expected_status_set_password_key =
8726 expected_status_set_password_key_arg;
8727 psa_status_t expected_status_input_output =
8728 expected_status_input_output_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008729 unsigned char *output_buffer = NULL;
8730 size_t output_len = 0;
8731
8732 PSA_INIT( );
8733
8734 ASSERT_ALLOC( output_buffer,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008735 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8736 PSA_PAKE_STEP_KEY_SHARE) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008737
8738 if( pw_data->len > 0 )
8739 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008740 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008741 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008742 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008743 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8744 &key ) );
8745 }
8746
8747 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8748 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8749 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8750
Neil Armstrong645cccd2022-06-08 17:36:23 +02008751 PSA_ASSERT( psa_pake_abort( &operation ) );
8752
8753 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8754 PSA_ERROR_BAD_STATE );
8755 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8756 PSA_ERROR_BAD_STATE );
8757 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8758 PSA_ERROR_BAD_STATE );
8759 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8760 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008761 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8762 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008763 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008764 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008765 PSA_ERROR_BAD_STATE );
8766
8767 PSA_ASSERT( psa_pake_abort( &operation ) );
8768
Neil Armstrong2a73f212022-09-06 11:34:54 +02008769 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8770 expected_status_setup );
8771 if( expected_status_setup != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008772 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008773
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008774 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8775 PSA_ERROR_BAD_STATE );
8776
Neil Armstrong2a73f212022-09-06 11:34:54 +02008777 TEST_EQUAL( psa_pake_set_role( &operation, role),
8778 expected_status_set_role );
8779 if( expected_status_set_role != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008780 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008781
8782 if( pw_data->len > 0 )
8783 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008784 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8785 expected_status_set_password_key );
8786 if( expected_status_set_password_key != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008787 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008788 }
8789
Neil Armstrong707d9572022-06-08 17:31:49 +02008790 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8791 PSA_ERROR_INVALID_ARGUMENT );
8792 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8793 PSA_ERROR_INVALID_ARGUMENT );
8794
8795 const uint8_t unsupported_id[] = "abcd";
8796
8797 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8798 PSA_ERROR_NOT_SUPPORTED );
8799 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8800 PSA_ERROR_NOT_SUPPORTED );
8801
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008802 /* First round */
8803 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008804 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008805 /* Invalid parameters */
8806 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8807 NULL, 0 ),
8808 PSA_ERROR_INVALID_ARGUMENT );
8809 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8810 output_buffer, 66 ),
8811 PSA_ERROR_INVALID_ARGUMENT );
8812 /* Invalid first step */
8813 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8814 output_buffer, 66 ),
8815 PSA_ERROR_BAD_STATE );
8816
8817 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8818 output_buffer, 66 ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008819 expected_status_input_output);
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008820
Neil Armstrong2a73f212022-09-06 11:34:54 +02008821 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008822 {
8823 /* Buffer too large */
8824 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8825 output_buffer, 512 ),
8826 PSA_ERROR_INSUFFICIENT_MEMORY );
8827
8828 /* The operation should be aborted at this point */
8829 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8830 output_buffer, 66 ),
8831 PSA_ERROR_BAD_STATE );
8832 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008833 }
8834 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008835 {
8836 /* Invalid parameters */
8837 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8838 NULL, 0, NULL ),
8839 PSA_ERROR_INVALID_ARGUMENT );
8840 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8841 output_buffer, 512, &output_len ),
8842 PSA_ERROR_INVALID_ARGUMENT );
8843 /* Invalid first step */
8844 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8845 output_buffer, 512, &output_len ),
8846 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008847
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008848 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8849 output_buffer, 512, &output_len ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008850 expected_status_input_output );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008851
Neil Armstrong2a73f212022-09-06 11:34:54 +02008852 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008853 {
8854 TEST_ASSERT( output_len > 0 );
8855
8856 /* Buffer too small */
8857 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8858 output_buffer, 5, &output_len ),
8859 PSA_ERROR_BUFFER_TOO_SMALL );
8860
8861 /* The operation should be aborted at this point */
8862 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8863 output_buffer, 512, &output_len ),
8864 PSA_ERROR_BAD_STATE );
8865 }
8866 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008867
8868exit:
8869 PSA_ASSERT( psa_destroy_key( key ) );
8870 PSA_ASSERT( psa_pake_abort( &operation ) );
8871 mbedtls_free( output_buffer );
8872 PSA_DONE( );
8873}
8874/* END_CASE */
8875
Neil Armstronga557cb82022-06-10 08:58:32 +02008876/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008877void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8878 int client_input_first, int inject_error,
8879 data_t *pw_data )
8880{
8881 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8882 psa_pake_operation_t server = psa_pake_operation_init();
8883 psa_pake_operation_t client = psa_pake_operation_init();
8884 psa_algorithm_t alg = alg_arg;
8885 psa_algorithm_t hash_alg = hash_arg;
8886 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8888
8889 PSA_INIT( );
8890
8891 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8892 psa_set_key_algorithm( &attributes, alg );
8893 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8894 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8895 &key ) );
8896
8897 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8898 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8899 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8900
8901
8902 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8903 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8904
8905 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8906 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8907
8908 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8909 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8910
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008911 ecjpake_do_round( alg, primitive_arg, &server, &client,
8912 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008913
8914 if( inject_error == 1 || inject_error == 2 )
8915 goto exit;
8916
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008917 ecjpake_do_round( alg, primitive_arg, &server, &client,
8918 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008919
8920exit:
8921 psa_destroy_key( key );
8922 psa_pake_abort( &server );
8923 psa_pake_abort( &client );
8924 PSA_DONE( );
8925}
8926/* END_CASE */
8927
8928/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008929void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008930 int derive_alg_arg, data_t *pw_data,
8931 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008932{
8933 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8934 psa_pake_operation_t server = psa_pake_operation_init();
8935 psa_pake_operation_t client = psa_pake_operation_init();
8936 psa_algorithm_t alg = alg_arg;
8937 psa_algorithm_t hash_alg = hash_arg;
8938 psa_algorithm_t derive_alg = derive_alg_arg;
8939 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8941 psa_key_derivation_operation_t server_derive =
8942 PSA_KEY_DERIVATION_OPERATION_INIT;
8943 psa_key_derivation_operation_t client_derive =
8944 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008945
8946 PSA_INIT( );
8947
Neil Armstrongd597bc72022-05-25 11:28:39 +02008948 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8949 psa_set_key_algorithm( &attributes, alg );
8950 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8951 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8952 &key ) );
8953
8954 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8955 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8956 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8957
Neil Armstrong1e855602022-06-15 11:32:11 +02008958 /* Get shared key */
8959 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
8960 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
8961
8962 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
8963 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
8964 {
8965 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
8966 PSA_KEY_DERIVATION_INPUT_SEED,
8967 (const uint8_t*) "", 0) );
8968 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
8969 PSA_KEY_DERIVATION_INPUT_SEED,
8970 (const uint8_t*) "", 0) );
8971 }
8972
Neil Armstrongd597bc72022-05-25 11:28:39 +02008973 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8974 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8975
8976 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8977 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8978
8979 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8980 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8981
Neil Armstrong1e855602022-06-15 11:32:11 +02008982 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8983 PSA_ERROR_BAD_STATE );
8984 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8985 PSA_ERROR_BAD_STATE );
8986
Neil Armstrongf983caf2022-06-15 15:27:48 +02008987 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008988 ecjpake_do_round( alg, primitive_arg, &server, &client,
8989 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008990
Neil Armstrong1e855602022-06-15 11:32:11 +02008991 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8992 PSA_ERROR_BAD_STATE );
8993 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8994 PSA_ERROR_BAD_STATE );
8995
Neil Armstrongf983caf2022-06-15 15:27:48 +02008996 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008997 ecjpake_do_round( alg, primitive_arg, &server, &client,
8998 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008999
Neil Armstrongd597bc72022-05-25 11:28:39 +02009000 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9001 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9002
9003exit:
9004 psa_key_derivation_abort( &server_derive );
9005 psa_key_derivation_abort( &client_derive );
9006 psa_destroy_key( key );
9007 psa_pake_abort( &server );
9008 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009009 PSA_DONE( );
9010}
9011/* END_CASE */