blob: ca1614befa64d6b339a9a9dca50bc9509cf70f2e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Przemek Stekiel8258ea72022-10-19 12:17:19 +020024#include "mbedtls/legacy_or_psa.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010025
Gilles Peskine4023c012021-05-27 13:21:20 +020026/* If this comes up, it's a bug in the test code or in the test data. */
27#define UNUSED 0xdeadbeef
28
Dave Rodgman647791d2021-06-23 12:49:59 +010029/* Assert that an operation is (not) active.
30 * This serves as a proxy for checking if the operation is aborted. */
31#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
32#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010033
Przemek Stekiel7c795482022-11-15 22:26:12 +010034#if defined(PSA_WANT_ALG_JPAKE)
Przemek Stekielf82effa2022-11-21 15:10:32 +010035int ecjpake_operation_setup( psa_pake_operation_t *operation,
Przemek Stekiel7c795482022-11-15 22:26:12 +010036 psa_pake_cipher_suite_t *cipher_suite,
37 psa_pake_role_t role,
38 mbedtls_svc_key_id_t key,
39 size_t key_available )
40{
Przemek Stekielf82effa2022-11-21 15:10:32 +010041 PSA_ASSERT( psa_pake_abort( operation ) );
Przemek Stekiel7c795482022-11-15 22:26:12 +010042
Przemek Stekielf82effa2022-11-21 15:10:32 +010043 PSA_ASSERT( psa_pake_setup( operation, cipher_suite ) );
Przemek Stekiel7c795482022-11-15 22:26:12 +010044
Przemek Stekielf82effa2022-11-21 15:10:32 +010045 PSA_ASSERT( psa_pake_set_role( operation, role) );
Przemek Stekiel7c795482022-11-15 22:26:12 +010046
47 if( key_available )
Przemek Stekielf82effa2022-11-21 15:10:32 +010048 PSA_ASSERT( psa_pake_set_password_key( operation, key ) );
49 return 0;
Przemek Stekiel7c795482022-11-15 22:26:12 +010050exit:
Przemek Stekielf82effa2022-11-21 15:10:32 +010051 return 1;
Przemek Stekiel7c795482022-11-15 22:26:12 +010052}
53#endif
54
Jaeden Amerof24c7f82018-06-27 17:20:43 +010055/** An invalid export length that will never be set by psa_export_key(). */
56static const size_t INVALID_EXPORT_LENGTH = ~0U;
57
Gilles Peskinea7aa4422018-08-14 15:17:54 +020058/** Test if a buffer contains a constant byte value.
59 *
60 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020061 *
62 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020063 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020064 * \param size Size of the buffer in bytes.
65 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020066 * \return 1 if the buffer is all-bits-zero.
67 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020068 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020069static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020070{
71 size_t i;
72 for( i = 0; i < size; i++ )
73 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020074 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020075 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020076 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020077 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020078}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010079#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020080/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
81static int asn1_write_10x( unsigned char **p,
82 unsigned char *start,
83 size_t bits,
84 unsigned char x )
85{
86 int ret;
87 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020088 if( bits == 0 )
89 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
90 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020091 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030092 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020093 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
94 *p -= len;
95 ( *p )[len-1] = x;
96 if( bits % 8 == 0 )
97 ( *p )[1] |= 1;
98 else
99 ( *p )[0] |= 1 << ( bits % 8 );
100 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
101 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
102 MBEDTLS_ASN1_INTEGER ) );
103 return( len );
104}
105
106static int construct_fake_rsa_key( unsigned char *buffer,
107 size_t buffer_size,
108 unsigned char **p,
109 size_t bits,
110 int keypair )
111{
112 size_t half_bits = ( bits + 1 ) / 2;
113 int ret;
114 int len = 0;
115 /* Construct something that looks like a DER encoding of
116 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
117 * RSAPrivateKey ::= SEQUENCE {
118 * version Version,
119 * modulus INTEGER, -- n
120 * publicExponent INTEGER, -- e
121 * privateExponent INTEGER, -- d
122 * prime1 INTEGER, -- p
123 * prime2 INTEGER, -- q
124 * exponent1 INTEGER, -- d mod (p-1)
125 * exponent2 INTEGER, -- d mod (q-1)
126 * coefficient INTEGER, -- (inverse of q) mod p
127 * otherPrimeInfos OtherPrimeInfos OPTIONAL
128 * }
129 * Or, for a public key, the same structure with only
130 * version, modulus and publicExponent.
131 */
132 *p = buffer + buffer_size;
133 if( keypair )
134 {
135 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
136 asn1_write_10x( p, buffer, half_bits, 1 ) );
137 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
138 asn1_write_10x( p, buffer, half_bits, 1 ) );
139 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
140 asn1_write_10x( p, buffer, half_bits, 1 ) );
141 MBEDTLS_ASN1_CHK_ADD( len, /* q */
142 asn1_write_10x( p, buffer, half_bits, 1 ) );
143 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
144 asn1_write_10x( p, buffer, half_bits, 3 ) );
145 MBEDTLS_ASN1_CHK_ADD( len, /* d */
146 asn1_write_10x( p, buffer, bits, 1 ) );
147 }
148 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
149 asn1_write_10x( p, buffer, 17, 1 ) );
150 MBEDTLS_ASN1_CHK_ADD( len, /* n */
151 asn1_write_10x( p, buffer, bits, 1 ) );
152 if( keypair )
153 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
154 mbedtls_asn1_write_int( p, buffer, 0 ) );
155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
156 {
157 const unsigned char tag =
158 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
159 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
160 }
161 return( len );
162}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100163#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200164
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100165int exercise_mac_setup( psa_key_type_t key_type,
166 const unsigned char *key_bytes,
167 size_t key_length,
168 psa_algorithm_t alg,
169 psa_mac_operation_t *operation,
170 psa_status_t *status )
171{
Ronald Cron5425a212020-08-04 14:58:35 +0200172 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100174
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100175 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200176 psa_set_key_algorithm( &attributes, alg );
177 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200178 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100179
Ronald Cron5425a212020-08-04 14:58:35 +0200180 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100181 /* Whether setup succeeded or failed, abort must succeed. */
182 PSA_ASSERT( psa_mac_abort( operation ) );
183 /* If setup failed, reproduce the failure, so that the caller can
184 * test the resulting state of the operation object. */
185 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100186 {
Ronald Cron5425a212020-08-04 14:58:35 +0200187 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100188 }
189
Ronald Cron5425a212020-08-04 14:58:35 +0200190 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100191 return( 1 );
192
193exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200194 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100195 return( 0 );
196}
197
198int exercise_cipher_setup( psa_key_type_t key_type,
199 const unsigned char *key_bytes,
200 size_t key_length,
201 psa_algorithm_t alg,
202 psa_cipher_operation_t *operation,
203 psa_status_t *status )
204{
Ronald Cron5425a212020-08-04 14:58:35 +0200205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200208 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
209 psa_set_key_algorithm( &attributes, alg );
210 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200211 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100212
Ronald Cron5425a212020-08-04 14:58:35 +0200213 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100214 /* Whether setup succeeded or failed, abort must succeed. */
215 PSA_ASSERT( psa_cipher_abort( operation ) );
216 /* If setup failed, reproduce the failure, so that the caller can
217 * test the resulting state of the operation object. */
218 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100219 {
Ronald Cron5425a212020-08-04 14:58:35 +0200220 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100221 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100222 }
223
Ronald Cron5425a212020-08-04 14:58:35 +0200224 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100225 return( 1 );
226
227exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200228 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100229 return( 0 );
230}
231
Ronald Cron5425a212020-08-04 14:58:35 +0200232static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233{
234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200235 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200236 uint8_t buffer[1];
237 size_t length;
238 int ok = 0;
239
Ronald Cronecfb2372020-07-23 17:13:42 +0200240 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200241 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
242 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
243 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200244 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000245 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200246 TEST_EQUAL(
247 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
248 TEST_EQUAL(
249 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200250 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200251 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
252 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
253 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
254 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
255
Ronald Cron5425a212020-08-04 14:58:35 +0200256 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000257 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200258 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200259 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000260 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200261
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200262 ok = 1;
263
264exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100265 /*
266 * Key attributes may have been returned by psa_get_key_attributes()
267 * thus reset them as required.
268 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200269 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100270
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200271 return( ok );
272}
273
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200274/* Assert that a key isn't reported as having a slot number. */
275#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
276#define ASSERT_NO_SLOT_NUMBER( attributes ) \
277 do \
278 { \
279 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
280 TEST_EQUAL( psa_get_key_slot_number( \
281 attributes, \
282 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
283 PSA_ERROR_INVALID_ARGUMENT ); \
284 } \
285 while( 0 )
286#else /* MBEDTLS_PSA_CRYPTO_SE_C */
287#define ASSERT_NO_SLOT_NUMBER( attributes ) \
288 ( (void) 0 )
289#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
290
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100291/* An overapproximation of the amount of storage needed for a key of the
292 * given type and with the given content. The API doesn't make it easy
293 * to find a good value for the size. The current implementation doesn't
294 * care about the value anyway. */
295#define KEY_BITS_FROM_DATA( type, data ) \
296 ( data )->len
297
Darryl Green0c6575a2018-11-07 16:05:30 +0000298typedef enum {
299 IMPORT_KEY = 0,
300 GENERATE_KEY = 1,
301 DERIVE_KEY = 2
302} generate_method;
303
Paul Elliott33746aa2021-09-15 16:40:40 +0100304typedef enum
305{
306 DO_NOT_SET_LENGTHS = 0,
307 SET_LENGTHS_BEFORE_NONCE = 1,
308 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100309} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100310
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100311typedef enum
312{
313 USE_NULL_TAG = 0,
314 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100315} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100316
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317/*!
318 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100319 * \param key_type_arg Type of key passed in
320 * \param key_data The encryption / decryption key data
321 * \param alg_arg The type of algorithm used
322 * \param nonce Nonce data
323 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100324 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100325 * feed additional data in to be encrypted /
326 * decrypted. If -1, no chunking.
327 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100328 * \param data_part_len_arg If not -1, the length of chunks to feed
329 * the data in to be encrypted / decrypted. If
330 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100331 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100332 * expected here, this controls whether or not
333 * to set lengths, and in what order with
334 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100335 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100336 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100337 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100338 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100339 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 */
341static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
342 int alg_arg,
343 data_t *nonce,
344 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100346 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100347 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100348 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100349 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100350 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100351 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100352{
353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
354 psa_key_type_t key_type = key_type_arg;
355 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100356 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100357 unsigned char *output_data = NULL;
358 unsigned char *part_data = NULL;
359 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100361 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100362 size_t output_size = 0;
363 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100364 size_t output_length = 0;
365 size_t key_bits = 0;
366 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100367 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100368 size_t part_length = 0;
369 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100370 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100371 size_t ad_part_len = 0;
372 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100373 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
375 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
376
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100377 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100378 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100379
Paul Elliottd3f82412021-06-16 16:52:21 +0100380 PSA_ASSERT( psa_crypto_init( ) );
381
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100382 if( is_encrypt )
383 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
384 else
385 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
386
Paul Elliottd3f82412021-06-16 16:52:21 +0100387 psa_set_key_algorithm( &attributes, alg );
388 psa_set_key_type( &attributes, key_type );
389
390 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
391 &key ) );
392
393 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
394 key_bits = psa_get_key_bits( &attributes );
395
396 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
397
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100398 if( is_encrypt )
399 {
400 /* Tag gets written at end of buffer. */
401 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
402 ( input_data->len +
403 tag_length ) );
404 data_true_size = input_data->len;
405 }
406 else
407 {
408 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
409 ( input_data->len -
410 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100411
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100412 /* Do not want to attempt to decrypt tag. */
413 data_true_size = input_data->len - tag_length;
414 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100415
416 ASSERT_ALLOC( output_data, output_size );
417
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100418 if( is_encrypt )
419 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100420 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200421 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100422 }
423 else
424 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100425 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200426 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100427 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100428
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100429 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100430
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100431 if( is_encrypt )
432 status = psa_aead_encrypt_setup( &operation, key, alg );
433 else
434 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100435
436 /* If the operation is not supported, just skip and not fail in case the
437 * encryption involves a common limitation of cryptography hardwares and
438 * an alternative implementation. */
439 if( status == PSA_ERROR_NOT_SUPPORTED )
440 {
441 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
442 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
443 }
444
445 PSA_ASSERT( status );
446
Paul Elliott33746aa2021-09-15 16:40:40 +0100447 if( set_lengths_method == DO_NOT_SET_LENGTHS )
448 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
449 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100450 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100451 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
452 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100453 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
454 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100455 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100456 {
457 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
458
Paul Elliott33746aa2021-09-15 16:40:40 +0100459 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
460 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100462
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100463 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100464 {
465 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100466 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100467
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100468 for( part_offset = 0, part_count = 0;
469 part_offset < additional_data->len;
470 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100471 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100472 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100473 {
Paul Elliott329d5382021-07-22 17:10:45 +0100474 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100475 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100476 else if( additional_data->len - part_offset < ad_part_len )
477 {
478 part_length = additional_data->len - part_offset;
479 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 else
481 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100482 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100483 }
484
485 PSA_ASSERT( psa_aead_update_ad( &operation,
486 additional_data->x + part_offset,
487 part_length ) );
488
Paul Elliottd3f82412021-06-16 16:52:21 +0100489 }
490 }
491 else
492 {
493 /* Pass additional data in one go. */
494 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
495 additional_data->len ) );
496 }
497
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100498 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100499 {
500 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100501 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100502 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100503 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100504
505 ASSERT_ALLOC( part_data, part_data_size );
506
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100507 for( part_offset = 0, part_count = 0;
508 part_offset < data_true_size;
509 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100510 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100511 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100512 {
Paul Elliott329d5382021-07-22 17:10:45 +0100513 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100515 else if( ( data_true_size - part_offset ) < data_part_len )
516 {
517 part_length = ( data_true_size - part_offset );
518 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100519 else
520 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100521 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 }
523
524 PSA_ASSERT( psa_aead_update( &operation,
525 ( input_data->x + part_offset ),
526 part_length, part_data,
527 part_data_size,
528 &output_part_length ) );
529
530 if( output_data && output_part_length )
531 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100532 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100533 output_part_length );
534 }
535
Paul Elliottd3f82412021-06-16 16:52:21 +0100536 output_length += output_part_length;
537 }
538 }
539 else
540 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100541 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100542 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100543 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100544 output_size, &output_length ) );
545 }
546
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100547 if( is_encrypt )
548 PSA_ASSERT( psa_aead_finish( &operation, final_data,
549 final_output_size,
550 &output_part_length,
551 tag_buffer, tag_length,
552 &tag_size ) );
553 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100554 {
Paul Elliott9961a662021-09-17 19:19:02 +0100555 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100556 final_output_size,
557 &output_part_length,
558 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100559 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100560 }
561
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100562 if( output_data && output_part_length )
563 memcpy( ( output_data + output_length ), final_data,
564 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100565
566 output_length += output_part_length;
567
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100568
569 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
570 * should be exact.*/
571 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100573 TEST_EQUAL( tag_length, tag_size );
574
575 if( output_data && tag_length )
576 memcpy( ( output_data + output_length ), tag_buffer,
577 tag_length );
578
579 output_length += tag_length;
580
581 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200582 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
583 input_data->len ) );
584 TEST_LE_U( output_length,
585 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100586 }
587 else
588 {
589 TEST_EQUAL( output_length,
590 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
591 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200592 TEST_LE_U( output_length,
593 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100594 }
595
Paul Elliottd3f82412021-06-16 16:52:21 +0100596
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100597 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100598 output_data, output_length );
599
Paul Elliottd3f82412021-06-16 16:52:21 +0100600
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100601 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100602
603exit:
604 psa_destroy_key( key );
605 psa_aead_abort( &operation );
606 mbedtls_free( output_data );
607 mbedtls_free( part_data );
608 mbedtls_free( final_data );
609 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100610
611 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100612}
613
Neil Armstrong4766f992022-02-28 16:23:59 +0100614/*!
615 * \brief Internal Function for MAC multipart tests.
616 * \param key_type_arg Type of key passed in
617 * \param key_data The encryption / decryption key data
618 * \param alg_arg The type of algorithm used
619 * \param input_data Data to encrypt / decrypt
620 * \param data_part_len_arg If not -1, the length of chunks to feed
621 * the data in to be encrypted / decrypted. If
622 * -1, no chunking
623 * \param expected_output Expected output
624 * \param is_verify If non-zero this is an verify operation.
625 * \param do_zero_parts If non-zero, interleave zero length chunks
626 * with normal length chunks.
627 * \return int Zero on failure, non-zero on success.
628 */
629static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
630 int alg_arg,
631 data_t *input_data,
632 int data_part_len_arg,
633 data_t *expected_output,
634 int is_verify,
635 int do_zero_parts )
636{
637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
638 psa_key_type_t key_type = key_type_arg;
639 psa_algorithm_t alg = alg_arg;
640 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
641 unsigned char mac[PSA_MAC_MAX_SIZE];
642 size_t part_offset = 0;
643 size_t part_length = 0;
644 size_t data_part_len = 0;
645 size_t mac_len = 0;
646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
647 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
648
649 int test_ok = 0;
650 size_t part_count = 0;
651
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100652 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100653
654 if( is_verify )
655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
656 else
657 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
658
659 psa_set_key_algorithm( &attributes, alg );
660 psa_set_key_type( &attributes, key_type );
661
662 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
663 &key ) );
664
665 if( is_verify )
666 status = psa_mac_verify_setup( &operation, key, alg );
667 else
668 status = psa_mac_sign_setup( &operation, key, alg );
669
670 PSA_ASSERT( status );
671
672 if( data_part_len_arg != -1 )
673 {
674 /* Pass data in parts */
675 data_part_len = ( size_t ) data_part_len_arg;
676
677 for( part_offset = 0, part_count = 0;
678 part_offset < input_data->len;
679 part_offset += part_length, part_count++ )
680 {
681 if( do_zero_parts && ( part_count & 0x01 ) )
682 {
683 part_length = 0;
684 }
685 else if( ( input_data->len - part_offset ) < data_part_len )
686 {
687 part_length = ( input_data->len - part_offset );
688 }
689 else
690 {
691 part_length = data_part_len;
692 }
693
694 PSA_ASSERT( psa_mac_update( &operation,
695 ( input_data->x + part_offset ),
696 part_length ) );
697 }
698 }
699 else
700 {
701 /* Pass all data in one go. */
702 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
703 input_data->len ) );
704 }
705
706 if( is_verify )
707 {
708 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
709 expected_output->len ) );
710 }
711 else
712 {
713 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
714 PSA_MAC_MAX_SIZE, &mac_len ) );
715
716 ASSERT_COMPARE( expected_output->x, expected_output->len,
717 mac, mac_len );
718 }
719
720 test_ok = 1;
721
722exit:
723 psa_destroy_key( key );
724 psa_mac_abort( &operation );
725 PSA_DONE( );
726
727 return( test_ok );
728}
729
Neil Armstrong75673ab2022-06-15 17:39:01 +0200730#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200731static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
732 psa_pake_operation_t *server,
733 psa_pake_operation_t *client,
734 int client_input_first,
735 int round, int inject_error )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200736{
737 unsigned char *buffer0 = NULL, *buffer1 = NULL;
738 size_t buffer_length = (
739 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
740 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
741 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200742 /* The output should be exactly this size according to the spec */
743 const size_t expected_size_key_share =
744 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
745 /* The output should be exactly this size according to the spec */
746 const size_t expected_size_zk_public =
747 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
748 /* The output can be smaller: the spec allows stripping leading zeroes */
749 const size_t max_expected_size_zk_proof =
750 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200751 size_t buffer0_off = 0;
752 size_t buffer1_off = 0;
753 size_t s_g1_len, s_g2_len, s_a_len;
754 size_t s_g1_off, s_g2_off, s_a_off;
755 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
756 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
757 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
758 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
759 size_t c_g1_len, c_g2_len, c_a_len;
760 size_t c_g1_off, c_g2_off, c_a_off;
761 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
762 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
763 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
764 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
765 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200766 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200767
768 ASSERT_ALLOC( buffer0, buffer_length );
769 ASSERT_ALLOC( buffer1, buffer_length );
770
771 switch( round )
772 {
773 case 1:
774 /* Server first round Output */
775 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
776 buffer0 + buffer0_off,
777 512 - buffer0_off, &s_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200778 TEST_EQUAL( s_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200779 s_g1_off = buffer0_off;
780 buffer0_off += s_g1_len;
781 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
782 buffer0 + buffer0_off,
783 512 - buffer0_off, &s_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200784 TEST_EQUAL( s_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200785 s_x1_pk_off = buffer0_off;
786 buffer0_off += s_x1_pk_len;
787 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
788 buffer0 + buffer0_off,
789 512 - buffer0_off, &s_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200790 TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200791 s_x1_pr_off = buffer0_off;
792 buffer0_off += s_x1_pr_len;
793 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
794 buffer0 + buffer0_off,
795 512 - buffer0_off, &s_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200796 TEST_EQUAL( s_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200797 s_g2_off = buffer0_off;
798 buffer0_off += s_g2_len;
799 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
800 buffer0 + buffer0_off,
801 512 - buffer0_off, &s_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200802 TEST_EQUAL( s_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200803 s_x2_pk_off = buffer0_off;
804 buffer0_off += s_x2_pk_len;
805 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
806 buffer0 + buffer0_off,
807 512 - buffer0_off, &s_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200808 TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200809 s_x2_pr_off = buffer0_off;
810 buffer0_off += s_x2_pr_len;
811
812 if( inject_error == 1 )
813 {
Andrzej Kurekc0182042022-11-08 08:12:56 -0500814 buffer0[s_x1_pr_off + 8] ^= 1;
815 buffer0[s_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200816 expected_status = PSA_ERROR_DATA_INVALID;
817 }
818
Neil Armstrong51009d72022-09-05 17:59:54 +0200819 /*
820 * When injecting errors in inputs, the implementation is
821 * free to detect it right away of with a delay.
822 * This permits delaying the error until the end of the input
823 * sequence, if no error appears then, this will be treated
824 * as an error.
825 */
826
Neil Armstrongf983caf2022-06-15 15:27:48 +0200827 if( client_input_first == 1 )
828 {
829 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200830 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
831 buffer0 + s_g1_off, s_g1_len );
832 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200833 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200834 TEST_EQUAL( status, expected_status );
835 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200836 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200837 else
838 {
839 TEST_EQUAL( status, PSA_SUCCESS );
840 }
841
842 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
843 buffer0 + s_x1_pk_off,
844 s_x1_pk_len );
845 if( inject_error == 1 && status != PSA_SUCCESS )
846 {
847 TEST_EQUAL( status, expected_status );
848 break;
849 }
850 else
851 {
852 TEST_EQUAL( status, PSA_SUCCESS );
853 }
854
855 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
856 buffer0 + s_x1_pr_off,
857 s_x1_pr_len );
858 if( inject_error == 1 && status != PSA_SUCCESS )
859 {
860 TEST_EQUAL( status, expected_status );
861 break;
862 }
863 else
864 {
865 TEST_EQUAL( status, PSA_SUCCESS );
866 }
867
868 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
869 buffer0 + s_g2_off,
870 s_g2_len );
871 if( inject_error == 1 && status != PSA_SUCCESS )
872 {
873 TEST_EQUAL( status, expected_status );
874 break;
875 }
876 else
877 {
878 TEST_EQUAL( status, PSA_SUCCESS );
879 }
880
881 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
882 buffer0 + s_x2_pk_off,
883 s_x2_pk_len );
884 if( inject_error == 1 && status != PSA_SUCCESS )
885 {
886 TEST_EQUAL( status, expected_status );
887 break;
888 }
889 else
890 {
891 TEST_EQUAL( status, PSA_SUCCESS );
892 }
893
894 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
895 buffer0 + s_x2_pr_off,
896 s_x2_pr_len );
897 if( inject_error == 1 && status != PSA_SUCCESS )
898 {
899 TEST_EQUAL( status, expected_status );
900 break;
901 }
902 else
903 {
904 TEST_EQUAL( status, PSA_SUCCESS );
905 }
906
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200907 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200908 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200909 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200910 }
911
912 /* Client first round Output */
913 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
914 buffer1 + buffer1_off,
915 512 - buffer1_off, &c_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200916 TEST_EQUAL( c_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200917 c_g1_off = buffer1_off;
918 buffer1_off += c_g1_len;
919 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
920 buffer1 + buffer1_off,
921 512 - buffer1_off, &c_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200922 TEST_EQUAL( c_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200923 c_x1_pk_off = buffer1_off;
924 buffer1_off += c_x1_pk_len;
925 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
926 buffer1 + buffer1_off,
927 512 - buffer1_off, &c_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200928 TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200929 c_x1_pr_off = buffer1_off;
930 buffer1_off += c_x1_pr_len;
931 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
932 buffer1 + buffer1_off,
933 512 - buffer1_off, &c_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200934 TEST_EQUAL( c_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200935 c_g2_off = buffer1_off;
936 buffer1_off += c_g2_len;
937 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
938 buffer1 + buffer1_off,
939 512 - buffer1_off, &c_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200940 TEST_EQUAL( c_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200941 c_x2_pk_off = buffer1_off;
942 buffer1_off += c_x2_pk_len;
943 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
944 buffer1 + buffer1_off,
945 512 - buffer1_off, &c_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200946 TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200947 c_x2_pr_off = buffer1_off;
948 buffer1_off += c_x2_pr_len;
949
950 if( client_input_first == 0 )
951 {
952 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200953 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
954 buffer0 + s_g1_off, s_g1_len );
955 if( inject_error == 1 && status != PSA_SUCCESS )
956 {
957 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200958 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200959 }
960 else
961 {
962 TEST_EQUAL( status, PSA_SUCCESS );
963 }
964
965 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
966 buffer0 + s_x1_pk_off,
967 s_x1_pk_len );
968 if( inject_error == 1 && status != PSA_SUCCESS )
969 {
970 TEST_EQUAL( status, expected_status );
971 break;
972 }
973 else
974 {
975 TEST_EQUAL( status, PSA_SUCCESS );
976 }
977
978 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
979 buffer0 + s_x1_pr_off,
980 s_x1_pr_len );
981 if( inject_error == 1 && status != PSA_SUCCESS )
982 {
983 TEST_EQUAL( status, expected_status );
984 break;
985 }
986 else
987 {
988 TEST_EQUAL( status, PSA_SUCCESS );
989 }
990
991 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
992 buffer0 + s_g2_off,
993 s_g2_len );
994 if( inject_error == 1 && status != PSA_SUCCESS )
995 {
996 TEST_EQUAL( status, expected_status );
997 break;
998 }
999 else
1000 {
1001 TEST_EQUAL( status, PSA_SUCCESS );
1002 }
1003
1004 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1005 buffer0 + s_x2_pk_off,
1006 s_x2_pk_len );
1007 if( inject_error == 1 && status != PSA_SUCCESS )
1008 {
1009 TEST_EQUAL( status, expected_status );
1010 break;
1011 }
1012 else
1013 {
1014 TEST_EQUAL( status, PSA_SUCCESS );
1015 }
1016
1017 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1018 buffer0 + s_x2_pr_off,
1019 s_x2_pr_len );
1020 if( inject_error == 1 && status != PSA_SUCCESS )
1021 {
1022 TEST_EQUAL( status, expected_status );
1023 break;
1024 }
1025 else
1026 {
1027 TEST_EQUAL( status, PSA_SUCCESS );
1028 }
1029
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001030 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001031 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001032 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001033 }
1034
1035 if( inject_error == 2 )
1036 {
Andrzej Kurekc0182042022-11-08 08:12:56 -05001037 buffer1[c_x1_pr_off + 12] ^= 1;
1038 buffer1[c_x2_pr_off + 7] ^= 1;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001039 expected_status = PSA_ERROR_DATA_INVALID;
1040 }
1041
1042 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001043 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1044 buffer1 + c_g1_off, c_g1_len );
1045 if( inject_error == 2 && status != PSA_SUCCESS )
1046 {
1047 TEST_EQUAL( status, expected_status );
1048 break;
1049 }
1050 else
1051 {
1052 TEST_EQUAL( status, PSA_SUCCESS );
1053 }
1054
1055 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1056 buffer1 + c_x1_pk_off, c_x1_pk_len );
1057 if( inject_error == 2 && status != PSA_SUCCESS )
1058 {
1059 TEST_EQUAL( status, expected_status );
1060 break;
1061 }
1062 else
1063 {
1064 TEST_EQUAL( status, PSA_SUCCESS );
1065 }
1066
1067 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1068 buffer1 + c_x1_pr_off, c_x1_pr_len );
1069 if( inject_error == 2 && status != PSA_SUCCESS )
1070 {
1071 TEST_EQUAL( status, expected_status );
1072 break;
1073 }
1074 else
1075 {
1076 TEST_EQUAL( status, PSA_SUCCESS );
1077 }
1078
1079 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1080 buffer1 + c_g2_off, c_g2_len );
1081 if( inject_error == 2 && status != PSA_SUCCESS )
1082 {
1083 TEST_EQUAL( status, expected_status );
1084 break;
1085 }
1086 else
1087 {
1088 TEST_EQUAL( status, PSA_SUCCESS );
1089 }
1090
1091 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1092 buffer1 + c_x2_pk_off, c_x2_pk_len );
1093 if( inject_error == 2 && status != PSA_SUCCESS )
1094 {
1095 TEST_EQUAL( status, expected_status );
1096 break;
1097 }
1098 else
1099 {
1100 TEST_EQUAL( status, PSA_SUCCESS );
1101 }
1102
1103 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1104 buffer1 + c_x2_pr_off, c_x2_pr_len );
1105 if( inject_error == 2 && status != PSA_SUCCESS )
1106 {
1107 TEST_EQUAL( status, expected_status );
1108 break;
1109 }
1110 else
1111 {
1112 TEST_EQUAL( status, PSA_SUCCESS );
1113 }
1114
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001115 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001116 if( inject_error == 2 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001117 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001118
1119 break;
1120
1121 case 2:
1122 /* Server second round Output */
1123 buffer0_off = 0;
1124
1125 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1126 buffer0 + buffer0_off,
1127 512 - buffer0_off, &s_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001128 TEST_EQUAL( s_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001129 s_a_off = buffer0_off;
1130 buffer0_off += s_a_len;
1131 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1132 buffer0 + buffer0_off,
1133 512 - buffer0_off, &s_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001134 TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001135 s_x2s_pk_off = buffer0_off;
1136 buffer0_off += s_x2s_pk_len;
1137 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1138 buffer0 + buffer0_off,
1139 512 - buffer0_off, &s_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001140 TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001141 s_x2s_pr_off = buffer0_off;
1142 buffer0_off += s_x2s_pr_len;
1143
1144 if( inject_error == 3 )
1145 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001146 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001147 expected_status = PSA_ERROR_DATA_INVALID;
1148 }
1149
1150 if( client_input_first == 1 )
1151 {
1152 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001153 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1154 buffer0 + s_a_off, s_a_len );
1155 if( inject_error == 3 && status != PSA_SUCCESS )
1156 {
1157 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001158 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001159 }
1160 else
1161 {
1162 TEST_EQUAL( status, PSA_SUCCESS );
1163 }
1164
1165 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1166 buffer0 + s_x2s_pk_off,
1167 s_x2s_pk_len );
1168 if( inject_error == 3 && status != PSA_SUCCESS )
1169 {
1170 TEST_EQUAL( status, expected_status );
1171 break;
1172 }
1173 else
1174 {
1175 TEST_EQUAL( status, PSA_SUCCESS );
1176 }
1177
1178 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1179 buffer0 + s_x2s_pr_off,
1180 s_x2s_pr_len );
1181 if( inject_error == 3 && status != PSA_SUCCESS )
1182 {
1183 TEST_EQUAL( status, expected_status );
1184 break;
1185 }
1186 else
1187 {
1188 TEST_EQUAL( status, PSA_SUCCESS );
1189 }
1190
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001191 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001192 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001193 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001194 }
1195
1196 /* Client second round Output */
1197 buffer1_off = 0;
1198
1199 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1200 buffer1 + buffer1_off,
1201 512 - buffer1_off, &c_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001202 TEST_EQUAL( c_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001203 c_a_off = buffer1_off;
1204 buffer1_off += c_a_len;
1205 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1206 buffer1 + buffer1_off,
1207 512 - buffer1_off, &c_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001208 TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001209 c_x2s_pk_off = buffer1_off;
1210 buffer1_off += c_x2s_pk_len;
1211 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1212 buffer1 + buffer1_off,
1213 512 - buffer1_off, &c_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001214 TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001215 c_x2s_pr_off = buffer1_off;
1216 buffer1_off += c_x2s_pr_len;
1217
1218 if( client_input_first == 0 )
1219 {
1220 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001221 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1222 buffer0 + s_a_off, s_a_len );
1223 if( inject_error == 3 && status != PSA_SUCCESS )
1224 {
1225 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001226 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001227 }
1228 else
1229 {
1230 TEST_EQUAL( status, PSA_SUCCESS );
1231 }
1232
1233 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1234 buffer0 + s_x2s_pk_off,
1235 s_x2s_pk_len );
1236 if( inject_error == 3 && status != PSA_SUCCESS )
1237 {
1238 TEST_EQUAL( status, expected_status );
1239 break;
1240 }
1241 else
1242 {
1243 TEST_EQUAL( status, PSA_SUCCESS );
1244 }
1245
1246 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1247 buffer0 + s_x2s_pr_off,
1248 s_x2s_pr_len );
1249 if( inject_error == 3 && 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 == 3 )
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
1264 if( inject_error == 4 )
1265 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001266 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001267 expected_status = PSA_ERROR_DATA_INVALID;
1268 }
1269
1270 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001271 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1272 buffer1 + c_a_off, c_a_len );
1273 if( inject_error == 4 && status != PSA_SUCCESS )
1274 {
1275 TEST_EQUAL( status, expected_status );
1276 break;
1277 }
1278 else
1279 {
1280 TEST_EQUAL( status, PSA_SUCCESS );
1281 }
1282
1283 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1284 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1285 if( inject_error == 4 && status != PSA_SUCCESS )
1286 {
1287 TEST_EQUAL( status, expected_status );
1288 break;
1289 }
1290 else
1291 {
1292 TEST_EQUAL( status, PSA_SUCCESS );
1293 }
1294
1295 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1296 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1297 if( inject_error == 4 && status != PSA_SUCCESS )
1298 {
1299 TEST_EQUAL( status, expected_status );
1300 break;
1301 }
1302 else
1303 {
1304 TEST_EQUAL( status, PSA_SUCCESS );
1305 }
1306
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001307 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001308 if( inject_error == 4 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001309 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001310
1311 break;
1312
1313 }
1314
Neil Armstrongf983caf2022-06-15 15:27:48 +02001315exit:
1316 mbedtls_free( buffer0 );
1317 mbedtls_free( buffer1 );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001318}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001319#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001320
Gilles Peskinee59236f2018-01-27 23:32:46 +01001321/* END_HEADER */
1322
1323/* BEGIN_DEPENDENCIES
1324 * depends_on:MBEDTLS_PSA_CRYPTO_C
1325 * END_DEPENDENCIES
1326 */
1327
1328/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001329void static_checks( )
1330{
1331 size_t max_truncated_mac_size =
1332 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1333
1334 /* Check that the length for a truncated MAC always fits in the algorithm
1335 * encoding. The shifted mask is the maximum truncated value. The
1336 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001337 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001338}
1339/* END_CASE */
1340
1341/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001342void import_with_policy( int type_arg,
1343 int usage_arg, int alg_arg,
1344 int expected_status_arg )
1345{
1346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1347 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001349 psa_key_type_t type = type_arg;
1350 psa_key_usage_t usage = usage_arg;
1351 psa_algorithm_t alg = alg_arg;
1352 psa_status_t expected_status = expected_status_arg;
1353 const uint8_t key_material[16] = {0};
1354 psa_status_t status;
1355
1356 PSA_ASSERT( psa_crypto_init( ) );
1357
1358 psa_set_key_type( &attributes, type );
1359 psa_set_key_usage_flags( &attributes, usage );
1360 psa_set_key_algorithm( &attributes, alg );
1361
1362 status = psa_import_key( &attributes,
1363 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001364 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001365 TEST_EQUAL( status, expected_status );
1366 if( status != PSA_SUCCESS )
1367 goto exit;
1368
Ronald Cron5425a212020-08-04 14:58:35 +02001369 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001370 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001371 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001372 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001373 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001374 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001375
Ronald Cron5425a212020-08-04 14:58:35 +02001376 PSA_ASSERT( psa_destroy_key( key ) );
1377 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001378
1379exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001380 /*
1381 * Key attributes may have been returned by psa_get_key_attributes()
1382 * thus reset them as required.
1383 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001384 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001385
1386 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001387 PSA_DONE( );
1388}
1389/* END_CASE */
1390
1391/* BEGIN_CASE */
1392void import_with_data( data_t *data, int type_arg,
1393 int attr_bits_arg,
1394 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001395{
1396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1397 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001398 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001399 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001400 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001401 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001402 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403
Gilles Peskine8817f612018-12-18 00:18:46 +01001404 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001405
Gilles Peskine4747d192019-04-17 15:05:45 +02001406 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001407 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001408
Ronald Cron5425a212020-08-04 14:58:35 +02001409 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001410 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001411 if( status != PSA_SUCCESS )
1412 goto exit;
1413
Ronald Cron5425a212020-08-04 14:58:35 +02001414 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001415 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001416 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001417 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001418 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001419
Ronald Cron5425a212020-08-04 14:58:35 +02001420 PSA_ASSERT( psa_destroy_key( key ) );
1421 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001422
1423exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001424 /*
1425 * Key attributes may have been returned by psa_get_key_attributes()
1426 * thus reset them as required.
1427 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001428 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001429
1430 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001431 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432}
1433/* END_CASE */
1434
1435/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001436void import_large_key( int type_arg, int byte_size_arg,
1437 int expected_status_arg )
1438{
1439 psa_key_type_t type = type_arg;
1440 size_t byte_size = byte_size_arg;
1441 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1442 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001443 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001444 psa_status_t status;
1445 uint8_t *buffer = NULL;
1446 size_t buffer_size = byte_size + 1;
1447 size_t n;
1448
Steven Cooreman69967ce2021-01-18 18:01:08 +01001449 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001450 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001451 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001452 memset( buffer, 'K', byte_size );
1453
1454 PSA_ASSERT( psa_crypto_init( ) );
1455
1456 /* Try importing the key */
1457 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1458 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001459 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001460 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001461 TEST_EQUAL( status, expected_status );
1462
1463 if( status == PSA_SUCCESS )
1464 {
Ronald Cron5425a212020-08-04 14:58:35 +02001465 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001466 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1467 TEST_EQUAL( psa_get_key_bits( &attributes ),
1468 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001469 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001470 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001471 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001472 for( n = 0; n < byte_size; n++ )
1473 TEST_EQUAL( buffer[n], 'K' );
1474 for( n = byte_size; n < buffer_size; n++ )
1475 TEST_EQUAL( buffer[n], 0 );
1476 }
1477
1478exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001479 /*
1480 * Key attributes may have been returned by psa_get_key_attributes()
1481 * thus reset them as required.
1482 */
1483 psa_reset_key_attributes( &attributes );
1484
Ronald Cron5425a212020-08-04 14:58:35 +02001485 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001486 PSA_DONE( );
1487 mbedtls_free( buffer );
1488}
1489/* END_CASE */
1490
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001491/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001492void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1493{
Ronald Cron5425a212020-08-04 14:58:35 +02001494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001495 size_t bits = bits_arg;
1496 psa_status_t expected_status = expected_status_arg;
1497 psa_status_t status;
1498 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001499 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001500 size_t buffer_size = /* Slight overapproximations */
1501 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001502 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001503 unsigned char *p;
1504 int ret;
1505 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001507
Gilles Peskine8817f612018-12-18 00:18:46 +01001508 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001509 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001510
1511 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1512 bits, keypair ) ) >= 0 );
1513 length = ret;
1514
1515 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001516 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001517 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001518 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001519
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001520 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001521 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001522
1523exit:
1524 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001525 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001526}
1527/* END_CASE */
1528
1529/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001530void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001531 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001532 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301533 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001534 int expected_bits,
1535 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001536 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001537 int canonical_input )
1538{
Ronald Cron5425a212020-08-04 14:58:35 +02001539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001541 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001542 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001543 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301544 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545 unsigned char *exported = NULL;
1546 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001547 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001548 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001549 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001551 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001552
Moran Pekercb088e72018-07-17 17:36:59 +03001553 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001554 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001556 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558
Archana4d7ae1d2021-07-07 02:50:22 +05301559 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 psa_set_key_usage_flags( &attributes, usage_arg );
1561 psa_set_key_algorithm( &attributes, alg );
1562 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001563
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001564 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001565 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566
1567 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001568 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001569 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1570 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001571 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572
1573 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001574 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001575 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001576
1577 /* The exported length must be set by psa_export_key() to a value between 0
1578 * and export_size. On errors, the exported length must be 0. */
1579 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1580 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001581 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001582
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001583 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001584 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001585 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001586 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001587 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001588 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001589 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590
Gilles Peskineea38a922021-02-13 00:05:16 +01001591 /* Run sanity checks on the exported key. For non-canonical inputs,
1592 * this validates the canonical representations. For canonical inputs,
1593 * this doesn't directly validate the implementation, but it still helps
1594 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301595 if( !psa_key_lifetime_is_external( lifetime ) )
1596 {
1597 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1598 goto exit;
1599 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001600
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001602 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603 else
1604 {
Ronald Cron5425a212020-08-04 14:58:35 +02001605 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001606 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001607 &key2 ) );
1608 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 reexported,
1610 export_size,
1611 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001612 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301613 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001614 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001616 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301617 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301618 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001619 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001620
1621destroy:
1622 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001623 PSA_ASSERT( psa_destroy_key( key ) );
1624 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625
1626exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001627 /*
1628 * Key attributes may have been returned by psa_get_key_attributes()
1629 * thus reset them as required.
1630 */
1631 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301632 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001633 mbedtls_free( exported );
1634 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001635 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001636}
1637/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001638
Moran Pekerf709f4a2018-06-06 17:26:04 +03001639/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001640void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001641 int type_arg,
1642 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301643 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001644 int export_size_delta,
1645 int expected_export_status_arg,
1646 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001647{
Ronald Cron5425a212020-08-04 14:58:35 +02001648 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001649 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001650 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001651 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001652 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301653 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001654 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001655 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001656 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001658
Gilles Peskine8817f612018-12-18 00:18:46 +01001659 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001660
Archana4d7ae1d2021-07-07 02:50:22 +05301661 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001662 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1663 psa_set_key_algorithm( &attributes, alg );
1664 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001665
1666 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001667 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001668
Gilles Peskine49c25912018-10-29 15:15:31 +01001669 /* Export the public key */
1670 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001671 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001672 exported, export_size,
1673 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001674 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001675 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001676 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001677 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001678 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001680 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001681 TEST_LE_U( expected_public_key->len,
1682 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1683 TEST_LE_U( expected_public_key->len,
1684 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1685 TEST_LE_U( expected_public_key->len,
1686 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001687 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1688 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001689 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001690exit:
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 */
1695 psa_reset_key_attributes( &attributes );
1696
itayzafrir3e02b3b2018-06-12 17:06:52 +03001697 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001698 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001699 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001700}
1701/* END_CASE */
1702
Gilles Peskine20035e32018-02-03 22:44:14 +01001703/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001704void import_and_exercise_key( data_t *data,
1705 int type_arg,
1706 int bits_arg,
1707 int alg_arg )
1708{
Ronald Cron5425a212020-08-04 14:58:35 +02001709 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001710 psa_key_type_t type = type_arg;
1711 size_t bits = bits_arg;
1712 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001713 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001715 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001716
Gilles Peskine8817f612018-12-18 00:18:46 +01001717 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001718
Gilles Peskine4747d192019-04-17 15:05:45 +02001719 psa_set_key_usage_flags( &attributes, usage );
1720 psa_set_key_algorithm( &attributes, alg );
1721 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001722
1723 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001724 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001725
1726 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001727 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001728 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1729 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001730
1731 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001732 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001733 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001734
Ronald Cron5425a212020-08-04 14:58:35 +02001735 PSA_ASSERT( psa_destroy_key( key ) );
1736 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001737
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001738exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001739 /*
1740 * Key attributes may have been returned by psa_get_key_attributes()
1741 * thus reset them as required.
1742 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001743 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001744
1745 psa_reset_key_attributes( &attributes );
1746 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001747 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001752void effective_key_attributes( int type_arg, int expected_type_arg,
1753 int bits_arg, int expected_bits_arg,
1754 int usage_arg, int expected_usage_arg,
1755 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001756{
Ronald Cron5425a212020-08-04 14:58:35 +02001757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001758 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001759 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001760 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001761 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001762 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001763 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001764 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001765 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001767
Gilles Peskine8817f612018-12-18 00:18:46 +01001768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001769
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001770 psa_set_key_usage_flags( &attributes, usage );
1771 psa_set_key_algorithm( &attributes, alg );
1772 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001773 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001774
Ronald Cron5425a212020-08-04 14:58:35 +02001775 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001776 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001777
Ronald Cron5425a212020-08-04 14:58:35 +02001778 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001779 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1780 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1781 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1782 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001783
1784exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001785 /*
1786 * Key attributes may have been returned by psa_get_key_attributes()
1787 * thus reset them as required.
1788 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001789 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001790
1791 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001792 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001793}
1794/* END_CASE */
1795
1796/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001797void check_key_policy( int type_arg, int bits_arg,
1798 int usage_arg, int alg_arg )
1799{
1800 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001801 usage_arg,
1802 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001803 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001804 goto exit;
1805}
1806/* END_CASE */
1807
1808/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001809void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001810{
1811 /* Test each valid way of initializing the object, except for `= {0}`, as
1812 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1813 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001814 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001815 psa_key_attributes_t func = psa_key_attributes_init( );
1816 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1817 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001818
1819 memset( &zero, 0, sizeof( zero ) );
1820
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001821 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1822 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1823 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001824
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001825 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1826 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1827 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1828
1829 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1830 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1831 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1832
1833 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1834 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1835 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1836
1837 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1838 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1839 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001840}
1841/* END_CASE */
1842
1843/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001844void mac_key_policy( int policy_usage_arg,
1845 int policy_alg_arg,
1846 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001848 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001849 int expected_status_sign_arg,
1850 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001851{
Ronald Cron5425a212020-08-04 14:58:35 +02001852 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001854 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001855 psa_key_type_t key_type = key_type_arg;
1856 psa_algorithm_t policy_alg = policy_alg_arg;
1857 psa_algorithm_t exercise_alg = exercise_alg_arg;
1858 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001859 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001860 psa_status_t expected_status_sign = expected_status_sign_arg;
1861 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001863
Gilles Peskine8817f612018-12-18 00:18:46 +01001864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001865
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001866 psa_set_key_usage_flags( &attributes, policy_usage );
1867 psa_set_key_algorithm( &attributes, policy_alg );
1868 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001869
Gilles Peskine049c7532019-05-15 20:22:09 +02001870 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001871 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001872
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001873 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1874 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001875
Ronald Cron5425a212020-08-04 14:58:35 +02001876 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001877 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001878
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001879 /* Calculate the MAC, one-shot case. */
1880 uint8_t input[128] = {0};
1881 size_t mac_len;
1882 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1883 input, 128,
1884 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1885 expected_status_sign );
1886
Neil Armstrong3af9b972022-02-07 12:20:21 +01001887 /* Calculate the MAC, multi-part case. */
1888 PSA_ASSERT( psa_mac_abort( &operation ) );
1889 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1890 if( status == PSA_SUCCESS )
1891 {
1892 status = psa_mac_update( &operation, input, 128 );
1893 if( status == PSA_SUCCESS )
1894 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1895 &mac_len ),
1896 expected_status_sign );
1897 else
1898 TEST_EQUAL( status, expected_status_sign );
1899 }
1900 else
1901 {
1902 TEST_EQUAL( status, expected_status_sign );
1903 }
1904 PSA_ASSERT( psa_mac_abort( &operation ) );
1905
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001906 /* Verify correct MAC, one-shot case. */
1907 status = psa_mac_verify( key, exercise_alg, input, 128,
1908 mac, mac_len );
1909
1910 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1911 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001912 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001913 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001914
Neil Armstrong3af9b972022-02-07 12:20:21 +01001915 /* Verify correct MAC, multi-part case. */
1916 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1917 if( status == PSA_SUCCESS )
1918 {
1919 status = psa_mac_update( &operation, input, 128 );
1920 if( status == PSA_SUCCESS )
1921 {
1922 status = psa_mac_verify_finish( &operation, mac, mac_len );
1923 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1924 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1925 else
1926 TEST_EQUAL( status, expected_status_verify );
1927 }
1928 else
1929 {
1930 TEST_EQUAL( status, expected_status_verify );
1931 }
1932 }
1933 else
1934 {
1935 TEST_EQUAL( status, expected_status_verify );
1936 }
1937
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001938 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001939
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001940 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001941 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001942 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943
1944exit:
1945 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001946 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001947 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001948}
1949/* END_CASE */
1950
1951/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001952void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953 int policy_alg,
1954 int key_type,
1955 data_t *key_data,
1956 int exercise_alg )
1957{
Ronald Cron5425a212020-08-04 14:58:35 +02001958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001960 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001961 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001962 size_t output_buffer_size = 0;
1963 size_t input_buffer_size = 0;
1964 size_t output_length = 0;
1965 uint8_t *output = NULL;
1966 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967 psa_status_t status;
1968
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001969 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1970 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1971 input_buffer_size );
1972
1973 ASSERT_ALLOC( input, input_buffer_size );
1974 ASSERT_ALLOC( output, output_buffer_size );
1975
Gilles Peskine8817f612018-12-18 00:18:46 +01001976 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001978 psa_set_key_usage_flags( &attributes, policy_usage );
1979 psa_set_key_algorithm( &attributes, policy_alg );
1980 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001981
Gilles Peskine049c7532019-05-15 20:22:09 +02001982 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001983 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001985 /* Check if no key usage flag implication is done */
1986 TEST_EQUAL( policy_usage,
1987 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001988
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001989 /* Encrypt check, one-shot */
1990 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1991 output, output_buffer_size,
1992 &output_length);
1993 if( policy_alg == exercise_alg &&
1994 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1995 PSA_ASSERT( status );
1996 else
1997 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1998
1999 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002000 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002001 if( policy_alg == exercise_alg &&
2002 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002003 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002005 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006 psa_cipher_abort( &operation );
2007
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002008 /* Decrypt check, one-shot */
2009 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
2010 input, input_buffer_size,
2011 &output_length);
2012 if( policy_alg == exercise_alg &&
2013 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
2014 PSA_ASSERT( status );
2015 else
2016 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2017
2018 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002019 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020 if( policy_alg == exercise_alg &&
2021 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002022 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002024 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002025
2026exit:
2027 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002028 mbedtls_free( input );
2029 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002030 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002031 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032}
2033/* END_CASE */
2034
2035/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002036void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037 int policy_alg,
2038 int key_type,
2039 data_t *key_data,
2040 int nonce_length_arg,
2041 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002042 int exercise_alg,
2043 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044{
Ronald Cron5425a212020-08-04 14:58:35 +02002045 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002047 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002048 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002049 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002050 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002051 unsigned char nonce[16] = {0};
2052 size_t nonce_length = nonce_length_arg;
2053 unsigned char tag[16];
2054 size_t tag_length = tag_length_arg;
2055 size_t output_length;
2056
Gilles Peskine7be11a72022-04-14 00:12:57 +02002057 TEST_LE_U( nonce_length, sizeof( nonce ) );
2058 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002059
Gilles Peskine8817f612018-12-18 00:18:46 +01002060 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002062 psa_set_key_usage_flags( &attributes, policy_usage );
2063 psa_set_key_algorithm( &attributes, policy_alg );
2064 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002065
Gilles Peskine049c7532019-05-15 20:22:09 +02002066 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002067 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002069 /* Check if no key usage implication is done */
2070 TEST_EQUAL( policy_usage,
2071 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002072
Neil Armstrong752d8112022-02-07 14:51:11 +01002073 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002074 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075 nonce, nonce_length,
2076 NULL, 0,
2077 NULL, 0,
2078 tag, tag_length,
2079 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002080 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2081 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002082 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002083 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
Neil Armstrong752d8112022-02-07 14:51:11 +01002085 /* Encrypt check, multi-part */
2086 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2087 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2088 TEST_EQUAL( status, expected_status );
2089 else
2090 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2091
2092 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002094 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002095 nonce, nonce_length,
2096 NULL, 0,
2097 tag, tag_length,
2098 NULL, 0,
2099 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002100 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2101 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2102 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002103 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002105 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002106
Neil Armstrong752d8112022-02-07 14:51:11 +01002107 /* Decrypt check, multi-part */
2108 PSA_ASSERT( psa_aead_abort( &operation ) );
2109 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2110 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2111 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2112 else
2113 TEST_EQUAL( status, expected_status );
2114
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002116 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002117 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002118 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002119}
2120/* END_CASE */
2121
2122/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002123void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124 int policy_alg,
2125 int key_type,
2126 data_t *key_data,
2127 int exercise_alg )
2128{
Ronald Cron5425a212020-08-04 14:58:35 +02002129 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002130 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002131 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132 psa_status_t status;
2133 size_t key_bits;
2134 size_t buffer_length;
2135 unsigned char *buffer = NULL;
2136 size_t output_length;
2137
Gilles Peskine8817f612018-12-18 00:18:46 +01002138 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002140 psa_set_key_usage_flags( &attributes, policy_usage );
2141 psa_set_key_algorithm( &attributes, policy_alg );
2142 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143
Gilles Peskine049c7532019-05-15 20:22:09 +02002144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002145 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002146
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002147 /* Check if no key usage implication is done */
2148 TEST_EQUAL( policy_usage,
2149 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002150
Ronald Cron5425a212020-08-04 14:58:35 +02002151 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002152 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002153 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2154 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002155 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156
Ronald Cron5425a212020-08-04 14:58:35 +02002157 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158 NULL, 0,
2159 NULL, 0,
2160 buffer, buffer_length,
2161 &output_length );
2162 if( policy_alg == exercise_alg &&
2163 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002164 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002165 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002166 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002168 if( buffer_length != 0 )
2169 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002170 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002171 buffer, buffer_length,
2172 NULL, 0,
2173 buffer, buffer_length,
2174 &output_length );
2175 if( policy_alg == exercise_alg &&
2176 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002177 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002178 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002179 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180
2181exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002182 /*
2183 * Key attributes may have been returned by psa_get_key_attributes()
2184 * thus reset them as required.
2185 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002186 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002187
2188 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002189 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002190 mbedtls_free( buffer );
2191}
2192/* END_CASE */
2193
2194/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002195void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196 int policy_alg,
2197 int key_type,
2198 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002199 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002200 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002201 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002202{
Ronald Cron5425a212020-08-04 14:58:35 +02002203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002204 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002205 psa_key_usage_t policy_usage = policy_usage_arg;
2206 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002207 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002208 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2209 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2210 * compatible with the policy and `payload_length_arg` is supposed to be
2211 * a valid input length to sign. If `payload_length_arg <= 0`,
2212 * `exercise_alg` is supposed to be forbidden by the policy. */
2213 int compatible_alg = payload_length_arg > 0;
2214 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002215 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002216 size_t signature_length;
2217
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002218 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002219 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002220 TEST_EQUAL( expected_usage,
2221 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002222
Gilles Peskine8817f612018-12-18 00:18:46 +01002223 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002224
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002225 psa_set_key_usage_flags( &attributes, policy_usage );
2226 psa_set_key_algorithm( &attributes, policy_alg );
2227 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002228
Gilles Peskine049c7532019-05-15 20:22:09 +02002229 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002230 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002231
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002232 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2233
Ronald Cron5425a212020-08-04 14:58:35 +02002234 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002235 payload, payload_length,
2236 signature, sizeof( signature ),
2237 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002238 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002240 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002241 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002242
2243 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002244 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002245 payload, payload_length,
2246 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002247 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002248 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002249 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002250 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002251
Gilles Peskinef7b41372021-09-22 16:15:05 +02002252 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002253 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002254 {
2255 status = psa_sign_message( key, exercise_alg,
2256 payload, payload_length,
2257 signature, sizeof( signature ),
2258 &signature_length );
2259 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2260 PSA_ASSERT( status );
2261 else
2262 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2263
2264 memset( signature, 0, sizeof( signature ) );
2265 status = psa_verify_message( key, exercise_alg,
2266 payload, payload_length,
2267 signature, sizeof( signature ) );
2268 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2269 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2270 else
2271 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2272 }
2273
Gilles Peskined5b33222018-06-18 22:20:03 +02002274exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002275 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002276 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002277}
2278/* END_CASE */
2279
Janos Follathba3fab92019-06-11 14:50:16 +01002280/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002281void derive_key_policy( int policy_usage,
2282 int policy_alg,
2283 int key_type,
2284 data_t *key_data,
2285 int exercise_alg )
2286{
Ronald Cron5425a212020-08-04 14:58:35 +02002287 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002289 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002290 psa_status_t status;
2291
Gilles Peskine8817f612018-12-18 00:18:46 +01002292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002293
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002294 psa_set_key_usage_flags( &attributes, policy_usage );
2295 psa_set_key_algorithm( &attributes, policy_alg );
2296 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002297
Gilles Peskine049c7532019-05-15 20:22:09 +02002298 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002299 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002300
Janos Follathba3fab92019-06-11 14:50:16 +01002301 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2302
2303 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2304 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002305 {
Janos Follathba3fab92019-06-11 14:50:16 +01002306 PSA_ASSERT( psa_key_derivation_input_bytes(
2307 &operation,
2308 PSA_KEY_DERIVATION_INPUT_SEED,
2309 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002310 }
Janos Follathba3fab92019-06-11 14:50:16 +01002311
2312 status = psa_key_derivation_input_key( &operation,
2313 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002314 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002315
Gilles Peskineea0fb492018-07-12 17:17:20 +02002316 if( policy_alg == exercise_alg &&
2317 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002318 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002319 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002320 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002321
2322exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002323 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002324 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002325 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002326}
2327/* END_CASE */
2328
2329/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002330void agreement_key_policy( int policy_usage,
2331 int policy_alg,
2332 int key_type_arg,
2333 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002334 int exercise_alg,
2335 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002336{
Ronald Cron5425a212020-08-04 14:58:35 +02002337 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002339 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002340 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002341 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002342 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002343
Gilles Peskine8817f612018-12-18 00:18:46 +01002344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002345
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002346 psa_set_key_usage_flags( &attributes, policy_usage );
2347 psa_set_key_algorithm( &attributes, policy_alg );
2348 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002349
Gilles Peskine049c7532019-05-15 20:22:09 +02002350 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002351 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002352
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002353 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002354 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002355
Steven Cooremance48e852020-10-05 16:02:45 +02002356 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002357
2358exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002359 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002360 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002361 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002362}
2363/* END_CASE */
2364
2365/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002366void key_policy_alg2( int key_type_arg, data_t *key_data,
2367 int usage_arg, int alg_arg, int alg2_arg )
2368{
Ronald Cron5425a212020-08-04 14:58:35 +02002369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002370 psa_key_type_t key_type = key_type_arg;
2371 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2372 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2373 psa_key_usage_t usage = usage_arg;
2374 psa_algorithm_t alg = alg_arg;
2375 psa_algorithm_t alg2 = alg2_arg;
2376
2377 PSA_ASSERT( psa_crypto_init( ) );
2378
2379 psa_set_key_usage_flags( &attributes, usage );
2380 psa_set_key_algorithm( &attributes, alg );
2381 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2382 psa_set_key_type( &attributes, key_type );
2383 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002384 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002385
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002386 /* Update the usage flags to obtain implicit usage flags */
2387 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002388 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002389 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2390 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2391 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2392
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002393 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002394 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002395 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002396 goto exit;
2397
2398exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002399 /*
2400 * Key attributes may have been returned by psa_get_key_attributes()
2401 * thus reset them as required.
2402 */
2403 psa_reset_key_attributes( &got_attributes );
2404
Ronald Cron5425a212020-08-04 14:58:35 +02002405 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002406 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002407}
2408/* END_CASE */
2409
2410/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002411void raw_agreement_key_policy( int policy_usage,
2412 int policy_alg,
2413 int key_type_arg,
2414 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002415 int exercise_alg,
2416 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002417{
Ronald Cron5425a212020-08-04 14:58:35 +02002418 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002420 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002421 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002422 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002423 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002424
2425 PSA_ASSERT( psa_crypto_init( ) );
2426
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002427 psa_set_key_usage_flags( &attributes, policy_usage );
2428 psa_set_key_algorithm( &attributes, policy_alg );
2429 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002430
Gilles Peskine049c7532019-05-15 20:22:09 +02002431 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002432 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002433
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002434 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002435
Steven Cooremance48e852020-10-05 16:02:45 +02002436 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002437
2438exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002439 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002440 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002441 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002442}
2443/* END_CASE */
2444
2445/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002446void copy_success( int source_usage_arg,
2447 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302448 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002449 int type_arg, data_t *material,
2450 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002451 int target_usage_arg,
2452 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302453 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002454 int expected_usage_arg,
2455 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002456{
Gilles Peskineca25db92019-04-19 11:43:08 +02002457 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2458 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002459 psa_key_usage_t expected_usage = expected_usage_arg;
2460 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002461 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302462 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2463 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002464 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2465 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002466 uint8_t *export_buffer = NULL;
2467
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468 PSA_ASSERT( psa_crypto_init( ) );
2469
Gilles Peskineca25db92019-04-19 11:43:08 +02002470 /* Prepare the source key. */
2471 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2472 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002473 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002474 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302475 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002476 PSA_ASSERT( psa_import_key( &source_attributes,
2477 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002478 &source_key ) );
2479 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002480
Gilles Peskineca25db92019-04-19 11:43:08 +02002481 /* Prepare the target attributes. */
2482 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002483 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002484 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002485 }
Archana8a180362021-07-05 02:18:48 +05302486 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002487
Gilles Peskineca25db92019-04-19 11:43:08 +02002488 if( target_usage_arg != -1 )
2489 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2490 if( target_alg_arg != -1 )
2491 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002492 if( target_alg2_arg != -1 )
2493 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002494
Archana8a180362021-07-05 02:18:48 +05302495
Gilles Peskine57ab7212019-01-28 13:03:09 +01002496 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002497 PSA_ASSERT( psa_copy_key( source_key,
2498 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002499
2500 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002501 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002502
2503 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002504 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002505 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2506 psa_get_key_type( &target_attributes ) );
2507 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2508 psa_get_key_bits( &target_attributes ) );
2509 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2510 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002511 TEST_EQUAL( expected_alg2,
2512 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002513 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2514 {
2515 size_t length;
2516 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002517 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002518 material->len, &length ) );
2519 ASSERT_COMPARE( material->x, material->len,
2520 export_buffer, length );
2521 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002522
Archana8a180362021-07-05 02:18:48 +05302523 if( !psa_key_lifetime_is_external( target_lifetime ) )
2524 {
2525 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2526 goto exit;
2527 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2528 goto exit;
2529 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002530
Ronald Cron5425a212020-08-04 14:58:35 +02002531 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002532
2533exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002534 /*
2535 * Source and target key attributes may have been returned by
2536 * psa_get_key_attributes() thus reset them as required.
2537 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002538 psa_reset_key_attributes( &source_attributes );
2539 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002540
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002541 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002542 mbedtls_free( export_buffer );
2543}
2544/* END_CASE */
2545
2546/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002547void copy_fail( int source_usage_arg,
2548 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302549 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002550 int type_arg, data_t *material,
2551 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002552 int target_usage_arg,
2553 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002554 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002555 int expected_status_arg )
2556{
2557 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2558 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002559 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2560 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002561 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002562
2563 PSA_ASSERT( psa_crypto_init( ) );
2564
2565 /* Prepare the source key. */
2566 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2567 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002568 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002569 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302570 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002571 PSA_ASSERT( psa_import_key( &source_attributes,
2572 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002573 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002574
2575 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002576 psa_set_key_id( &target_attributes, key_id );
2577 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002578 psa_set_key_type( &target_attributes, target_type_arg );
2579 psa_set_key_bits( &target_attributes, target_bits_arg );
2580 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2581 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002582 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002583
2584 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002585 TEST_EQUAL( psa_copy_key( source_key,
2586 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002587 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002588
Ronald Cron5425a212020-08-04 14:58:35 +02002589 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002590
Gilles Peskine4a644642019-05-03 17:14:08 +02002591exit:
2592 psa_reset_key_attributes( &source_attributes );
2593 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002594 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002595}
2596/* END_CASE */
2597
2598/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002599void hash_operation_init( )
2600{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002601 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002602 /* Test each valid way of initializing the object, except for `= {0}`, as
2603 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2604 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002605 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002606 psa_hash_operation_t func = psa_hash_operation_init( );
2607 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2608 psa_hash_operation_t zero;
2609
2610 memset( &zero, 0, sizeof( zero ) );
2611
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002612 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002613 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2614 PSA_ERROR_BAD_STATE );
2615 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2616 PSA_ERROR_BAD_STATE );
2617 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2618 PSA_ERROR_BAD_STATE );
2619
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002620 /* A default hash operation should be abortable without error. */
2621 PSA_ASSERT( psa_hash_abort( &func ) );
2622 PSA_ASSERT( psa_hash_abort( &init ) );
2623 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002624}
2625/* END_CASE */
2626
2627/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002628void hash_setup( int alg_arg,
2629 int expected_status_arg )
2630{
2631 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002632 uint8_t *output = NULL;
2633 size_t output_size = 0;
2634 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002635 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002636 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002637 psa_status_t status;
2638
Gilles Peskine8817f612018-12-18 00:18:46 +01002639 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002640
Neil Armstrongedb20862022-02-07 15:47:44 +01002641 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002642 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002643 ASSERT_ALLOC( output, output_size );
2644
2645 status = psa_hash_compute( alg, NULL, 0,
2646 output, output_size, &output_length );
2647 TEST_EQUAL( status, expected_status );
2648
2649 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002650 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002651 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002652
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002653 /* Whether setup succeeded or failed, abort must succeed. */
2654 PSA_ASSERT( psa_hash_abort( &operation ) );
2655
2656 /* If setup failed, reproduce the failure, so as to
2657 * test the resulting state of the operation object. */
2658 if( status != PSA_SUCCESS )
2659 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2660
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002661 /* Now the operation object should be reusable. */
2662#if defined(KNOWN_SUPPORTED_HASH_ALG)
2663 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2664 PSA_ASSERT( psa_hash_abort( &operation ) );
2665#endif
2666
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002667exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002668 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002669 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002670}
2671/* END_CASE */
2672
2673/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002674void hash_compute_fail( int alg_arg, data_t *input,
2675 int output_size_arg, int expected_status_arg )
2676{
2677 psa_algorithm_t alg = alg_arg;
2678 uint8_t *output = NULL;
2679 size_t output_size = output_size_arg;
2680 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002681 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002682 psa_status_t expected_status = expected_status_arg;
2683 psa_status_t status;
2684
2685 ASSERT_ALLOC( output, output_size );
2686
2687 PSA_ASSERT( psa_crypto_init( ) );
2688
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002689 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002690 status = psa_hash_compute( alg, input->x, input->len,
2691 output, output_size, &output_length );
2692 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002693 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002694
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002695 /* Hash Compute, 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_finish( &operation, output, output_size,
2703 &output_length );
2704 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002705 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002706 else
2707 TEST_EQUAL( status, expected_status );
2708 }
2709 else
2710 {
2711 TEST_EQUAL( status, expected_status );
2712 }
2713 }
2714 else
2715 {
2716 TEST_EQUAL( status, expected_status );
2717 }
2718
Gilles Peskine0a749c82019-11-28 19:33:58 +01002719exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002720 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002721 mbedtls_free( output );
2722 PSA_DONE( );
2723}
2724/* END_CASE */
2725
2726/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002727void hash_compare_fail( int alg_arg, data_t *input,
2728 data_t *reference_hash,
2729 int expected_status_arg )
2730{
2731 psa_algorithm_t alg = alg_arg;
2732 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002733 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002734 psa_status_t status;
2735
2736 PSA_ASSERT( psa_crypto_init( ) );
2737
Neil Armstrong55a1be12022-02-07 11:23:20 +01002738 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002739 status = psa_hash_compare( alg, input->x, input->len,
2740 reference_hash->x, reference_hash->len );
2741 TEST_EQUAL( status, expected_status );
2742
Neil Armstrong55a1be12022-02-07 11:23:20 +01002743 /* Hash Compare, multi-part */
2744 status = psa_hash_setup( &operation, alg );
2745 if( status == PSA_SUCCESS )
2746 {
2747 status = psa_hash_update( &operation, input->x, input->len );
2748 if( status == PSA_SUCCESS )
2749 {
2750 status = psa_hash_verify( &operation, reference_hash->x,
2751 reference_hash->len );
2752 TEST_EQUAL( status, expected_status );
2753 }
2754 else
2755 {
2756 TEST_EQUAL( status, expected_status );
2757 }
2758 }
2759 else
2760 {
2761 TEST_EQUAL( status, expected_status );
2762 }
2763
Gilles Peskine88e08462020-01-28 20:43:00 +01002764exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002765 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002766 PSA_DONE( );
2767}
2768/* END_CASE */
2769
2770/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002771void hash_compute_compare( int alg_arg, data_t *input,
2772 data_t *expected_output )
2773{
2774 psa_algorithm_t alg = alg_arg;
2775 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2776 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002777 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002778 size_t i;
2779
2780 PSA_ASSERT( psa_crypto_init( ) );
2781
Neil Armstrongca30a002022-02-07 11:40:23 +01002782 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002783 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002784 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002785 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002786 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002787 ASSERT_COMPARE( output, output_length,
2788 expected_output->x, expected_output->len );
2789
Neil Armstrongca30a002022-02-07 11:40:23 +01002790 /* Compute with tight buffer, multi-part */
2791 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2792 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2793 PSA_ASSERT( psa_hash_finish( &operation, output,
2794 PSA_HASH_LENGTH( alg ),
2795 &output_length ) );
2796 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2797 ASSERT_COMPARE( output, output_length,
2798 expected_output->x, expected_output->len );
2799
2800 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002801 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2802 output, sizeof( output ),
2803 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002804 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002805 ASSERT_COMPARE( output, output_length,
2806 expected_output->x, expected_output->len );
2807
Neil Armstrongca30a002022-02-07 11:40:23 +01002808 /* Compute with larger buffer, multi-part */
2809 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2810 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2811 PSA_ASSERT( psa_hash_finish( &operation, output,
2812 sizeof( output ), &output_length ) );
2813 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2814 ASSERT_COMPARE( output, output_length,
2815 expected_output->x, expected_output->len );
2816
2817 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002818 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2819 output, output_length ) );
2820
Neil Armstrongca30a002022-02-07 11:40:23 +01002821 /* Compare with correct hash, multi-part */
2822 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2823 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2824 PSA_ASSERT( psa_hash_verify( &operation, output,
2825 output_length ) );
2826
2827 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002828 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2829 output, output_length + 1 ),
2830 PSA_ERROR_INVALID_SIGNATURE );
2831
Neil Armstrongca30a002022-02-07 11:40:23 +01002832 /* Compare with trailing garbage, multi-part */
2833 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2834 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2835 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2836 PSA_ERROR_INVALID_SIGNATURE );
2837
2838 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002839 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2840 output, output_length - 1 ),
2841 PSA_ERROR_INVALID_SIGNATURE );
2842
Neil Armstrongca30a002022-02-07 11:40:23 +01002843 /* Compare with truncated hash, multi-part */
2844 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2845 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2846 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2847 PSA_ERROR_INVALID_SIGNATURE );
2848
Gilles Peskine0a749c82019-11-28 19:33:58 +01002849 /* Compare with corrupted value */
2850 for( i = 0; i < output_length; i++ )
2851 {
Chris Jones9634bb12021-01-20 15:56:42 +00002852 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002853 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002854
2855 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002856 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2857 output, output_length ),
2858 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002859
2860 /* Multi-Part */
2861 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2862 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2863 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2864 PSA_ERROR_INVALID_SIGNATURE );
2865
Gilles Peskine0a749c82019-11-28 19:33:58 +01002866 output[i] ^= 1;
2867 }
2868
2869exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002870 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002871 PSA_DONE( );
2872}
2873/* END_CASE */
2874
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002875/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002876void hash_bad_order( )
2877{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002878 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002879 unsigned char input[] = "";
2880 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002881 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002882 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2883 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2884 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002885 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002886 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002887 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002888
Gilles Peskine8817f612018-12-18 00:18:46 +01002889 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002890
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002891 /* Call setup twice in a row. */
2892 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002893 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002894 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2895 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002896 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002897 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002898 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002899
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002900 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002901 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002902 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002903 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002904
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002905 /* Check that update calls abort on error. */
2906 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002907 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002908 ASSERT_OPERATION_IS_ACTIVE( operation );
2909 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2910 PSA_ERROR_BAD_STATE );
2911 ASSERT_OPERATION_IS_INACTIVE( operation );
2912 PSA_ASSERT( psa_hash_abort( &operation ) );
2913 ASSERT_OPERATION_IS_INACTIVE( operation );
2914
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002915 /* Call update after finish. */
2916 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2917 PSA_ASSERT( psa_hash_finish( &operation,
2918 hash, sizeof( hash ), &hash_len ) );
2919 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002920 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002921 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002922
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002923 /* Call verify without calling setup beforehand. */
2924 TEST_EQUAL( psa_hash_verify( &operation,
2925 valid_hash, sizeof( valid_hash ) ),
2926 PSA_ERROR_BAD_STATE );
2927 PSA_ASSERT( psa_hash_abort( &operation ) );
2928
2929 /* Call verify after finish. */
2930 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2931 PSA_ASSERT( psa_hash_finish( &operation,
2932 hash, sizeof( hash ), &hash_len ) );
2933 TEST_EQUAL( psa_hash_verify( &operation,
2934 valid_hash, sizeof( valid_hash ) ),
2935 PSA_ERROR_BAD_STATE );
2936 PSA_ASSERT( psa_hash_abort( &operation ) );
2937
2938 /* Call verify twice in a row. */
2939 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002940 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002941 PSA_ASSERT( psa_hash_verify( &operation,
2942 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002943 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002944 TEST_EQUAL( psa_hash_verify( &operation,
2945 valid_hash, sizeof( valid_hash ) ),
2946 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002947 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002948 PSA_ASSERT( psa_hash_abort( &operation ) );
2949
2950 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002951 TEST_EQUAL( psa_hash_finish( &operation,
2952 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002953 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002954 PSA_ASSERT( psa_hash_abort( &operation ) );
2955
2956 /* Call finish twice in a row. */
2957 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2958 PSA_ASSERT( psa_hash_finish( &operation,
2959 hash, sizeof( hash ), &hash_len ) );
2960 TEST_EQUAL( psa_hash_finish( &operation,
2961 hash, sizeof( hash ), &hash_len ),
2962 PSA_ERROR_BAD_STATE );
2963 PSA_ASSERT( psa_hash_abort( &operation ) );
2964
2965 /* Call finish after calling verify. */
2966 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2967 PSA_ASSERT( psa_hash_verify( &operation,
2968 valid_hash, sizeof( valid_hash ) ) );
2969 TEST_EQUAL( psa_hash_finish( &operation,
2970 hash, sizeof( hash ), &hash_len ),
2971 PSA_ERROR_BAD_STATE );
2972 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002973
2974exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002975 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002976}
2977/* END_CASE */
2978
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002979/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002980void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002981{
2982 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002983 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2984 * appended to it */
2985 unsigned char hash[] = {
2986 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2987 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2988 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002989 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002990 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002991
Gilles Peskine8817f612018-12-18 00:18:46 +01002992 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002993
itayzafrir27e69452018-11-01 14:26:34 +02002994 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002995 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002996 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002997 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002998 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002999 ASSERT_OPERATION_IS_INACTIVE( operation );
3000 PSA_ASSERT( psa_hash_abort( &operation ) );
3001 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03003002
itayzafrir27e69452018-11-01 14:26:34 +02003003 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01003004 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003005 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003006 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03003007
itayzafrir27e69452018-11-01 14:26:34 +02003008 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003010 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003011 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03003012
itayzafrirec93d302018-10-18 18:01:10 +03003013exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003014 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03003015}
3016/* END_CASE */
3017
Ronald Cronee414c72021-03-18 18:50:08 +01003018/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003019void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03003020{
3021 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003022 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003023 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003024 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003025 size_t hash_len;
3026
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003028
itayzafrir58028322018-10-25 10:22:01 +03003029 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003031 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003032 hash, expected_size - 1, &hash_len ),
3033 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003034
3035exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003036 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003037}
3038/* END_CASE */
3039
Ronald Cronee414c72021-03-18 18:50:08 +01003040/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003041void hash_clone_source_state( )
3042{
3043 psa_algorithm_t alg = PSA_ALG_SHA_256;
3044 unsigned char hash[PSA_HASH_MAX_SIZE];
3045 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3046 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3047 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3048 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3049 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3050 size_t hash_len;
3051
3052 PSA_ASSERT( psa_crypto_init( ) );
3053 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3054
3055 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3056 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3057 PSA_ASSERT( psa_hash_finish( &op_finished,
3058 hash, sizeof( hash ), &hash_len ) );
3059 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3060 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3061
3062 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3063 PSA_ERROR_BAD_STATE );
3064
3065 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3066 PSA_ASSERT( psa_hash_finish( &op_init,
3067 hash, sizeof( hash ), &hash_len ) );
3068 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3069 PSA_ASSERT( psa_hash_finish( &op_finished,
3070 hash, sizeof( hash ), &hash_len ) );
3071 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3072 PSA_ASSERT( psa_hash_finish( &op_aborted,
3073 hash, sizeof( hash ), &hash_len ) );
3074
3075exit:
3076 psa_hash_abort( &op_source );
3077 psa_hash_abort( &op_init );
3078 psa_hash_abort( &op_setup );
3079 psa_hash_abort( &op_finished );
3080 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003081 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003082}
3083/* END_CASE */
3084
Ronald Cronee414c72021-03-18 18:50:08 +01003085/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003086void hash_clone_target_state( )
3087{
3088 psa_algorithm_t alg = PSA_ALG_SHA_256;
3089 unsigned char hash[PSA_HASH_MAX_SIZE];
3090 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3091 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3092 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3093 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3094 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3095 size_t hash_len;
3096
3097 PSA_ASSERT( psa_crypto_init( ) );
3098
3099 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3100 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3101 PSA_ASSERT( psa_hash_finish( &op_finished,
3102 hash, sizeof( hash ), &hash_len ) );
3103 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3104 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3105
3106 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3107 PSA_ASSERT( psa_hash_finish( &op_target,
3108 hash, sizeof( hash ), &hash_len ) );
3109
3110 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3111 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3112 PSA_ERROR_BAD_STATE );
3113 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3114 PSA_ERROR_BAD_STATE );
3115
3116exit:
3117 psa_hash_abort( &op_target );
3118 psa_hash_abort( &op_init );
3119 psa_hash_abort( &op_setup );
3120 psa_hash_abort( &op_finished );
3121 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003122 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003123}
3124/* END_CASE */
3125
itayzafrir58028322018-10-25 10:22:01 +03003126/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003127void mac_operation_init( )
3128{
Jaeden Amero252ef282019-02-15 14:05:35 +00003129 const uint8_t input[1] = { 0 };
3130
Jaeden Amero769ce272019-01-04 11:48:03 +00003131 /* Test each valid way of initializing the object, except for `= {0}`, as
3132 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3133 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003134 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003135 psa_mac_operation_t func = psa_mac_operation_init( );
3136 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3137 psa_mac_operation_t zero;
3138
3139 memset( &zero, 0, sizeof( zero ) );
3140
Jaeden Amero252ef282019-02-15 14:05:35 +00003141 /* A freshly-initialized MAC operation should not be usable. */
3142 TEST_EQUAL( psa_mac_update( &func,
3143 input, sizeof( input ) ),
3144 PSA_ERROR_BAD_STATE );
3145 TEST_EQUAL( psa_mac_update( &init,
3146 input, sizeof( input ) ),
3147 PSA_ERROR_BAD_STATE );
3148 TEST_EQUAL( psa_mac_update( &zero,
3149 input, sizeof( input ) ),
3150 PSA_ERROR_BAD_STATE );
3151
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003152 /* A default MAC operation should be abortable without error. */
3153 PSA_ASSERT( psa_mac_abort( &func ) );
3154 PSA_ASSERT( psa_mac_abort( &init ) );
3155 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003156}
3157/* END_CASE */
3158
3159/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003160void mac_setup( int key_type_arg,
3161 data_t *key,
3162 int alg_arg,
3163 int expected_status_arg )
3164{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003165 psa_key_type_t key_type = key_type_arg;
3166 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003167 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003168 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003169 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3170#if defined(KNOWN_SUPPORTED_MAC_ALG)
3171 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3172#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003173
Gilles Peskine8817f612018-12-18 00:18:46 +01003174 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003175
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003176 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3177 &operation, &status ) )
3178 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003179 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003180
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003181 /* The operation object should be reusable. */
3182#if defined(KNOWN_SUPPORTED_MAC_ALG)
3183 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3184 smoke_test_key_data,
3185 sizeof( smoke_test_key_data ),
3186 KNOWN_SUPPORTED_MAC_ALG,
3187 &operation, &status ) )
3188 goto exit;
3189 TEST_EQUAL( status, PSA_SUCCESS );
3190#endif
3191
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003192exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003193 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003194}
3195/* END_CASE */
3196
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003197/* 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 +00003198void mac_bad_order( )
3199{
Ronald Cron5425a212020-08-04 14:58:35 +02003200 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003201 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3202 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003203 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003204 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3205 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3206 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003208 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3209 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3210 size_t sign_mac_length = 0;
3211 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3212 const uint8_t verify_mac[] = {
3213 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3214 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3215 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3216
3217 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003218 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003219 psa_set_key_algorithm( &attributes, alg );
3220 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003221
Ronald Cron5425a212020-08-04 14:58:35 +02003222 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3223 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003224
Jaeden Amero252ef282019-02-15 14:05:35 +00003225 /* Call update without calling setup beforehand. */
3226 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3227 PSA_ERROR_BAD_STATE );
3228 PSA_ASSERT( psa_mac_abort( &operation ) );
3229
3230 /* Call sign finish without calling setup beforehand. */
3231 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3232 &sign_mac_length),
3233 PSA_ERROR_BAD_STATE );
3234 PSA_ASSERT( psa_mac_abort( &operation ) );
3235
3236 /* Call verify finish without calling setup beforehand. */
3237 TEST_EQUAL( psa_mac_verify_finish( &operation,
3238 verify_mac, sizeof( verify_mac ) ),
3239 PSA_ERROR_BAD_STATE );
3240 PSA_ASSERT( psa_mac_abort( &operation ) );
3241
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003242 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003243 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003244 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003245 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003246 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003247 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003248 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003249 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003250
Jaeden Amero252ef282019-02-15 14:05:35 +00003251 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003252 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003253 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3254 PSA_ASSERT( psa_mac_sign_finish( &operation,
3255 sign_mac, sizeof( sign_mac ),
3256 &sign_mac_length ) );
3257 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3258 PSA_ERROR_BAD_STATE );
3259 PSA_ASSERT( psa_mac_abort( &operation ) );
3260
3261 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003262 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003263 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3264 PSA_ASSERT( psa_mac_verify_finish( &operation,
3265 verify_mac, sizeof( verify_mac ) ) );
3266 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3267 PSA_ERROR_BAD_STATE );
3268 PSA_ASSERT( psa_mac_abort( &operation ) );
3269
3270 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003271 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003272 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3273 PSA_ASSERT( psa_mac_sign_finish( &operation,
3274 sign_mac, sizeof( sign_mac ),
3275 &sign_mac_length ) );
3276 TEST_EQUAL( psa_mac_sign_finish( &operation,
3277 sign_mac, sizeof( sign_mac ),
3278 &sign_mac_length ),
3279 PSA_ERROR_BAD_STATE );
3280 PSA_ASSERT( psa_mac_abort( &operation ) );
3281
3282 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003283 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003284 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3285 PSA_ASSERT( psa_mac_verify_finish( &operation,
3286 verify_mac, sizeof( verify_mac ) ) );
3287 TEST_EQUAL( psa_mac_verify_finish( &operation,
3288 verify_mac, sizeof( verify_mac ) ),
3289 PSA_ERROR_BAD_STATE );
3290 PSA_ASSERT( psa_mac_abort( &operation ) );
3291
3292 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003293 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003294 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003295 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003296 TEST_EQUAL( psa_mac_verify_finish( &operation,
3297 verify_mac, sizeof( verify_mac ) ),
3298 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003299 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003300 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003301 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003302
3303 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003304 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003305 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003306 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003307 TEST_EQUAL( psa_mac_sign_finish( &operation,
3308 sign_mac, sizeof( sign_mac ),
3309 &sign_mac_length ),
3310 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003311 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003312 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003313 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003314
Ronald Cron5425a212020-08-04 14:58:35 +02003315 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003316
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003317exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003318 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003319}
3320/* END_CASE */
3321
3322/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003323void mac_sign_verify_multi( int key_type_arg,
3324 data_t *key_data,
3325 int alg_arg,
3326 data_t *input,
3327 int is_verify,
3328 data_t *expected_mac )
3329{
3330 size_t data_part_len = 0;
3331
3332 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3333 {
3334 /* Split data into length(data_part_len) parts. */
3335 mbedtls_test_set_step( 2000 + data_part_len );
3336
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003337 if( mac_multipart_internal_func( key_type_arg, key_data,
3338 alg_arg,
3339 input, data_part_len,
3340 expected_mac,
3341 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003342 break;
3343
3344 /* length(0) part, length(data_part_len) part, length(0) part... */
3345 mbedtls_test_set_step( 3000 + data_part_len );
3346
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003347 if( mac_multipart_internal_func( key_type_arg, key_data,
3348 alg_arg,
3349 input, data_part_len,
3350 expected_mac,
3351 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003352 break;
3353 }
3354
3355 /* Goto is required to silence warnings about unused labels, as we
3356 * don't actually do any test assertions in this function. */
3357 goto exit;
3358}
3359/* END_CASE */
3360
3361/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003362void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003363 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003364 int alg_arg,
3365 data_t *input,
3366 data_t *expected_mac )
3367{
Ronald Cron5425a212020-08-04 14:58:35 +02003368 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003369 psa_key_type_t key_type = key_type_arg;
3370 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003371 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003373 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003374 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003375 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003376 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003377 const size_t output_sizes_to_test[] = {
3378 0,
3379 1,
3380 expected_mac->len - 1,
3381 expected_mac->len,
3382 expected_mac->len + 1,
3383 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003384
Gilles Peskine7be11a72022-04-14 00:12:57 +02003385 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003386 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003387 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003388
Gilles Peskine8817f612018-12-18 00:18:46 +01003389 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003390
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003391 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003392 psa_set_key_algorithm( &attributes, alg );
3393 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003394
Ronald Cron5425a212020-08-04 14:58:35 +02003395 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3396 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003397
Gilles Peskine8b356b52020-08-25 23:44:59 +02003398 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3399 {
3400 const size_t output_size = output_sizes_to_test[i];
3401 psa_status_t expected_status =
3402 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3403 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003404
Chris Jones9634bb12021-01-20 15:56:42 +00003405 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003406 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003407
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003408 /* Calculate the MAC, one-shot case. */
3409 TEST_EQUAL( psa_mac_compute( key, alg,
3410 input->x, input->len,
3411 actual_mac, output_size, &mac_length ),
3412 expected_status );
3413 if( expected_status == PSA_SUCCESS )
3414 {
3415 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3416 actual_mac, mac_length );
3417 }
3418
3419 if( output_size > 0 )
3420 memset( actual_mac, 0, output_size );
3421
3422 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003423 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003424 PSA_ASSERT( psa_mac_update( &operation,
3425 input->x, input->len ) );
3426 TEST_EQUAL( psa_mac_sign_finish( &operation,
3427 actual_mac, output_size,
3428 &mac_length ),
3429 expected_status );
3430 PSA_ASSERT( psa_mac_abort( &operation ) );
3431
3432 if( expected_status == PSA_SUCCESS )
3433 {
3434 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3435 actual_mac, mac_length );
3436 }
3437 mbedtls_free( actual_mac );
3438 actual_mac = NULL;
3439 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003440
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003441exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003442 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003443 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003444 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003445 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003446}
3447/* END_CASE */
3448
3449/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003450void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003451 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003452 int alg_arg,
3453 data_t *input,
3454 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003455{
Ronald Cron5425a212020-08-04 14:58:35 +02003456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003457 psa_key_type_t key_type = key_type_arg;
3458 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003459 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003461 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003462
Gilles Peskine7be11a72022-04-14 00:12:57 +02003463 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003464
Gilles Peskine8817f612018-12-18 00:18:46 +01003465 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003466
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003467 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003468 psa_set_key_algorithm( &attributes, alg );
3469 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003470
Ronald Cron5425a212020-08-04 14:58:35 +02003471 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3472 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003473
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003474 /* Verify correct MAC, one-shot case. */
3475 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3476 expected_mac->x, expected_mac->len ) );
3477
3478 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003479 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003480 PSA_ASSERT( psa_mac_update( &operation,
3481 input->x, input->len ) );
3482 PSA_ASSERT( psa_mac_verify_finish( &operation,
3483 expected_mac->x,
3484 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003485
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003486 /* Test a MAC that's too short, one-shot case. */
3487 TEST_EQUAL( psa_mac_verify( key, alg,
3488 input->x, input->len,
3489 expected_mac->x,
3490 expected_mac->len - 1 ),
3491 PSA_ERROR_INVALID_SIGNATURE );
3492
3493 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003494 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003495 PSA_ASSERT( psa_mac_update( &operation,
3496 input->x, input->len ) );
3497 TEST_EQUAL( psa_mac_verify_finish( &operation,
3498 expected_mac->x,
3499 expected_mac->len - 1 ),
3500 PSA_ERROR_INVALID_SIGNATURE );
3501
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003502 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003503 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3504 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003505 TEST_EQUAL( psa_mac_verify( key, alg,
3506 input->x, input->len,
3507 perturbed_mac, expected_mac->len + 1 ),
3508 PSA_ERROR_INVALID_SIGNATURE );
3509
3510 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003511 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003512 PSA_ASSERT( psa_mac_update( &operation,
3513 input->x, input->len ) );
3514 TEST_EQUAL( psa_mac_verify_finish( &operation,
3515 perturbed_mac,
3516 expected_mac->len + 1 ),
3517 PSA_ERROR_INVALID_SIGNATURE );
3518
3519 /* Test changing one byte. */
3520 for( size_t i = 0; i < expected_mac->len; i++ )
3521 {
Chris Jones9634bb12021-01-20 15:56:42 +00003522 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003523 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003524
3525 TEST_EQUAL( psa_mac_verify( key, alg,
3526 input->x, input->len,
3527 perturbed_mac, expected_mac->len ),
3528 PSA_ERROR_INVALID_SIGNATURE );
3529
Ronald Cron5425a212020-08-04 14:58:35 +02003530 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003531 PSA_ASSERT( psa_mac_update( &operation,
3532 input->x, input->len ) );
3533 TEST_EQUAL( psa_mac_verify_finish( &operation,
3534 perturbed_mac,
3535 expected_mac->len ),
3536 PSA_ERROR_INVALID_SIGNATURE );
3537 perturbed_mac[i] ^= 1;
3538 }
3539
Gilles Peskine8c9def32018-02-08 10:02:12 +01003540exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003541 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003542 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003543 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003544 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003545}
3546/* END_CASE */
3547
3548/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003549void cipher_operation_init( )
3550{
Jaeden Ameroab439972019-02-15 14:12:05 +00003551 const uint8_t input[1] = { 0 };
3552 unsigned char output[1] = { 0 };
3553 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003554 /* Test each valid way of initializing the object, except for `= {0}`, as
3555 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3556 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003557 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003558 psa_cipher_operation_t func = psa_cipher_operation_init( );
3559 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3560 psa_cipher_operation_t zero;
3561
3562 memset( &zero, 0, sizeof( zero ) );
3563
Jaeden Ameroab439972019-02-15 14:12:05 +00003564 /* A freshly-initialized cipher operation should not be usable. */
3565 TEST_EQUAL( psa_cipher_update( &func,
3566 input, sizeof( input ),
3567 output, sizeof( output ),
3568 &output_length ),
3569 PSA_ERROR_BAD_STATE );
3570 TEST_EQUAL( psa_cipher_update( &init,
3571 input, sizeof( input ),
3572 output, sizeof( output ),
3573 &output_length ),
3574 PSA_ERROR_BAD_STATE );
3575 TEST_EQUAL( psa_cipher_update( &zero,
3576 input, sizeof( input ),
3577 output, sizeof( output ),
3578 &output_length ),
3579 PSA_ERROR_BAD_STATE );
3580
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003581 /* A default cipher operation should be abortable without error. */
3582 PSA_ASSERT( psa_cipher_abort( &func ) );
3583 PSA_ASSERT( psa_cipher_abort( &init ) );
3584 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003585}
3586/* END_CASE */
3587
3588/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003589void cipher_setup( int key_type_arg,
3590 data_t *key,
3591 int alg_arg,
3592 int expected_status_arg )
3593{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003594 psa_key_type_t key_type = key_type_arg;
3595 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003596 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003597 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003598 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003599#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003600 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3601#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003602
Gilles Peskine8817f612018-12-18 00:18:46 +01003603 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003604
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003605 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3606 &operation, &status ) )
3607 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003608 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003609
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003610 /* The operation object should be reusable. */
3611#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3612 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3613 smoke_test_key_data,
3614 sizeof( smoke_test_key_data ),
3615 KNOWN_SUPPORTED_CIPHER_ALG,
3616 &operation, &status ) )
3617 goto exit;
3618 TEST_EQUAL( status, PSA_SUCCESS );
3619#endif
3620
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003621exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003622 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003623 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003624}
3625/* END_CASE */
3626
Ronald Cronee414c72021-03-18 18:50:08 +01003627/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003628void cipher_bad_order( )
3629{
Ronald Cron5425a212020-08-04 14:58:35 +02003630 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003631 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3632 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003634 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003635 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003636 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003637 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3638 0xaa, 0xaa, 0xaa, 0xaa };
3639 const uint8_t text[] = {
3640 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3641 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003642 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003643 size_t length = 0;
3644
3645 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3647 psa_set_key_algorithm( &attributes, alg );
3648 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003649 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3650 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003651
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003652 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003653 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003654 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003655 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003656 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003657 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003658 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003659 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003660
3661 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003662 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003663 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003664 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003665 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003666 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003667 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003668 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003669
Jaeden Ameroab439972019-02-15 14:12:05 +00003670 /* Generate an IV without calling setup beforehand. */
3671 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3672 buffer, sizeof( buffer ),
3673 &length ),
3674 PSA_ERROR_BAD_STATE );
3675 PSA_ASSERT( psa_cipher_abort( &operation ) );
3676
3677 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003678 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003679 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3680 buffer, sizeof( buffer ),
3681 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003682 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003683 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3684 buffer, sizeof( buffer ),
3685 &length ),
3686 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003687 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003688 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003689 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003690
3691 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003692 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003693 PSA_ASSERT( psa_cipher_set_iv( &operation,
3694 iv, sizeof( iv ) ) );
3695 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3696 buffer, sizeof( buffer ),
3697 &length ),
3698 PSA_ERROR_BAD_STATE );
3699 PSA_ASSERT( psa_cipher_abort( &operation ) );
3700
3701 /* Set an IV without calling setup beforehand. */
3702 TEST_EQUAL( psa_cipher_set_iv( &operation,
3703 iv, sizeof( iv ) ),
3704 PSA_ERROR_BAD_STATE );
3705 PSA_ASSERT( psa_cipher_abort( &operation ) );
3706
3707 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003708 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003709 PSA_ASSERT( psa_cipher_set_iv( &operation,
3710 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003711 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003712 TEST_EQUAL( psa_cipher_set_iv( &operation,
3713 iv, sizeof( iv ) ),
3714 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003715 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003716 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003717 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003718
3719 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003720 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003721 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3722 buffer, sizeof( buffer ),
3723 &length ) );
3724 TEST_EQUAL( psa_cipher_set_iv( &operation,
3725 iv, sizeof( iv ) ),
3726 PSA_ERROR_BAD_STATE );
3727 PSA_ASSERT( psa_cipher_abort( &operation ) );
3728
3729 /* Call update without calling setup beforehand. */
3730 TEST_EQUAL( psa_cipher_update( &operation,
3731 text, sizeof( text ),
3732 buffer, sizeof( buffer ),
3733 &length ),
3734 PSA_ERROR_BAD_STATE );
3735 PSA_ASSERT( psa_cipher_abort( &operation ) );
3736
3737 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003738 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003739 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003740 TEST_EQUAL( psa_cipher_update( &operation,
3741 text, sizeof( text ),
3742 buffer, sizeof( buffer ),
3743 &length ),
3744 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003745 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003746 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003747 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003748
3749 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003750 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003751 PSA_ASSERT( psa_cipher_set_iv( &operation,
3752 iv, sizeof( iv ) ) );
3753 PSA_ASSERT( psa_cipher_finish( &operation,
3754 buffer, sizeof( buffer ), &length ) );
3755 TEST_EQUAL( psa_cipher_update( &operation,
3756 text, sizeof( text ),
3757 buffer, sizeof( buffer ),
3758 &length ),
3759 PSA_ERROR_BAD_STATE );
3760 PSA_ASSERT( psa_cipher_abort( &operation ) );
3761
3762 /* Call finish without calling setup beforehand. */
3763 TEST_EQUAL( psa_cipher_finish( &operation,
3764 buffer, sizeof( buffer ), &length ),
3765 PSA_ERROR_BAD_STATE );
3766 PSA_ASSERT( psa_cipher_abort( &operation ) );
3767
3768 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003769 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003770 /* Not calling update means we are encrypting an empty buffer, which is OK
3771 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003772 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003773 TEST_EQUAL( psa_cipher_finish( &operation,
3774 buffer, sizeof( buffer ), &length ),
3775 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003776 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003777 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003778 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003779
3780 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003781 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003782 PSA_ASSERT( psa_cipher_set_iv( &operation,
3783 iv, sizeof( iv ) ) );
3784 PSA_ASSERT( psa_cipher_finish( &operation,
3785 buffer, sizeof( buffer ), &length ) );
3786 TEST_EQUAL( psa_cipher_finish( &operation,
3787 buffer, sizeof( buffer ), &length ),
3788 PSA_ERROR_BAD_STATE );
3789 PSA_ASSERT( psa_cipher_abort( &operation ) );
3790
Ronald Cron5425a212020-08-04 14:58:35 +02003791 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003792
Jaeden Ameroab439972019-02-15 14:12:05 +00003793exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003794 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003795 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003796}
3797/* END_CASE */
3798
3799/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003800void cipher_encrypt_fail( int alg_arg,
3801 int key_type_arg,
3802 data_t *key_data,
3803 data_t *input,
3804 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003805{
Ronald Cron5425a212020-08-04 14:58:35 +02003806 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003807 psa_status_t status;
3808 psa_key_type_t key_type = key_type_arg;
3809 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003810 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003811 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3812 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3813 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003814 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003815 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003816 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003817 size_t function_output_length;
3818 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003819 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3820
3821 if ( PSA_ERROR_BAD_STATE != expected_status )
3822 {
3823 PSA_ASSERT( psa_crypto_init( ) );
3824
3825 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3826 psa_set_key_algorithm( &attributes, alg );
3827 psa_set_key_type( &attributes, key_type );
3828
3829 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3830 input->len );
3831 ASSERT_ALLOC( output, output_buffer_size );
3832
3833 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3834 &key ) );
3835 }
3836
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003837 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003838 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3839 output_buffer_size, &output_length );
3840
3841 TEST_EQUAL( status, expected_status );
3842
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003843 /* Encrypt, multi-part */
3844 status = psa_cipher_encrypt_setup( &operation, key, alg );
3845 if( status == PSA_SUCCESS )
3846 {
3847 if( alg != PSA_ALG_ECB_NO_PADDING )
3848 {
3849 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3850 iv, iv_size,
3851 &iv_length ) );
3852 }
3853
3854 status = psa_cipher_update( &operation, input->x, input->len,
3855 output, output_buffer_size,
3856 &function_output_length );
3857 if( status == PSA_SUCCESS )
3858 {
3859 output_length += function_output_length;
3860
3861 status = psa_cipher_finish( &operation, output + output_length,
3862 output_buffer_size - output_length,
3863 &function_output_length );
3864
3865 TEST_EQUAL( status, expected_status );
3866 }
3867 else
3868 {
3869 TEST_EQUAL( status, expected_status );
3870 }
3871 }
3872 else
3873 {
3874 TEST_EQUAL( status, expected_status );
3875 }
3876
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003877exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003878 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003879 mbedtls_free( output );
3880 psa_destroy_key( key );
3881 PSA_DONE( );
3882}
3883/* END_CASE */
3884
3885/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003886void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3887 data_t *input, int iv_length,
3888 int expected_result )
3889{
3890 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3891 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3892 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3893 size_t output_buffer_size = 0;
3894 unsigned char *output = NULL;
3895
3896 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3897 ASSERT_ALLOC( output, output_buffer_size );
3898
3899 PSA_ASSERT( psa_crypto_init( ) );
3900
3901 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3902 psa_set_key_algorithm( &attributes, alg );
3903 psa_set_key_type( &attributes, key_type );
3904
3905 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3906 &key ) );
3907 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3908 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3909 iv_length ) );
3910
3911exit:
3912 psa_cipher_abort( &operation );
3913 mbedtls_free( output );
3914 psa_destroy_key( key );
3915 PSA_DONE( );
3916}
3917/* END_CASE */
3918
3919/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003920void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3921 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003922{
3923 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3924 psa_key_type_t key_type = key_type_arg;
3925 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003926 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3927 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003928 unsigned char *output = NULL;
3929 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003930 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3932
3933 PSA_ASSERT( psa_crypto_init( ) );
3934
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003935 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003936 TEST_LE_U( ciphertext->len,
3937 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3938 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003939 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003940 TEST_LE_U( plaintext->len,
3941 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3942 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3943 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003944
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003945
3946 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003947 psa_set_key_usage_flags( &attributes,
3948 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003949 psa_set_key_algorithm( &attributes, alg );
3950 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003951 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3952 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003953 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3954 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003955 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003956
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003957 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003958 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3959 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3960 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003961 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3962 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003963 PSA_ERROR_BAD_STATE );
3964
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003965 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003966 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3967 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003968 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003969 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003970 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3971 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003972 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003973 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003974
Gilles Peskine286c3142022-04-20 17:09:38 +02003975 /* Multipart encryption */
3976 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3977 output_length = 0;
3978 length = ~0;
3979 PSA_ASSERT( psa_cipher_update( &operation,
3980 plaintext->x, plaintext->len,
3981 output, output_buffer_size,
3982 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003983 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003984 output_length += length;
3985 PSA_ASSERT( psa_cipher_finish( &operation,
3986 output + output_length,
3987 output_buffer_size - output_length,
3988 &length ) );
3989 output_length += length;
3990 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003991 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003992
Gilles Peskine286c3142022-04-20 17:09:38 +02003993 /* Multipart encryption */
3994 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3995 output_length = 0;
3996 length = ~0;
3997 PSA_ASSERT( psa_cipher_update( &operation,
3998 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003999 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02004000 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004001 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02004002 output_length += length;
4003 PSA_ASSERT( psa_cipher_finish( &operation,
4004 output + output_length,
4005 output_buffer_size - output_length,
4006 &length ) );
4007 output_length += length;
4008 ASSERT_COMPARE( plaintext->x, plaintext->len,
4009 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004010
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004011 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004012 output_length = ~0;
4013 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
4014 output, output_buffer_size,
4015 &output_length ) );
4016 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
4017 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004018
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004019 /* One-shot decryption */
4020 output_length = ~0;
4021 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4022 output, output_buffer_size,
4023 &output_length ) );
4024 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004025 output, output_length );
4026
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004027exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004028 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004029 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004030 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004031 psa_destroy_key( key );
4032 PSA_DONE( );
4033}
4034/* END_CASE */
4035
4036/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004037void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4038{
4039 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4040 psa_algorithm_t alg = alg_arg;
4041 psa_key_type_t key_type = key_type_arg;
4042 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4043 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4044 psa_status_t status;
4045
4046 PSA_ASSERT( psa_crypto_init( ) );
4047
4048 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4049 psa_set_key_algorithm( &attributes, alg );
4050 psa_set_key_type( &attributes, key_type );
4051
4052 /* Usage of either of these two size macros would cause divide by zero
4053 * with incorrect key types previously. Input length should be irrelevant
4054 * here. */
4055 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4056 0 );
4057 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4058
4059
4060 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4061 &key ) );
4062
4063 /* Should fail due to invalid alg type (to support invalid key type).
4064 * Encrypt or decrypt will end up in the same place. */
4065 status = psa_cipher_encrypt_setup( &operation, key, alg );
4066
4067 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4068
4069exit:
4070 psa_cipher_abort( &operation );
4071 psa_destroy_key( key );
4072 PSA_DONE( );
4073}
4074/* END_CASE */
4075
4076/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004077void cipher_encrypt_validation( int alg_arg,
4078 int key_type_arg,
4079 data_t *key_data,
4080 data_t *input )
4081{
4082 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4083 psa_key_type_t key_type = key_type_arg;
4084 psa_algorithm_t alg = alg_arg;
4085 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4086 unsigned char *output1 = NULL;
4087 size_t output1_buffer_size = 0;
4088 size_t output1_length = 0;
4089 unsigned char *output2 = NULL;
4090 size_t output2_buffer_size = 0;
4091 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004092 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004093 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004095
Gilles Peskine8817f612018-12-18 00:18:46 +01004096 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004097
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004098 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4099 psa_set_key_algorithm( &attributes, alg );
4100 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004101
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004102 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4103 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4104 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4105 ASSERT_ALLOC( output1, output1_buffer_size );
4106 ASSERT_ALLOC( output2, output2_buffer_size );
4107
Ronald Cron5425a212020-08-04 14:58:35 +02004108 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4109 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004110
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004111 /* The one-shot cipher encryption uses generated iv so validating
4112 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004113 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4114 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004115 TEST_LE_U( output1_length,
4116 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4117 TEST_LE_U( output1_length,
4118 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004119
4120 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4121 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004122
Gilles Peskine8817f612018-12-18 00:18:46 +01004123 PSA_ASSERT( psa_cipher_update( &operation,
4124 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004125 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004126 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004127 TEST_LE_U( function_output_length,
4128 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4129 TEST_LE_U( function_output_length,
4130 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004131 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004132
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004133 PSA_ASSERT( psa_cipher_finish( &operation,
4134 output2 + output2_length,
4135 output2_buffer_size - output2_length,
4136 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004137 TEST_LE_U( function_output_length,
4138 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4139 TEST_LE_U( function_output_length,
4140 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004141 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004142
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004143 PSA_ASSERT( psa_cipher_abort( &operation ) );
4144 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4145 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004146
Gilles Peskine50e586b2018-06-08 14:28:46 +02004147exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004148 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004149 mbedtls_free( output1 );
4150 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004151 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004152 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004153}
4154/* END_CASE */
4155
4156/* BEGIN_CASE */
4157void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004158 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004159 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004160 int first_part_size_arg,
4161 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004162 data_t *expected_output,
4163 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004164{
Ronald Cron5425a212020-08-04 14:58:35 +02004165 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004166 psa_key_type_t key_type = key_type_arg;
4167 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004168 psa_status_t status;
4169 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004170 size_t first_part_size = first_part_size_arg;
4171 size_t output1_length = output1_length_arg;
4172 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004173 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004174 size_t output_buffer_size = 0;
4175 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004176 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004177 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004178 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004179
Gilles Peskine8817f612018-12-18 00:18:46 +01004180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004181
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004182 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4183 psa_set_key_algorithm( &attributes, alg );
4184 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004185
Ronald Cron5425a212020-08-04 14:58:35 +02004186 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4187 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004188
Ronald Cron5425a212020-08-04 14:58:35 +02004189 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004190
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004191 if( iv->len > 0 )
4192 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004193 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004194 }
4195
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004196 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4197 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004198 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004199
Gilles Peskine7be11a72022-04-14 00:12:57 +02004200 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004201 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4202 output, output_buffer_size,
4203 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004204 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004205 TEST_LE_U( function_output_length,
4206 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4207 TEST_LE_U( function_output_length,
4208 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004209 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004210
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004211 if( first_part_size < input->len )
4212 {
4213 PSA_ASSERT( psa_cipher_update( &operation,
4214 input->x + first_part_size,
4215 input->len - first_part_size,
4216 ( output_buffer_size == 0 ? NULL :
4217 output + total_output_length ),
4218 output_buffer_size - total_output_length,
4219 &function_output_length ) );
4220 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004221 TEST_LE_U( function_output_length,
4222 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4223 alg,
4224 input->len - first_part_size ) );
4225 TEST_LE_U( function_output_length,
4226 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004227 total_output_length += function_output_length;
4228 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004229
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004230 status = psa_cipher_finish( &operation,
4231 ( output_buffer_size == 0 ? NULL :
4232 output + total_output_length ),
4233 output_buffer_size - total_output_length,
4234 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004235 TEST_LE_U( function_output_length,
4236 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4237 TEST_LE_U( function_output_length,
4238 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004239 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004240 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004241
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004242 if( expected_status == PSA_SUCCESS )
4243 {
4244 PSA_ASSERT( psa_cipher_abort( &operation ) );
4245
4246 ASSERT_COMPARE( expected_output->x, expected_output->len,
4247 output, total_output_length );
4248 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004249
4250exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004251 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004252 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004253 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004254 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004255}
4256/* END_CASE */
4257
4258/* BEGIN_CASE */
4259void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004260 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004261 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004262 int first_part_size_arg,
4263 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004264 data_t *expected_output,
4265 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004266{
Ronald Cron5425a212020-08-04 14:58:35 +02004267 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268 psa_key_type_t key_type = key_type_arg;
4269 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004270 psa_status_t status;
4271 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004272 size_t first_part_size = first_part_size_arg;
4273 size_t output1_length = output1_length_arg;
4274 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004275 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004276 size_t output_buffer_size = 0;
4277 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004278 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004279 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004281
Gilles Peskine8817f612018-12-18 00:18:46 +01004282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004283
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4285 psa_set_key_algorithm( &attributes, alg );
4286 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004287
Ronald Cron5425a212020-08-04 14:58:35 +02004288 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4289 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004290
Ronald Cron5425a212020-08-04 14:58:35 +02004291 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004292
Steven Cooreman177deba2020-09-07 17:14:14 +02004293 if( iv->len > 0 )
4294 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004295 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004296 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004297
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004298 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4299 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004300 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004301
Gilles Peskine7be11a72022-04-14 00:12:57 +02004302 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004303 PSA_ASSERT( psa_cipher_update( &operation,
4304 input->x, first_part_size,
4305 output, output_buffer_size,
4306 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004307 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004308 TEST_LE_U( function_output_length,
4309 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4310 TEST_LE_U( function_output_length,
4311 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004312 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004313
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004314 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004315 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004316 PSA_ASSERT( psa_cipher_update( &operation,
4317 input->x + first_part_size,
4318 input->len - first_part_size,
4319 ( output_buffer_size == 0 ? NULL :
4320 output + total_output_length ),
4321 output_buffer_size - total_output_length,
4322 &function_output_length ) );
4323 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004324 TEST_LE_U( function_output_length,
4325 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4326 alg,
4327 input->len - first_part_size ) );
4328 TEST_LE_U( function_output_length,
4329 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004330 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004331 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004332
Gilles Peskine50e586b2018-06-08 14:28:46 +02004333 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004334 ( output_buffer_size == 0 ? NULL :
4335 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004336 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004337 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004338 TEST_LE_U( function_output_length,
4339 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4340 TEST_LE_U( function_output_length,
4341 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004342 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004343 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004344
4345 if( expected_status == PSA_SUCCESS )
4346 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004347 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004348
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004349 ASSERT_COMPARE( expected_output->x, expected_output->len,
4350 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004351 }
4352
Gilles Peskine50e586b2018-06-08 14:28:46 +02004353exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004354 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004355 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004356 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004357 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004358}
4359/* END_CASE */
4360
Gilles Peskine50e586b2018-06-08 14:28:46 +02004361/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004362void cipher_decrypt_fail( int alg_arg,
4363 int key_type_arg,
4364 data_t *key_data,
4365 data_t *iv,
4366 data_t *input_arg,
4367 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004368{
4369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4370 psa_status_t status;
4371 psa_key_type_t key_type = key_type_arg;
4372 psa_algorithm_t alg = alg_arg;
4373 psa_status_t expected_status = expected_status_arg;
4374 unsigned char *input = NULL;
4375 size_t input_buffer_size = 0;
4376 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004377 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004378 size_t output_buffer_size = 0;
4379 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004380 size_t function_output_length;
4381 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004382 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4383
4384 if ( PSA_ERROR_BAD_STATE != expected_status )
4385 {
4386 PSA_ASSERT( psa_crypto_init( ) );
4387
4388 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4389 psa_set_key_algorithm( &attributes, alg );
4390 psa_set_key_type( &attributes, key_type );
4391
4392 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4393 &key ) );
4394 }
4395
4396 /* Allocate input buffer and copy the iv and the plaintext */
4397 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4398 if ( input_buffer_size > 0 )
4399 {
4400 ASSERT_ALLOC( input, input_buffer_size );
4401 memcpy( input, iv->x, iv->len );
4402 memcpy( input + iv->len, input_arg->x, input_arg->len );
4403 }
4404
4405 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4406 ASSERT_ALLOC( output, output_buffer_size );
4407
Neil Armstrong66a479f2022-02-07 15:41:19 +01004408 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004409 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4410 output_buffer_size, &output_length );
4411 TEST_EQUAL( status, expected_status );
4412
Neil Armstrong66a479f2022-02-07 15:41:19 +01004413 /* Decrypt, multi-part */
4414 status = psa_cipher_decrypt_setup( &operation, key, alg );
4415 if( status == PSA_SUCCESS )
4416 {
4417 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4418 input_arg->len ) +
4419 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4420 ASSERT_ALLOC( output_multi, output_buffer_size );
4421
4422 if( iv->len > 0 )
4423 {
4424 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4425
4426 if( status != PSA_SUCCESS )
4427 TEST_EQUAL( status, expected_status );
4428 }
4429
4430 if( status == PSA_SUCCESS )
4431 {
4432 status = psa_cipher_update( &operation,
4433 input_arg->x, input_arg->len,
4434 output_multi, output_buffer_size,
4435 &function_output_length );
4436 if( status == PSA_SUCCESS )
4437 {
4438 output_length = function_output_length;
4439
4440 status = psa_cipher_finish( &operation,
4441 output_multi + output_length,
4442 output_buffer_size - output_length,
4443 &function_output_length );
4444
4445 TEST_EQUAL( status, expected_status );
4446 }
4447 else
4448 {
4449 TEST_EQUAL( status, expected_status );
4450 }
4451 }
4452 else
4453 {
4454 TEST_EQUAL( status, expected_status );
4455 }
4456 }
4457 else
4458 {
4459 TEST_EQUAL( status, expected_status );
4460 }
4461
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004462exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004463 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004464 mbedtls_free( input );
4465 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004466 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004467 psa_destroy_key( key );
4468 PSA_DONE( );
4469}
4470/* END_CASE */
4471
4472/* BEGIN_CASE */
4473void cipher_decrypt( int alg_arg,
4474 int key_type_arg,
4475 data_t *key_data,
4476 data_t *iv,
4477 data_t *input_arg,
4478 data_t *expected_output )
4479{
4480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4481 psa_key_type_t key_type = key_type_arg;
4482 psa_algorithm_t alg = alg_arg;
4483 unsigned char *input = NULL;
4484 size_t input_buffer_size = 0;
4485 unsigned char *output = NULL;
4486 size_t output_buffer_size = 0;
4487 size_t output_length = 0;
4488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4489
4490 PSA_ASSERT( psa_crypto_init( ) );
4491
4492 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4493 psa_set_key_algorithm( &attributes, alg );
4494 psa_set_key_type( &attributes, key_type );
4495
4496 /* Allocate input buffer and copy the iv and the plaintext */
4497 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4498 if ( input_buffer_size > 0 )
4499 {
4500 ASSERT_ALLOC( input, input_buffer_size );
4501 memcpy( input, iv->x, iv->len );
4502 memcpy( input + iv->len, input_arg->x, input_arg->len );
4503 }
4504
4505 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4506 ASSERT_ALLOC( output, output_buffer_size );
4507
4508 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4509 &key ) );
4510
4511 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4512 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004513 TEST_LE_U( output_length,
4514 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4515 TEST_LE_U( output_length,
4516 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004517
4518 ASSERT_COMPARE( expected_output->x, expected_output->len,
4519 output, output_length );
4520exit:
4521 mbedtls_free( input );
4522 mbedtls_free( output );
4523 psa_destroy_key( key );
4524 PSA_DONE( );
4525}
4526/* END_CASE */
4527
4528/* BEGIN_CASE */
4529void cipher_verify_output( int alg_arg,
4530 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004531 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004532 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004533{
Ronald Cron5425a212020-08-04 14:58:35 +02004534 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004535 psa_key_type_t key_type = key_type_arg;
4536 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004537 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004538 size_t output1_size = 0;
4539 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004540 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004541 size_t output2_size = 0;
4542 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004543 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004544
Gilles Peskine8817f612018-12-18 00:18:46 +01004545 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004546
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004547 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4548 psa_set_key_algorithm( &attributes, alg );
4549 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004550
Ronald Cron5425a212020-08-04 14:58:35 +02004551 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4552 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004553 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004554 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004555
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004556 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4557 output1, output1_size,
4558 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004559 TEST_LE_U( output1_length,
4560 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4561 TEST_LE_U( output1_length,
4562 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004563
4564 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004565 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004566
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004567 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4568 output2, output2_size,
4569 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004570 TEST_LE_U( output2_length,
4571 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4572 TEST_LE_U( output2_length,
4573 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004574
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004575 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004576
4577exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004578 mbedtls_free( output1 );
4579 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004580 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004581 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004582}
4583/* END_CASE */
4584
4585/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004586void cipher_verify_output_multipart( int alg_arg,
4587 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004588 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004589 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004590 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004591{
Ronald Cron5425a212020-08-04 14:58:35 +02004592 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004593 psa_key_type_t key_type = key_type_arg;
4594 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004595 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004596 unsigned char iv[16] = {0};
4597 size_t iv_size = 16;
4598 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004599 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004600 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004601 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004602 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004603 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004604 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004605 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004606 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4607 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004609
Gilles Peskine8817f612018-12-18 00:18:46 +01004610 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004611
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004612 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4613 psa_set_key_algorithm( &attributes, alg );
4614 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004615
Ronald Cron5425a212020-08-04 14:58:35 +02004616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4617 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004618
Ronald Cron5425a212020-08-04 14:58:35 +02004619 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4620 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004621
Steven Cooreman177deba2020-09-07 17:14:14 +02004622 if( alg != PSA_ALG_ECB_NO_PADDING )
4623 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004624 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4625 iv, iv_size,
4626 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004627 }
4628
gabor-mezei-armceface22021-01-21 12:26:17 +01004629 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004630 TEST_LE_U( output1_buffer_size,
4631 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004632 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004633
Gilles Peskine7be11a72022-04-14 00:12:57 +02004634 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004635
Gilles Peskine8817f612018-12-18 00:18:46 +01004636 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4637 output1, output1_buffer_size,
4638 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004639 TEST_LE_U( function_output_length,
4640 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4641 TEST_LE_U( function_output_length,
4642 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004643 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004644
Gilles Peskine8817f612018-12-18 00:18:46 +01004645 PSA_ASSERT( psa_cipher_update( &operation1,
4646 input->x + first_part_size,
4647 input->len - first_part_size,
4648 output1, output1_buffer_size,
4649 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004650 TEST_LE_U( function_output_length,
4651 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4652 alg,
4653 input->len - first_part_size ) );
4654 TEST_LE_U( function_output_length,
4655 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004656 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004657
Gilles Peskine8817f612018-12-18 00:18:46 +01004658 PSA_ASSERT( psa_cipher_finish( &operation1,
4659 output1 + output1_length,
4660 output1_buffer_size - output1_length,
4661 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004662 TEST_LE_U( function_output_length,
4663 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4664 TEST_LE_U( function_output_length,
4665 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004666 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004667
Gilles Peskine8817f612018-12-18 00:18:46 +01004668 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004669
Gilles Peskine048b7f02018-06-08 14:20:49 +02004670 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004671 TEST_LE_U( output2_buffer_size,
4672 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4673 TEST_LE_U( output2_buffer_size,
4674 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004675 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004676
Steven Cooreman177deba2020-09-07 17:14:14 +02004677 if( iv_length > 0 )
4678 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004679 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4680 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004681 }
Moran Pekerded84402018-06-06 16:36:50 +03004682
Gilles Peskine8817f612018-12-18 00:18:46 +01004683 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4684 output2, output2_buffer_size,
4685 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004686 TEST_LE_U( function_output_length,
4687 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4688 TEST_LE_U( function_output_length,
4689 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004690 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004691
Gilles Peskine8817f612018-12-18 00:18:46 +01004692 PSA_ASSERT( psa_cipher_update( &operation2,
4693 output1 + first_part_size,
4694 output1_length - first_part_size,
4695 output2, output2_buffer_size,
4696 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004697 TEST_LE_U( function_output_length,
4698 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4699 alg,
4700 output1_length - first_part_size ) );
4701 TEST_LE_U( function_output_length,
4702 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004703 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004704
Gilles Peskine8817f612018-12-18 00:18:46 +01004705 PSA_ASSERT( psa_cipher_finish( &operation2,
4706 output2 + output2_length,
4707 output2_buffer_size - output2_length,
4708 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004709 TEST_LE_U( function_output_length,
4710 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4711 TEST_LE_U( function_output_length,
4712 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004713 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004714
Gilles Peskine8817f612018-12-18 00:18:46 +01004715 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004716
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004717 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004718
4719exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004720 psa_cipher_abort( &operation1 );
4721 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004722 mbedtls_free( output1 );
4723 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004724 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004725 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004726}
4727/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004728
Gilles Peskine20035e32018-02-03 22:44:14 +01004729/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004730void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004731 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004732 data_t *nonce,
4733 data_t *additional_data,
4734 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004735 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004736{
Ronald Cron5425a212020-08-04 14:58:35 +02004737 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004738 psa_key_type_t key_type = key_type_arg;
4739 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004740 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004741 unsigned char *output_data = NULL;
4742 size_t output_size = 0;
4743 size_t output_length = 0;
4744 unsigned char *output_data2 = NULL;
4745 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004746 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004747 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004749
Gilles Peskine8817f612018-12-18 00:18:46 +01004750 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004751
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4753 psa_set_key_algorithm( &attributes, alg );
4754 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004755
Gilles Peskine049c7532019-05-15 20:22:09 +02004756 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004757 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004758 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4759 key_bits = psa_get_key_bits( &attributes );
4760
4761 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4762 alg );
4763 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4764 * should be exact. */
4765 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4766 expected_result != PSA_ERROR_NOT_SUPPORTED )
4767 {
4768 TEST_EQUAL( output_size,
4769 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004770 TEST_LE_U( output_size,
4771 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004772 }
4773 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004774
Steven Cooremanf49478b2021-02-15 15:19:25 +01004775 status = psa_aead_encrypt( key, alg,
4776 nonce->x, nonce->len,
4777 additional_data->x,
4778 additional_data->len,
4779 input_data->x, input_data->len,
4780 output_data, output_size,
4781 &output_length );
4782
4783 /* If the operation is not supported, just skip and not fail in case the
4784 * encryption involves a common limitation of cryptography hardwares and
4785 * an alternative implementation. */
4786 if( status == PSA_ERROR_NOT_SUPPORTED )
4787 {
4788 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4789 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4790 }
4791
4792 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004793
4794 if( PSA_SUCCESS == expected_result )
4795 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004796 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797
Gilles Peskine003a4a92019-05-14 16:09:40 +02004798 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4799 * should be exact. */
4800 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004801 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004802
Gilles Peskine7be11a72022-04-14 00:12:57 +02004803 TEST_LE_U( input_data->len,
4804 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004805
Ronald Cron5425a212020-08-04 14:58:35 +02004806 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004807 nonce->x, nonce->len,
4808 additional_data->x,
4809 additional_data->len,
4810 output_data, output_length,
4811 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004812 &output_length2 ),
4813 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004814
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004815 ASSERT_COMPARE( input_data->x, input_data->len,
4816 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004818
Gilles Peskinea1cac842018-06-11 19:33:02 +02004819exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004820 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004821 mbedtls_free( output_data );
4822 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004823 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004824}
4825/* END_CASE */
4826
4827/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004828void aead_encrypt( int key_type_arg, data_t *key_data,
4829 int alg_arg,
4830 data_t *nonce,
4831 data_t *additional_data,
4832 data_t *input_data,
4833 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004834{
Ronald Cron5425a212020-08-04 14:58:35 +02004835 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004836 psa_key_type_t key_type = key_type_arg;
4837 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004838 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004839 unsigned char *output_data = NULL;
4840 size_t output_size = 0;
4841 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004843 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004844
Gilles Peskine8817f612018-12-18 00:18:46 +01004845 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004846
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004847 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4848 psa_set_key_algorithm( &attributes, alg );
4849 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004850
Gilles Peskine049c7532019-05-15 20:22:09 +02004851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004852 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004853 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4854 key_bits = psa_get_key_bits( &attributes );
4855
4856 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4857 alg );
4858 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4859 * should be exact. */
4860 TEST_EQUAL( output_size,
4861 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004862 TEST_LE_U( output_size,
4863 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004864 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865
Steven Cooremand588ea12021-01-11 19:36:04 +01004866 status = psa_aead_encrypt( key, alg,
4867 nonce->x, nonce->len,
4868 additional_data->x, additional_data->len,
4869 input_data->x, input_data->len,
4870 output_data, output_size,
4871 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004872
Ronald Cron28a45ed2021-02-09 20:35:42 +01004873 /* If the operation is not supported, just skip and not fail in case the
4874 * encryption involves a common limitation of cryptography hardwares and
4875 * an alternative implementation. */
4876 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004877 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004878 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4879 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004880 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004881
4882 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004883 ASSERT_COMPARE( expected_result->x, expected_result->len,
4884 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004885
Gilles Peskinea1cac842018-06-11 19:33:02 +02004886exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004887 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004888 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004889 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004890}
4891/* END_CASE */
4892
4893/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004894void aead_decrypt( int key_type_arg, data_t *key_data,
4895 int alg_arg,
4896 data_t *nonce,
4897 data_t *additional_data,
4898 data_t *input_data,
4899 data_t *expected_data,
4900 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004901{
Ronald Cron5425a212020-08-04 14:58:35 +02004902 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004903 psa_key_type_t key_type = key_type_arg;
4904 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004905 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004906 unsigned char *output_data = NULL;
4907 size_t output_size = 0;
4908 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004910 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004911 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004912
Gilles Peskine8817f612018-12-18 00:18:46 +01004913 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004914
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004915 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4916 psa_set_key_algorithm( &attributes, alg );
4917 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004918
Gilles Peskine049c7532019-05-15 20:22:09 +02004919 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004920 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004921 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4922 key_bits = psa_get_key_bits( &attributes );
4923
4924 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4925 alg );
4926 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4927 expected_result != PSA_ERROR_NOT_SUPPORTED )
4928 {
4929 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4930 * should be exact. */
4931 TEST_EQUAL( output_size,
4932 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004933 TEST_LE_U( output_size,
4934 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004935 }
4936 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004937
Steven Cooremand588ea12021-01-11 19:36:04 +01004938 status = psa_aead_decrypt( key, alg,
4939 nonce->x, nonce->len,
4940 additional_data->x,
4941 additional_data->len,
4942 input_data->x, input_data->len,
4943 output_data, output_size,
4944 &output_length );
4945
Ronald Cron28a45ed2021-02-09 20:35:42 +01004946 /* If the operation is not supported, just skip and not fail in case the
4947 * decryption involves a common limitation of cryptography hardwares and
4948 * an alternative implementation. */
4949 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004950 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004951 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4952 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004953 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004954
4955 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004956
Gilles Peskine2d277862018-06-18 15:41:12 +02004957 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004958 ASSERT_COMPARE( expected_data->x, expected_data->len,
4959 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004960
Gilles Peskinea1cac842018-06-11 19:33:02 +02004961exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004962 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004963 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004964 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004965}
4966/* END_CASE */
4967
4968/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004969void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4970 int alg_arg,
4971 data_t *nonce,
4972 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004973 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004974 int do_set_lengths,
4975 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004976{
Paul Elliottd3f82412021-06-16 16:52:21 +01004977 size_t ad_part_len = 0;
4978 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004979 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004980
Paul Elliott32f46ba2021-09-23 18:24:36 +01004981 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004982 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004983 mbedtls_test_set_step( ad_part_len );
4984
4985 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004986 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004987 if( ad_part_len & 0x01 )
4988 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4989 else
4990 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004991 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004992
4993 /* Split ad into length(ad_part_len) parts. */
4994 if( !aead_multipart_internal_func( key_type_arg, key_data,
4995 alg_arg, nonce,
4996 additional_data,
4997 ad_part_len,
4998 input_data, -1,
4999 set_lengths_method,
5000 expected_output,
5001 1, 0 ) )
5002 break;
5003
5004 /* length(0) part, length(ad_part_len) part, length(0) part... */
5005 mbedtls_test_set_step( 1000 + ad_part_len );
5006
5007 if( !aead_multipart_internal_func( key_type_arg, key_data,
5008 alg_arg, nonce,
5009 additional_data,
5010 ad_part_len,
5011 input_data, -1,
5012 set_lengths_method,
5013 expected_output,
5014 1, 1 ) )
5015 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005016 }
Paul Elliottd3f82412021-06-16 16:52:21 +01005017
Paul Elliott32f46ba2021-09-23 18:24:36 +01005018 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005019 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005020 /* Split data into length(data_part_len) parts. */
5021 mbedtls_test_set_step( 2000 + data_part_len );
5022
5023 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005024 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005025 if( data_part_len & 0x01 )
5026 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5027 else
5028 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005029 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005030
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031 if( !aead_multipart_internal_func( key_type_arg, key_data,
5032 alg_arg, nonce,
5033 additional_data, -1,
5034 input_data, data_part_len,
5035 set_lengths_method,
5036 expected_output,
5037 1, 0 ) )
5038 break;
5039
5040 /* length(0) part, length(data_part_len) part, length(0) part... */
5041 mbedtls_test_set_step( 3000 + data_part_len );
5042
5043 if( !aead_multipart_internal_func( key_type_arg, key_data,
5044 alg_arg, nonce,
5045 additional_data, -1,
5046 input_data, data_part_len,
5047 set_lengths_method,
5048 expected_output,
5049 1, 1 ) )
5050 break;
5051 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005052
Paul Elliott8fc45162021-06-23 16:06:01 +01005053 /* Goto is required to silence warnings about unused labels, as we
5054 * don't actually do any test assertions in this function. */
5055 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005056}
5057/* END_CASE */
5058
5059/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005060void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5061 int alg_arg,
5062 data_t *nonce,
5063 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005064 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005065 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005066 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005067{
Paul Elliottd3f82412021-06-16 16:52:21 +01005068 size_t ad_part_len = 0;
5069 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005070 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005071
Paul Elliott32f46ba2021-09-23 18:24:36 +01005072 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005073 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005074 /* Split ad into length(ad_part_len) parts. */
5075 mbedtls_test_set_step( ad_part_len );
5076
5077 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005078 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005079 if( ad_part_len & 0x01 )
5080 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5081 else
5082 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005083 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005084
5085 if( !aead_multipart_internal_func( key_type_arg, key_data,
5086 alg_arg, nonce,
5087 additional_data,
5088 ad_part_len,
5089 input_data, -1,
5090 set_lengths_method,
5091 expected_output,
5092 0, 0 ) )
5093 break;
5094
5095 /* length(0) part, length(ad_part_len) part, length(0) part... */
5096 mbedtls_test_set_step( 1000 + ad_part_len );
5097
5098 if( !aead_multipart_internal_func( key_type_arg, key_data,
5099 alg_arg, nonce,
5100 additional_data,
5101 ad_part_len,
5102 input_data, -1,
5103 set_lengths_method,
5104 expected_output,
5105 0, 1 ) )
5106 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005107 }
5108
Paul Elliott32f46ba2021-09-23 18:24:36 +01005109 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005110 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005111 /* Split data into length(data_part_len) parts. */
5112 mbedtls_test_set_step( 2000 + data_part_len );
5113
5114 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005115 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005116 if( data_part_len & 0x01 )
5117 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5118 else
5119 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005120 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005121
5122 if( !aead_multipart_internal_func( key_type_arg, key_data,
5123 alg_arg, nonce,
5124 additional_data, -1,
5125 input_data, data_part_len,
5126 set_lengths_method,
5127 expected_output,
5128 0, 0 ) )
5129 break;
5130
5131 /* length(0) part, length(data_part_len) part, length(0) part... */
5132 mbedtls_test_set_step( 3000 + data_part_len );
5133
5134 if( !aead_multipart_internal_func( key_type_arg, key_data,
5135 alg_arg, nonce,
5136 additional_data, -1,
5137 input_data, data_part_len,
5138 set_lengths_method,
5139 expected_output,
5140 0, 1 ) )
5141 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005142 }
5143
Paul Elliott8fc45162021-06-23 16:06:01 +01005144 /* Goto is required to silence warnings about unused labels, as we
5145 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005146 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005147}
5148/* END_CASE */
5149
5150/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005151void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5152 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005153 int nonce_length,
5154 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005155 data_t *additional_data,
5156 data_t *input_data,
5157 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005158{
5159
5160 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5161 psa_key_type_t key_type = key_type_arg;
5162 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005163 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005164 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5166 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005167 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005168 size_t actual_nonce_length = 0;
5169 size_t expected_nonce_length = expected_nonce_length_arg;
5170 unsigned char *output = NULL;
5171 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005172 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005173 size_t ciphertext_size = 0;
5174 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005175 size_t tag_length = 0;
5176 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005177
5178 PSA_ASSERT( psa_crypto_init( ) );
5179
5180 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5181 psa_set_key_algorithm( & attributes, alg );
5182 psa_set_key_type( & attributes, key_type );
5183
5184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5185 &key ) );
5186
5187 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5188
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005189 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5190
Paul Elliottf1277632021-08-24 18:11:37 +01005191 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005192
Paul Elliottf1277632021-08-24 18:11:37 +01005193 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005194
Gilles Peskine7be11a72022-04-14 00:12:57 +02005195 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005196
Paul Elliottf1277632021-08-24 18:11:37 +01005197 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005198
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005199 status = psa_aead_encrypt_setup( &operation, key, alg );
5200
5201 /* If the operation is not supported, just skip and not fail in case the
5202 * encryption involves a common limitation of cryptography hardwares and
5203 * an alternative implementation. */
5204 if( status == PSA_ERROR_NOT_SUPPORTED )
5205 {
5206 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005207 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005208 }
5209
5210 PSA_ASSERT( status );
5211
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005212 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005213 nonce_length,
5214 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005215
Paul Elliott693bf312021-07-23 17:40:41 +01005216 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005217
Paul Elliottf1277632021-08-24 18:11:37 +01005218 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005219
Paul Elliott88ecbe12021-09-22 17:23:03 +01005220 if( expected_status == PSA_SUCCESS )
5221 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5222 alg ) );
5223
Gilles Peskine7be11a72022-04-14 00:12:57 +02005224 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005225
Paul Elliott693bf312021-07-23 17:40:41 +01005226 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005227 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005228 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005229 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5230 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005231
5232 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5233 additional_data->len ) );
5234
5235 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005236 output, output_size,
5237 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005238
Paul Elliottf1277632021-08-24 18:11:37 +01005239 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5240 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005241 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5242 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005243
5244exit:
5245 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005246 mbedtls_free( output );
5247 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005248 psa_aead_abort( &operation );
5249 PSA_DONE( );
5250}
5251/* END_CASE */
5252
5253/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005254void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5255 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005256 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005257 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005258 data_t *additional_data,
5259 data_t *input_data,
5260 int expected_status_arg )
5261{
5262
5263 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5264 psa_key_type_t key_type = key_type_arg;
5265 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005266 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005267 uint8_t *nonce_buffer = NULL;
5268 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5269 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5270 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005271 unsigned char *output = NULL;
5272 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005273 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005274 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005275 size_t ciphertext_size = 0;
5276 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005277 size_t tag_length = 0;
5278 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005279 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005280 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005281
5282 PSA_ASSERT( psa_crypto_init( ) );
5283
5284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5285 psa_set_key_algorithm( &attributes, alg );
5286 psa_set_key_type( &attributes, key_type );
5287
5288 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5289 &key ) );
5290
5291 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5292
5293 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5294
Paul Elliott6f0e7202021-08-25 12:57:18 +01005295 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005296
Paul Elliott6f0e7202021-08-25 12:57:18 +01005297 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005298
Gilles Peskine7be11a72022-04-14 00:12:57 +02005299 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005300
Paul Elliott6f0e7202021-08-25 12:57:18 +01005301 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005302
Paul Elliott863864a2021-07-23 17:28:31 +01005303 status = psa_aead_encrypt_setup( &operation, key, alg );
5304
5305 /* If the operation is not supported, just skip and not fail in case the
5306 * encryption involves a common limitation of cryptography hardwares and
5307 * an alternative implementation. */
5308 if( status == PSA_ERROR_NOT_SUPPORTED )
5309 {
5310 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005311 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005312 }
5313
5314 PSA_ASSERT( status );
5315
Paul Elliott4023ffd2021-09-10 16:21:22 +01005316 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5317 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005318 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005319 /* Arbitrary size buffer, to test zero length valid buffer. */
5320 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005321 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005322 }
5323 else
5324 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005325 /* If length is zero, then this will return NULL. */
5326 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005327 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005328
Paul Elliott4023ffd2021-09-10 16:21:22 +01005329 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005330 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005331 for( index = 0; index < nonce_length - 1; ++index )
5332 {
5333 nonce_buffer[index] = 'a' + index;
5334 }
Paul Elliott66696b52021-08-16 18:42:41 +01005335 }
Paul Elliott863864a2021-07-23 17:28:31 +01005336 }
5337
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005338 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5339 {
5340 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5341 input_data->len ) );
5342 }
5343
Paul Elliott6f0e7202021-08-25 12:57:18 +01005344 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005345
Paul Elliott693bf312021-07-23 17:40:41 +01005346 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005347
5348 if( expected_status == PSA_SUCCESS )
5349 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005350 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5351 {
5352 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5353 input_data->len ) );
5354 }
5355 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5356 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005357
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005358 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5359 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5360 additional_data->len ),
5361 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005362
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005363 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005364 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005365 &ciphertext_length ),
5366 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005367
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005368 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005369 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005370 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5371 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005372 }
5373
5374exit:
5375 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005376 mbedtls_free( output );
5377 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005378 mbedtls_free( nonce_buffer );
5379 psa_aead_abort( &operation );
5380 PSA_DONE( );
5381}
5382/* END_CASE */
5383
5384/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005385void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5386 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005387 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005388 data_t *nonce,
5389 data_t *additional_data,
5390 data_t *input_data,
5391 int expected_status_arg )
5392{
5393
5394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5395 psa_key_type_t key_type = key_type_arg;
5396 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005397 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5399 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5400 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005401 unsigned char *output = NULL;
5402 unsigned char *ciphertext = NULL;
5403 size_t output_size = output_size_arg;
5404 size_t ciphertext_size = 0;
5405 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005406 size_t tag_length = 0;
5407 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5408
5409 PSA_ASSERT( psa_crypto_init( ) );
5410
5411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5412 psa_set_key_algorithm( &attributes, alg );
5413 psa_set_key_type( &attributes, key_type );
5414
5415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5416 &key ) );
5417
5418 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5419
Paul Elliottc6d11d02021-09-01 12:04:23 +01005420 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005421
Paul Elliottc6d11d02021-09-01 12:04:23 +01005422 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005423
Paul Elliottc6d11d02021-09-01 12:04:23 +01005424 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005425
Paul Elliott43fbda62021-07-23 18:30:59 +01005426 status = psa_aead_encrypt_setup( &operation, key, alg );
5427
5428 /* If the operation is not supported, just skip and not fail in case the
5429 * encryption involves a common limitation of cryptography hardwares and
5430 * an alternative implementation. */
5431 if( status == PSA_ERROR_NOT_SUPPORTED )
5432 {
5433 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5434 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5435 }
5436
5437 PSA_ASSERT( status );
5438
Paul Elliott47b9a142021-10-07 15:04:57 +01005439 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5440 input_data->len ) );
5441
Paul Elliott43fbda62021-07-23 18:30:59 +01005442 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5443
5444 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5445 additional_data->len ) );
5446
5447 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005448 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005449
5450 TEST_EQUAL( status, expected_status );
5451
5452 if( expected_status == PSA_SUCCESS )
5453 {
5454 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005455 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5456 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005457 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5458 }
5459
5460exit:
5461 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005462 mbedtls_free( output );
5463 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005464 psa_aead_abort( &operation );
5465 PSA_DONE( );
5466}
5467/* END_CASE */
5468
Paul Elliott91b021e2021-07-23 18:52:31 +01005469/* BEGIN_CASE */
5470void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5471 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005472 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005473 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005474 data_t *nonce,
5475 data_t *additional_data,
5476 data_t *input_data,
5477 int expected_status_arg )
5478{
5479
5480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5481 psa_key_type_t key_type = key_type_arg;
5482 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005483 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5485 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5486 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005487 unsigned char *ciphertext = NULL;
5488 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005489 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005490 size_t ciphertext_size = 0;
5491 size_t ciphertext_length = 0;
5492 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005493 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005494 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005495
5496 PSA_ASSERT( psa_crypto_init( ) );
5497
5498 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5499 psa_set_key_algorithm( &attributes, alg );
5500 psa_set_key_type( &attributes, key_type );
5501
5502 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5503 &key ) );
5504
5505 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5506
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005507 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005508
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005509 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005510
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005511 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005512
Paul Elliott719c1322021-09-13 18:27:22 +01005513 ASSERT_ALLOC( tag_buffer, tag_size );
5514
Paul Elliott91b021e2021-07-23 18:52:31 +01005515 status = psa_aead_encrypt_setup( &operation, key, alg );
5516
5517 /* If the operation is not supported, just skip and not fail in case the
5518 * encryption involves a common limitation of cryptography hardwares and
5519 * an alternative implementation. */
5520 if( status == PSA_ERROR_NOT_SUPPORTED )
5521 {
5522 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5523 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5524 }
5525
5526 PSA_ASSERT( status );
5527
5528 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5529
Paul Elliott76bda482021-10-07 17:07:23 +01005530 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5531 input_data->len ) );
5532
Paul Elliott91b021e2021-07-23 18:52:31 +01005533 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5534 additional_data->len ) );
5535
5536 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005537 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005538
5539 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005540 status = psa_aead_finish( &operation, finish_ciphertext,
5541 finish_ciphertext_size,
5542 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005543 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005544
5545 TEST_EQUAL( status, expected_status );
5546
5547exit:
5548 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005549 mbedtls_free( ciphertext );
5550 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005551 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005552 psa_aead_abort( &operation );
5553 PSA_DONE( );
5554}
5555/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005556
5557/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005558void aead_multipart_verify( int key_type_arg, data_t *key_data,
5559 int alg_arg,
5560 data_t *nonce,
5561 data_t *additional_data,
5562 data_t *input_data,
5563 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005564 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005565 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005566 int expected_status_arg )
5567{
5568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5569 psa_key_type_t key_type = key_type_arg;
5570 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005571 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5573 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5574 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005575 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005576 unsigned char *plaintext = NULL;
5577 unsigned char *finish_plaintext = NULL;
5578 size_t plaintext_size = 0;
5579 size_t plaintext_length = 0;
5580 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005581 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005582 unsigned char *tag_buffer = NULL;
5583 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005584
5585 PSA_ASSERT( psa_crypto_init( ) );
5586
5587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5588 psa_set_key_algorithm( &attributes, alg );
5589 psa_set_key_type( &attributes, key_type );
5590
5591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5592 &key ) );
5593
5594 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5595
5596 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5597 input_data->len );
5598
5599 ASSERT_ALLOC( plaintext, plaintext_size );
5600
5601 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5602
5603 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5604
Paul Elliott9961a662021-09-17 19:19:02 +01005605 status = psa_aead_decrypt_setup( &operation, key, alg );
5606
5607 /* If the operation is not supported, just skip and not fail in case the
5608 * encryption involves a common limitation of cryptography hardwares and
5609 * an alternative implementation. */
5610 if( status == PSA_ERROR_NOT_SUPPORTED )
5611 {
5612 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5613 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5614 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005615 TEST_EQUAL( status, expected_setup_status );
5616
5617 if( status != PSA_SUCCESS )
5618 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005619
5620 PSA_ASSERT( status );
5621
5622 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5623
Paul Elliottfec6f372021-10-06 17:15:02 +01005624 status = psa_aead_set_lengths( &operation, additional_data->len,
5625 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005626 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005627
Paul Elliott9961a662021-09-17 19:19:02 +01005628 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5629 additional_data->len ) );
5630
5631 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5632 input_data->len,
5633 plaintext, plaintext_size,
5634 &plaintext_length ) );
5635
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005636 if( tag_usage == USE_GIVEN_TAG )
5637 {
5638 tag_buffer = tag->x;
5639 tag_size = tag->len;
5640 }
5641
Paul Elliott9961a662021-09-17 19:19:02 +01005642 status = psa_aead_verify( &operation, finish_plaintext,
5643 verify_plaintext_size,
5644 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005645 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005646
5647 TEST_EQUAL( status, expected_status );
5648
5649exit:
5650 psa_destroy_key( key );
5651 mbedtls_free( plaintext );
5652 mbedtls_free( finish_plaintext );
5653 psa_aead_abort( &operation );
5654 PSA_DONE( );
5655}
5656/* END_CASE */
5657
Paul Elliott9961a662021-09-17 19:19:02 +01005658/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005659void aead_multipart_setup( int key_type_arg, data_t *key_data,
5660 int alg_arg, int expected_status_arg )
5661{
5662 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5663 psa_key_type_t key_type = key_type_arg;
5664 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005665 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5667 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5668 psa_status_t expected_status = expected_status_arg;
5669
5670 PSA_ASSERT( psa_crypto_init( ) );
5671
5672 psa_set_key_usage_flags( &attributes,
5673 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5674 psa_set_key_algorithm( &attributes, alg );
5675 psa_set_key_type( &attributes, key_type );
5676
5677 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5678 &key ) );
5679
Paul Elliott5221ef62021-09-19 17:33:03 +01005680 status = psa_aead_encrypt_setup( &operation, key, alg );
5681
5682 TEST_EQUAL( status, expected_status );
5683
5684 psa_aead_abort( &operation );
5685
Paul Elliott5221ef62021-09-19 17:33:03 +01005686 status = psa_aead_decrypt_setup( &operation, key, alg );
5687
5688 TEST_EQUAL(status, expected_status );
5689
5690exit:
5691 psa_destroy_key( key );
5692 psa_aead_abort( &operation );
5693 PSA_DONE( );
5694}
5695/* END_CASE */
5696
5697/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5699 int alg_arg,
5700 data_t *nonce,
5701 data_t *additional_data,
5702 data_t *input_data )
5703{
5704 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5705 psa_key_type_t key_type = key_type_arg;
5706 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005707 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005708 unsigned char *output_data = NULL;
5709 unsigned char *final_data = NULL;
5710 size_t output_size = 0;
5711 size_t finish_output_size = 0;
5712 size_t output_length = 0;
5713 size_t key_bits = 0;
5714 size_t tag_length = 0;
5715 size_t tag_size = 0;
5716 size_t nonce_length = 0;
5717 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5718 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5719 size_t output_part_length = 0;
5720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5721
5722 PSA_ASSERT( psa_crypto_init( ) );
5723
5724 psa_set_key_usage_flags( & attributes,
5725 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5726 psa_set_key_algorithm( & attributes, alg );
5727 psa_set_key_type( & attributes, key_type );
5728
5729 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5730 &key ) );
5731
5732 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5733 key_bits = psa_get_key_bits( &attributes );
5734
5735 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5736
Gilles Peskine7be11a72022-04-14 00:12:57 +02005737 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005738
5739 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5740
5741 ASSERT_ALLOC( output_data, output_size );
5742
5743 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5744
Gilles Peskine7be11a72022-04-14 00:12:57 +02005745 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005746
5747 ASSERT_ALLOC( final_data, finish_output_size );
5748
5749 /* Test all operations error without calling setup first. */
5750
Paul Elliottc23a9a02021-06-21 18:32:46 +01005751 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5752 PSA_ERROR_BAD_STATE );
5753
5754 psa_aead_abort( &operation );
5755
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5757 PSA_AEAD_NONCE_MAX_SIZE,
5758 &nonce_length ),
5759 PSA_ERROR_BAD_STATE );
5760
5761 psa_aead_abort( &operation );
5762
Paul Elliott481be342021-07-16 17:38:47 +01005763 /* ------------------------------------------------------- */
5764
Paul Elliottc23a9a02021-06-21 18:32:46 +01005765 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5766 input_data->len ),
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 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5774 additional_data->len ),
5775 PSA_ERROR_BAD_STATE );
5776
5777 psa_aead_abort( &operation );
5778
Paul Elliott481be342021-07-16 17:38:47 +01005779 /* ------------------------------------------------------- */
5780
Paul Elliottc23a9a02021-06-21 18:32:46 +01005781 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5782 input_data->len, output_data,
5783 output_size, &output_length ),
5784 PSA_ERROR_BAD_STATE );
5785
5786 psa_aead_abort( &operation );
5787
Paul Elliott481be342021-07-16 17:38:47 +01005788 /* ------------------------------------------------------- */
5789
Paul Elliottc23a9a02021-06-21 18:32:46 +01005790 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5791 finish_output_size,
5792 &output_part_length,
5793 tag_buffer, tag_length,
5794 &tag_size ),
5795 PSA_ERROR_BAD_STATE );
5796
5797 psa_aead_abort( &operation );
5798
Paul Elliott481be342021-07-16 17:38:47 +01005799 /* ------------------------------------------------------- */
5800
Paul Elliottc23a9a02021-06-21 18:32:46 +01005801 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5802 finish_output_size,
5803 &output_part_length,
5804 tag_buffer,
5805 tag_length ),
5806 PSA_ERROR_BAD_STATE );
5807
5808 psa_aead_abort( &operation );
5809
5810 /* Test for double setups. */
5811
Paul Elliottc23a9a02021-06-21 18:32:46 +01005812 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5813
5814 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5815 PSA_ERROR_BAD_STATE );
5816
5817 psa_aead_abort( &operation );
5818
Paul Elliott481be342021-07-16 17:38:47 +01005819 /* ------------------------------------------------------- */
5820
Paul Elliottc23a9a02021-06-21 18:32:46 +01005821 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5822
5823 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5824 PSA_ERROR_BAD_STATE );
5825
5826 psa_aead_abort( &operation );
5827
Paul Elliott374a2be2021-07-16 17:53:40 +01005828 /* ------------------------------------------------------- */
5829
Paul Elliott374a2be2021-07-16 17:53:40 +01005830 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5831
5832 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5833 PSA_ERROR_BAD_STATE );
5834
5835 psa_aead_abort( &operation );
5836
5837 /* ------------------------------------------------------- */
5838
Paul Elliott374a2be2021-07-16 17:53:40 +01005839 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5840
5841 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5842 PSA_ERROR_BAD_STATE );
5843
5844 psa_aead_abort( &operation );
5845
Paul Elliottc23a9a02021-06-21 18:32:46 +01005846 /* Test for not setting a nonce. */
5847
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5849
5850 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5851 additional_data->len ),
5852 PSA_ERROR_BAD_STATE );
5853
5854 psa_aead_abort( &operation );
5855
Paul Elliott7f628422021-09-01 12:08:29 +01005856 /* ------------------------------------------------------- */
5857
Paul Elliott7f628422021-09-01 12:08:29 +01005858 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5859
5860 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5861 input_data->len, output_data,
5862 output_size, &output_length ),
5863 PSA_ERROR_BAD_STATE );
5864
5865 psa_aead_abort( &operation );
5866
Paul Elliottbdc2c682021-09-21 18:37:10 +01005867 /* ------------------------------------------------------- */
5868
Paul Elliottbdc2c682021-09-21 18:37:10 +01005869 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5870
5871 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5872 finish_output_size,
5873 &output_part_length,
5874 tag_buffer, tag_length,
5875 &tag_size ),
5876 PSA_ERROR_BAD_STATE );
5877
5878 psa_aead_abort( &operation );
5879
5880 /* ------------------------------------------------------- */
5881
Paul Elliottbdc2c682021-09-21 18:37:10 +01005882 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5883
5884 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5885 finish_output_size,
5886 &output_part_length,
5887 tag_buffer,
5888 tag_length ),
5889 PSA_ERROR_BAD_STATE );
5890
5891 psa_aead_abort( &operation );
5892
Paul Elliottc23a9a02021-06-21 18:32:46 +01005893 /* Test for double setting nonce. */
5894
Paul Elliottc23a9a02021-06-21 18:32:46 +01005895 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5896
5897 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5898
5899 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5900 PSA_ERROR_BAD_STATE );
5901
5902 psa_aead_abort( &operation );
5903
Paul Elliott374a2be2021-07-16 17:53:40 +01005904 /* Test for double generating nonce. */
5905
Paul Elliott374a2be2021-07-16 17:53:40 +01005906 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5907
5908 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5909 PSA_AEAD_NONCE_MAX_SIZE,
5910 &nonce_length ) );
5911
5912 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5913 PSA_AEAD_NONCE_MAX_SIZE,
5914 &nonce_length ),
5915 PSA_ERROR_BAD_STATE );
5916
5917
5918 psa_aead_abort( &operation );
5919
5920 /* Test for generate nonce then set and vice versa */
5921
Paul Elliott374a2be2021-07-16 17:53:40 +01005922 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5923
5924 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5925 PSA_AEAD_NONCE_MAX_SIZE,
5926 &nonce_length ) );
5927
5928 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5929 PSA_ERROR_BAD_STATE );
5930
5931 psa_aead_abort( &operation );
5932
Andrzej Kurekad837522021-12-15 15:28:49 +01005933 /* Test for generating nonce after calling set lengths */
5934
5935 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5936
5937 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5938 input_data->len ) );
5939
5940 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5941 PSA_AEAD_NONCE_MAX_SIZE,
5942 &nonce_length ) );
5943
5944 psa_aead_abort( &operation );
5945
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005946 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005947
5948 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5949
5950 if( operation.alg == PSA_ALG_CCM )
5951 {
5952 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5953 input_data->len ),
5954 PSA_ERROR_INVALID_ARGUMENT );
5955 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5956 PSA_AEAD_NONCE_MAX_SIZE,
5957 &nonce_length ),
5958 PSA_ERROR_BAD_STATE );
5959 }
5960 else
5961 {
5962 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5963 input_data->len ) );
5964 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5965 PSA_AEAD_NONCE_MAX_SIZE,
5966 &nonce_length ) );
5967 }
5968
5969 psa_aead_abort( &operation );
5970
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005971 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005972#if SIZE_MAX > UINT32_MAX
5973 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5974
5975 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5976 {
5977 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5978 input_data->len ),
5979 PSA_ERROR_INVALID_ARGUMENT );
5980 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5981 PSA_AEAD_NONCE_MAX_SIZE,
5982 &nonce_length ),
5983 PSA_ERROR_BAD_STATE );
5984 }
5985 else
5986 {
5987 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5988 input_data->len ) );
5989 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5990 PSA_AEAD_NONCE_MAX_SIZE,
5991 &nonce_length ) );
5992 }
5993
5994 psa_aead_abort( &operation );
5995#endif
5996
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005997 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005998
5999 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6000
6001 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6002 PSA_AEAD_NONCE_MAX_SIZE,
6003 &nonce_length ) );
6004
6005 if( operation.alg == PSA_ALG_CCM )
6006 {
6007 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6008 input_data->len ),
6009 PSA_ERROR_INVALID_ARGUMENT );
6010 }
6011 else
6012 {
6013 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6014 input_data->len ) );
6015 }
6016
6017 psa_aead_abort( &operation );
6018
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006019 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006020 /* Test for setting nonce after calling set lengths */
6021
6022 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6023
6024 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6025 input_data->len ) );
6026
6027 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6028
6029 psa_aead_abort( &operation );
6030
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006031 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006032
6033 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6034
6035 if( operation.alg == PSA_ALG_CCM )
6036 {
6037 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6038 input_data->len ),
6039 PSA_ERROR_INVALID_ARGUMENT );
6040 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6041 PSA_ERROR_BAD_STATE );
6042 }
6043 else
6044 {
6045 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6046 input_data->len ) );
6047 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6048 }
6049
6050 psa_aead_abort( &operation );
6051
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006052 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006053#if SIZE_MAX > UINT32_MAX
6054 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6055
6056 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6057 {
6058 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6059 input_data->len ),
6060 PSA_ERROR_INVALID_ARGUMENT );
6061 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6062 PSA_ERROR_BAD_STATE );
6063 }
6064 else
6065 {
6066 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6067 input_data->len ) );
6068 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6069 }
6070
6071 psa_aead_abort( &operation );
6072#endif
6073
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006074 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006075
6076 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6077
6078 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6079
6080 if( operation.alg == PSA_ALG_CCM )
6081 {
6082 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6083 input_data->len ),
6084 PSA_ERROR_INVALID_ARGUMENT );
6085 }
6086 else
6087 {
6088 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6089 input_data->len ) );
6090 }
6091
6092 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006093
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006094 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006095#if SIZE_MAX > UINT32_MAX
6096 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6097
6098 if( operation.alg == PSA_ALG_GCM )
6099 {
6100 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6101 SIZE_MAX ),
6102 PSA_ERROR_INVALID_ARGUMENT );
6103 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6104 PSA_ERROR_BAD_STATE );
6105 }
6106 else if ( operation.alg != PSA_ALG_CCM )
6107 {
6108 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6109 SIZE_MAX ) );
6110 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6111 }
6112
6113 psa_aead_abort( &operation );
6114
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006115 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006116 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6117
6118 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6119
6120 if( operation.alg == PSA_ALG_GCM )
6121 {
6122 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6123 SIZE_MAX ),
6124 PSA_ERROR_INVALID_ARGUMENT );
6125 }
6126 else if ( operation.alg != PSA_ALG_CCM )
6127 {
6128 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6129 SIZE_MAX ) );
6130 }
6131
6132 psa_aead_abort( &operation );
6133#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006134
6135 /* ------------------------------------------------------- */
6136
Paul Elliott374a2be2021-07-16 17:53:40 +01006137 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6138
6139 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6140
6141 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6142 PSA_AEAD_NONCE_MAX_SIZE,
6143 &nonce_length ),
6144 PSA_ERROR_BAD_STATE );
6145
6146 psa_aead_abort( &operation );
6147
Paul Elliott7220cae2021-06-22 17:25:57 +01006148 /* Test for generating nonce in decrypt setup. */
6149
Paul Elliott7220cae2021-06-22 17:25:57 +01006150 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6151
6152 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6153 PSA_AEAD_NONCE_MAX_SIZE,
6154 &nonce_length ),
6155 PSA_ERROR_BAD_STATE );
6156
6157 psa_aead_abort( &operation );
6158
Paul Elliottc23a9a02021-06-21 18:32:46 +01006159 /* Test for setting lengths twice. */
6160
Paul Elliottc23a9a02021-06-21 18:32:46 +01006161 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6162
6163 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6164
6165 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6166 input_data->len ) );
6167
6168 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6169 input_data->len ),
6170 PSA_ERROR_BAD_STATE );
6171
6172 psa_aead_abort( &operation );
6173
Andrzej Kurekad837522021-12-15 15:28:49 +01006174 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006175
Paul Elliottc23a9a02021-06-21 18:32:46 +01006176 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6177
6178 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6179
Andrzej Kurekad837522021-12-15 15:28:49 +01006180 if( operation.alg == PSA_ALG_CCM )
6181 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006182
Andrzej Kurekad837522021-12-15 15:28:49 +01006183 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6184 additional_data->len ),
6185 PSA_ERROR_BAD_STATE );
6186 }
6187 else
6188 {
6189 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6190 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006191
Andrzej Kurekad837522021-12-15 15:28:49 +01006192 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6193 input_data->len ),
6194 PSA_ERROR_BAD_STATE );
6195 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006196 psa_aead_abort( &operation );
6197
6198 /* ------------------------------------------------------- */
6199
Paul Elliottf94bd992021-09-19 18:15:59 +01006200 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6201
6202 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6203
Andrzej Kurekad837522021-12-15 15:28:49 +01006204 if( operation.alg == PSA_ALG_CCM )
6205 {
6206 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6207 input_data->len, output_data,
6208 output_size, &output_length ),
6209 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006210
Andrzej Kurekad837522021-12-15 15:28:49 +01006211 }
6212 else
6213 {
6214 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6215 input_data->len, output_data,
6216 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006217
Andrzej Kurekad837522021-12-15 15:28:49 +01006218 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6219 input_data->len ),
6220 PSA_ERROR_BAD_STATE );
6221 }
6222 psa_aead_abort( &operation );
6223
6224 /* ------------------------------------------------------- */
6225
6226 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6227
6228 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6229
6230 if( operation.alg == PSA_ALG_CCM )
6231 {
6232 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6233 finish_output_size,
6234 &output_part_length,
6235 tag_buffer, tag_length,
6236 &tag_size ) );
6237 }
6238 else
6239 {
6240 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6241 finish_output_size,
6242 &output_part_length,
6243 tag_buffer, tag_length,
6244 &tag_size ) );
6245
6246 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6247 input_data->len ),
6248 PSA_ERROR_BAD_STATE );
6249 }
6250 psa_aead_abort( &operation );
6251
6252 /* Test for setting lengths after generating nonce + already starting data. */
6253
6254 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6255
6256 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6257 PSA_AEAD_NONCE_MAX_SIZE,
6258 &nonce_length ) );
6259 if( operation.alg == PSA_ALG_CCM )
6260 {
6261
6262 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6263 additional_data->len ),
6264 PSA_ERROR_BAD_STATE );
6265 }
6266 else
6267 {
6268 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6269 additional_data->len ) );
6270
6271 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6272 input_data->len ),
6273 PSA_ERROR_BAD_STATE );
6274 }
6275 psa_aead_abort( &operation );
6276
6277 /* ------------------------------------------------------- */
6278
6279 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6280
6281 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6282 PSA_AEAD_NONCE_MAX_SIZE,
6283 &nonce_length ) );
6284 if( operation.alg == PSA_ALG_CCM )
6285 {
6286 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6287 input_data->len, output_data,
6288 output_size, &output_length ),
6289 PSA_ERROR_BAD_STATE );
6290
6291 }
6292 else
6293 {
6294 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6295 input_data->len, output_data,
6296 output_size, &output_length ) );
6297
6298 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6299 input_data->len ),
6300 PSA_ERROR_BAD_STATE );
6301 }
6302 psa_aead_abort( &operation );
6303
6304 /* ------------------------------------------------------- */
6305
6306 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6307
6308 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6309 PSA_AEAD_NONCE_MAX_SIZE,
6310 &nonce_length ) );
6311 if( operation.alg == PSA_ALG_CCM )
6312 {
6313 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6314 finish_output_size,
6315 &output_part_length,
6316 tag_buffer, tag_length,
6317 &tag_size ) );
6318 }
6319 else
6320 {
6321 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6322 finish_output_size,
6323 &output_part_length,
6324 tag_buffer, tag_length,
6325 &tag_size ) );
6326
6327 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6328 input_data->len ),
6329 PSA_ERROR_BAD_STATE );
6330 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006331 psa_aead_abort( &operation );
6332
Paul Elliott243080c2021-07-21 19:01:17 +01006333 /* Test for not sending any additional data or data after setting non zero
6334 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006335
Paul Elliottc23a9a02021-06-21 18:32:46 +01006336 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6337
6338 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6339
6340 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6341 input_data->len ) );
6342
6343 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6344 finish_output_size,
6345 &output_part_length,
6346 tag_buffer, tag_length,
6347 &tag_size ),
6348 PSA_ERROR_INVALID_ARGUMENT );
6349
6350 psa_aead_abort( &operation );
6351
Paul Elliott243080c2021-07-21 19:01:17 +01006352 /* Test for not sending any additional data or data after setting non-zero
6353 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006354
Paul Elliottc23a9a02021-06-21 18:32:46 +01006355 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6356
6357 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6358
6359 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6360 input_data->len ) );
6361
6362 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6363 finish_output_size,
6364 &output_part_length,
6365 tag_buffer,
6366 tag_length ),
6367 PSA_ERROR_INVALID_ARGUMENT );
6368
6369 psa_aead_abort( &operation );
6370
Paul Elliott243080c2021-07-21 19:01:17 +01006371 /* Test for not sending any additional data after setting a non-zero length
6372 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006373
Paul Elliottc23a9a02021-06-21 18:32:46 +01006374 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6375
6376 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6377
6378 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6379 input_data->len ) );
6380
6381 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6382 input_data->len, output_data,
6383 output_size, &output_length ),
6384 PSA_ERROR_INVALID_ARGUMENT );
6385
6386 psa_aead_abort( &operation );
6387
Paul Elliottf94bd992021-09-19 18:15:59 +01006388 /* Test for not sending any data after setting a non-zero length for it.*/
6389
Paul Elliottf94bd992021-09-19 18:15:59 +01006390 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6391
6392 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6393
6394 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6395 input_data->len ) );
6396
6397 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6398 additional_data->len ) );
6399
6400 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6401 finish_output_size,
6402 &output_part_length,
6403 tag_buffer, tag_length,
6404 &tag_size ),
6405 PSA_ERROR_INVALID_ARGUMENT );
6406
6407 psa_aead_abort( &operation );
6408
Paul Elliottb0450fe2021-09-01 15:06:26 +01006409 /* Test for sending too much additional data after setting lengths. */
6410
Paul Elliottb0450fe2021-09-01 15:06:26 +01006411 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, 0, 0 ) );
6416
6417
6418 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6419 additional_data->len ),
6420 PSA_ERROR_INVALID_ARGUMENT );
6421
6422 psa_aead_abort( &operation );
6423
Paul Elliotta2a09b02021-09-22 14:56:40 +01006424 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006425
6426 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6427
6428 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6429
6430 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6431 input_data->len ) );
6432
6433 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6434 additional_data->len ) );
6435
6436 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6437 1 ),
6438 PSA_ERROR_INVALID_ARGUMENT );
6439
6440 psa_aead_abort( &operation );
6441
Paul Elliottb0450fe2021-09-01 15:06:26 +01006442 /* Test for sending too much data after setting lengths. */
6443
Paul Elliottb0450fe2021-09-01 15:06:26 +01006444 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6445
6446 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6447
6448 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6449
6450 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6451 input_data->len, output_data,
6452 output_size, &output_length ),
6453 PSA_ERROR_INVALID_ARGUMENT );
6454
6455 psa_aead_abort( &operation );
6456
Paul Elliotta2a09b02021-09-22 14:56:40 +01006457 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006458
6459 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6460
6461 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6462
6463 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6464 input_data->len ) );
6465
6466 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6467 additional_data->len ) );
6468
6469 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6470 input_data->len, output_data,
6471 output_size, &output_length ) );
6472
6473 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6474 1, output_data,
6475 output_size, &output_length ),
6476 PSA_ERROR_INVALID_ARGUMENT );
6477
6478 psa_aead_abort( &operation );
6479
Paul Elliottc23a9a02021-06-21 18:32:46 +01006480 /* Test sending additional data after data. */
6481
Paul Elliottc23a9a02021-06-21 18:32:46 +01006482 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6483
6484 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6485
Andrzej Kurekad837522021-12-15 15:28:49 +01006486 if( operation.alg != PSA_ALG_CCM )
6487 {
6488 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6489 input_data->len, output_data,
6490 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006491
Andrzej Kurekad837522021-12-15 15:28:49 +01006492 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6493 additional_data->len ),
6494 PSA_ERROR_BAD_STATE );
6495 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006496 psa_aead_abort( &operation );
6497
Paul Elliott534d0b42021-06-22 19:15:20 +01006498 /* Test calling finish on decryption. */
6499
Paul Elliott534d0b42021-06-22 19:15:20 +01006500 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6501
6502 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6503
6504 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6505 finish_output_size,
6506 &output_part_length,
6507 tag_buffer, tag_length,
6508 &tag_size ),
6509 PSA_ERROR_BAD_STATE );
6510
6511 psa_aead_abort( &operation );
6512
6513 /* Test calling verify on encryption. */
6514
Paul Elliott534d0b42021-06-22 19:15:20 +01006515 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6516
6517 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6518
6519 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6520 finish_output_size,
6521 &output_part_length,
6522 tag_buffer,
6523 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006524 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006525
6526 psa_aead_abort( &operation );
6527
6528
Paul Elliottc23a9a02021-06-21 18:32:46 +01006529exit:
6530 psa_destroy_key( key );
6531 psa_aead_abort( &operation );
6532 mbedtls_free( output_data );
6533 mbedtls_free( final_data );
6534 PSA_DONE( );
6535}
6536/* END_CASE */
6537
6538/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006539void signature_size( int type_arg,
6540 int bits,
6541 int alg_arg,
6542 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006543{
6544 psa_key_type_t type = type_arg;
6545 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006546 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006547
Gilles Peskinefe11b722018-12-18 00:24:04 +01006548 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006549
Gilles Peskinee59236f2018-01-27 23:32:46 +01006550exit:
6551 ;
6552}
6553/* END_CASE */
6554
6555/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006556void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6557 int alg_arg, data_t *input_data,
6558 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006559{
Ronald Cron5425a212020-08-04 14:58:35 +02006560 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006561 psa_key_type_t key_type = key_type_arg;
6562 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006563 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006564 unsigned char *signature = NULL;
6565 size_t signature_size;
6566 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006568
Gilles Peskine8817f612018-12-18 00:18:46 +01006569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006570
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006572 psa_set_key_algorithm( &attributes, alg );
6573 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006574
Gilles Peskine049c7532019-05-15 20:22:09 +02006575 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006576 &key ) );
6577 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006578 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006579
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006580 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006581 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006582 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006583 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006584 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006585 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006586 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006587
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006588 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006589 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006590 input_data->x, input_data->len,
6591 signature, signature_size,
6592 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006593 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006594 ASSERT_COMPARE( output_data->x, output_data->len,
6595 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006596
6597exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006598 /*
6599 * Key attributes may have been returned by psa_get_key_attributes()
6600 * thus reset them as required.
6601 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006602 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006603
Ronald Cron5425a212020-08-04 14:58:35 +02006604 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006605 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006606 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006607}
6608/* END_CASE */
6609
6610/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006611void sign_hash_fail( int key_type_arg, data_t *key_data,
6612 int alg_arg, data_t *input_data,
6613 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006614{
Ronald Cron5425a212020-08-04 14:58:35 +02006615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006616 psa_key_type_t key_type = key_type_arg;
6617 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006618 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006619 psa_status_t actual_status;
6620 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006621 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006622 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006624
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006625 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006626
Gilles Peskine8817f612018-12-18 00:18:46 +01006627 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006628
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006629 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006630 psa_set_key_algorithm( &attributes, alg );
6631 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006632
Gilles Peskine049c7532019-05-15 20:22:09 +02006633 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006634 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006635
Ronald Cron5425a212020-08-04 14:58:35 +02006636 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006637 input_data->x, input_data->len,
6638 signature, signature_size,
6639 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006640 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006641 /* The value of *signature_length is unspecified on error, but
6642 * whatever it is, it should be less than signature_size, so that
6643 * if the caller tries to read *signature_length bytes without
6644 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006645 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006646
6647exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006648 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006649 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006650 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006651 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006652}
6653/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006654
6655/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006656void sign_verify_hash( int key_type_arg, data_t *key_data,
6657 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006658{
Ronald Cron5425a212020-08-04 14:58:35 +02006659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006660 psa_key_type_t key_type = key_type_arg;
6661 psa_algorithm_t alg = alg_arg;
6662 size_t key_bits;
6663 unsigned char *signature = NULL;
6664 size_t signature_size;
6665 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006667
Gilles Peskine8817f612018-12-18 00:18:46 +01006668 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006669
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006670 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006671 psa_set_key_algorithm( &attributes, alg );
6672 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006673
Gilles Peskine049c7532019-05-15 20:22:09 +02006674 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006675 &key ) );
6676 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006677 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006678
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006679 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006680 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006681 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006682 key_bits, alg );
6683 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006684 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006685 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006686
6687 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006688 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006689 input_data->x, input_data->len,
6690 signature, signature_size,
6691 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006692 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006693 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006694 TEST_ASSERT( signature_length > 0 );
6695
6696 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006697 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006698 input_data->x, input_data->len,
6699 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006700
6701 if( input_data->len != 0 )
6702 {
6703 /* Flip a bit in the input and verify that the signature is now
6704 * detected as invalid. Flip a bit at the beginning, not at the end,
6705 * because ECDSA may ignore the last few bits of the input. */
6706 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006707 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006708 input_data->x, input_data->len,
6709 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006710 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006711 }
6712
6713exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006714 /*
6715 * Key attributes may have been returned by psa_get_key_attributes()
6716 * thus reset them as required.
6717 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006718 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006719
Ronald Cron5425a212020-08-04 14:58:35 +02006720 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006721 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006722 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006723}
6724/* END_CASE */
6725
6726/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006727void verify_hash( int key_type_arg, data_t *key_data,
6728 int alg_arg, data_t *hash_data,
6729 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006730{
Ronald Cron5425a212020-08-04 14:58:35 +02006731 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006732 psa_key_type_t key_type = key_type_arg;
6733 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006735
Gilles Peskine7be11a72022-04-14 00:12:57 +02006736 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006737
Gilles Peskine8817f612018-12-18 00:18:46 +01006738 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006739
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006740 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006741 psa_set_key_algorithm( &attributes, alg );
6742 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006743
Gilles Peskine049c7532019-05-15 20:22:09 +02006744 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006745 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006746
Ronald Cron5425a212020-08-04 14:58:35 +02006747 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006748 hash_data->x, hash_data->len,
6749 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006750
itayzafrir5c753392018-05-08 11:18:38 +03006751exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006752 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006753 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006754 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006755}
6756/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006757
6758/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006759void verify_hash_fail( int key_type_arg, data_t *key_data,
6760 int alg_arg, data_t *hash_data,
6761 data_t *signature_data,
6762 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006763{
Ronald Cron5425a212020-08-04 14:58:35 +02006764 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006765 psa_key_type_t key_type = key_type_arg;
6766 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006767 psa_status_t actual_status;
6768 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006769 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006770
Gilles Peskine8817f612018-12-18 00:18:46 +01006771 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006772
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006773 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006774 psa_set_key_algorithm( &attributes, alg );
6775 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006776
Gilles Peskine049c7532019-05-15 20:22:09 +02006777 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006778 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006779
Ronald Cron5425a212020-08-04 14:58:35 +02006780 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006781 hash_data->x, hash_data->len,
6782 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006783 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006784
6785exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006786 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006787 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006788 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006789}
6790/* END_CASE */
6791
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006792/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006793void sign_message_deterministic( int key_type_arg,
6794 data_t *key_data,
6795 int alg_arg,
6796 data_t *input_data,
6797 data_t *output_data )
6798{
6799 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6800 psa_key_type_t key_type = key_type_arg;
6801 psa_algorithm_t alg = alg_arg;
6802 size_t key_bits;
6803 unsigned char *signature = NULL;
6804 size_t signature_size;
6805 size_t signature_length = 0xdeadbeef;
6806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6807
6808 PSA_ASSERT( psa_crypto_init( ) );
6809
6810 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6811 psa_set_key_algorithm( &attributes, alg );
6812 psa_set_key_type( &attributes, key_type );
6813
6814 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6815 &key ) );
6816 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6817 key_bits = psa_get_key_bits( &attributes );
6818
6819 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6820 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006821 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006822 ASSERT_ALLOC( signature, signature_size );
6823
6824 PSA_ASSERT( psa_sign_message( key, alg,
6825 input_data->x, input_data->len,
6826 signature, signature_size,
6827 &signature_length ) );
6828
6829 ASSERT_COMPARE( output_data->x, output_data->len,
6830 signature, signature_length );
6831
6832exit:
6833 psa_reset_key_attributes( &attributes );
6834
6835 psa_destroy_key( key );
6836 mbedtls_free( signature );
6837 PSA_DONE( );
6838
6839}
6840/* END_CASE */
6841
6842/* BEGIN_CASE */
6843void sign_message_fail( int key_type_arg,
6844 data_t *key_data,
6845 int alg_arg,
6846 data_t *input_data,
6847 int signature_size_arg,
6848 int expected_status_arg )
6849{
6850 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6851 psa_key_type_t key_type = key_type_arg;
6852 psa_algorithm_t alg = alg_arg;
6853 size_t signature_size = signature_size_arg;
6854 psa_status_t actual_status;
6855 psa_status_t expected_status = expected_status_arg;
6856 unsigned char *signature = NULL;
6857 size_t signature_length = 0xdeadbeef;
6858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6859
6860 ASSERT_ALLOC( signature, signature_size );
6861
6862 PSA_ASSERT( psa_crypto_init( ) );
6863
6864 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6865 psa_set_key_algorithm( &attributes, alg );
6866 psa_set_key_type( &attributes, key_type );
6867
6868 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6869 &key ) );
6870
6871 actual_status = psa_sign_message( key, alg,
6872 input_data->x, input_data->len,
6873 signature, signature_size,
6874 &signature_length );
6875 TEST_EQUAL( actual_status, expected_status );
6876 /* The value of *signature_length is unspecified on error, but
6877 * whatever it is, it should be less than signature_size, so that
6878 * if the caller tries to read *signature_length bytes without
6879 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006880 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006881
6882exit:
6883 psa_reset_key_attributes( &attributes );
6884 psa_destroy_key( key );
6885 mbedtls_free( signature );
6886 PSA_DONE( );
6887}
6888/* END_CASE */
6889
6890/* BEGIN_CASE */
6891void sign_verify_message( int key_type_arg,
6892 data_t *key_data,
6893 int alg_arg,
6894 data_t *input_data )
6895{
6896 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6897 psa_key_type_t key_type = key_type_arg;
6898 psa_algorithm_t alg = alg_arg;
6899 size_t key_bits;
6900 unsigned char *signature = NULL;
6901 size_t signature_size;
6902 size_t signature_length = 0xdeadbeef;
6903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6904
6905 PSA_ASSERT( psa_crypto_init( ) );
6906
6907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6908 PSA_KEY_USAGE_VERIFY_MESSAGE );
6909 psa_set_key_algorithm( &attributes, alg );
6910 psa_set_key_type( &attributes, key_type );
6911
6912 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6913 &key ) );
6914 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6915 key_bits = psa_get_key_bits( &attributes );
6916
6917 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6918 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006919 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006920 ASSERT_ALLOC( signature, signature_size );
6921
6922 PSA_ASSERT( psa_sign_message( key, alg,
6923 input_data->x, input_data->len,
6924 signature, signature_size,
6925 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006926 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006927 TEST_ASSERT( signature_length > 0 );
6928
6929 PSA_ASSERT( psa_verify_message( key, alg,
6930 input_data->x, input_data->len,
6931 signature, signature_length ) );
6932
6933 if( input_data->len != 0 )
6934 {
6935 /* Flip a bit in the input and verify that the signature is now
6936 * detected as invalid. Flip a bit at the beginning, not at the end,
6937 * because ECDSA may ignore the last few bits of the input. */
6938 input_data->x[0] ^= 1;
6939 TEST_EQUAL( psa_verify_message( key, alg,
6940 input_data->x, input_data->len,
6941 signature, signature_length ),
6942 PSA_ERROR_INVALID_SIGNATURE );
6943 }
6944
6945exit:
6946 psa_reset_key_attributes( &attributes );
6947
6948 psa_destroy_key( key );
6949 mbedtls_free( signature );
6950 PSA_DONE( );
6951}
6952/* END_CASE */
6953
6954/* BEGIN_CASE */
6955void verify_message( int key_type_arg,
6956 data_t *key_data,
6957 int alg_arg,
6958 data_t *input_data,
6959 data_t *signature_data )
6960{
6961 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6962 psa_key_type_t key_type = key_type_arg;
6963 psa_algorithm_t alg = alg_arg;
6964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6965
Gilles Peskine7be11a72022-04-14 00:12:57 +02006966 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006967
6968 PSA_ASSERT( psa_crypto_init( ) );
6969
6970 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6971 psa_set_key_algorithm( &attributes, alg );
6972 psa_set_key_type( &attributes, key_type );
6973
6974 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6975 &key ) );
6976
6977 PSA_ASSERT( psa_verify_message( key, alg,
6978 input_data->x, input_data->len,
6979 signature_data->x, signature_data->len ) );
6980
6981exit:
6982 psa_reset_key_attributes( &attributes );
6983 psa_destroy_key( key );
6984 PSA_DONE( );
6985}
6986/* END_CASE */
6987
6988/* BEGIN_CASE */
6989void verify_message_fail( int key_type_arg,
6990 data_t *key_data,
6991 int alg_arg,
6992 data_t *hash_data,
6993 data_t *signature_data,
6994 int expected_status_arg )
6995{
6996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6997 psa_key_type_t key_type = key_type_arg;
6998 psa_algorithm_t alg = alg_arg;
6999 psa_status_t actual_status;
7000 psa_status_t expected_status = expected_status_arg;
7001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7002
7003 PSA_ASSERT( psa_crypto_init( ) );
7004
7005 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
7006 psa_set_key_algorithm( &attributes, alg );
7007 psa_set_key_type( &attributes, key_type );
7008
7009 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
7010 &key ) );
7011
7012 actual_status = psa_verify_message( key, alg,
7013 hash_data->x, hash_data->len,
7014 signature_data->x,
7015 signature_data->len );
7016 TEST_EQUAL( actual_status, expected_status );
7017
7018exit:
7019 psa_reset_key_attributes( &attributes );
7020 psa_destroy_key( key );
7021 PSA_DONE( );
7022}
7023/* END_CASE */
7024
7025/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007026void asymmetric_encrypt( int key_type_arg,
7027 data_t *key_data,
7028 int alg_arg,
7029 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007030 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007031 int expected_output_length_arg,
7032 int expected_status_arg )
7033{
Ronald Cron5425a212020-08-04 14:58:35 +02007034 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007035 psa_key_type_t key_type = key_type_arg;
7036 psa_algorithm_t alg = alg_arg;
7037 size_t expected_output_length = expected_output_length_arg;
7038 size_t key_bits;
7039 unsigned char *output = NULL;
7040 size_t output_size;
7041 size_t output_length = ~0;
7042 psa_status_t actual_status;
7043 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007045
Gilles Peskine8817f612018-12-18 00:18:46 +01007046 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007047
Gilles Peskine656896e2018-06-29 19:12:28 +02007048 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007049 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7050 psa_set_key_algorithm( &attributes, alg );
7051 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007052 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007053 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007054
7055 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007056 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007057 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007058
Gilles Peskine656896e2018-06-29 19:12:28 +02007059 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007060 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007061 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007062
7063 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007064 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007065 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007066 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007067 output, output_size,
7068 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007069 TEST_EQUAL( actual_status, expected_status );
7070 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007071
Gilles Peskine68428122018-06-30 18:42:41 +02007072 /* If the label is empty, the test framework puts a non-null pointer
7073 * in label->x. Test that a null pointer works as well. */
7074 if( label->len == 0 )
7075 {
7076 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007077 if( output_size != 0 )
7078 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007079 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007080 input_data->x, input_data->len,
7081 NULL, label->len,
7082 output, output_size,
7083 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007084 TEST_EQUAL( actual_status, expected_status );
7085 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007086 }
7087
Gilles Peskine656896e2018-06-29 19:12:28 +02007088exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007089 /*
7090 * Key attributes may have been returned by psa_get_key_attributes()
7091 * thus reset them as required.
7092 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007093 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007094
Ronald Cron5425a212020-08-04 14:58:35 +02007095 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007096 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007097 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007098}
7099/* END_CASE */
7100
7101/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007102void asymmetric_encrypt_decrypt( int key_type_arg,
7103 data_t *key_data,
7104 int alg_arg,
7105 data_t *input_data,
7106 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007107{
Ronald Cron5425a212020-08-04 14:58:35 +02007108 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007109 psa_key_type_t key_type = key_type_arg;
7110 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007111 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007112 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007113 size_t output_size;
7114 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007115 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007116 size_t output2_size;
7117 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007119
Gilles Peskine8817f612018-12-18 00:18:46 +01007120 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007121
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007122 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7123 psa_set_key_algorithm( &attributes, alg );
7124 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007125
Gilles Peskine049c7532019-05-15 20:22:09 +02007126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007127 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007128
7129 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007130 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007131 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007132
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007133 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007134 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007135 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007136
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007137 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007138 TEST_LE_U( output2_size,
7139 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7140 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007141 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007142
Gilles Peskineeebd7382018-06-08 18:11:54 +02007143 /* We test encryption by checking that encrypt-then-decrypt gives back
7144 * the original plaintext because of the non-optional random
7145 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007146 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007147 input_data->x, input_data->len,
7148 label->x, label->len,
7149 output, output_size,
7150 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007151 /* We don't know what ciphertext length to expect, but check that
7152 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007153 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007154
Ronald Cron5425a212020-08-04 14:58:35 +02007155 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007156 output, output_length,
7157 label->x, label->len,
7158 output2, output2_size,
7159 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007160 ASSERT_COMPARE( input_data->x, input_data->len,
7161 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007162
7163exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007164 /*
7165 * Key attributes may have been returned by psa_get_key_attributes()
7166 * thus reset them as required.
7167 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007168 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007169
Ronald Cron5425a212020-08-04 14:58:35 +02007170 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007171 mbedtls_free( output );
7172 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007173 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007174}
7175/* END_CASE */
7176
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007177/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007178void asymmetric_decrypt( int key_type_arg,
7179 data_t *key_data,
7180 int alg_arg,
7181 data_t *input_data,
7182 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007183 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007184{
Ronald Cron5425a212020-08-04 14:58:35 +02007185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007186 psa_key_type_t key_type = key_type_arg;
7187 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007188 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007189 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007190 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007191 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007193
Gilles Peskine8817f612018-12-18 00:18:46 +01007194 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007195
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007196 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7197 psa_set_key_algorithm( &attributes, alg );
7198 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007199
Gilles Peskine049c7532019-05-15 20:22:09 +02007200 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007201 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007202
gabor-mezei-armceface22021-01-21 12:26:17 +01007203 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7204 key_bits = psa_get_key_bits( &attributes );
7205
7206 /* Determine the maximum ciphertext length */
7207 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007208 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007209 ASSERT_ALLOC( output, output_size );
7210
Ronald Cron5425a212020-08-04 14:58:35 +02007211 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007212 input_data->x, input_data->len,
7213 label->x, label->len,
7214 output,
7215 output_size,
7216 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007217 ASSERT_COMPARE( expected_data->x, expected_data->len,
7218 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007219
Gilles Peskine68428122018-06-30 18:42:41 +02007220 /* If the label is empty, the test framework puts a non-null pointer
7221 * in label->x. Test that a null pointer works as well. */
7222 if( label->len == 0 )
7223 {
7224 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007225 if( output_size != 0 )
7226 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007227 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007228 input_data->x, input_data->len,
7229 NULL, label->len,
7230 output,
7231 output_size,
7232 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007233 ASSERT_COMPARE( expected_data->x, expected_data->len,
7234 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007235 }
7236
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007237exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007238 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007239 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007240 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007241 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007242}
7243/* END_CASE */
7244
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007245/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007246void asymmetric_decrypt_fail( int key_type_arg,
7247 data_t *key_data,
7248 int alg_arg,
7249 data_t *input_data,
7250 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007251 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007252 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007253{
Ronald Cron5425a212020-08-04 14:58:35 +02007254 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007255 psa_key_type_t key_type = key_type_arg;
7256 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007257 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007258 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007259 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007260 psa_status_t actual_status;
7261 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007262 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007263
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007264 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007265
Gilles Peskine8817f612018-12-18 00:18:46 +01007266 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007267
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007268 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7269 psa_set_key_algorithm( &attributes, alg );
7270 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007271
Gilles Peskine049c7532019-05-15 20:22:09 +02007272 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007273 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007274
Ronald Cron5425a212020-08-04 14:58:35 +02007275 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007276 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007277 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007278 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007279 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007280 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007281 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007282
Gilles Peskine68428122018-06-30 18:42:41 +02007283 /* If the label is empty, the test framework puts a non-null pointer
7284 * in label->x. Test that a null pointer works as well. */
7285 if( label->len == 0 )
7286 {
7287 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007288 if( output_size != 0 )
7289 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007290 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007291 input_data->x, input_data->len,
7292 NULL, label->len,
7293 output, output_size,
7294 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007295 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007296 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007297 }
7298
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007299exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007300 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007301 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007302 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007303 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007304}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007305/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007306
7307/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007308void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007309{
7310 /* Test each valid way of initializing the object, except for `= {0}`, as
7311 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7312 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007313 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007314 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007315 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7316 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7317 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007318
7319 memset( &zero, 0, sizeof( zero ) );
7320
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007321 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007322 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007323 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007324 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007325 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007326 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007327 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007328
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007329 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007330 PSA_ASSERT( psa_key_derivation_abort(&func) );
7331 PSA_ASSERT( psa_key_derivation_abort(&init) );
7332 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007333}
7334/* END_CASE */
7335
Janos Follath16de4a42019-06-13 16:32:24 +01007336/* BEGIN_CASE */
7337void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007338{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007339 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007340 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007341 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007342
Gilles Peskine8817f612018-12-18 00:18:46 +01007343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007344
Janos Follath16de4a42019-06-13 16:32:24 +01007345 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007346 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007347
7348exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007349 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007350 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007351}
7352/* END_CASE */
7353
Janos Follathaf3c2a02019-06-12 12:34:34 +01007354/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007355void derive_set_capacity( int alg_arg, int capacity_arg,
7356 int expected_status_arg )
7357{
7358 psa_algorithm_t alg = alg_arg;
7359 size_t capacity = capacity_arg;
7360 psa_status_t expected_status = expected_status_arg;
7361 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7362
7363 PSA_ASSERT( psa_crypto_init( ) );
7364
7365 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7366
7367 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7368 expected_status );
7369
7370exit:
7371 psa_key_derivation_abort( &operation );
7372 PSA_DONE( );
7373}
7374/* END_CASE */
7375
7376/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007377void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007378 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007379 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007380 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007381 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007382 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007383 int expected_status_arg3,
7384 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007385{
7386 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007387 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7388 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007389 psa_status_t expected_statuses[] = {expected_status_arg1,
7390 expected_status_arg2,
7391 expected_status_arg3};
7392 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007393 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7394 MBEDTLS_SVC_KEY_ID_INIT,
7395 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007396 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7398 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007399 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007400 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007401 psa_status_t expected_output_status = expected_output_status_arg;
7402 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007403
7404 PSA_ASSERT( psa_crypto_init( ) );
7405
7406 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7407 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007408
7409 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7410
7411 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7412 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007413 mbedtls_test_set_step( i );
7414 if( steps[i] == 0 )
7415 {
7416 /* Skip this step */
7417 }
7418 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007419 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007420 psa_set_key_type( &attributes, key_types[i] );
7421 PSA_ASSERT( psa_import_key( &attributes,
7422 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007423 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007424 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7425 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7426 {
7427 // When taking a private key as secret input, use key agreement
7428 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007429 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7430 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007431 expected_statuses[i] );
7432 }
7433 else
7434 {
7435 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007436 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007437 expected_statuses[i] );
7438 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007439 }
7440 else
7441 {
7442 TEST_EQUAL( psa_key_derivation_input_bytes(
7443 &operation, steps[i],
7444 inputs[i]->x, inputs[i]->len ),
7445 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007446 }
7447 }
7448
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007449 if( output_key_type != PSA_KEY_TYPE_NONE )
7450 {
7451 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007452 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007453 psa_set_key_bits( &attributes, 8 );
7454 actual_output_status =
7455 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007456 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007457 }
7458 else
7459 {
7460 uint8_t buffer[1];
7461 actual_output_status =
7462 psa_key_derivation_output_bytes( &operation,
7463 buffer, sizeof( buffer ) );
7464 }
7465 TEST_EQUAL( actual_output_status, expected_output_status );
7466
Janos Follathaf3c2a02019-06-12 12:34:34 +01007467exit:
7468 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007469 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7470 psa_destroy_key( keys[i] );
7471 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007472 PSA_DONE( );
7473}
7474/* END_CASE */
7475
Janos Follathd958bb72019-07-03 15:02:16 +01007476/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007477void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007478{
Janos Follathd958bb72019-07-03 15:02:16 +01007479 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007480 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007481 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007482 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007483 unsigned char input1[] = "Input 1";
7484 size_t input1_length = sizeof( input1 );
7485 unsigned char input2[] = "Input 2";
7486 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007487 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007488 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007489 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7490 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7491 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007493
Gilles Peskine8817f612018-12-18 00:18:46 +01007494 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007495
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007496 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7497 psa_set_key_algorithm( &attributes, alg );
7498 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007499
Gilles Peskine73676cb2019-05-15 20:15:10 +02007500 PSA_ASSERT( psa_import_key( &attributes,
7501 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007502 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007503
7504 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007505 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7506 input1, input1_length,
7507 input2, input2_length,
7508 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007509 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007510
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007511 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007512 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007513 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007514
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007515 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007516
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007517 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007518 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007519
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007520exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007521 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007522 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007523 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007524}
7525/* END_CASE */
7526
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007527/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007528void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007529{
7530 uint8_t output_buffer[16];
7531 size_t buffer_size = 16;
7532 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007533 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007534
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007535 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7536 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007537 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007538
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007539 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007540 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007541
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007542 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007543
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007544 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7545 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007546 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007547
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007548 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007549 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007550
7551exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007552 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007553}
7554/* END_CASE */
7555
7556/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007557void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007558 int step1_arg, data_t *input1, int expected_status_arg1,
7559 int step2_arg, data_t *input2, int expected_status_arg2,
7560 int step3_arg, data_t *input3, int expected_status_arg3,
7561 int step4_arg, data_t *input4, int expected_status_arg4,
7562 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007563 int requested_capacity_arg,
7564 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007565 data_t *expected_output2,
7566 int other_key_input_type,
7567 int key_input_type,
7568 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007569{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007570 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007571 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7572 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007573 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7574 MBEDTLS_SVC_KEY_ID_INIT,
7575 MBEDTLS_SVC_KEY_ID_INIT,
7576 MBEDTLS_SVC_KEY_ID_INIT};
7577 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7578 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007579 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007580 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007581 uint8_t *expected_outputs[2] =
7582 {expected_output1->x, expected_output2->x};
7583 size_t output_sizes[2] =
7584 {expected_output1->len, expected_output2->len};
7585 size_t output_buffer_size = 0;
7586 uint8_t *output_buffer = NULL;
7587 size_t expected_capacity;
7588 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007589 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7590 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7591 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7592 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007593 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007594 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007595 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007596
7597 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7598 {
7599 if( output_sizes[i] > output_buffer_size )
7600 output_buffer_size = output_sizes[i];
7601 if( output_sizes[i] == 0 )
7602 expected_outputs[i] = NULL;
7603 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007604 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007605 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007606
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007607 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007608 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7609 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7610 requested_capacity ) );
7611 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007612 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007613 switch( steps[i] )
7614 {
7615 case 0:
7616 break;
7617 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007618 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007619 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007620 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007621 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007622 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007623 inputs[i]->x, inputs[i]->len ),
7624 statuses[i] );
7625
7626 if( statuses[i] != PSA_SUCCESS )
7627 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007628 break;
7629 case 1: // input key
7630 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7631 psa_set_key_algorithm( &attributes1, alg );
7632 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7633
7634 PSA_ASSERT( psa_import_key( &attributes1,
7635 inputs[i]->x, inputs[i]->len,
7636 &keys[i] ) );
7637
Przemek Stekiel38647de2022-04-19 13:27:47 +02007638 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007639 {
7640 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007641 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7642 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007643 }
7644
Przemek Stekiel38647de2022-04-19 13:27:47 +02007645 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7646 steps[i],
7647 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007648 break;
7649 default:
7650 TEST_ASSERT( ! "default case not supported" );
7651 break;
7652 }
7653 break;
7654 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007655 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007656 {
7657 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007658 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7659 steps[i],
7660 inputs[i]->x,
7661 inputs[i]->len ),
7662 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007663 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007664 case 1: // input key, type DERIVE
7665 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007666 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7667 psa_set_key_algorithm( &attributes2, alg );
7668 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7669
7670 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007671 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007672 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7673
7674 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007675 inputs[i]->x, inputs[i]->len,
7676 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007677
Przemek Stekiel38647de2022-04-19 13:27:47 +02007678 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7679 steps[i],
7680 keys[i] ),
7681 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007682 break;
7683 case 2: // key agreement
7684 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7685 psa_set_key_algorithm( &attributes3, alg );
7686 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7687
7688 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007689 inputs[i]->x, inputs[i]->len,
7690 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007691
7692 TEST_EQUAL( psa_key_derivation_key_agreement(
7693 &operation,
7694 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7695 keys[i], key_agreement_peer_key->x,
7696 key_agreement_peer_key->len ), statuses[i] );
7697 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007698 default:
7699 TEST_ASSERT( ! "default case not supported" );
7700 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007701 }
7702
Przemek Stekiel38647de2022-04-19 13:27:47 +02007703 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007704 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007705 break;
7706 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007707 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007708 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007709 inputs[i]->x, inputs[i]->len ), statuses[i] );
7710
7711 if( statuses[i] != PSA_SUCCESS )
7712 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007713 break;
7714 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007715 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007716
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007717 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007718 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007719 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007720 expected_capacity = requested_capacity;
7721
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007722 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007723 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007724 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7725
7726 /* For output key derivation secret must be provided using
7727 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007728 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007729 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007730
7731 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7732 psa_set_key_algorithm( &attributes4, alg );
7733 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007734 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007735
7736 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007737 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007738 }
7739 else // output bytes
7740 {
7741 /* Expansion phase. */
7742 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007743 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007744 /* Read some bytes. */
7745 status = psa_key_derivation_output_bytes( &operation,
7746 output_buffer, output_sizes[i] );
7747 if( expected_capacity == 0 && output_sizes[i] == 0 )
7748 {
7749 /* Reading 0 bytes when 0 bytes are available can go either way. */
7750 TEST_ASSERT( status == PSA_SUCCESS ||
7751 status == PSA_ERROR_INSUFFICIENT_DATA );
7752 continue;
7753 }
7754 else if( expected_capacity == 0 ||
7755 output_sizes[i] > expected_capacity )
7756 {
7757 /* Capacity exceeded. */
7758 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7759 expected_capacity = 0;
7760 continue;
7761 }
7762 /* Success. Check the read data. */
7763 PSA_ASSERT( status );
7764 if( output_sizes[i] != 0 )
7765 ASSERT_COMPARE( output_buffer, output_sizes[i],
7766 expected_outputs[i], output_sizes[i] );
7767 /* Check the operation status. */
7768 expected_capacity -= output_sizes[i];
7769 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7770 &current_capacity ) );
7771 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007772 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007773 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007774 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007775
7776exit:
7777 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007778 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007779 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7780 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007781 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007782 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007783}
7784/* END_CASE */
7785
7786/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007787void derive_full( int alg_arg,
7788 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007789 data_t *input1,
7790 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007791 int requested_capacity_arg )
7792{
Ronald Cron5425a212020-08-04 14:58:35 +02007793 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007794 psa_algorithm_t alg = alg_arg;
7795 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007796 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007797 unsigned char output_buffer[16];
7798 size_t expected_capacity = requested_capacity;
7799 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007801
Gilles Peskine8817f612018-12-18 00:18:46 +01007802 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007803
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007804 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7805 psa_set_key_algorithm( &attributes, alg );
7806 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007807
Gilles Peskine049c7532019-05-15 20:22:09 +02007808 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007809 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007810
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007811 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7812 input1->x, input1->len,
7813 input2->x, input2->len,
7814 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007815 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007816
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007817 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007818 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007819 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007820
7821 /* Expansion phase. */
7822 while( current_capacity > 0 )
7823 {
7824 size_t read_size = sizeof( output_buffer );
7825 if( read_size > current_capacity )
7826 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007827 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007828 output_buffer,
7829 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007830 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007831 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007832 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007833 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007834 }
7835
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007836 /* Check that the operation refuses to go over capacity. */
7837 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007838 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007839
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007840 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007841
7842exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007843 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007844 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007845 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007846}
7847/* END_CASE */
7848
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007849/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007850void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007851 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007852 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007853 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007854 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007855{
7856 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7857 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007858 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007859 uint8_t *output_buffer = NULL;
7860 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007861 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7862 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7863 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007864
7865 ASSERT_ALLOC( output_buffer, expected_output->len );
7866 PSA_ASSERT( psa_crypto_init() );
7867
7868 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007869 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007870 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007871
7872 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007873 step, input->x, input->len ),
7874 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007875
7876 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7877 goto exit;
7878
7879 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7880 expected_output->len );
7881
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007882 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007883 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7884 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7885 expected_output->len );
7886
7887exit:
7888 mbedtls_free( output_buffer );
7889 psa_key_derivation_abort( &operation );
7890 PSA_DONE();
7891}
7892/* END_CASE */
7893
Janos Follathe60c9052019-07-03 13:51:30 +01007894/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007895void derive_key_exercise( int alg_arg,
7896 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007897 data_t *input1,
7898 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007899 int derived_type_arg,
7900 int derived_bits_arg,
7901 int derived_usage_arg,
7902 int derived_alg_arg )
7903{
Ronald Cron5425a212020-08-04 14:58:35 +02007904 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7905 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007906 psa_algorithm_t alg = alg_arg;
7907 psa_key_type_t derived_type = derived_type_arg;
7908 size_t derived_bits = derived_bits_arg;
7909 psa_key_usage_t derived_usage = derived_usage_arg;
7910 psa_algorithm_t derived_alg = derived_alg_arg;
7911 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007912 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007914 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007915
Gilles Peskine8817f612018-12-18 00:18:46 +01007916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007917
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007918 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7919 psa_set_key_algorithm( &attributes, alg );
7920 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007921 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007922 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007923
7924 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007925 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7926 input1->x, input1->len,
7927 input2->x, input2->len,
7928 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007929 goto exit;
7930
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007931 psa_set_key_usage_flags( &attributes, derived_usage );
7932 psa_set_key_algorithm( &attributes, derived_alg );
7933 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007934 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007935 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007936 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007937
7938 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007939 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007940 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7941 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007942
7943 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007944 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007945 goto exit;
7946
7947exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007948 /*
7949 * Key attributes may have been returned by psa_get_key_attributes()
7950 * thus reset them as required.
7951 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007952 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007953
7954 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007955 psa_destroy_key( base_key );
7956 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007957 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007958}
7959/* END_CASE */
7960
Janos Follath42fd8882019-07-03 14:17:09 +01007961/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007962void derive_key_export( int alg_arg,
7963 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007964 data_t *input1,
7965 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007966 int bytes1_arg,
7967 int bytes2_arg )
7968{
Ronald Cron5425a212020-08-04 14:58:35 +02007969 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7970 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007971 psa_algorithm_t alg = alg_arg;
7972 size_t bytes1 = bytes1_arg;
7973 size_t bytes2 = bytes2_arg;
7974 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007975 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007976 uint8_t *output_buffer = NULL;
7977 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007978 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7979 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007980 size_t length;
7981
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007982 ASSERT_ALLOC( output_buffer, capacity );
7983 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007985
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007986 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7987 psa_set_key_algorithm( &base_attributes, alg );
7988 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007989 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007990 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007991
7992 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007993 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7994 input1->x, input1->len,
7995 input2->x, input2->len,
7996 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007997 goto exit;
7998
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007999 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008000 output_buffer,
8001 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008002 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008003
8004 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008005 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8006 input1->x, input1->len,
8007 input2->x, input2->len,
8008 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008009 goto exit;
8010
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008011 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8012 psa_set_key_algorithm( &derived_attributes, 0 );
8013 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008014 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008015 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008016 &derived_key ) );
8017 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008018 export_buffer, bytes1,
8019 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008020 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02008021 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008022 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008023 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008024 &derived_key ) );
8025 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008026 export_buffer + bytes1, bytes2,
8027 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008028 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008029
8030 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008031 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8032 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008033
8034exit:
8035 mbedtls_free( output_buffer );
8036 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008037 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008038 psa_destroy_key( base_key );
8039 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008040 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008041}
8042/* END_CASE */
8043
8044/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008045void derive_key_type( int alg_arg,
8046 data_t *key_data,
8047 data_t *input1,
8048 data_t *input2,
8049 int key_type_arg, int bits_arg,
8050 data_t *expected_export )
8051{
8052 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8053 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8054 const psa_algorithm_t alg = alg_arg;
8055 const psa_key_type_t key_type = key_type_arg;
8056 const size_t bits = bits_arg;
8057 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8058 const size_t export_buffer_size =
8059 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8060 uint8_t *export_buffer = NULL;
8061 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8062 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8063 size_t export_length;
8064
8065 ASSERT_ALLOC( export_buffer, export_buffer_size );
8066 PSA_ASSERT( psa_crypto_init( ) );
8067
8068 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8069 psa_set_key_algorithm( &base_attributes, alg );
8070 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8071 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8072 &base_key ) );
8073
Przemek Stekielc85f0912022-03-08 11:37:54 +01008074 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008075 &operation, base_key, alg,
8076 input1->x, input1->len,
8077 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008078 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008079 goto exit;
8080
8081 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8082 psa_set_key_algorithm( &derived_attributes, 0 );
8083 psa_set_key_type( &derived_attributes, key_type );
8084 psa_set_key_bits( &derived_attributes, bits );
8085 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8086 &derived_key ) );
8087
8088 PSA_ASSERT( psa_export_key( derived_key,
8089 export_buffer, export_buffer_size,
8090 &export_length ) );
8091 ASSERT_COMPARE( export_buffer, export_length,
8092 expected_export->x, expected_export->len );
8093
8094exit:
8095 mbedtls_free( export_buffer );
8096 psa_key_derivation_abort( &operation );
8097 psa_destroy_key( base_key );
8098 psa_destroy_key( derived_key );
8099 PSA_DONE( );
8100}
8101/* END_CASE */
8102
8103/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008104void derive_key( int alg_arg,
8105 data_t *key_data, data_t *input1, data_t *input2,
8106 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008107 int expected_status_arg,
8108 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008109{
Ronald Cron5425a212020-08-04 14:58:35 +02008110 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8111 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008112 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008113 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008114 size_t bits = bits_arg;
8115 psa_status_t expected_status = expected_status_arg;
8116 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8117 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8118 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8119
8120 PSA_ASSERT( psa_crypto_init( ) );
8121
8122 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8123 psa_set_key_algorithm( &base_attributes, alg );
8124 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8125 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008126 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008127
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008128 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8129 input1->x, input1->len,
8130 input2->x, input2->len,
8131 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008132 goto exit;
8133
8134 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8135 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008136 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008137 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008138
8139 psa_status_t status =
8140 psa_key_derivation_output_key( &derived_attributes,
8141 &operation,
8142 &derived_key );
8143 if( is_large_output > 0 )
8144 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8145 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008146
8147exit:
8148 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008149 psa_destroy_key( base_key );
8150 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008151 PSA_DONE( );
8152}
8153/* END_CASE */
8154
8155/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008156void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008157 int our_key_type_arg, int our_key_alg_arg,
8158 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008159 int expected_status_arg )
8160{
Ronald Cron5425a212020-08-04 14:58:35 +02008161 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008162 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008163 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008164 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008165 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008167 psa_status_t expected_status = expected_status_arg;
8168 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008169
Gilles Peskine8817f612018-12-18 00:18:46 +01008170 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008171
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008172 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008173 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008174 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 Peskine01d718c2018-09-18 12:01:02 +02008178
Gilles Peskine77f40d82019-04-11 21:27:06 +02008179 /* The tests currently include inputs that should fail at either step.
8180 * Test cases that fail at the setup step should be changed to call
8181 * key_derivation_setup instead, and this function should be renamed
8182 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008183 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008184 if( status == PSA_SUCCESS )
8185 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008186 TEST_EQUAL( psa_key_derivation_key_agreement(
8187 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8188 our_key,
8189 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008190 expected_status );
8191 }
8192 else
8193 {
8194 TEST_ASSERT( status == expected_status );
8195 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008196
8197exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008198 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008199 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008200 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008201}
8202/* END_CASE */
8203
8204/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008205void raw_key_agreement( int alg_arg,
8206 int our_key_type_arg, data_t *our_key_data,
8207 data_t *peer_key_data,
8208 data_t *expected_output )
8209{
Ronald Cron5425a212020-08-04 14:58:35 +02008210 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008211 psa_algorithm_t alg = alg_arg;
8212 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008214 unsigned char *output = NULL;
8215 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008216 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008217
Gilles Peskinef0cba732019-04-11 22:12:38 +02008218 PSA_ASSERT( psa_crypto_init( ) );
8219
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008220 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8221 psa_set_key_algorithm( &attributes, alg );
8222 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008223 PSA_ASSERT( psa_import_key( &attributes,
8224 our_key_data->x, our_key_data->len,
8225 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008226
gabor-mezei-armceface22021-01-21 12:26:17 +01008227 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8228 key_bits = psa_get_key_bits( &attributes );
8229
Gilles Peskine992bee82022-04-13 23:25:52 +02008230 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008231 TEST_LE_U( expected_output->len,
8232 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8233 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8234 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008235
8236 /* Good case with exact output size */
8237 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008238 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8239 peer_key_data->x, peer_key_data->len,
8240 output, expected_output->len,
8241 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008242 ASSERT_COMPARE( output, output_length,
8243 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008244 mbedtls_free( output );
8245 output = NULL;
8246 output_length = ~0;
8247
8248 /* Larger buffer */
8249 ASSERT_ALLOC( output, expected_output->len + 1 );
8250 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8251 peer_key_data->x, peer_key_data->len,
8252 output, expected_output->len + 1,
8253 &output_length ) );
8254 ASSERT_COMPARE( output, output_length,
8255 expected_output->x, expected_output->len );
8256 mbedtls_free( output );
8257 output = NULL;
8258 output_length = ~0;
8259
8260 /* Buffer too small */
8261 ASSERT_ALLOC( output, expected_output->len - 1 );
8262 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8263 peer_key_data->x, peer_key_data->len,
8264 output, expected_output->len - 1,
8265 &output_length ),
8266 PSA_ERROR_BUFFER_TOO_SMALL );
8267 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008268 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008269 mbedtls_free( output );
8270 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008271
8272exit:
8273 mbedtls_free( output );
8274 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008275 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008276}
8277/* END_CASE */
8278
8279/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008280void key_agreement_capacity( int alg_arg,
8281 int our_key_type_arg, data_t *our_key_data,
8282 data_t *peer_key_data,
8283 int expected_capacity_arg )
8284{
Ronald Cron5425a212020-08-04 14:58:35 +02008285 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008286 psa_algorithm_t alg = alg_arg;
8287 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008288 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008290 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008291 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008292
Gilles Peskine8817f612018-12-18 00:18:46 +01008293 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008294
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008295 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8296 psa_set_key_algorithm( &attributes, alg );
8297 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008298 PSA_ASSERT( psa_import_key( &attributes,
8299 our_key_data->x, our_key_data->len,
8300 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008301
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008302 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008303 PSA_ASSERT( psa_key_derivation_key_agreement(
8304 &operation,
8305 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8306 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008307 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8308 {
8309 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008310 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008311 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008312 NULL, 0 ) );
8313 }
Gilles Peskine59685592018-09-18 12:11:34 +02008314
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008315 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008316 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008317 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008318 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008319
Gilles Peskinebf491972018-10-25 22:36:12 +02008320 /* Test the actual capacity by reading the output. */
8321 while( actual_capacity > sizeof( output ) )
8322 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008323 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008324 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008325 actual_capacity -= sizeof( output );
8326 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008327 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008328 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008329 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008330 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008331
Gilles Peskine59685592018-09-18 12:11:34 +02008332exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008333 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008334 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008335 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008336}
8337/* END_CASE */
8338
8339/* BEGIN_CASE */
8340void key_agreement_output( int alg_arg,
8341 int our_key_type_arg, data_t *our_key_data,
8342 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008343 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008344{
Ronald Cron5425a212020-08-04 14:58:35 +02008345 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008346 psa_algorithm_t alg = alg_arg;
8347 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008348 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008350 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008351
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008352 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8353 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008354
Gilles Peskine8817f612018-12-18 00:18:46 +01008355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008356
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8358 psa_set_key_algorithm( &attributes, alg );
8359 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008360 PSA_ASSERT( psa_import_key( &attributes,
8361 our_key_data->x, our_key_data->len,
8362 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008363
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008364 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008365 PSA_ASSERT( psa_key_derivation_key_agreement(
8366 &operation,
8367 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8368 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008369 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8370 {
8371 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008372 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008373 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008374 NULL, 0 ) );
8375 }
Gilles Peskine59685592018-09-18 12:11:34 +02008376
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008377 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008378 actual_output,
8379 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008380 ASSERT_COMPARE( actual_output, expected_output1->len,
8381 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008382 if( expected_output2->len != 0 )
8383 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008384 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008385 actual_output,
8386 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008387 ASSERT_COMPARE( actual_output, expected_output2->len,
8388 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008389 }
Gilles Peskine59685592018-09-18 12:11:34 +02008390
8391exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008392 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008393 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008394 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008395 mbedtls_free( actual_output );
8396}
8397/* END_CASE */
8398
8399/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008400void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008401{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008402 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008403 unsigned char *output = NULL;
8404 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008405 size_t i;
8406 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008407
Simon Butcher49f8e312020-03-03 15:51:50 +00008408 TEST_ASSERT( bytes_arg >= 0 );
8409
Gilles Peskine91892022021-02-08 19:50:26 +01008410 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008411 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008412
Gilles Peskine8817f612018-12-18 00:18:46 +01008413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008414
Gilles Peskinea50d7392018-06-21 10:22:13 +02008415 /* Run several times, to ensure that every output byte will be
8416 * nonzero at least once with overwhelming probability
8417 * (2^(-8*number_of_runs)). */
8418 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008419 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008420 if( bytes != 0 )
8421 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008422 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008423
Gilles Peskinea50d7392018-06-21 10:22:13 +02008424 for( i = 0; i < bytes; i++ )
8425 {
8426 if( output[i] != 0 )
8427 ++changed[i];
8428 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008429 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008430
8431 /* Check that every byte was changed to nonzero at least once. This
8432 * validates that psa_generate_random is overwriting every byte of
8433 * the output buffer. */
8434 for( i = 0; i < bytes; i++ )
8435 {
8436 TEST_ASSERT( changed[i] != 0 );
8437 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008438
8439exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008440 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008441 mbedtls_free( output );
8442 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008443}
8444/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008445
8446/* BEGIN_CASE */
8447void generate_key( int type_arg,
8448 int bits_arg,
8449 int usage_arg,
8450 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008451 int expected_status_arg,
8452 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008453{
Ronald Cron5425a212020-08-04 14:58:35 +02008454 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008455 psa_key_type_t type = type_arg;
8456 psa_key_usage_t usage = usage_arg;
8457 size_t bits = bits_arg;
8458 psa_algorithm_t alg = alg_arg;
8459 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008461 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008462
Gilles Peskine8817f612018-12-18 00:18:46 +01008463 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008464
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008465 psa_set_key_usage_flags( &attributes, usage );
8466 psa_set_key_algorithm( &attributes, alg );
8467 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008468 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008469
8470 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008471 psa_status_t status = psa_generate_key( &attributes, &key );
8472
8473 if( is_large_key > 0 )
8474 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8475 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008476 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008477 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008478
8479 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008480 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008481 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8482 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008483
Gilles Peskine818ca122018-06-20 18:16:48 +02008484 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008485 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008486 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008487
8488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008489 /*
8490 * Key attributes may have been returned by psa_get_key_attributes()
8491 * thus reset them as required.
8492 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008493 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008494
Ronald Cron5425a212020-08-04 14:58:35 +02008495 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008496 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008497}
8498/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008499
Ronald Cronee414c72021-03-18 18:50:08 +01008500/* 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 +02008501void generate_key_rsa( int bits_arg,
8502 data_t *e_arg,
8503 int expected_status_arg )
8504{
Ronald Cron5425a212020-08-04 14:58:35 +02008505 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008506 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008507 size_t bits = bits_arg;
8508 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8509 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8510 psa_status_t expected_status = expected_status_arg;
8511 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8512 uint8_t *exported = NULL;
8513 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008514 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008515 size_t exported_length = SIZE_MAX;
8516 uint8_t *e_read_buffer = NULL;
8517 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008518 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008519 size_t e_read_length = SIZE_MAX;
8520
8521 if( e_arg->len == 0 ||
8522 ( e_arg->len == 3 &&
8523 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8524 {
8525 is_default_public_exponent = 1;
8526 e_read_size = 0;
8527 }
8528 ASSERT_ALLOC( e_read_buffer, e_read_size );
8529 ASSERT_ALLOC( exported, exported_size );
8530
8531 PSA_ASSERT( psa_crypto_init( ) );
8532
8533 psa_set_key_usage_flags( &attributes, usage );
8534 psa_set_key_algorithm( &attributes, alg );
8535 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8536 e_arg->x, e_arg->len ) );
8537 psa_set_key_bits( &attributes, bits );
8538
8539 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008540 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008541 if( expected_status != PSA_SUCCESS )
8542 goto exit;
8543
8544 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008545 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008546 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8547 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8548 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8549 e_read_buffer, e_read_size,
8550 &e_read_length ) );
8551 if( is_default_public_exponent )
8552 TEST_EQUAL( e_read_length, 0 );
8553 else
8554 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8555
8556 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008557 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008558 goto exit;
8559
8560 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008561 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008562 exported, exported_size,
8563 &exported_length ) );
8564 {
8565 uint8_t *p = exported;
8566 uint8_t *end = exported + exported_length;
8567 size_t len;
8568 /* RSAPublicKey ::= SEQUENCE {
8569 * modulus INTEGER, -- n
8570 * publicExponent INTEGER } -- e
8571 */
8572 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008573 MBEDTLS_ASN1_SEQUENCE |
8574 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008575 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008576 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8577 MBEDTLS_ASN1_INTEGER ) );
8578 if( len >= 1 && p[0] == 0 )
8579 {
8580 ++p;
8581 --len;
8582 }
8583 if( e_arg->len == 0 )
8584 {
8585 TEST_EQUAL( len, 3 );
8586 TEST_EQUAL( p[0], 1 );
8587 TEST_EQUAL( p[1], 0 );
8588 TEST_EQUAL( p[2], 1 );
8589 }
8590 else
8591 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8592 }
8593
8594exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008595 /*
8596 * Key attributes may have been returned by psa_get_key_attributes() or
8597 * set by psa_set_key_domain_parameters() thus reset them as required.
8598 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008599 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008600
Ronald Cron5425a212020-08-04 14:58:35 +02008601 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008602 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008603 mbedtls_free( e_read_buffer );
8604 mbedtls_free( exported );
8605}
8606/* END_CASE */
8607
Darryl Greend49a4992018-06-18 17:27:26 +01008608/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008609void persistent_key_load_key_from_storage( data_t *data,
8610 int type_arg, int bits_arg,
8611 int usage_flags_arg, int alg_arg,
8612 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008613{
Ronald Cron71016a92020-08-28 19:01:50 +02008614 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008615 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008616 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8617 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008618 psa_key_type_t type = type_arg;
8619 size_t bits = bits_arg;
8620 psa_key_usage_t usage_flags = usage_flags_arg;
8621 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008622 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008623 unsigned char *first_export = NULL;
8624 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008625 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008626 size_t first_exported_length;
8627 size_t second_exported_length;
8628
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008629 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8630 {
8631 ASSERT_ALLOC( first_export, export_size );
8632 ASSERT_ALLOC( second_export, export_size );
8633 }
Darryl Greend49a4992018-06-18 17:27:26 +01008634
Gilles Peskine8817f612018-12-18 00:18:46 +01008635 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008636
Gilles Peskinec87af662019-05-15 16:12:22 +02008637 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008638 psa_set_key_usage_flags( &attributes, usage_flags );
8639 psa_set_key_algorithm( &attributes, alg );
8640 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008641 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008642
Darryl Green0c6575a2018-11-07 16:05:30 +00008643 switch( generation_method )
8644 {
8645 case IMPORT_KEY:
8646 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008647 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008648 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008649 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008650
Darryl Green0c6575a2018-11-07 16:05:30 +00008651 case GENERATE_KEY:
8652 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008653 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008654 break;
8655
8656 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008657#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008658 {
8659 /* Create base key */
8660 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8661 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8662 psa_set_key_usage_flags( &base_attributes,
8663 PSA_KEY_USAGE_DERIVE );
8664 psa_set_key_algorithm( &base_attributes, derive_alg );
8665 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008666 PSA_ASSERT( psa_import_key( &base_attributes,
8667 data->x, data->len,
8668 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008669 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008670 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008671 PSA_ASSERT( psa_key_derivation_input_key(
8672 &operation,
8673 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008674 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008675 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008676 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008677 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8678 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008679 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008680 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008681 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008682 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008683 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008684#else
8685 TEST_ASSUME( ! "KDF not supported in this configuration" );
8686#endif
8687 break;
8688
8689 default:
8690 TEST_ASSERT( ! "generation_method not implemented in test" );
8691 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008692 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008693 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008694
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008695 /* Export the key if permitted by the key policy. */
8696 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8697 {
Ronald Cron5425a212020-08-04 14:58:35 +02008698 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008699 first_export, export_size,
8700 &first_exported_length ) );
8701 if( generation_method == IMPORT_KEY )
8702 ASSERT_COMPARE( data->x, data->len,
8703 first_export, first_exported_length );
8704 }
Darryl Greend49a4992018-06-18 17:27:26 +01008705
8706 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008707 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008708 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008709 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008710
Darryl Greend49a4992018-06-18 17:27:26 +01008711 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008712 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008713 TEST_ASSERT( mbedtls_svc_key_id_equal(
8714 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008715 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8716 PSA_KEY_LIFETIME_PERSISTENT );
8717 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8718 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008719 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008720 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008721 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008722
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008723 /* Export the key again if permitted by the key policy. */
8724 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008725 {
Ronald Cron5425a212020-08-04 14:58:35 +02008726 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008727 second_export, export_size,
8728 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008729 ASSERT_COMPARE( first_export, first_exported_length,
8730 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008731 }
8732
8733 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008734 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008735 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008736
8737exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008738 /*
8739 * Key attributes may have been returned by psa_get_key_attributes()
8740 * thus reset them as required.
8741 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008742 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008743
Darryl Greend49a4992018-06-18 17:27:26 +01008744 mbedtls_free( first_export );
8745 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008746 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008747 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008748 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008749 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008750}
8751/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008752
Neil Armstronga557cb82022-06-10 08:58:32 +02008753/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008754void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8755 int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008756 int input_first, data_t *pw_data,
Neil Armstrong2a73f212022-09-06 11:34:54 +02008757 int expected_status_setup_arg,
8758 int expected_status_set_role_arg,
8759 int expected_status_set_password_key_arg,
8760 int expected_status_input_output_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008761{
8762 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8763 psa_pake_operation_t operation = psa_pake_operation_init();
8764 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008765 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008766 psa_key_type_t key_type_pw = key_type_pw_arg;
8767 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008768 psa_algorithm_t hash_alg = hash_arg;
8769 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008770 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008772 psa_status_t expected_status_setup = expected_status_setup_arg;
8773 psa_status_t expected_status_set_role = expected_status_set_role_arg;
8774 psa_status_t expected_status_set_password_key =
8775 expected_status_set_password_key_arg;
8776 psa_status_t expected_status_input_output =
8777 expected_status_input_output_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008778 unsigned char *output_buffer = NULL;
8779 size_t output_len = 0;
8780
8781 PSA_INIT( );
8782
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008783 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8784 PSA_PAKE_STEP_KEY_SHARE);
8785 ASSERT_ALLOC( output_buffer, buf_size );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008786
8787 if( pw_data->len > 0 )
8788 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008789 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008790 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008791 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008792 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8793 &key ) );
8794 }
8795
8796 psa_pake_cs_set_algorithm( &cipher_suite, alg );
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008797 psa_pake_cs_set_primitive( &cipher_suite, primitive );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008798 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8799
Neil Armstrong645cccd2022-06-08 17:36:23 +02008800 PSA_ASSERT( psa_pake_abort( &operation ) );
8801
8802 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8803 PSA_ERROR_BAD_STATE );
8804 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8805 PSA_ERROR_BAD_STATE );
8806 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8807 PSA_ERROR_BAD_STATE );
8808 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8809 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008810 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8811 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008812 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008813 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008814 PSA_ERROR_BAD_STATE );
8815
8816 PSA_ASSERT( psa_pake_abort( &operation ) );
8817
Neil Armstrong2a73f212022-09-06 11:34:54 +02008818 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8819 expected_status_setup );
8820 if( expected_status_setup != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008821 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008822
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008823 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8824 PSA_ERROR_BAD_STATE );
8825
Neil Armstrong2a73f212022-09-06 11:34:54 +02008826 TEST_EQUAL( psa_pake_set_role( &operation, role),
8827 expected_status_set_role );
8828 if( expected_status_set_role != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008829 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008830
8831 if( pw_data->len > 0 )
8832 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008833 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8834 expected_status_set_password_key );
8835 if( expected_status_set_password_key != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008836 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008837 }
8838
Neil Armstrong707d9572022-06-08 17:31:49 +02008839 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8840 PSA_ERROR_INVALID_ARGUMENT );
8841 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8842 PSA_ERROR_INVALID_ARGUMENT );
8843
8844 const uint8_t unsupported_id[] = "abcd";
8845
8846 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8847 PSA_ERROR_NOT_SUPPORTED );
8848 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8849 PSA_ERROR_NOT_SUPPORTED );
8850
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008851 const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive,
8852 PSA_PAKE_STEP_KEY_SHARE );
8853 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive,
8854 PSA_PAKE_STEP_ZK_PUBLIC );
8855 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive,
8856 PSA_PAKE_STEP_ZK_PROOF );
8857
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008858 /* First round */
8859 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008860 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008861 /* Invalid parameters (input) */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008862 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008863 NULL, 0 ),
8864 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008865 /* Invalid parameters (step) */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008866 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8867 key, pw_data->len ) , 0 );
Przemek Stekiel7c795482022-11-15 22:26:12 +01008868 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008869 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008870 PSA_ERROR_INVALID_ARGUMENT );
8871 /* Invalid first step */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008872 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8873 key, pw_data->len ), 0 );
Przemek Stekiel7c795482022-11-15 22:26:12 +01008874 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008875 output_buffer, size_zk_proof ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008876 PSA_ERROR_BAD_STATE );
8877
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008878 /* Possibly valid */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008879 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8880 key, pw_data->len ), 0 );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008881 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008882 output_buffer, size_key_share ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008883 expected_status_input_output);
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008884
Neil Armstrong2a73f212022-09-06 11:34:54 +02008885 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008886 {
8887 /* Buffer too large */
8888 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008889 output_buffer, size_zk_public + 1 ),
8890 PSA_ERROR_INVALID_ARGUMENT );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008891
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008892 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008893 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008894 output_buffer, size_zk_public ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008895 PSA_ERROR_BAD_STATE );
8896 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008897 }
8898 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008899 {
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008900 /* Invalid parameters (output) */
Przemek Stekiel7c795482022-11-15 22:26:12 +01008901 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008902 NULL, 0, NULL ),
8903 PSA_ERROR_INVALID_ARGUMENT );
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008904 /* Invalid parameters (step) */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008905 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8906 key, pw_data->len ), 0 );
Przemek Stekiel7c795482022-11-15 22:26:12 +01008907 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008908 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008909 PSA_ERROR_INVALID_ARGUMENT );
8910 /* Invalid first step */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008911 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8912 key, pw_data->len ), 0 );
Przemek Stekiel7c795482022-11-15 22:26:12 +01008913 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008914 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008915 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008916
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008917 /* Possibly valid */
Przemek Stekielf82effa2022-11-21 15:10:32 +01008918 TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role,
8919 key, pw_data->len ), 0 );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008920 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008921 output_buffer, buf_size, &output_len ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008922 expected_status_input_output );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008923
Neil Armstrong2a73f212022-09-06 11:34:54 +02008924 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008925 {
8926 TEST_ASSERT( output_len > 0 );
8927
8928 /* Buffer too small */
8929 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008930 output_buffer, size_zk_public - 1, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008931 PSA_ERROR_BUFFER_TOO_SMALL );
8932
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008933 /* The operation's state should be invalidated at this point */
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008934 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008935 output_buffer, buf_size, &output_len ),
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008936 PSA_ERROR_BAD_STATE );
8937 }
8938 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008939
8940exit:
8941 PSA_ASSERT( psa_destroy_key( key ) );
8942 PSA_ASSERT( psa_pake_abort( &operation ) );
8943 mbedtls_free( output_buffer );
8944 PSA_DONE( );
8945}
8946/* END_CASE */
8947
Neil Armstronga557cb82022-06-10 08:58:32 +02008948/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008949void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8950 int client_input_first, int inject_error,
8951 data_t *pw_data )
8952{
8953 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8954 psa_pake_operation_t server = psa_pake_operation_init();
8955 psa_pake_operation_t client = psa_pake_operation_init();
8956 psa_algorithm_t alg = alg_arg;
8957 psa_algorithm_t hash_alg = hash_arg;
8958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8960
8961 PSA_INIT( );
8962
8963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8964 psa_set_key_algorithm( &attributes, alg );
8965 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8966 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8967 &key ) );
8968
8969 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8970 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8971 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8972
8973
8974 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8975 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8976
8977 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8978 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8979
8980 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8981 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8982
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008983 ecjpake_do_round( alg, primitive_arg, &server, &client,
8984 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008985
8986 if( inject_error == 1 || inject_error == 2 )
8987 goto exit;
8988
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008989 ecjpake_do_round( alg, primitive_arg, &server, &client,
8990 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008991
8992exit:
8993 psa_destroy_key( key );
8994 psa_pake_abort( &server );
8995 psa_pake_abort( &client );
8996 PSA_DONE( );
8997}
8998/* END_CASE */
8999
9000/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009001void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02009002 int derive_alg_arg, data_t *pw_data,
Przemek Stekielcd356c32022-11-20 19:05:20 +01009003 int client_input_first, int destroy_key )
Neil Armstrongd597bc72022-05-25 11:28:39 +02009004{
9005 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9006 psa_pake_operation_t server = psa_pake_operation_init();
9007 psa_pake_operation_t client = psa_pake_operation_init();
9008 psa_algorithm_t alg = alg_arg;
9009 psa_algorithm_t hash_alg = hash_arg;
9010 psa_algorithm_t derive_alg = derive_alg_arg;
9011 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9012 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9013 psa_key_derivation_operation_t server_derive =
9014 PSA_KEY_DERIVATION_OPERATION_INIT;
9015 psa_key_derivation_operation_t client_derive =
9016 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009017
9018 PSA_INIT( );
9019
Neil Armstrongd597bc72022-05-25 11:28:39 +02009020 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9021 psa_set_key_algorithm( &attributes, alg );
9022 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9023 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9024 &key ) );
9025
9026 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9027 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9028 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9029
Neil Armstrong1e855602022-06-15 11:32:11 +02009030 /* Get shared key */
9031 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
9032 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
9033
9034 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
9035 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
9036 {
9037 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
9038 PSA_KEY_DERIVATION_INPUT_SEED,
9039 (const uint8_t*) "", 0) );
9040 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
9041 PSA_KEY_DERIVATION_INPUT_SEED,
9042 (const uint8_t*) "", 0) );
9043 }
9044
Neil Armstrongd597bc72022-05-25 11:28:39 +02009045 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9046 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9047
9048 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9049 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9050
9051 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9052 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9053
Przemek Stekielcd356c32022-11-20 19:05:20 +01009054 if( destroy_key == 1 )
9055 psa_destroy_key( key );
9056
Neil Armstrong1e855602022-06-15 11:32:11 +02009057 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9058 PSA_ERROR_BAD_STATE );
9059 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9060 PSA_ERROR_BAD_STATE );
9061
Neil Armstrongf983caf2022-06-15 15:27:48 +02009062 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009063 ecjpake_do_round( alg, primitive_arg, &server, &client,
9064 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009065
Neil Armstrong1e855602022-06-15 11:32:11 +02009066 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9067 PSA_ERROR_BAD_STATE );
9068 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9069 PSA_ERROR_BAD_STATE );
9070
Neil Armstrongf983caf2022-06-15 15:27:48 +02009071 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009072 ecjpake_do_round( alg, primitive_arg, &server, &client,
9073 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009074
Neil Armstrongd597bc72022-05-25 11:28:39 +02009075 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9076 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9077
9078exit:
9079 psa_key_derivation_abort( &server_derive );
9080 psa_key_derivation_abort( &client_derive );
9081 psa_destroy_key( key );
9082 psa_pake_abort( &server );
9083 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009084 PSA_DONE( );
9085}
9086/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009087
9088/* BEGIN_CASE */
9089void ecjpake_size_macros( )
9090{
9091 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9092 const size_t bits = 256;
9093 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9094 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9095 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9096 PSA_ECC_FAMILY_SECP_R1 );
9097
9098 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9099 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9100 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9101 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9102 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9103 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9104 /* The output for ZK_PROOF is the same bitsize as the curve */
9105 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9106 PSA_BITS_TO_BYTES( bits ) );
9107
9108 /* Input sizes are the same as output sizes */
9109 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9110 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9111 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9112 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9113 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9114 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9115
9116 /* These inequalities will always hold even when other PAKEs are added */
9117 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9118 PSA_PAKE_OUTPUT_MAX_SIZE );
9119 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9120 PSA_PAKE_OUTPUT_MAX_SIZE );
9121 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9122 PSA_PAKE_OUTPUT_MAX_SIZE );
9123 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9124 PSA_PAKE_INPUT_MAX_SIZE );
9125 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9126 PSA_PAKE_INPUT_MAX_SIZE );
9127 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9128 PSA_PAKE_INPUT_MAX_SIZE );
9129}
9130/* END_CASE */