blob: 8d42bf9b346d5429bc9ebec83740ce3186f6c029 [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
Valerio Setti1070aed2022-11-11 19:37:31 +01001321typedef enum
1322{
1323 INJECT_ERR_NONE = 0,
1324 INJECT_ERR_UNINITIALIZED_ACCESS,
1325 INJECT_ERR_DUPLICATE_SETUP,
1326 INJECT_ERR_INVALID_USER,
1327 INJECT_ERR_INVALID_PEER,
1328 INJECT_ERR_SET_USER,
1329 INJECT_ERR_SET_PEER,
1330 INJECT_EMPTY_IO_BUFFER,
1331 INJECT_UNKNOWN_STEP,
1332 INJECT_INVALID_FIRST_STEP,
1333 INJECT_WRONG_BUFFER_SIZE,
1334 INJECT_VALID_OPERATION_AFTER_FAILURE,
1335 INJECT_ANTICIPATE_KEY_DERIVATION_1,
1336 INJECT_ANTICIPATE_KEY_DERIVATION_2,
1337} ecjpake_injected_failure_t;
1338
Gilles Peskinee59236f2018-01-27 23:32:46 +01001339/* END_HEADER */
1340
1341/* BEGIN_DEPENDENCIES
1342 * depends_on:MBEDTLS_PSA_CRYPTO_C
1343 * END_DEPENDENCIES
1344 */
1345
1346/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001347void static_checks( )
1348{
1349 size_t max_truncated_mac_size =
1350 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1351
1352 /* Check that the length for a truncated MAC always fits in the algorithm
1353 * encoding. The shifted mask is the maximum truncated value. The
1354 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001355 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001356}
1357/* END_CASE */
1358
1359/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001360void import_with_policy( int type_arg,
1361 int usage_arg, int alg_arg,
1362 int expected_status_arg )
1363{
1364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1365 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001366 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001367 psa_key_type_t type = type_arg;
1368 psa_key_usage_t usage = usage_arg;
1369 psa_algorithm_t alg = alg_arg;
1370 psa_status_t expected_status = expected_status_arg;
1371 const uint8_t key_material[16] = {0};
1372 psa_status_t status;
1373
1374 PSA_ASSERT( psa_crypto_init( ) );
1375
1376 psa_set_key_type( &attributes, type );
1377 psa_set_key_usage_flags( &attributes, usage );
1378 psa_set_key_algorithm( &attributes, alg );
1379
1380 status = psa_import_key( &attributes,
1381 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001382 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001383 TEST_EQUAL( status, expected_status );
1384 if( status != PSA_SUCCESS )
1385 goto exit;
1386
Ronald Cron5425a212020-08-04 14:58:35 +02001387 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001388 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001389 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001390 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001391 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001392 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001393
Ronald Cron5425a212020-08-04 14:58:35 +02001394 PSA_ASSERT( psa_destroy_key( key ) );
1395 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001396
1397exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001398 /*
1399 * Key attributes may have been returned by psa_get_key_attributes()
1400 * thus reset them as required.
1401 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001402 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001403
1404 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001405 PSA_DONE( );
1406}
1407/* END_CASE */
1408
1409/* BEGIN_CASE */
1410void import_with_data( data_t *data, int type_arg,
1411 int attr_bits_arg,
1412 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001413{
1414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1415 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001417 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001418 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001419 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001420 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001421
Gilles Peskine8817f612018-12-18 00:18:46 +01001422 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001423
Gilles Peskine4747d192019-04-17 15:05:45 +02001424 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001425 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001426
Ronald Cron5425a212020-08-04 14:58:35 +02001427 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001428 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001429 if( status != PSA_SUCCESS )
1430 goto exit;
1431
Ronald Cron5425a212020-08-04 14:58:35 +02001432 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001433 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001434 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001435 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001436 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001437
Ronald Cron5425a212020-08-04 14:58:35 +02001438 PSA_ASSERT( psa_destroy_key( key ) );
1439 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001440
1441exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001442 /*
1443 * Key attributes may have been returned by psa_get_key_attributes()
1444 * thus reset them as required.
1445 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001446 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001447
1448 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001449 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001454void import_large_key( int type_arg, int byte_size_arg,
1455 int expected_status_arg )
1456{
1457 psa_key_type_t type = type_arg;
1458 size_t byte_size = byte_size_arg;
1459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1460 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001461 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001462 psa_status_t status;
1463 uint8_t *buffer = NULL;
1464 size_t buffer_size = byte_size + 1;
1465 size_t n;
1466
Steven Cooreman69967ce2021-01-18 18:01:08 +01001467 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001468 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001469 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001470 memset( buffer, 'K', byte_size );
1471
1472 PSA_ASSERT( psa_crypto_init( ) );
1473
1474 /* Try importing the key */
1475 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1476 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001477 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001478 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001479 TEST_EQUAL( status, expected_status );
1480
1481 if( status == PSA_SUCCESS )
1482 {
Ronald Cron5425a212020-08-04 14:58:35 +02001483 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001484 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1485 TEST_EQUAL( psa_get_key_bits( &attributes ),
1486 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001487 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001488 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001489 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001490 for( n = 0; n < byte_size; n++ )
1491 TEST_EQUAL( buffer[n], 'K' );
1492 for( n = byte_size; n < buffer_size; n++ )
1493 TEST_EQUAL( buffer[n], 0 );
1494 }
1495
1496exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001497 /*
1498 * Key attributes may have been returned by psa_get_key_attributes()
1499 * thus reset them as required.
1500 */
1501 psa_reset_key_attributes( &attributes );
1502
Ronald Cron5425a212020-08-04 14:58:35 +02001503 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001504 PSA_DONE( );
1505 mbedtls_free( buffer );
1506}
1507/* END_CASE */
1508
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001509/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001510void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1511{
Ronald Cron5425a212020-08-04 14:58:35 +02001512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001513 size_t bits = bits_arg;
1514 psa_status_t expected_status = expected_status_arg;
1515 psa_status_t status;
1516 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001517 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001518 size_t buffer_size = /* Slight overapproximations */
1519 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001520 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001521 unsigned char *p;
1522 int ret;
1523 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001525
Gilles Peskine8817f612018-12-18 00:18:46 +01001526 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001527 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001528
1529 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1530 bits, keypair ) ) >= 0 );
1531 length = ret;
1532
1533 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001534 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001535 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001536 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001537
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001538 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001539 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001540
1541exit:
1542 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001543 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001544}
1545/* END_CASE */
1546
1547/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001548void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001549 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001550 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301551 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001552 int expected_bits,
1553 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001554 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555 int canonical_input )
1556{
Ronald Cron5425a212020-08-04 14:58:35 +02001557 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001559 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001560 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001561 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301562 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563 unsigned char *exported = NULL;
1564 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001565 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001566 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001569 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001570
Moran Pekercb088e72018-07-17 17:36:59 +03001571 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001572 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001573 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001574 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001575 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001576
Archana4d7ae1d2021-07-07 02:50:22 +05301577 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 psa_set_key_usage_flags( &attributes, usage_arg );
1579 psa_set_key_algorithm( &attributes, alg );
1580 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001581
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001582 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001583 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001584
1585 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001586 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001587 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1588 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001589 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001590
1591 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001592 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001593 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001594
1595 /* The exported length must be set by psa_export_key() to a value between 0
1596 * and export_size. On errors, the exported length must be 0. */
1597 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1598 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001599 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001600
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001601 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001602 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001604 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001605 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001607 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001608
Gilles Peskineea38a922021-02-13 00:05:16 +01001609 /* Run sanity checks on the exported key. For non-canonical inputs,
1610 * this validates the canonical representations. For canonical inputs,
1611 * this doesn't directly validate the implementation, but it still helps
1612 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301613 if( !psa_key_lifetime_is_external( lifetime ) )
1614 {
1615 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1616 goto exit;
1617 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001618
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001619 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001620 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001621 else
1622 {
Ronald Cron5425a212020-08-04 14:58:35 +02001623 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001624 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001625 &key2 ) );
1626 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001627 reexported,
1628 export_size,
1629 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001630 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301631 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001632 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001633 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001634 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301635 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301636 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001637 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001638
1639destroy:
1640 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001641 PSA_ASSERT( psa_destroy_key( key ) );
1642 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001643
1644exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001645 /*
1646 * Key attributes may have been returned by psa_get_key_attributes()
1647 * thus reset them as required.
1648 */
1649 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301650 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001651 mbedtls_free( exported );
1652 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001653 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001654}
1655/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001656
Moran Pekerf709f4a2018-06-06 17:26:04 +03001657/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001658void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001659 int type_arg,
1660 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301661 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001662 int export_size_delta,
1663 int expected_export_status_arg,
1664 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001665{
Ronald Cron5425a212020-08-04 14:58:35 +02001666 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001667 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001668 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001669 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001670 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301671 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001672 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001673 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001674 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001676
Gilles Peskine8817f612018-12-18 00:18:46 +01001677 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001678
Archana4d7ae1d2021-07-07 02:50:22 +05301679 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1681 psa_set_key_algorithm( &attributes, alg );
1682 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001683
1684 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001685 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001686
Gilles Peskine49c25912018-10-29 15:15:31 +01001687 /* Export the public key */
1688 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001689 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001690 exported, export_size,
1691 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001693 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001694 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001695 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001696 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001697 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001698 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001699 TEST_LE_U( expected_public_key->len,
1700 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1701 TEST_LE_U( expected_public_key->len,
1702 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1703 TEST_LE_U( expected_public_key->len,
1704 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001705 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1706 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001707 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001708exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001709 /*
1710 * Key attributes may have been returned by psa_get_key_attributes()
1711 * thus reset them as required.
1712 */
1713 psa_reset_key_attributes( &attributes );
1714
itayzafrir3e02b3b2018-06-12 17:06:52 +03001715 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001716 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001717 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001718}
1719/* END_CASE */
1720
Gilles Peskine20035e32018-02-03 22:44:14 +01001721/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001722void import_and_exercise_key( data_t *data,
1723 int type_arg,
1724 int bits_arg,
1725 int alg_arg )
1726{
Ronald Cron5425a212020-08-04 14:58:35 +02001727 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001728 psa_key_type_t type = type_arg;
1729 size_t bits = bits_arg;
1730 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001731 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001733 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001734
Gilles Peskine8817f612018-12-18 00:18:46 +01001735 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001736
Gilles Peskine4747d192019-04-17 15:05:45 +02001737 psa_set_key_usage_flags( &attributes, usage );
1738 psa_set_key_algorithm( &attributes, alg );
1739 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001740
1741 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001742 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001743
1744 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001745 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001746 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1747 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001748
1749 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001750 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001751 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001752
Ronald Cron5425a212020-08-04 14:58:35 +02001753 PSA_ASSERT( psa_destroy_key( key ) );
1754 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001755
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001756exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001757 /*
1758 * Key attributes may have been returned by psa_get_key_attributes()
1759 * thus reset them as required.
1760 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001761 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001762
1763 psa_reset_key_attributes( &attributes );
1764 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001765 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001770void effective_key_attributes( int type_arg, int expected_type_arg,
1771 int bits_arg, int expected_bits_arg,
1772 int usage_arg, int expected_usage_arg,
1773 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001774{
Ronald Cron5425a212020-08-04 14:58:35 +02001775 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001776 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001777 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001778 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001779 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001780 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001781 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001782 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001783 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001784 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001785
Gilles Peskine8817f612018-12-18 00:18:46 +01001786 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001787
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001788 psa_set_key_usage_flags( &attributes, usage );
1789 psa_set_key_algorithm( &attributes, alg );
1790 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001791 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001792
Ronald Cron5425a212020-08-04 14:58:35 +02001793 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001794 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001795
Ronald Cron5425a212020-08-04 14:58:35 +02001796 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001797 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1798 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1799 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1800 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001801
1802exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001803 /*
1804 * Key attributes may have been returned by psa_get_key_attributes()
1805 * thus reset them as required.
1806 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001807 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001808
1809 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001810 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001811}
1812/* END_CASE */
1813
1814/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001815void check_key_policy( int type_arg, int bits_arg,
1816 int usage_arg, int alg_arg )
1817{
1818 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001819 usage_arg,
1820 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001821 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001822 goto exit;
1823}
1824/* END_CASE */
1825
1826/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001827void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001828{
1829 /* Test each valid way of initializing the object, except for `= {0}`, as
1830 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1831 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001832 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001833 psa_key_attributes_t func = psa_key_attributes_init( );
1834 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1835 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001836
1837 memset( &zero, 0, sizeof( zero ) );
1838
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001839 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1840 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1841 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001842
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001843 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1844 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1845 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1846
1847 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1848 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1849 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1850
1851 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1852 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1853 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1854
1855 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1856 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1857 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001858}
1859/* END_CASE */
1860
1861/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001862void mac_key_policy( int policy_usage_arg,
1863 int policy_alg_arg,
1864 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001865 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001866 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001867 int expected_status_sign_arg,
1868 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001869{
Ronald Cron5425a212020-08-04 14:58:35 +02001870 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001872 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001873 psa_key_type_t key_type = key_type_arg;
1874 psa_algorithm_t policy_alg = policy_alg_arg;
1875 psa_algorithm_t exercise_alg = exercise_alg_arg;
1876 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001877 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001878 psa_status_t expected_status_sign = expected_status_sign_arg;
1879 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001880 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001881
Gilles Peskine8817f612018-12-18 00:18:46 +01001882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001883
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001884 psa_set_key_usage_flags( &attributes, policy_usage );
1885 psa_set_key_algorithm( &attributes, policy_alg );
1886 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001887
Gilles Peskine049c7532019-05-15 20:22:09 +02001888 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001889 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001890
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001891 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1892 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001893
Ronald Cron5425a212020-08-04 14:58:35 +02001894 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001895 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001896
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001897 /* Calculate the MAC, one-shot case. */
1898 uint8_t input[128] = {0};
1899 size_t mac_len;
1900 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1901 input, 128,
1902 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1903 expected_status_sign );
1904
Neil Armstrong3af9b972022-02-07 12:20:21 +01001905 /* Calculate the MAC, multi-part case. */
1906 PSA_ASSERT( psa_mac_abort( &operation ) );
1907 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1908 if( status == PSA_SUCCESS )
1909 {
1910 status = psa_mac_update( &operation, input, 128 );
1911 if( status == PSA_SUCCESS )
1912 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1913 &mac_len ),
1914 expected_status_sign );
1915 else
1916 TEST_EQUAL( status, expected_status_sign );
1917 }
1918 else
1919 {
1920 TEST_EQUAL( status, expected_status_sign );
1921 }
1922 PSA_ASSERT( psa_mac_abort( &operation ) );
1923
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001924 /* Verify correct MAC, one-shot case. */
1925 status = psa_mac_verify( key, exercise_alg, input, 128,
1926 mac, mac_len );
1927
1928 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1929 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001930 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001931 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001932
Neil Armstrong3af9b972022-02-07 12:20:21 +01001933 /* Verify correct MAC, multi-part case. */
1934 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1935 if( status == PSA_SUCCESS )
1936 {
1937 status = psa_mac_update( &operation, input, 128 );
1938 if( status == PSA_SUCCESS )
1939 {
1940 status = psa_mac_verify_finish( &operation, mac, mac_len );
1941 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1942 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1943 else
1944 TEST_EQUAL( status, expected_status_verify );
1945 }
1946 else
1947 {
1948 TEST_EQUAL( status, expected_status_verify );
1949 }
1950 }
1951 else
1952 {
1953 TEST_EQUAL( status, expected_status_verify );
1954 }
1955
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001957
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001958 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001959 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001960 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961
1962exit:
1963 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001964 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001965 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001966}
1967/* END_CASE */
1968
1969/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001970void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001971 int policy_alg,
1972 int key_type,
1973 data_t *key_data,
1974 int exercise_alg )
1975{
Ronald Cron5425a212020-08-04 14:58:35 +02001976 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001977 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001978 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001979 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001980 size_t output_buffer_size = 0;
1981 size_t input_buffer_size = 0;
1982 size_t output_length = 0;
1983 uint8_t *output = NULL;
1984 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985 psa_status_t status;
1986
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001987 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1988 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1989 input_buffer_size );
1990
1991 ASSERT_ALLOC( input, input_buffer_size );
1992 ASSERT_ALLOC( output, output_buffer_size );
1993
Gilles Peskine8817f612018-12-18 00:18:46 +01001994 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001996 psa_set_key_usage_flags( &attributes, policy_usage );
1997 psa_set_key_algorithm( &attributes, policy_alg );
1998 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001999
Gilles Peskine049c7532019-05-15 20:22:09 +02002000 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002001 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002003 /* Check if no key usage flag implication is done */
2004 TEST_EQUAL( policy_usage,
2005 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002006
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002007 /* Encrypt check, one-shot */
2008 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
2009 output, output_buffer_size,
2010 &output_length);
2011 if( policy_alg == exercise_alg &&
2012 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2013 PSA_ASSERT( status );
2014 else
2015 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2016
2017 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002018 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019 if( policy_alg == exercise_alg &&
2020 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002021 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002023 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 psa_cipher_abort( &operation );
2025
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002026 /* Decrypt check, one-shot */
2027 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
2028 input, input_buffer_size,
2029 &output_length);
2030 if( policy_alg == exercise_alg &&
2031 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
2032 PSA_ASSERT( status );
2033 else
2034 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2035
2036 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02002037 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038 if( policy_alg == exercise_alg &&
2039 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002040 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002042 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
2044exit:
2045 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002046 mbedtls_free( input );
2047 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002048 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002049 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050}
2051/* END_CASE */
2052
2053/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002054void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055 int policy_alg,
2056 int key_type,
2057 data_t *key_data,
2058 int nonce_length_arg,
2059 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002060 int exercise_alg,
2061 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002062{
Ronald Cron5425a212020-08-04 14:58:35 +02002063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002065 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002066 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002068 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069 unsigned char nonce[16] = {0};
2070 size_t nonce_length = nonce_length_arg;
2071 unsigned char tag[16];
2072 size_t tag_length = tag_length_arg;
2073 size_t output_length;
2074
Gilles Peskine7be11a72022-04-14 00:12:57 +02002075 TEST_LE_U( nonce_length, sizeof( nonce ) );
2076 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077
Gilles Peskine8817f612018-12-18 00:18:46 +01002078 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002080 psa_set_key_usage_flags( &attributes, policy_usage );
2081 psa_set_key_algorithm( &attributes, policy_alg );
2082 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083
Gilles Peskine049c7532019-05-15 20:22:09 +02002084 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002085 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002087 /* Check if no key usage implication is done */
2088 TEST_EQUAL( policy_usage,
2089 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002090
Neil Armstrong752d8112022-02-07 14:51:11 +01002091 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002092 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093 nonce, nonce_length,
2094 NULL, 0,
2095 NULL, 0,
2096 tag, tag_length,
2097 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002098 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2099 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002101 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102
Neil Armstrong752d8112022-02-07 14:51:11 +01002103 /* Encrypt check, multi-part */
2104 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2105 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2106 TEST_EQUAL( status, expected_status );
2107 else
2108 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2109
2110 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002112 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113 nonce, nonce_length,
2114 NULL, 0,
2115 tag, tag_length,
2116 NULL, 0,
2117 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002118 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2119 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2120 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002121 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002123 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
Neil Armstrong752d8112022-02-07 14:51:11 +01002125 /* Decrypt check, multi-part */
2126 PSA_ASSERT( psa_aead_abort( &operation ) );
2127 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2128 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2129 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2130 else
2131 TEST_EQUAL( status, expected_status );
2132
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002133exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002134 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002135 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002136 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137}
2138/* END_CASE */
2139
2140/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002141void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002142 int policy_alg,
2143 int key_type,
2144 data_t *key_data,
2145 int exercise_alg )
2146{
Ronald Cron5425a212020-08-04 14:58:35 +02002147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002149 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002150 psa_status_t status;
2151 size_t key_bits;
2152 size_t buffer_length;
2153 unsigned char *buffer = NULL;
2154 size_t output_length;
2155
Gilles Peskine8817f612018-12-18 00:18:46 +01002156 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002157
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002158 psa_set_key_usage_flags( &attributes, policy_usage );
2159 psa_set_key_algorithm( &attributes, policy_alg );
2160 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002161
Gilles Peskine049c7532019-05-15 20:22:09 +02002162 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002163 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002164
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002165 /* Check if no key usage implication is done */
2166 TEST_EQUAL( policy_usage,
2167 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002168
Ronald Cron5425a212020-08-04 14:58:35 +02002169 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002170 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002171 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2172 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002173 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002174
Ronald Cron5425a212020-08-04 14:58:35 +02002175 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002176 NULL, 0,
2177 NULL, 0,
2178 buffer, buffer_length,
2179 &output_length );
2180 if( policy_alg == exercise_alg &&
2181 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002182 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002183 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002184 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002185
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002186 if( buffer_length != 0 )
2187 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002188 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002189 buffer, buffer_length,
2190 NULL, 0,
2191 buffer, buffer_length,
2192 &output_length );
2193 if( policy_alg == exercise_alg &&
2194 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002195 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002197 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002198
2199exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002200 /*
2201 * Key attributes may have been returned by psa_get_key_attributes()
2202 * thus reset them as required.
2203 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002204 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002205
2206 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002207 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002208 mbedtls_free( buffer );
2209}
2210/* END_CASE */
2211
2212/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002213void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002214 int policy_alg,
2215 int key_type,
2216 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002217 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002218 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002219 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002220{
Ronald Cron5425a212020-08-04 14:58:35 +02002221 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002223 psa_key_usage_t policy_usage = policy_usage_arg;
2224 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002225 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002226 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2227 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2228 * compatible with the policy and `payload_length_arg` is supposed to be
2229 * a valid input length to sign. If `payload_length_arg <= 0`,
2230 * `exercise_alg` is supposed to be forbidden by the policy. */
2231 int compatible_alg = payload_length_arg > 0;
2232 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002233 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002234 size_t signature_length;
2235
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002236 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002237 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002238 TEST_EQUAL( expected_usage,
2239 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002240
Gilles Peskine8817f612018-12-18 00:18:46 +01002241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002242
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002243 psa_set_key_usage_flags( &attributes, policy_usage );
2244 psa_set_key_algorithm( &attributes, policy_alg );
2245 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002246
Gilles Peskine049c7532019-05-15 20:22:09 +02002247 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002248 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002249
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002250 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2251
Ronald Cron5425a212020-08-04 14:58:35 +02002252 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002253 payload, payload_length,
2254 signature, sizeof( signature ),
2255 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002256 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002257 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002258 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002259 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002260
2261 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002262 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002263 payload, payload_length,
2264 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002265 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002266 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002267 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002268 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002269
Gilles Peskinef7b41372021-09-22 16:15:05 +02002270 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002271 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002272 {
2273 status = psa_sign_message( key, exercise_alg,
2274 payload, payload_length,
2275 signature, sizeof( signature ),
2276 &signature_length );
2277 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2278 PSA_ASSERT( status );
2279 else
2280 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2281
2282 memset( signature, 0, sizeof( signature ) );
2283 status = psa_verify_message( key, exercise_alg,
2284 payload, payload_length,
2285 signature, sizeof( signature ) );
2286 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2287 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2288 else
2289 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2290 }
2291
Gilles Peskined5b33222018-06-18 22:20:03 +02002292exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002293 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002294 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002295}
2296/* END_CASE */
2297
Janos Follathba3fab92019-06-11 14:50:16 +01002298/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002299void derive_key_policy( int policy_usage,
2300 int policy_alg,
2301 int key_type,
2302 data_t *key_data,
2303 int exercise_alg )
2304{
Ronald Cron5425a212020-08-04 14:58:35 +02002305 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002307 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002308 psa_status_t status;
2309
Gilles Peskine8817f612018-12-18 00:18:46 +01002310 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002311
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002312 psa_set_key_usage_flags( &attributes, policy_usage );
2313 psa_set_key_algorithm( &attributes, policy_alg );
2314 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002315
Gilles Peskine049c7532019-05-15 20:22:09 +02002316 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002317 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002318
Janos Follathba3fab92019-06-11 14:50:16 +01002319 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2320
2321 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2322 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002323 {
Janos Follathba3fab92019-06-11 14:50:16 +01002324 PSA_ASSERT( psa_key_derivation_input_bytes(
2325 &operation,
2326 PSA_KEY_DERIVATION_INPUT_SEED,
2327 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002328 }
Janos Follathba3fab92019-06-11 14:50:16 +01002329
2330 status = psa_key_derivation_input_key( &operation,
2331 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002332 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002333
Gilles Peskineea0fb492018-07-12 17:17:20 +02002334 if( policy_alg == exercise_alg &&
2335 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002336 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002337 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002338 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002339
2340exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002341 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002342 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002343 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002344}
2345/* END_CASE */
2346
2347/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002348void agreement_key_policy( int policy_usage,
2349 int policy_alg,
2350 int key_type_arg,
2351 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002352 int exercise_alg,
2353 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002354{
Ronald Cron5425a212020-08-04 14:58:35 +02002355 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002356 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002357 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002358 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002359 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002360 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002361
Gilles Peskine8817f612018-12-18 00:18:46 +01002362 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002363
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002364 psa_set_key_usage_flags( &attributes, policy_usage );
2365 psa_set_key_algorithm( &attributes, policy_alg );
2366 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002367
Gilles Peskine049c7532019-05-15 20:22:09 +02002368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002369 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002370
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002371 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002372 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002373
Steven Cooremance48e852020-10-05 16:02:45 +02002374 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002375
2376exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002377 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002378 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002379 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002380}
2381/* END_CASE */
2382
2383/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002384void key_policy_alg2( int key_type_arg, data_t *key_data,
2385 int usage_arg, int alg_arg, int alg2_arg )
2386{
Ronald Cron5425a212020-08-04 14:58:35 +02002387 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002388 psa_key_type_t key_type = key_type_arg;
2389 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2390 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2391 psa_key_usage_t usage = usage_arg;
2392 psa_algorithm_t alg = alg_arg;
2393 psa_algorithm_t alg2 = alg2_arg;
2394
2395 PSA_ASSERT( psa_crypto_init( ) );
2396
2397 psa_set_key_usage_flags( &attributes, usage );
2398 psa_set_key_algorithm( &attributes, alg );
2399 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2400 psa_set_key_type( &attributes, key_type );
2401 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002402 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002403
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002404 /* Update the usage flags to obtain implicit usage flags */
2405 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002406 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002407 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2408 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2409 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2410
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002411 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002412 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002413 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002414 goto exit;
2415
2416exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002417 /*
2418 * Key attributes may have been returned by psa_get_key_attributes()
2419 * thus reset them as required.
2420 */
2421 psa_reset_key_attributes( &got_attributes );
2422
Ronald Cron5425a212020-08-04 14:58:35 +02002423 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002424 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002425}
2426/* END_CASE */
2427
2428/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002429void raw_agreement_key_policy( int policy_usage,
2430 int policy_alg,
2431 int key_type_arg,
2432 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002433 int exercise_alg,
2434 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002435{
Ronald Cron5425a212020-08-04 14:58:35 +02002436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002438 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002439 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002440 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002441 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002442
2443 PSA_ASSERT( psa_crypto_init( ) );
2444
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002445 psa_set_key_usage_flags( &attributes, policy_usage );
2446 psa_set_key_algorithm( &attributes, policy_alg );
2447 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002448
Gilles Peskine049c7532019-05-15 20:22:09 +02002449 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002450 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002451
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002452 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002453
Steven Cooremance48e852020-10-05 16:02:45 +02002454 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002455
2456exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002457 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002458 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002459 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002460}
2461/* END_CASE */
2462
2463/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002464void copy_success( int source_usage_arg,
2465 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302466 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002467 int type_arg, data_t *material,
2468 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002469 int target_usage_arg,
2470 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302471 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002472 int expected_usage_arg,
2473 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002474{
Gilles Peskineca25db92019-04-19 11:43:08 +02002475 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2476 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002477 psa_key_usage_t expected_usage = expected_usage_arg;
2478 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002479 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302480 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2481 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002482 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2483 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002484 uint8_t *export_buffer = NULL;
2485
Gilles Peskine57ab7212019-01-28 13:03:09 +01002486 PSA_ASSERT( psa_crypto_init( ) );
2487
Gilles Peskineca25db92019-04-19 11:43:08 +02002488 /* Prepare the source key. */
2489 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2490 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002491 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002492 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302493 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002494 PSA_ASSERT( psa_import_key( &source_attributes,
2495 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002496 &source_key ) );
2497 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002498
Gilles Peskineca25db92019-04-19 11:43:08 +02002499 /* Prepare the target attributes. */
2500 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002501 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002502 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002503 }
Archana8a180362021-07-05 02:18:48 +05302504 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002505
Gilles Peskineca25db92019-04-19 11:43:08 +02002506 if( target_usage_arg != -1 )
2507 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2508 if( target_alg_arg != -1 )
2509 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002510 if( target_alg2_arg != -1 )
2511 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002512
Archana8a180362021-07-05 02:18:48 +05302513
Gilles Peskine57ab7212019-01-28 13:03:09 +01002514 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002515 PSA_ASSERT( psa_copy_key( source_key,
2516 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002517
2518 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002519 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002520
2521 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002522 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002523 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2524 psa_get_key_type( &target_attributes ) );
2525 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2526 psa_get_key_bits( &target_attributes ) );
2527 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2528 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002529 TEST_EQUAL( expected_alg2,
2530 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002531 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2532 {
2533 size_t length;
2534 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002535 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002536 material->len, &length ) );
2537 ASSERT_COMPARE( material->x, material->len,
2538 export_buffer, length );
2539 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002540
Archana8a180362021-07-05 02:18:48 +05302541 if( !psa_key_lifetime_is_external( target_lifetime ) )
2542 {
2543 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2544 goto exit;
2545 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2546 goto exit;
2547 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002548
Ronald Cron5425a212020-08-04 14:58:35 +02002549 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002550
2551exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002552 /*
2553 * Source and target key attributes may have been returned by
2554 * psa_get_key_attributes() thus reset them as required.
2555 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002556 psa_reset_key_attributes( &source_attributes );
2557 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002558
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002559 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002560 mbedtls_free( export_buffer );
2561}
2562/* END_CASE */
2563
2564/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002565void copy_fail( int source_usage_arg,
2566 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302567 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002568 int type_arg, data_t *material,
2569 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002570 int target_usage_arg,
2571 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002572 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002573 int expected_status_arg )
2574{
2575 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2576 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002577 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2578 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002579 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002580
2581 PSA_ASSERT( psa_crypto_init( ) );
2582
2583 /* Prepare the source key. */
2584 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2585 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002586 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002587 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302588 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002589 PSA_ASSERT( psa_import_key( &source_attributes,
2590 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002591 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002592
2593 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002594 psa_set_key_id( &target_attributes, key_id );
2595 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002596 psa_set_key_type( &target_attributes, target_type_arg );
2597 psa_set_key_bits( &target_attributes, target_bits_arg );
2598 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2599 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002600 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002601
2602 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002603 TEST_EQUAL( psa_copy_key( source_key,
2604 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002605 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002606
Ronald Cron5425a212020-08-04 14:58:35 +02002607 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002608
Gilles Peskine4a644642019-05-03 17:14:08 +02002609exit:
2610 psa_reset_key_attributes( &source_attributes );
2611 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002612 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002613}
2614/* END_CASE */
2615
2616/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002617void hash_operation_init( )
2618{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002619 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002620 /* Test each valid way of initializing the object, except for `= {0}`, as
2621 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2622 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002623 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002624 psa_hash_operation_t func = psa_hash_operation_init( );
2625 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2626 psa_hash_operation_t zero;
2627
2628 memset( &zero, 0, sizeof( zero ) );
2629
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002630 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002631 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2632 PSA_ERROR_BAD_STATE );
2633 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2634 PSA_ERROR_BAD_STATE );
2635 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2636 PSA_ERROR_BAD_STATE );
2637
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002638 /* A default hash operation should be abortable without error. */
2639 PSA_ASSERT( psa_hash_abort( &func ) );
2640 PSA_ASSERT( psa_hash_abort( &init ) );
2641 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002642}
2643/* END_CASE */
2644
2645/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002646void hash_setup( int alg_arg,
2647 int expected_status_arg )
2648{
2649 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002650 uint8_t *output = NULL;
2651 size_t output_size = 0;
2652 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002653 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002654 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002655 psa_status_t status;
2656
Gilles Peskine8817f612018-12-18 00:18:46 +01002657 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002658
Neil Armstrongedb20862022-02-07 15:47:44 +01002659 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002660 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002661 ASSERT_ALLOC( output, output_size );
2662
2663 status = psa_hash_compute( alg, NULL, 0,
2664 output, output_size, &output_length );
2665 TEST_EQUAL( status, expected_status );
2666
2667 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002668 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002669 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002670
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002671 /* Whether setup succeeded or failed, abort must succeed. */
2672 PSA_ASSERT( psa_hash_abort( &operation ) );
2673
2674 /* If setup failed, reproduce the failure, so as to
2675 * test the resulting state of the operation object. */
2676 if( status != PSA_SUCCESS )
2677 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2678
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002679 /* Now the operation object should be reusable. */
2680#if defined(KNOWN_SUPPORTED_HASH_ALG)
2681 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2682 PSA_ASSERT( psa_hash_abort( &operation ) );
2683#endif
2684
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002685exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002686 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002687 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002688}
2689/* END_CASE */
2690
2691/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002692void hash_compute_fail( int alg_arg, data_t *input,
2693 int output_size_arg, int expected_status_arg )
2694{
2695 psa_algorithm_t alg = alg_arg;
2696 uint8_t *output = NULL;
2697 size_t output_size = output_size_arg;
2698 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002699 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002700 psa_status_t expected_status = expected_status_arg;
2701 psa_status_t status;
2702
2703 ASSERT_ALLOC( output, output_size );
2704
2705 PSA_ASSERT( psa_crypto_init( ) );
2706
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002707 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002708 status = psa_hash_compute( alg, input->x, input->len,
2709 output, output_size, &output_length );
2710 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002711 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002712
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002713 /* Hash Compute, multi-part */
2714 status = psa_hash_setup( &operation, alg );
2715 if( status == PSA_SUCCESS )
2716 {
2717 status = psa_hash_update( &operation, input->x, input->len );
2718 if( status == PSA_SUCCESS )
2719 {
2720 status = psa_hash_finish( &operation, output, output_size,
2721 &output_length );
2722 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002723 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002724 else
2725 TEST_EQUAL( status, expected_status );
2726 }
2727 else
2728 {
2729 TEST_EQUAL( status, expected_status );
2730 }
2731 }
2732 else
2733 {
2734 TEST_EQUAL( status, expected_status );
2735 }
2736
Gilles Peskine0a749c82019-11-28 19:33:58 +01002737exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002738 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002739 mbedtls_free( output );
2740 PSA_DONE( );
2741}
2742/* END_CASE */
2743
2744/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002745void hash_compare_fail( int alg_arg, data_t *input,
2746 data_t *reference_hash,
2747 int expected_status_arg )
2748{
2749 psa_algorithm_t alg = alg_arg;
2750 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002751 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002752 psa_status_t status;
2753
2754 PSA_ASSERT( psa_crypto_init( ) );
2755
Neil Armstrong55a1be12022-02-07 11:23:20 +01002756 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002757 status = psa_hash_compare( alg, input->x, input->len,
2758 reference_hash->x, reference_hash->len );
2759 TEST_EQUAL( status, expected_status );
2760
Neil Armstrong55a1be12022-02-07 11:23:20 +01002761 /* Hash Compare, multi-part */
2762 status = psa_hash_setup( &operation, alg );
2763 if( status == PSA_SUCCESS )
2764 {
2765 status = psa_hash_update( &operation, input->x, input->len );
2766 if( status == PSA_SUCCESS )
2767 {
2768 status = psa_hash_verify( &operation, reference_hash->x,
2769 reference_hash->len );
2770 TEST_EQUAL( status, expected_status );
2771 }
2772 else
2773 {
2774 TEST_EQUAL( status, expected_status );
2775 }
2776 }
2777 else
2778 {
2779 TEST_EQUAL( status, expected_status );
2780 }
2781
Gilles Peskine88e08462020-01-28 20:43:00 +01002782exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002783 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002784 PSA_DONE( );
2785}
2786/* END_CASE */
2787
2788/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002789void hash_compute_compare( int alg_arg, data_t *input,
2790 data_t *expected_output )
2791{
2792 psa_algorithm_t alg = alg_arg;
2793 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2794 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002795 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002796 size_t i;
2797
2798 PSA_ASSERT( psa_crypto_init( ) );
2799
Neil Armstrongca30a002022-02-07 11:40:23 +01002800 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002801 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002802 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002803 &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 tight 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 PSA_HASH_LENGTH( alg ),
2813 &output_length ) );
2814 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2815 ASSERT_COMPARE( output, output_length,
2816 expected_output->x, expected_output->len );
2817
2818 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002819 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2820 output, sizeof( output ),
2821 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002822 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002823 ASSERT_COMPARE( output, output_length,
2824 expected_output->x, expected_output->len );
2825
Neil Armstrongca30a002022-02-07 11:40:23 +01002826 /* Compute with larger buffer, multi-part */
2827 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2828 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2829 PSA_ASSERT( psa_hash_finish( &operation, output,
2830 sizeof( output ), &output_length ) );
2831 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2832 ASSERT_COMPARE( output, output_length,
2833 expected_output->x, expected_output->len );
2834
2835 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002836 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2837 output, output_length ) );
2838
Neil Armstrongca30a002022-02-07 11:40:23 +01002839 /* Compare with correct hash, multi-part */
2840 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2841 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2842 PSA_ASSERT( psa_hash_verify( &operation, output,
2843 output_length ) );
2844
2845 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002846 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2847 output, output_length + 1 ),
2848 PSA_ERROR_INVALID_SIGNATURE );
2849
Neil Armstrongca30a002022-02-07 11:40:23 +01002850 /* Compare with trailing garbage, multi-part */
2851 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2852 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2853 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2854 PSA_ERROR_INVALID_SIGNATURE );
2855
2856 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002857 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2858 output, output_length - 1 ),
2859 PSA_ERROR_INVALID_SIGNATURE );
2860
Neil Armstrongca30a002022-02-07 11:40:23 +01002861 /* Compare with truncated hash, multi-part */
2862 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2863 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2864 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2865 PSA_ERROR_INVALID_SIGNATURE );
2866
Gilles Peskine0a749c82019-11-28 19:33:58 +01002867 /* Compare with corrupted value */
2868 for( i = 0; i < output_length; i++ )
2869 {
Chris Jones9634bb12021-01-20 15:56:42 +00002870 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002871 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002872
2873 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002874 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2875 output, output_length ),
2876 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002877
2878 /* Multi-Part */
2879 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2880 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2881 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2882 PSA_ERROR_INVALID_SIGNATURE );
2883
Gilles Peskine0a749c82019-11-28 19:33:58 +01002884 output[i] ^= 1;
2885 }
2886
2887exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002888 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002889 PSA_DONE( );
2890}
2891/* END_CASE */
2892
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002893/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002894void hash_bad_order( )
2895{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002896 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002897 unsigned char input[] = "";
2898 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002899 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002900 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2901 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2902 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002903 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002904 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002905 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002906
Gilles Peskine8817f612018-12-18 00:18:46 +01002907 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002908
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002909 /* Call setup twice in a row. */
2910 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002911 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002912 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2913 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002914 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002915 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002916 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002917
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002918 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002919 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
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002923 /* Check that update calls abort on error. */
2924 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002925 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002926 ASSERT_OPERATION_IS_ACTIVE( operation );
2927 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2928 PSA_ERROR_BAD_STATE );
2929 ASSERT_OPERATION_IS_INACTIVE( operation );
2930 PSA_ASSERT( psa_hash_abort( &operation ) );
2931 ASSERT_OPERATION_IS_INACTIVE( operation );
2932
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002933 /* Call update after finish. */
2934 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2935 PSA_ASSERT( psa_hash_finish( &operation,
2936 hash, sizeof( hash ), &hash_len ) );
2937 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002938 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002939 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002940
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002941 /* Call verify without calling setup beforehand. */
2942 TEST_EQUAL( psa_hash_verify( &operation,
2943 valid_hash, sizeof( valid_hash ) ),
2944 PSA_ERROR_BAD_STATE );
2945 PSA_ASSERT( psa_hash_abort( &operation ) );
2946
2947 /* Call verify after finish. */
2948 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2949 PSA_ASSERT( psa_hash_finish( &operation,
2950 hash, sizeof( hash ), &hash_len ) );
2951 TEST_EQUAL( psa_hash_verify( &operation,
2952 valid_hash, sizeof( valid_hash ) ),
2953 PSA_ERROR_BAD_STATE );
2954 PSA_ASSERT( psa_hash_abort( &operation ) );
2955
2956 /* Call verify twice in a row. */
2957 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002958 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002959 PSA_ASSERT( psa_hash_verify( &operation,
2960 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002961 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002962 TEST_EQUAL( psa_hash_verify( &operation,
2963 valid_hash, sizeof( valid_hash ) ),
2964 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002965 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002966 PSA_ASSERT( psa_hash_abort( &operation ) );
2967
2968 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002969 TEST_EQUAL( psa_hash_finish( &operation,
2970 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002971 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002972 PSA_ASSERT( psa_hash_abort( &operation ) );
2973
2974 /* Call finish twice in a row. */
2975 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2976 PSA_ASSERT( psa_hash_finish( &operation,
2977 hash, sizeof( hash ), &hash_len ) );
2978 TEST_EQUAL( psa_hash_finish( &operation,
2979 hash, sizeof( hash ), &hash_len ),
2980 PSA_ERROR_BAD_STATE );
2981 PSA_ASSERT( psa_hash_abort( &operation ) );
2982
2983 /* Call finish after calling verify. */
2984 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2985 PSA_ASSERT( psa_hash_verify( &operation,
2986 valid_hash, sizeof( valid_hash ) ) );
2987 TEST_EQUAL( psa_hash_finish( &operation,
2988 hash, sizeof( hash ), &hash_len ),
2989 PSA_ERROR_BAD_STATE );
2990 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002991
2992exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002993 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002994}
2995/* END_CASE */
2996
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002997/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002998void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002999{
3000 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02003001 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
3002 * appended to it */
3003 unsigned char hash[] = {
3004 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
3005 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
3006 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003007 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003008 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03003009
Gilles Peskine8817f612018-12-18 00:18:46 +01003010 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03003011
itayzafrir27e69452018-11-01 14:26:34 +02003012 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003013 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003014 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003015 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003016 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003017 ASSERT_OPERATION_IS_INACTIVE( operation );
3018 PSA_ASSERT( psa_hash_abort( &operation ) );
3019 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03003020
itayzafrir27e69452018-11-01 14:26:34 +02003021 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003023 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003024 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03003025
itayzafrir27e69452018-11-01 14:26:34 +02003026 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003027 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003028 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01003029 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03003030
itayzafrirec93d302018-10-18 18:01:10 +03003031exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003032 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03003033}
3034/* END_CASE */
3035
Ronald Cronee414c72021-03-18 18:50:08 +01003036/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003037void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03003038{
3039 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003040 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003041 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003042 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003043 size_t hash_len;
3044
Gilles Peskine8817f612018-12-18 00:18:46 +01003045 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003046
itayzafrir58028322018-10-25 10:22:01 +03003047 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003049 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003050 hash, expected_size - 1, &hash_len ),
3051 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003052
3053exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003054 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003055}
3056/* END_CASE */
3057
Ronald Cronee414c72021-03-18 18:50:08 +01003058/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003059void hash_clone_source_state( )
3060{
3061 psa_algorithm_t alg = PSA_ALG_SHA_256;
3062 unsigned char hash[PSA_HASH_MAX_SIZE];
3063 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3064 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3065 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3066 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3067 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3068 size_t hash_len;
3069
3070 PSA_ASSERT( psa_crypto_init( ) );
3071 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3072
3073 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3074 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3075 PSA_ASSERT( psa_hash_finish( &op_finished,
3076 hash, sizeof( hash ), &hash_len ) );
3077 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3078 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3079
3080 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3081 PSA_ERROR_BAD_STATE );
3082
3083 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3084 PSA_ASSERT( psa_hash_finish( &op_init,
3085 hash, sizeof( hash ), &hash_len ) );
3086 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3087 PSA_ASSERT( psa_hash_finish( &op_finished,
3088 hash, sizeof( hash ), &hash_len ) );
3089 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3090 PSA_ASSERT( psa_hash_finish( &op_aborted,
3091 hash, sizeof( hash ), &hash_len ) );
3092
3093exit:
3094 psa_hash_abort( &op_source );
3095 psa_hash_abort( &op_init );
3096 psa_hash_abort( &op_setup );
3097 psa_hash_abort( &op_finished );
3098 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003099 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003100}
3101/* END_CASE */
3102
Ronald Cronee414c72021-03-18 18:50:08 +01003103/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003104void hash_clone_target_state( )
3105{
3106 psa_algorithm_t alg = PSA_ALG_SHA_256;
3107 unsigned char hash[PSA_HASH_MAX_SIZE];
3108 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3109 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3110 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3111 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3112 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3113 size_t hash_len;
3114
3115 PSA_ASSERT( psa_crypto_init( ) );
3116
3117 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3118 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3119 PSA_ASSERT( psa_hash_finish( &op_finished,
3120 hash, sizeof( hash ), &hash_len ) );
3121 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3122 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3123
3124 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3125 PSA_ASSERT( psa_hash_finish( &op_target,
3126 hash, sizeof( hash ), &hash_len ) );
3127
3128 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3129 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3130 PSA_ERROR_BAD_STATE );
3131 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3132 PSA_ERROR_BAD_STATE );
3133
3134exit:
3135 psa_hash_abort( &op_target );
3136 psa_hash_abort( &op_init );
3137 psa_hash_abort( &op_setup );
3138 psa_hash_abort( &op_finished );
3139 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003140 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003141}
3142/* END_CASE */
3143
itayzafrir58028322018-10-25 10:22:01 +03003144/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003145void mac_operation_init( )
3146{
Jaeden Amero252ef282019-02-15 14:05:35 +00003147 const uint8_t input[1] = { 0 };
3148
Jaeden Amero769ce272019-01-04 11:48:03 +00003149 /* Test each valid way of initializing the object, except for `= {0}`, as
3150 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3151 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003152 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003153 psa_mac_operation_t func = psa_mac_operation_init( );
3154 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3155 psa_mac_operation_t zero;
3156
3157 memset( &zero, 0, sizeof( zero ) );
3158
Jaeden Amero252ef282019-02-15 14:05:35 +00003159 /* A freshly-initialized MAC operation should not be usable. */
3160 TEST_EQUAL( psa_mac_update( &func,
3161 input, sizeof( input ) ),
3162 PSA_ERROR_BAD_STATE );
3163 TEST_EQUAL( psa_mac_update( &init,
3164 input, sizeof( input ) ),
3165 PSA_ERROR_BAD_STATE );
3166 TEST_EQUAL( psa_mac_update( &zero,
3167 input, sizeof( input ) ),
3168 PSA_ERROR_BAD_STATE );
3169
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003170 /* A default MAC operation should be abortable without error. */
3171 PSA_ASSERT( psa_mac_abort( &func ) );
3172 PSA_ASSERT( psa_mac_abort( &init ) );
3173 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003174}
3175/* END_CASE */
3176
3177/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003178void mac_setup( int key_type_arg,
3179 data_t *key,
3180 int alg_arg,
3181 int expected_status_arg )
3182{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003183 psa_key_type_t key_type = key_type_arg;
3184 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003185 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003186 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003187 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3188#if defined(KNOWN_SUPPORTED_MAC_ALG)
3189 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3190#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003193
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003194 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3195 &operation, &status ) )
3196 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003197 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003198
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003199 /* The operation object should be reusable. */
3200#if defined(KNOWN_SUPPORTED_MAC_ALG)
3201 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3202 smoke_test_key_data,
3203 sizeof( smoke_test_key_data ),
3204 KNOWN_SUPPORTED_MAC_ALG,
3205 &operation, &status ) )
3206 goto exit;
3207 TEST_EQUAL( status, PSA_SUCCESS );
3208#endif
3209
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003210exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003211 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003212}
3213/* END_CASE */
3214
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003215/* 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 +00003216void mac_bad_order( )
3217{
Ronald Cron5425a212020-08-04 14:58:35 +02003218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003219 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3220 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003221 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003222 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3223 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3224 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003226 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3227 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3228 size_t sign_mac_length = 0;
3229 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3230 const uint8_t verify_mac[] = {
3231 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3232 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3233 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3234
3235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003236 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003237 psa_set_key_algorithm( &attributes, alg );
3238 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003239
Ronald Cron5425a212020-08-04 14:58:35 +02003240 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3241 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003242
Jaeden Amero252ef282019-02-15 14:05:35 +00003243 /* Call update without calling setup beforehand. */
3244 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3245 PSA_ERROR_BAD_STATE );
3246 PSA_ASSERT( psa_mac_abort( &operation ) );
3247
3248 /* Call sign finish without calling setup beforehand. */
3249 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3250 &sign_mac_length),
3251 PSA_ERROR_BAD_STATE );
3252 PSA_ASSERT( psa_mac_abort( &operation ) );
3253
3254 /* Call verify finish without calling setup beforehand. */
3255 TEST_EQUAL( psa_mac_verify_finish( &operation,
3256 verify_mac, sizeof( verify_mac ) ),
3257 PSA_ERROR_BAD_STATE );
3258 PSA_ASSERT( psa_mac_abort( &operation ) );
3259
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003260 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003261 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003262 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003263 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003264 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003265 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003266 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003267 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003268
Jaeden Amero252ef282019-02-15 14:05:35 +00003269 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003270 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003271 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3272 PSA_ASSERT( psa_mac_sign_finish( &operation,
3273 sign_mac, sizeof( sign_mac ),
3274 &sign_mac_length ) );
3275 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3276 PSA_ERROR_BAD_STATE );
3277 PSA_ASSERT( psa_mac_abort( &operation ) );
3278
3279 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003280 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003281 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3282 PSA_ASSERT( psa_mac_verify_finish( &operation,
3283 verify_mac, sizeof( verify_mac ) ) );
3284 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3285 PSA_ERROR_BAD_STATE );
3286 PSA_ASSERT( psa_mac_abort( &operation ) );
3287
3288 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003289 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003290 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3291 PSA_ASSERT( psa_mac_sign_finish( &operation,
3292 sign_mac, sizeof( sign_mac ),
3293 &sign_mac_length ) );
3294 TEST_EQUAL( psa_mac_sign_finish( &operation,
3295 sign_mac, sizeof( sign_mac ),
3296 &sign_mac_length ),
3297 PSA_ERROR_BAD_STATE );
3298 PSA_ASSERT( psa_mac_abort( &operation ) );
3299
3300 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003301 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003302 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3303 PSA_ASSERT( psa_mac_verify_finish( &operation,
3304 verify_mac, sizeof( verify_mac ) ) );
3305 TEST_EQUAL( psa_mac_verify_finish( &operation,
3306 verify_mac, sizeof( verify_mac ) ),
3307 PSA_ERROR_BAD_STATE );
3308 PSA_ASSERT( psa_mac_abort( &operation ) );
3309
3310 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003311 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003312 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003313 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003314 TEST_EQUAL( psa_mac_verify_finish( &operation,
3315 verify_mac, sizeof( verify_mac ) ),
3316 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003317 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003318 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003319 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003320
3321 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003322 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003323 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003324 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003325 TEST_EQUAL( psa_mac_sign_finish( &operation,
3326 sign_mac, sizeof( sign_mac ),
3327 &sign_mac_length ),
3328 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003329 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003330 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003331 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003332
Ronald Cron5425a212020-08-04 14:58:35 +02003333 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003334
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003335exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003336 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003337}
3338/* END_CASE */
3339
3340/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003341void mac_sign_verify_multi( int key_type_arg,
3342 data_t *key_data,
3343 int alg_arg,
3344 data_t *input,
3345 int is_verify,
3346 data_t *expected_mac )
3347{
3348 size_t data_part_len = 0;
3349
3350 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3351 {
3352 /* Split data into length(data_part_len) parts. */
3353 mbedtls_test_set_step( 2000 + data_part_len );
3354
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003355 if( mac_multipart_internal_func( key_type_arg, key_data,
3356 alg_arg,
3357 input, data_part_len,
3358 expected_mac,
3359 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003360 break;
3361
3362 /* length(0) part, length(data_part_len) part, length(0) part... */
3363 mbedtls_test_set_step( 3000 + data_part_len );
3364
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003365 if( mac_multipart_internal_func( key_type_arg, key_data,
3366 alg_arg,
3367 input, data_part_len,
3368 expected_mac,
3369 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003370 break;
3371 }
3372
3373 /* Goto is required to silence warnings about unused labels, as we
3374 * don't actually do any test assertions in this function. */
3375 goto exit;
3376}
3377/* END_CASE */
3378
3379/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003380void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003381 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003382 int alg_arg,
3383 data_t *input,
3384 data_t *expected_mac )
3385{
Ronald Cron5425a212020-08-04 14:58:35 +02003386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003387 psa_key_type_t key_type = key_type_arg;
3388 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003389 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003390 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003391 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003392 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003393 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003394 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003395 const size_t output_sizes_to_test[] = {
3396 0,
3397 1,
3398 expected_mac->len - 1,
3399 expected_mac->len,
3400 expected_mac->len + 1,
3401 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003402
Gilles Peskine7be11a72022-04-14 00:12:57 +02003403 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003404 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003405 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003406
Gilles Peskine8817f612018-12-18 00:18:46 +01003407 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003408
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003409 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003410 psa_set_key_algorithm( &attributes, alg );
3411 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003412
Ronald Cron5425a212020-08-04 14:58:35 +02003413 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3414 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003415
Gilles Peskine8b356b52020-08-25 23:44:59 +02003416 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3417 {
3418 const size_t output_size = output_sizes_to_test[i];
3419 psa_status_t expected_status =
3420 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3421 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003422
Chris Jones9634bb12021-01-20 15:56:42 +00003423 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003424 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003425
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003426 /* Calculate the MAC, one-shot case. */
3427 TEST_EQUAL( psa_mac_compute( key, alg,
3428 input->x, input->len,
3429 actual_mac, output_size, &mac_length ),
3430 expected_status );
3431 if( expected_status == PSA_SUCCESS )
3432 {
3433 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3434 actual_mac, mac_length );
3435 }
3436
3437 if( output_size > 0 )
3438 memset( actual_mac, 0, output_size );
3439
3440 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003441 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003442 PSA_ASSERT( psa_mac_update( &operation,
3443 input->x, input->len ) );
3444 TEST_EQUAL( psa_mac_sign_finish( &operation,
3445 actual_mac, output_size,
3446 &mac_length ),
3447 expected_status );
3448 PSA_ASSERT( psa_mac_abort( &operation ) );
3449
3450 if( expected_status == PSA_SUCCESS )
3451 {
3452 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3453 actual_mac, mac_length );
3454 }
3455 mbedtls_free( actual_mac );
3456 actual_mac = NULL;
3457 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003458
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003459exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003460 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003461 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003462 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003463 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003464}
3465/* END_CASE */
3466
3467/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003468void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003469 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003470 int alg_arg,
3471 data_t *input,
3472 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003473{
Ronald Cron5425a212020-08-04 14:58:35 +02003474 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003475 psa_key_type_t key_type = key_type_arg;
3476 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003477 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003479 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003480
Gilles Peskine7be11a72022-04-14 00:12:57 +02003481 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003484
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003486 psa_set_key_algorithm( &attributes, alg );
3487 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003488
Ronald Cron5425a212020-08-04 14:58:35 +02003489 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3490 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003491
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003492 /* Verify correct MAC, one-shot case. */
3493 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3494 expected_mac->x, expected_mac->len ) );
3495
3496 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003497 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003498 PSA_ASSERT( psa_mac_update( &operation,
3499 input->x, input->len ) );
3500 PSA_ASSERT( psa_mac_verify_finish( &operation,
3501 expected_mac->x,
3502 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003503
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003504 /* Test a MAC that's too short, one-shot case. */
3505 TEST_EQUAL( psa_mac_verify( key, alg,
3506 input->x, input->len,
3507 expected_mac->x,
3508 expected_mac->len - 1 ),
3509 PSA_ERROR_INVALID_SIGNATURE );
3510
3511 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003512 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003513 PSA_ASSERT( psa_mac_update( &operation,
3514 input->x, input->len ) );
3515 TEST_EQUAL( psa_mac_verify_finish( &operation,
3516 expected_mac->x,
3517 expected_mac->len - 1 ),
3518 PSA_ERROR_INVALID_SIGNATURE );
3519
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003520 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003521 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3522 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003523 TEST_EQUAL( psa_mac_verify( key, alg,
3524 input->x, input->len,
3525 perturbed_mac, expected_mac->len + 1 ),
3526 PSA_ERROR_INVALID_SIGNATURE );
3527
3528 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003529 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003530 PSA_ASSERT( psa_mac_update( &operation,
3531 input->x, input->len ) );
3532 TEST_EQUAL( psa_mac_verify_finish( &operation,
3533 perturbed_mac,
3534 expected_mac->len + 1 ),
3535 PSA_ERROR_INVALID_SIGNATURE );
3536
3537 /* Test changing one byte. */
3538 for( size_t i = 0; i < expected_mac->len; i++ )
3539 {
Chris Jones9634bb12021-01-20 15:56:42 +00003540 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003541 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003542
3543 TEST_EQUAL( psa_mac_verify( key, alg,
3544 input->x, input->len,
3545 perturbed_mac, expected_mac->len ),
3546 PSA_ERROR_INVALID_SIGNATURE );
3547
Ronald Cron5425a212020-08-04 14:58:35 +02003548 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003549 PSA_ASSERT( psa_mac_update( &operation,
3550 input->x, input->len ) );
3551 TEST_EQUAL( psa_mac_verify_finish( &operation,
3552 perturbed_mac,
3553 expected_mac->len ),
3554 PSA_ERROR_INVALID_SIGNATURE );
3555 perturbed_mac[i] ^= 1;
3556 }
3557
Gilles Peskine8c9def32018-02-08 10:02:12 +01003558exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003559 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003560 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003561 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003562 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003563}
3564/* END_CASE */
3565
3566/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003567void cipher_operation_init( )
3568{
Jaeden Ameroab439972019-02-15 14:12:05 +00003569 const uint8_t input[1] = { 0 };
3570 unsigned char output[1] = { 0 };
3571 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003572 /* Test each valid way of initializing the object, except for `= {0}`, as
3573 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3574 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003575 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003576 psa_cipher_operation_t func = psa_cipher_operation_init( );
3577 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3578 psa_cipher_operation_t zero;
3579
3580 memset( &zero, 0, sizeof( zero ) );
3581
Jaeden Ameroab439972019-02-15 14:12:05 +00003582 /* A freshly-initialized cipher operation should not be usable. */
3583 TEST_EQUAL( psa_cipher_update( &func,
3584 input, sizeof( input ),
3585 output, sizeof( output ),
3586 &output_length ),
3587 PSA_ERROR_BAD_STATE );
3588 TEST_EQUAL( psa_cipher_update( &init,
3589 input, sizeof( input ),
3590 output, sizeof( output ),
3591 &output_length ),
3592 PSA_ERROR_BAD_STATE );
3593 TEST_EQUAL( psa_cipher_update( &zero,
3594 input, sizeof( input ),
3595 output, sizeof( output ),
3596 &output_length ),
3597 PSA_ERROR_BAD_STATE );
3598
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003599 /* A default cipher operation should be abortable without error. */
3600 PSA_ASSERT( psa_cipher_abort( &func ) );
3601 PSA_ASSERT( psa_cipher_abort( &init ) );
3602 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003603}
3604/* END_CASE */
3605
3606/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003607void cipher_setup( int key_type_arg,
3608 data_t *key,
3609 int alg_arg,
3610 int expected_status_arg )
3611{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003612 psa_key_type_t key_type = key_type_arg;
3613 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003614 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003615 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003616 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003617#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003618 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3619#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003620
Gilles Peskine8817f612018-12-18 00:18:46 +01003621 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003622
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003623 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3624 &operation, &status ) )
3625 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003626 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003627
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003628 /* The operation object should be reusable. */
3629#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3630 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3631 smoke_test_key_data,
3632 sizeof( smoke_test_key_data ),
3633 KNOWN_SUPPORTED_CIPHER_ALG,
3634 &operation, &status ) )
3635 goto exit;
3636 TEST_EQUAL( status, PSA_SUCCESS );
3637#endif
3638
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003639exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003640 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003641 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003642}
3643/* END_CASE */
3644
Ronald Cronee414c72021-03-18 18:50:08 +01003645/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003646void cipher_bad_order( )
3647{
Ronald Cron5425a212020-08-04 14:58:35 +02003648 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003649 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3650 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003651 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003652 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003653 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003654 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003655 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3656 0xaa, 0xaa, 0xaa, 0xaa };
3657 const uint8_t text[] = {
3658 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3659 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003660 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003661 size_t length = 0;
3662
3663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003664 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3665 psa_set_key_algorithm( &attributes, alg );
3666 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003667 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3668 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003669
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003670 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003671 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003672 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003673 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003674 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003675 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003676 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003677 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003678
3679 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003680 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003681 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003682 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003683 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003684 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003685 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003686 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003687
Jaeden Ameroab439972019-02-15 14:12:05 +00003688 /* Generate an IV without calling setup beforehand. */
3689 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3690 buffer, sizeof( buffer ),
3691 &length ),
3692 PSA_ERROR_BAD_STATE );
3693 PSA_ASSERT( psa_cipher_abort( &operation ) );
3694
3695 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003696 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003697 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3698 buffer, sizeof( buffer ),
3699 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003700 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003701 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3702 buffer, sizeof( buffer ),
3703 &length ),
3704 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003705 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003706 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003707 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003708
3709 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003710 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003711 PSA_ASSERT( psa_cipher_set_iv( &operation,
3712 iv, sizeof( iv ) ) );
3713 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3714 buffer, sizeof( buffer ),
3715 &length ),
3716 PSA_ERROR_BAD_STATE );
3717 PSA_ASSERT( psa_cipher_abort( &operation ) );
3718
3719 /* Set an IV without calling setup beforehand. */
3720 TEST_EQUAL( psa_cipher_set_iv( &operation,
3721 iv, sizeof( iv ) ),
3722 PSA_ERROR_BAD_STATE );
3723 PSA_ASSERT( psa_cipher_abort( &operation ) );
3724
3725 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003726 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003727 PSA_ASSERT( psa_cipher_set_iv( &operation,
3728 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003729 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003730 TEST_EQUAL( psa_cipher_set_iv( &operation,
3731 iv, sizeof( iv ) ),
3732 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003733 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003734 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003735 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003736
3737 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003738 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003739 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3740 buffer, sizeof( buffer ),
3741 &length ) );
3742 TEST_EQUAL( psa_cipher_set_iv( &operation,
3743 iv, sizeof( iv ) ),
3744 PSA_ERROR_BAD_STATE );
3745 PSA_ASSERT( psa_cipher_abort( &operation ) );
3746
3747 /* Call update without calling setup beforehand. */
3748 TEST_EQUAL( psa_cipher_update( &operation,
3749 text, sizeof( text ),
3750 buffer, sizeof( buffer ),
3751 &length ),
3752 PSA_ERROR_BAD_STATE );
3753 PSA_ASSERT( psa_cipher_abort( &operation ) );
3754
3755 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003756 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003757 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003758 TEST_EQUAL( psa_cipher_update( &operation,
3759 text, sizeof( text ),
3760 buffer, sizeof( buffer ),
3761 &length ),
3762 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003763 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003764 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003765 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003766
3767 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003768 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003769 PSA_ASSERT( psa_cipher_set_iv( &operation,
3770 iv, sizeof( iv ) ) );
3771 PSA_ASSERT( psa_cipher_finish( &operation,
3772 buffer, sizeof( buffer ), &length ) );
3773 TEST_EQUAL( psa_cipher_update( &operation,
3774 text, sizeof( text ),
3775 buffer, sizeof( buffer ),
3776 &length ),
3777 PSA_ERROR_BAD_STATE );
3778 PSA_ASSERT( psa_cipher_abort( &operation ) );
3779
3780 /* Call finish without calling setup beforehand. */
3781 TEST_EQUAL( psa_cipher_finish( &operation,
3782 buffer, sizeof( buffer ), &length ),
3783 PSA_ERROR_BAD_STATE );
3784 PSA_ASSERT( psa_cipher_abort( &operation ) );
3785
3786 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003787 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003788 /* Not calling update means we are encrypting an empty buffer, which is OK
3789 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003790 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003791 TEST_EQUAL( psa_cipher_finish( &operation,
3792 buffer, sizeof( buffer ), &length ),
3793 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003794 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003795 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003796 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003797
3798 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003799 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003800 PSA_ASSERT( psa_cipher_set_iv( &operation,
3801 iv, sizeof( iv ) ) );
3802 PSA_ASSERT( psa_cipher_finish( &operation,
3803 buffer, sizeof( buffer ), &length ) );
3804 TEST_EQUAL( psa_cipher_finish( &operation,
3805 buffer, sizeof( buffer ), &length ),
3806 PSA_ERROR_BAD_STATE );
3807 PSA_ASSERT( psa_cipher_abort( &operation ) );
3808
Ronald Cron5425a212020-08-04 14:58:35 +02003809 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003810
Jaeden Ameroab439972019-02-15 14:12:05 +00003811exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003812 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003813 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003814}
3815/* END_CASE */
3816
3817/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003818void cipher_encrypt_fail( int alg_arg,
3819 int key_type_arg,
3820 data_t *key_data,
3821 data_t *input,
3822 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003823{
Ronald Cron5425a212020-08-04 14:58:35 +02003824 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003825 psa_status_t status;
3826 psa_key_type_t key_type = key_type_arg;
3827 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003828 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003829 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3830 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3831 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003832 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003833 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003834 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003835 size_t function_output_length;
3836 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3838
3839 if ( PSA_ERROR_BAD_STATE != expected_status )
3840 {
3841 PSA_ASSERT( psa_crypto_init( ) );
3842
3843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3844 psa_set_key_algorithm( &attributes, alg );
3845 psa_set_key_type( &attributes, key_type );
3846
3847 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3848 input->len );
3849 ASSERT_ALLOC( output, output_buffer_size );
3850
3851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3852 &key ) );
3853 }
3854
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003855 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003856 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3857 output_buffer_size, &output_length );
3858
3859 TEST_EQUAL( status, expected_status );
3860
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003861 /* Encrypt, multi-part */
3862 status = psa_cipher_encrypt_setup( &operation, key, alg );
3863 if( status == PSA_SUCCESS )
3864 {
3865 if( alg != PSA_ALG_ECB_NO_PADDING )
3866 {
3867 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3868 iv, iv_size,
3869 &iv_length ) );
3870 }
3871
3872 status = psa_cipher_update( &operation, input->x, input->len,
3873 output, output_buffer_size,
3874 &function_output_length );
3875 if( status == PSA_SUCCESS )
3876 {
3877 output_length += function_output_length;
3878
3879 status = psa_cipher_finish( &operation, output + output_length,
3880 output_buffer_size - output_length,
3881 &function_output_length );
3882
3883 TEST_EQUAL( status, expected_status );
3884 }
3885 else
3886 {
3887 TEST_EQUAL( status, expected_status );
3888 }
3889 }
3890 else
3891 {
3892 TEST_EQUAL( status, expected_status );
3893 }
3894
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003895exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003896 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003897 mbedtls_free( output );
3898 psa_destroy_key( key );
3899 PSA_DONE( );
3900}
3901/* END_CASE */
3902
3903/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003904void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3905 data_t *input, int iv_length,
3906 int expected_result )
3907{
3908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3909 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3911 size_t output_buffer_size = 0;
3912 unsigned char *output = NULL;
3913
3914 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3915 ASSERT_ALLOC( output, output_buffer_size );
3916
3917 PSA_ASSERT( psa_crypto_init( ) );
3918
3919 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3920 psa_set_key_algorithm( &attributes, alg );
3921 psa_set_key_type( &attributes, key_type );
3922
3923 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3924 &key ) );
3925 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3926 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3927 iv_length ) );
3928
3929exit:
3930 psa_cipher_abort( &operation );
3931 mbedtls_free( output );
3932 psa_destroy_key( key );
3933 PSA_DONE( );
3934}
3935/* END_CASE */
3936
3937/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003938void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3939 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003940{
3941 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3942 psa_key_type_t key_type = key_type_arg;
3943 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003944 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3945 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003946 unsigned char *output = NULL;
3947 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003948 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3950
3951 PSA_ASSERT( psa_crypto_init( ) );
3952
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003953 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003954 TEST_LE_U( ciphertext->len,
3955 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3956 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003957 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003958 TEST_LE_U( plaintext->len,
3959 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3960 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3961 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003962
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003963
3964 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003965 psa_set_key_usage_flags( &attributes,
3966 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003967 psa_set_key_algorithm( &attributes, alg );
3968 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003969 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3970 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003971 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3972 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003973 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003974
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003975 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003976 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3977 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3978 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003979 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3980 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003981 PSA_ERROR_BAD_STATE );
3982
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003983 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003984 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3985 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003986 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003987 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003988 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3989 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003990 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003991 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003992
Gilles Peskine286c3142022-04-20 17:09:38 +02003993 /* Multipart encryption */
3994 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3995 output_length = 0;
3996 length = ~0;
3997 PSA_ASSERT( psa_cipher_update( &operation,
3998 plaintext->x, plaintext->len,
3999 output, output_buffer_size,
4000 &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( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004009 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004010
Gilles Peskine286c3142022-04-20 17:09:38 +02004011 /* Multipart encryption */
4012 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
4013 output_length = 0;
4014 length = ~0;
4015 PSA_ASSERT( psa_cipher_update( &operation,
4016 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004017 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02004018 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004019 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02004020 output_length += length;
4021 PSA_ASSERT( psa_cipher_finish( &operation,
4022 output + output_length,
4023 output_buffer_size - output_length,
4024 &length ) );
4025 output_length += length;
4026 ASSERT_COMPARE( plaintext->x, plaintext->len,
4027 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004028
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004029 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004030 output_length = ~0;
4031 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
4032 output, output_buffer_size,
4033 &output_length ) );
4034 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
4035 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004036
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02004037 /* One-shot decryption */
4038 output_length = ~0;
4039 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4040 output, output_buffer_size,
4041 &output_length ) );
4042 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004043 output, output_length );
4044
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004045exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004046 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004047 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004048 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004049 psa_destroy_key( key );
4050 PSA_DONE( );
4051}
4052/* END_CASE */
4053
4054/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004055void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4056{
4057 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4058 psa_algorithm_t alg = alg_arg;
4059 psa_key_type_t key_type = key_type_arg;
4060 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4061 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4062 psa_status_t status;
4063
4064 PSA_ASSERT( psa_crypto_init( ) );
4065
4066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4067 psa_set_key_algorithm( &attributes, alg );
4068 psa_set_key_type( &attributes, key_type );
4069
4070 /* Usage of either of these two size macros would cause divide by zero
4071 * with incorrect key types previously. Input length should be irrelevant
4072 * here. */
4073 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4074 0 );
4075 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4076
4077
4078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4079 &key ) );
4080
4081 /* Should fail due to invalid alg type (to support invalid key type).
4082 * Encrypt or decrypt will end up in the same place. */
4083 status = psa_cipher_encrypt_setup( &operation, key, alg );
4084
4085 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4086
4087exit:
4088 psa_cipher_abort( &operation );
4089 psa_destroy_key( key );
4090 PSA_DONE( );
4091}
4092/* END_CASE */
4093
4094/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004095void cipher_encrypt_validation( int alg_arg,
4096 int key_type_arg,
4097 data_t *key_data,
4098 data_t *input )
4099{
4100 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4101 psa_key_type_t key_type = key_type_arg;
4102 psa_algorithm_t alg = alg_arg;
4103 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4104 unsigned char *output1 = NULL;
4105 size_t output1_buffer_size = 0;
4106 size_t output1_length = 0;
4107 unsigned char *output2 = NULL;
4108 size_t output2_buffer_size = 0;
4109 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004110 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004111 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004113
Gilles Peskine8817f612018-12-18 00:18:46 +01004114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004115
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004116 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4117 psa_set_key_algorithm( &attributes, alg );
4118 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004119
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004120 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4121 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4122 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4123 ASSERT_ALLOC( output1, output1_buffer_size );
4124 ASSERT_ALLOC( output2, output2_buffer_size );
4125
Ronald Cron5425a212020-08-04 14:58:35 +02004126 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4127 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004128
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004129 /* The one-shot cipher encryption uses generated iv so validating
4130 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004131 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4132 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004133 TEST_LE_U( output1_length,
4134 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4135 TEST_LE_U( output1_length,
4136 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004137
4138 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4139 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004140
Gilles Peskine8817f612018-12-18 00:18:46 +01004141 PSA_ASSERT( psa_cipher_update( &operation,
4142 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004143 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004144 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004145 TEST_LE_U( function_output_length,
4146 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4147 TEST_LE_U( function_output_length,
4148 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004149 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004150
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004151 PSA_ASSERT( psa_cipher_finish( &operation,
4152 output2 + output2_length,
4153 output2_buffer_size - output2_length,
4154 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004155 TEST_LE_U( function_output_length,
4156 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4157 TEST_LE_U( function_output_length,
4158 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004159 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004160
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004161 PSA_ASSERT( psa_cipher_abort( &operation ) );
4162 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4163 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004164
Gilles Peskine50e586b2018-06-08 14:28:46 +02004165exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004166 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004167 mbedtls_free( output1 );
4168 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004169 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004170 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004171}
4172/* END_CASE */
4173
4174/* BEGIN_CASE */
4175void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004176 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004177 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004178 int first_part_size_arg,
4179 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004180 data_t *expected_output,
4181 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004182{
Ronald Cron5425a212020-08-04 14:58:35 +02004183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004184 psa_key_type_t key_type = key_type_arg;
4185 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004186 psa_status_t status;
4187 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004188 size_t first_part_size = first_part_size_arg;
4189 size_t output1_length = output1_length_arg;
4190 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004191 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004192 size_t output_buffer_size = 0;
4193 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004194 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004195 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004196 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004197
Gilles Peskine8817f612018-12-18 00:18:46 +01004198 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004199
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004200 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4201 psa_set_key_algorithm( &attributes, alg );
4202 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004203
Ronald Cron5425a212020-08-04 14:58:35 +02004204 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4205 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004206
Ronald Cron5425a212020-08-04 14:58:35 +02004207 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004208
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004209 if( iv->len > 0 )
4210 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004211 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004212 }
4213
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004214 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4215 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004216 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004217
Gilles Peskine7be11a72022-04-14 00:12:57 +02004218 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004219 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4220 output, output_buffer_size,
4221 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004222 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004223 TEST_LE_U( function_output_length,
4224 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4225 TEST_LE_U( function_output_length,
4226 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004227 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004228
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004229 if( first_part_size < input->len )
4230 {
4231 PSA_ASSERT( psa_cipher_update( &operation,
4232 input->x + first_part_size,
4233 input->len - first_part_size,
4234 ( output_buffer_size == 0 ? NULL :
4235 output + total_output_length ),
4236 output_buffer_size - total_output_length,
4237 &function_output_length ) );
4238 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004239 TEST_LE_U( function_output_length,
4240 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4241 alg,
4242 input->len - first_part_size ) );
4243 TEST_LE_U( function_output_length,
4244 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004245 total_output_length += function_output_length;
4246 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004247
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004248 status = psa_cipher_finish( &operation,
4249 ( output_buffer_size == 0 ? NULL :
4250 output + total_output_length ),
4251 output_buffer_size - total_output_length,
4252 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004253 TEST_LE_U( function_output_length,
4254 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4255 TEST_LE_U( function_output_length,
4256 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004257 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004258 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004260 if( expected_status == PSA_SUCCESS )
4261 {
4262 PSA_ASSERT( psa_cipher_abort( &operation ) );
4263
4264 ASSERT_COMPARE( expected_output->x, expected_output->len,
4265 output, total_output_length );
4266 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004267
4268exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004269 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004270 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004271 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004272 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004273}
4274/* END_CASE */
4275
4276/* BEGIN_CASE */
4277void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004278 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004279 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004280 int first_part_size_arg,
4281 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004282 data_t *expected_output,
4283 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004284{
Ronald Cron5425a212020-08-04 14:58:35 +02004285 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004286 psa_key_type_t key_type = key_type_arg;
4287 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004288 psa_status_t status;
4289 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004290 size_t first_part_size = first_part_size_arg;
4291 size_t output1_length = output1_length_arg;
4292 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004293 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004294 size_t output_buffer_size = 0;
4295 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004296 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004297 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004298 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004299
Gilles Peskine8817f612018-12-18 00:18:46 +01004300 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004301
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004302 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4303 psa_set_key_algorithm( &attributes, alg );
4304 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004305
Ronald Cron5425a212020-08-04 14:58:35 +02004306 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4307 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004308
Ronald Cron5425a212020-08-04 14:58:35 +02004309 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004310
Steven Cooreman177deba2020-09-07 17:14:14 +02004311 if( iv->len > 0 )
4312 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004313 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004314 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004315
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004316 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4317 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004318 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004319
Gilles Peskine7be11a72022-04-14 00:12:57 +02004320 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004321 PSA_ASSERT( psa_cipher_update( &operation,
4322 input->x, first_part_size,
4323 output, output_buffer_size,
4324 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004325 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004326 TEST_LE_U( function_output_length,
4327 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4328 TEST_LE_U( function_output_length,
4329 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004330 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004331
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004332 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004333 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004334 PSA_ASSERT( psa_cipher_update( &operation,
4335 input->x + first_part_size,
4336 input->len - first_part_size,
4337 ( output_buffer_size == 0 ? NULL :
4338 output + total_output_length ),
4339 output_buffer_size - total_output_length,
4340 &function_output_length ) );
4341 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004342 TEST_LE_U( function_output_length,
4343 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4344 alg,
4345 input->len - first_part_size ) );
4346 TEST_LE_U( function_output_length,
4347 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004348 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004349 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004350
Gilles Peskine50e586b2018-06-08 14:28:46 +02004351 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004352 ( output_buffer_size == 0 ? NULL :
4353 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004354 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004355 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004356 TEST_LE_U( function_output_length,
4357 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4358 TEST_LE_U( function_output_length,
4359 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004360 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004361 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004362
4363 if( expected_status == PSA_SUCCESS )
4364 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004365 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004366
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004367 ASSERT_COMPARE( expected_output->x, expected_output->len,
4368 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004369 }
4370
Gilles Peskine50e586b2018-06-08 14:28:46 +02004371exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004372 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004373 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004374 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004375 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004376}
4377/* END_CASE */
4378
Gilles Peskine50e586b2018-06-08 14:28:46 +02004379/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004380void cipher_decrypt_fail( int alg_arg,
4381 int key_type_arg,
4382 data_t *key_data,
4383 data_t *iv,
4384 data_t *input_arg,
4385 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004386{
4387 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4388 psa_status_t status;
4389 psa_key_type_t key_type = key_type_arg;
4390 psa_algorithm_t alg = alg_arg;
4391 psa_status_t expected_status = expected_status_arg;
4392 unsigned char *input = NULL;
4393 size_t input_buffer_size = 0;
4394 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004395 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004396 size_t output_buffer_size = 0;
4397 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004398 size_t function_output_length;
4399 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4401
4402 if ( PSA_ERROR_BAD_STATE != expected_status )
4403 {
4404 PSA_ASSERT( psa_crypto_init( ) );
4405
4406 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4407 psa_set_key_algorithm( &attributes, alg );
4408 psa_set_key_type( &attributes, key_type );
4409
4410 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4411 &key ) );
4412 }
4413
4414 /* Allocate input buffer and copy the iv and the plaintext */
4415 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4416 if ( input_buffer_size > 0 )
4417 {
4418 ASSERT_ALLOC( input, input_buffer_size );
4419 memcpy( input, iv->x, iv->len );
4420 memcpy( input + iv->len, input_arg->x, input_arg->len );
4421 }
4422
4423 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4424 ASSERT_ALLOC( output, output_buffer_size );
4425
Neil Armstrong66a479f2022-02-07 15:41:19 +01004426 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004427 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4428 output_buffer_size, &output_length );
4429 TEST_EQUAL( status, expected_status );
4430
Neil Armstrong66a479f2022-02-07 15:41:19 +01004431 /* Decrypt, multi-part */
4432 status = psa_cipher_decrypt_setup( &operation, key, alg );
4433 if( status == PSA_SUCCESS )
4434 {
4435 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4436 input_arg->len ) +
4437 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4438 ASSERT_ALLOC( output_multi, output_buffer_size );
4439
4440 if( iv->len > 0 )
4441 {
4442 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4443
4444 if( status != PSA_SUCCESS )
4445 TEST_EQUAL( status, expected_status );
4446 }
4447
4448 if( status == PSA_SUCCESS )
4449 {
4450 status = psa_cipher_update( &operation,
4451 input_arg->x, input_arg->len,
4452 output_multi, output_buffer_size,
4453 &function_output_length );
4454 if( status == PSA_SUCCESS )
4455 {
4456 output_length = function_output_length;
4457
4458 status = psa_cipher_finish( &operation,
4459 output_multi + output_length,
4460 output_buffer_size - output_length,
4461 &function_output_length );
4462
4463 TEST_EQUAL( status, expected_status );
4464 }
4465 else
4466 {
4467 TEST_EQUAL( status, expected_status );
4468 }
4469 }
4470 else
4471 {
4472 TEST_EQUAL( status, expected_status );
4473 }
4474 }
4475 else
4476 {
4477 TEST_EQUAL( status, expected_status );
4478 }
4479
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004480exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004481 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004482 mbedtls_free( input );
4483 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004484 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004485 psa_destroy_key( key );
4486 PSA_DONE( );
4487}
4488/* END_CASE */
4489
4490/* BEGIN_CASE */
4491void cipher_decrypt( int alg_arg,
4492 int key_type_arg,
4493 data_t *key_data,
4494 data_t *iv,
4495 data_t *input_arg,
4496 data_t *expected_output )
4497{
4498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4499 psa_key_type_t key_type = key_type_arg;
4500 psa_algorithm_t alg = alg_arg;
4501 unsigned char *input = NULL;
4502 size_t input_buffer_size = 0;
4503 unsigned char *output = NULL;
4504 size_t output_buffer_size = 0;
4505 size_t output_length = 0;
4506 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4507
4508 PSA_ASSERT( psa_crypto_init( ) );
4509
4510 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4511 psa_set_key_algorithm( &attributes, alg );
4512 psa_set_key_type( &attributes, key_type );
4513
4514 /* Allocate input buffer and copy the iv and the plaintext */
4515 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4516 if ( input_buffer_size > 0 )
4517 {
4518 ASSERT_ALLOC( input, input_buffer_size );
4519 memcpy( input, iv->x, iv->len );
4520 memcpy( input + iv->len, input_arg->x, input_arg->len );
4521 }
4522
4523 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4524 ASSERT_ALLOC( output, output_buffer_size );
4525
4526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4527 &key ) );
4528
4529 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4530 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004531 TEST_LE_U( output_length,
4532 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4533 TEST_LE_U( output_length,
4534 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004535
4536 ASSERT_COMPARE( expected_output->x, expected_output->len,
4537 output, output_length );
4538exit:
4539 mbedtls_free( input );
4540 mbedtls_free( output );
4541 psa_destroy_key( key );
4542 PSA_DONE( );
4543}
4544/* END_CASE */
4545
4546/* BEGIN_CASE */
4547void cipher_verify_output( int alg_arg,
4548 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004549 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004550 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004551{
Ronald Cron5425a212020-08-04 14:58:35 +02004552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004553 psa_key_type_t key_type = key_type_arg;
4554 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004555 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004556 size_t output1_size = 0;
4557 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004558 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004559 size_t output2_size = 0;
4560 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004562
Gilles Peskine8817f612018-12-18 00:18:46 +01004563 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004564
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004565 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4566 psa_set_key_algorithm( &attributes, alg );
4567 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004568
Ronald Cron5425a212020-08-04 14:58:35 +02004569 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4570 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004571 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004572 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004573
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004574 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4575 output1, output1_size,
4576 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004577 TEST_LE_U( output1_length,
4578 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4579 TEST_LE_U( output1_length,
4580 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004581
4582 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004583 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004584
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004585 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4586 output2, output2_size,
4587 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004588 TEST_LE_U( output2_length,
4589 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4590 TEST_LE_U( output2_length,
4591 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004592
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004593 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004594
4595exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004596 mbedtls_free( output1 );
4597 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004598 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004599 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004600}
4601/* END_CASE */
4602
4603/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004604void cipher_verify_output_multipart( int alg_arg,
4605 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004606 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004607 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004608 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004609{
Ronald Cron5425a212020-08-04 14:58:35 +02004610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004611 psa_key_type_t key_type = key_type_arg;
4612 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004613 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004614 unsigned char iv[16] = {0};
4615 size_t iv_size = 16;
4616 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004617 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004618 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004619 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004620 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004621 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004622 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004623 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004624 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4625 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004627
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004629
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4631 psa_set_key_algorithm( &attributes, alg );
4632 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004633
Ronald Cron5425a212020-08-04 14:58:35 +02004634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4635 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004636
Ronald Cron5425a212020-08-04 14:58:35 +02004637 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4638 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004639
Steven Cooreman177deba2020-09-07 17:14:14 +02004640 if( alg != PSA_ALG_ECB_NO_PADDING )
4641 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004642 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4643 iv, iv_size,
4644 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004645 }
4646
gabor-mezei-armceface22021-01-21 12:26:17 +01004647 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004648 TEST_LE_U( output1_buffer_size,
4649 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004650 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004651
Gilles Peskine7be11a72022-04-14 00:12:57 +02004652 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004653
Gilles Peskine8817f612018-12-18 00:18:46 +01004654 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4655 output1, output1_buffer_size,
4656 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004657 TEST_LE_U( function_output_length,
4658 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4659 TEST_LE_U( function_output_length,
4660 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004661 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004662
Gilles Peskine8817f612018-12-18 00:18:46 +01004663 PSA_ASSERT( psa_cipher_update( &operation1,
4664 input->x + first_part_size,
4665 input->len - first_part_size,
4666 output1, output1_buffer_size,
4667 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004668 TEST_LE_U( function_output_length,
4669 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4670 alg,
4671 input->len - first_part_size ) );
4672 TEST_LE_U( function_output_length,
4673 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004674 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004675
Gilles Peskine8817f612018-12-18 00:18:46 +01004676 PSA_ASSERT( psa_cipher_finish( &operation1,
4677 output1 + output1_length,
4678 output1_buffer_size - output1_length,
4679 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004680 TEST_LE_U( function_output_length,
4681 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4682 TEST_LE_U( function_output_length,
4683 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004684 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004685
Gilles Peskine8817f612018-12-18 00:18:46 +01004686 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004687
Gilles Peskine048b7f02018-06-08 14:20:49 +02004688 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004689 TEST_LE_U( output2_buffer_size,
4690 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4691 TEST_LE_U( output2_buffer_size,
4692 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004693 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004694
Steven Cooreman177deba2020-09-07 17:14:14 +02004695 if( iv_length > 0 )
4696 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004697 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4698 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004699 }
Moran Pekerded84402018-06-06 16:36:50 +03004700
Gilles Peskine8817f612018-12-18 00:18:46 +01004701 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4702 output2, output2_buffer_size,
4703 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004704 TEST_LE_U( function_output_length,
4705 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4706 TEST_LE_U( function_output_length,
4707 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004708 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004709
Gilles Peskine8817f612018-12-18 00:18:46 +01004710 PSA_ASSERT( psa_cipher_update( &operation2,
4711 output1 + first_part_size,
4712 output1_length - first_part_size,
4713 output2, output2_buffer_size,
4714 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004715 TEST_LE_U( function_output_length,
4716 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4717 alg,
4718 output1_length - first_part_size ) );
4719 TEST_LE_U( function_output_length,
4720 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004721 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004722
Gilles Peskine8817f612018-12-18 00:18:46 +01004723 PSA_ASSERT( psa_cipher_finish( &operation2,
4724 output2 + output2_length,
4725 output2_buffer_size - output2_length,
4726 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004727 TEST_LE_U( function_output_length,
4728 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4729 TEST_LE_U( function_output_length,
4730 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004731 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004732
Gilles Peskine8817f612018-12-18 00:18:46 +01004733 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004734
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004735 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004736
4737exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004738 psa_cipher_abort( &operation1 );
4739 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004740 mbedtls_free( output1 );
4741 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004742 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004743 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004744}
4745/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004746
Gilles Peskine20035e32018-02-03 22:44:14 +01004747/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004748void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004749 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004750 data_t *nonce,
4751 data_t *additional_data,
4752 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004753 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004754{
Ronald Cron5425a212020-08-04 14:58:35 +02004755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004756 psa_key_type_t key_type = key_type_arg;
4757 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004758 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004759 unsigned char *output_data = NULL;
4760 size_t output_size = 0;
4761 size_t output_length = 0;
4762 unsigned char *output_data2 = NULL;
4763 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004764 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004765 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004767
Gilles Peskine8817f612018-12-18 00:18:46 +01004768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004769
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004770 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4771 psa_set_key_algorithm( &attributes, alg );
4772 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004773
Gilles Peskine049c7532019-05-15 20:22:09 +02004774 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004775 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004776 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4777 key_bits = psa_get_key_bits( &attributes );
4778
4779 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4780 alg );
4781 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4782 * should be exact. */
4783 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4784 expected_result != PSA_ERROR_NOT_SUPPORTED )
4785 {
4786 TEST_EQUAL( output_size,
4787 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004788 TEST_LE_U( output_size,
4789 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004790 }
4791 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004792
Steven Cooremanf49478b2021-02-15 15:19:25 +01004793 status = psa_aead_encrypt( key, alg,
4794 nonce->x, nonce->len,
4795 additional_data->x,
4796 additional_data->len,
4797 input_data->x, input_data->len,
4798 output_data, output_size,
4799 &output_length );
4800
4801 /* If the operation is not supported, just skip and not fail in case the
4802 * encryption involves a common limitation of cryptography hardwares and
4803 * an alternative implementation. */
4804 if( status == PSA_ERROR_NOT_SUPPORTED )
4805 {
4806 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4807 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4808 }
4809
4810 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004811
4812 if( PSA_SUCCESS == expected_result )
4813 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004814 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004815
Gilles Peskine003a4a92019-05-14 16:09:40 +02004816 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4817 * should be exact. */
4818 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004819 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004820
Gilles Peskine7be11a72022-04-14 00:12:57 +02004821 TEST_LE_U( input_data->len,
4822 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004823
Ronald Cron5425a212020-08-04 14:58:35 +02004824 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004825 nonce->x, nonce->len,
4826 additional_data->x,
4827 additional_data->len,
4828 output_data, output_length,
4829 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004830 &output_length2 ),
4831 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004832
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004833 ASSERT_COMPARE( input_data->x, input_data->len,
4834 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004835 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004836
Gilles Peskinea1cac842018-06-11 19:33:02 +02004837exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004838 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004839 mbedtls_free( output_data );
4840 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004841 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004842}
4843/* END_CASE */
4844
4845/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004846void aead_encrypt( int key_type_arg, data_t *key_data,
4847 int alg_arg,
4848 data_t *nonce,
4849 data_t *additional_data,
4850 data_t *input_data,
4851 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852{
Ronald Cron5425a212020-08-04 14:58:35 +02004853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004854 psa_key_type_t key_type = key_type_arg;
4855 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004856 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004857 unsigned char *output_data = NULL;
4858 size_t output_size = 0;
4859 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004861 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004862
Gilles Peskine8817f612018-12-18 00:18:46 +01004863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004864
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004865 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4866 psa_set_key_algorithm( &attributes, alg );
4867 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004868
Gilles Peskine049c7532019-05-15 20:22:09 +02004869 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004870 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004871 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4872 key_bits = psa_get_key_bits( &attributes );
4873
4874 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4875 alg );
4876 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4877 * should be exact. */
4878 TEST_EQUAL( output_size,
4879 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004880 TEST_LE_U( output_size,
4881 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004882 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004883
Steven Cooremand588ea12021-01-11 19:36:04 +01004884 status = psa_aead_encrypt( key, alg,
4885 nonce->x, nonce->len,
4886 additional_data->x, additional_data->len,
4887 input_data->x, input_data->len,
4888 output_data, output_size,
4889 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004890
Ronald Cron28a45ed2021-02-09 20:35:42 +01004891 /* If the operation is not supported, just skip and not fail in case the
4892 * encryption involves a common limitation of cryptography hardwares and
4893 * an alternative implementation. */
4894 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004895 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004896 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4897 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004898 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004899
4900 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004901 ASSERT_COMPARE( expected_result->x, expected_result->len,
4902 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004903
Gilles Peskinea1cac842018-06-11 19:33:02 +02004904exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004905 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004906 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004907 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004908}
4909/* END_CASE */
4910
4911/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004912void aead_decrypt( int key_type_arg, data_t *key_data,
4913 int alg_arg,
4914 data_t *nonce,
4915 data_t *additional_data,
4916 data_t *input_data,
4917 data_t *expected_data,
4918 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004919{
Ronald Cron5425a212020-08-04 14:58:35 +02004920 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004921 psa_key_type_t key_type = key_type_arg;
4922 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004923 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004924 unsigned char *output_data = NULL;
4925 size_t output_size = 0;
4926 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004928 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004929 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004930
Gilles Peskine8817f612018-12-18 00:18:46 +01004931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004932
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004933 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4934 psa_set_key_algorithm( &attributes, alg );
4935 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004936
Gilles Peskine049c7532019-05-15 20:22:09 +02004937 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004938 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004939 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4940 key_bits = psa_get_key_bits( &attributes );
4941
4942 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4943 alg );
4944 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4945 expected_result != PSA_ERROR_NOT_SUPPORTED )
4946 {
4947 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4948 * should be exact. */
4949 TEST_EQUAL( output_size,
4950 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004951 TEST_LE_U( output_size,
4952 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004953 }
4954 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004955
Steven Cooremand588ea12021-01-11 19:36:04 +01004956 status = psa_aead_decrypt( key, alg,
4957 nonce->x, nonce->len,
4958 additional_data->x,
4959 additional_data->len,
4960 input_data->x, input_data->len,
4961 output_data, output_size,
4962 &output_length );
4963
Ronald Cron28a45ed2021-02-09 20:35:42 +01004964 /* If the operation is not supported, just skip and not fail in case the
4965 * decryption involves a common limitation of cryptography hardwares and
4966 * an alternative implementation. */
4967 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004968 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004969 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4970 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004971 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004972
4973 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004974
Gilles Peskine2d277862018-06-18 15:41:12 +02004975 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004976 ASSERT_COMPARE( expected_data->x, expected_data->len,
4977 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004978
Gilles Peskinea1cac842018-06-11 19:33:02 +02004979exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004980 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004981 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004982 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004983}
4984/* END_CASE */
4985
4986/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004987void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4988 int alg_arg,
4989 data_t *nonce,
4990 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004991 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004992 int do_set_lengths,
4993 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004994{
Paul Elliottd3f82412021-06-16 16:52:21 +01004995 size_t ad_part_len = 0;
4996 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004997 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004998
Paul Elliott32f46ba2021-09-23 18:24:36 +01004999 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005000 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005001 mbedtls_test_set_step( ad_part_len );
5002
5003 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005004 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005005 if( ad_part_len & 0x01 )
5006 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5007 else
5008 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005009 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005010
5011 /* Split ad into length(ad_part_len) parts. */
5012 if( !aead_multipart_internal_func( key_type_arg, key_data,
5013 alg_arg, nonce,
5014 additional_data,
5015 ad_part_len,
5016 input_data, -1,
5017 set_lengths_method,
5018 expected_output,
5019 1, 0 ) )
5020 break;
5021
5022 /* length(0) part, length(ad_part_len) part, length(0) part... */
5023 mbedtls_test_set_step( 1000 + ad_part_len );
5024
5025 if( !aead_multipart_internal_func( key_type_arg, key_data,
5026 alg_arg, nonce,
5027 additional_data,
5028 ad_part_len,
5029 input_data, -1,
5030 set_lengths_method,
5031 expected_output,
5032 1, 1 ) )
5033 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005034 }
Paul Elliottd3f82412021-06-16 16:52:21 +01005035
Paul Elliott32f46ba2021-09-23 18:24:36 +01005036 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005037 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005038 /* Split data into length(data_part_len) parts. */
5039 mbedtls_test_set_step( 2000 + data_part_len );
5040
5041 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005042 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005043 if( data_part_len & 0x01 )
5044 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5045 else
5046 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005047 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005048
Paul Elliott32f46ba2021-09-23 18:24:36 +01005049 if( !aead_multipart_internal_func( key_type_arg, key_data,
5050 alg_arg, nonce,
5051 additional_data, -1,
5052 input_data, data_part_len,
5053 set_lengths_method,
5054 expected_output,
5055 1, 0 ) )
5056 break;
5057
5058 /* length(0) part, length(data_part_len) part, length(0) part... */
5059 mbedtls_test_set_step( 3000 + data_part_len );
5060
5061 if( !aead_multipart_internal_func( key_type_arg, key_data,
5062 alg_arg, nonce,
5063 additional_data, -1,
5064 input_data, data_part_len,
5065 set_lengths_method,
5066 expected_output,
5067 1, 1 ) )
5068 break;
5069 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005070
Paul Elliott8fc45162021-06-23 16:06:01 +01005071 /* Goto is required to silence warnings about unused labels, as we
5072 * don't actually do any test assertions in this function. */
5073 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005074}
5075/* END_CASE */
5076
5077/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005078void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5079 int alg_arg,
5080 data_t *nonce,
5081 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005082 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005083 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005084 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005085{
Paul Elliottd3f82412021-06-16 16:52:21 +01005086 size_t ad_part_len = 0;
5087 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005088 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005089
Paul Elliott32f46ba2021-09-23 18:24:36 +01005090 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005091 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005092 /* Split ad into length(ad_part_len) parts. */
5093 mbedtls_test_set_step( ad_part_len );
5094
5095 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005096 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005097 if( ad_part_len & 0x01 )
5098 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5099 else
5100 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005101 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005102
5103 if( !aead_multipart_internal_func( key_type_arg, key_data,
5104 alg_arg, nonce,
5105 additional_data,
5106 ad_part_len,
5107 input_data, -1,
5108 set_lengths_method,
5109 expected_output,
5110 0, 0 ) )
5111 break;
5112
5113 /* length(0) part, length(ad_part_len) part, length(0) part... */
5114 mbedtls_test_set_step( 1000 + ad_part_len );
5115
5116 if( !aead_multipart_internal_func( key_type_arg, key_data,
5117 alg_arg, nonce,
5118 additional_data,
5119 ad_part_len,
5120 input_data, -1,
5121 set_lengths_method,
5122 expected_output,
5123 0, 1 ) )
5124 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005125 }
5126
Paul Elliott32f46ba2021-09-23 18:24:36 +01005127 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005128 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005129 /* Split data into length(data_part_len) parts. */
5130 mbedtls_test_set_step( 2000 + data_part_len );
5131
5132 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005133 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005134 if( data_part_len & 0x01 )
5135 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5136 else
5137 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005138 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005139
5140 if( !aead_multipart_internal_func( key_type_arg, key_data,
5141 alg_arg, nonce,
5142 additional_data, -1,
5143 input_data, data_part_len,
5144 set_lengths_method,
5145 expected_output,
5146 0, 0 ) )
5147 break;
5148
5149 /* length(0) part, length(data_part_len) part, length(0) part... */
5150 mbedtls_test_set_step( 3000 + data_part_len );
5151
5152 if( !aead_multipart_internal_func( key_type_arg, key_data,
5153 alg_arg, nonce,
5154 additional_data, -1,
5155 input_data, data_part_len,
5156 set_lengths_method,
5157 expected_output,
5158 0, 1 ) )
5159 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005160 }
5161
Paul Elliott8fc45162021-06-23 16:06:01 +01005162 /* Goto is required to silence warnings about unused labels, as we
5163 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005164 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005165}
5166/* END_CASE */
5167
5168/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005169void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5170 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005171 int nonce_length,
5172 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005173 data_t *additional_data,
5174 data_t *input_data,
5175 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005176{
5177
5178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5179 psa_key_type_t key_type = key_type_arg;
5180 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005181 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005182 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5184 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005185 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005186 size_t actual_nonce_length = 0;
5187 size_t expected_nonce_length = expected_nonce_length_arg;
5188 unsigned char *output = NULL;
5189 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005190 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005191 size_t ciphertext_size = 0;
5192 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005193 size_t tag_length = 0;
5194 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005195
5196 PSA_ASSERT( psa_crypto_init( ) );
5197
5198 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5199 psa_set_key_algorithm( & attributes, alg );
5200 psa_set_key_type( & attributes, key_type );
5201
5202 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5203 &key ) );
5204
5205 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5206
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005207 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5208
Paul Elliottf1277632021-08-24 18:11:37 +01005209 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005210
Paul Elliottf1277632021-08-24 18:11:37 +01005211 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005212
Gilles Peskine7be11a72022-04-14 00:12:57 +02005213 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005214
Paul Elliottf1277632021-08-24 18:11:37 +01005215 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005216
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005217 status = psa_aead_encrypt_setup( &operation, key, alg );
5218
5219 /* If the operation is not supported, just skip and not fail in case the
5220 * encryption involves a common limitation of cryptography hardwares and
5221 * an alternative implementation. */
5222 if( status == PSA_ERROR_NOT_SUPPORTED )
5223 {
5224 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005225 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005226 }
5227
5228 PSA_ASSERT( status );
5229
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005230 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005231 nonce_length,
5232 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005233
Paul Elliott693bf312021-07-23 17:40:41 +01005234 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005235
Paul Elliottf1277632021-08-24 18:11:37 +01005236 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005237
Paul Elliott88ecbe12021-09-22 17:23:03 +01005238 if( expected_status == PSA_SUCCESS )
5239 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5240 alg ) );
5241
Gilles Peskine7be11a72022-04-14 00:12:57 +02005242 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005243
Paul Elliott693bf312021-07-23 17:40:41 +01005244 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005245 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005246 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005247 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5248 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005249
5250 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5251 additional_data->len ) );
5252
5253 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005254 output, output_size,
5255 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005256
Paul Elliottf1277632021-08-24 18:11:37 +01005257 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5258 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005259 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5260 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005261
5262exit:
5263 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005264 mbedtls_free( output );
5265 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005266 psa_aead_abort( &operation );
5267 PSA_DONE( );
5268}
5269/* END_CASE */
5270
5271/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005272void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5273 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005274 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005275 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005276 data_t *additional_data,
5277 data_t *input_data,
5278 int expected_status_arg )
5279{
5280
5281 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5282 psa_key_type_t key_type = key_type_arg;
5283 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005284 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005285 uint8_t *nonce_buffer = NULL;
5286 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5287 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5288 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005289 unsigned char *output = NULL;
5290 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005291 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005292 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005293 size_t ciphertext_size = 0;
5294 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005295 size_t tag_length = 0;
5296 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005297 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005298 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005299
5300 PSA_ASSERT( psa_crypto_init( ) );
5301
5302 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5303 psa_set_key_algorithm( &attributes, alg );
5304 psa_set_key_type( &attributes, key_type );
5305
5306 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5307 &key ) );
5308
5309 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5310
5311 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5312
Paul Elliott6f0e7202021-08-25 12:57:18 +01005313 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005314
Paul Elliott6f0e7202021-08-25 12:57:18 +01005315 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005316
Gilles Peskine7be11a72022-04-14 00:12:57 +02005317 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005318
Paul Elliott6f0e7202021-08-25 12:57:18 +01005319 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005320
Paul Elliott863864a2021-07-23 17:28:31 +01005321 status = psa_aead_encrypt_setup( &operation, key, alg );
5322
5323 /* If the operation is not supported, just skip and not fail in case the
5324 * encryption involves a common limitation of cryptography hardwares and
5325 * an alternative implementation. */
5326 if( status == PSA_ERROR_NOT_SUPPORTED )
5327 {
5328 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005329 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005330 }
5331
5332 PSA_ASSERT( status );
5333
Paul Elliott4023ffd2021-09-10 16:21:22 +01005334 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5335 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005336 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005337 /* Arbitrary size buffer, to test zero length valid buffer. */
5338 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005339 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005340 }
5341 else
5342 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005343 /* If length is zero, then this will return NULL. */
5344 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005345 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005346
Paul Elliott4023ffd2021-09-10 16:21:22 +01005347 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005348 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005349 for( index = 0; index < nonce_length - 1; ++index )
5350 {
5351 nonce_buffer[index] = 'a' + index;
5352 }
Paul Elliott66696b52021-08-16 18:42:41 +01005353 }
Paul Elliott863864a2021-07-23 17:28:31 +01005354 }
5355
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005356 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5357 {
5358 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5359 input_data->len ) );
5360 }
5361
Paul Elliott6f0e7202021-08-25 12:57:18 +01005362 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005363
Paul Elliott693bf312021-07-23 17:40:41 +01005364 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005365
5366 if( expected_status == PSA_SUCCESS )
5367 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005368 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5369 {
5370 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5371 input_data->len ) );
5372 }
5373 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5374 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005375
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005376 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5377 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5378 additional_data->len ),
5379 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005380
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005381 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005382 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005383 &ciphertext_length ),
5384 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005385
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005386 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005387 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005388 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5389 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005390 }
5391
5392exit:
5393 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005394 mbedtls_free( output );
5395 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005396 mbedtls_free( nonce_buffer );
5397 psa_aead_abort( &operation );
5398 PSA_DONE( );
5399}
5400/* END_CASE */
5401
5402/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005403void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5404 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005405 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005406 data_t *nonce,
5407 data_t *additional_data,
5408 data_t *input_data,
5409 int expected_status_arg )
5410{
5411
5412 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5413 psa_key_type_t key_type = key_type_arg;
5414 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005415 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5417 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5418 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005419 unsigned char *output = NULL;
5420 unsigned char *ciphertext = NULL;
5421 size_t output_size = output_size_arg;
5422 size_t ciphertext_size = 0;
5423 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005424 size_t tag_length = 0;
5425 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5426
5427 PSA_ASSERT( psa_crypto_init( ) );
5428
5429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5430 psa_set_key_algorithm( &attributes, alg );
5431 psa_set_key_type( &attributes, key_type );
5432
5433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5434 &key ) );
5435
5436 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5437
Paul Elliottc6d11d02021-09-01 12:04:23 +01005438 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005439
Paul Elliottc6d11d02021-09-01 12:04:23 +01005440 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005441
Paul Elliottc6d11d02021-09-01 12:04:23 +01005442 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005443
Paul Elliott43fbda62021-07-23 18:30:59 +01005444 status = psa_aead_encrypt_setup( &operation, key, alg );
5445
5446 /* If the operation is not supported, just skip and not fail in case the
5447 * encryption involves a common limitation of cryptography hardwares and
5448 * an alternative implementation. */
5449 if( status == PSA_ERROR_NOT_SUPPORTED )
5450 {
5451 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5452 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5453 }
5454
5455 PSA_ASSERT( status );
5456
Paul Elliott47b9a142021-10-07 15:04:57 +01005457 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5458 input_data->len ) );
5459
Paul Elliott43fbda62021-07-23 18:30:59 +01005460 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5461
5462 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5463 additional_data->len ) );
5464
5465 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005466 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005467
5468 TEST_EQUAL( status, expected_status );
5469
5470 if( expected_status == PSA_SUCCESS )
5471 {
5472 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005473 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5474 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005475 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5476 }
5477
5478exit:
5479 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005480 mbedtls_free( output );
5481 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005482 psa_aead_abort( &operation );
5483 PSA_DONE( );
5484}
5485/* END_CASE */
5486
Paul Elliott91b021e2021-07-23 18:52:31 +01005487/* BEGIN_CASE */
5488void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5489 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005490 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005491 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005492 data_t *nonce,
5493 data_t *additional_data,
5494 data_t *input_data,
5495 int expected_status_arg )
5496{
5497
5498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5499 psa_key_type_t key_type = key_type_arg;
5500 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005501 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5503 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5504 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005505 unsigned char *ciphertext = NULL;
5506 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005507 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005508 size_t ciphertext_size = 0;
5509 size_t ciphertext_length = 0;
5510 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005511 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005512 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005513
5514 PSA_ASSERT( psa_crypto_init( ) );
5515
5516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5517 psa_set_key_algorithm( &attributes, alg );
5518 psa_set_key_type( &attributes, key_type );
5519
5520 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5521 &key ) );
5522
5523 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5524
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005525 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005526
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005527 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005528
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005529 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005530
Paul Elliott719c1322021-09-13 18:27:22 +01005531 ASSERT_ALLOC( tag_buffer, tag_size );
5532
Paul Elliott91b021e2021-07-23 18:52:31 +01005533 status = psa_aead_encrypt_setup( &operation, key, alg );
5534
5535 /* If the operation is not supported, just skip and not fail in case the
5536 * encryption involves a common limitation of cryptography hardwares and
5537 * an alternative implementation. */
5538 if( status == PSA_ERROR_NOT_SUPPORTED )
5539 {
5540 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5541 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5542 }
5543
5544 PSA_ASSERT( status );
5545
5546 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5547
Paul Elliott76bda482021-10-07 17:07:23 +01005548 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5549 input_data->len ) );
5550
Paul Elliott91b021e2021-07-23 18:52:31 +01005551 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5552 additional_data->len ) );
5553
5554 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005555 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005556
5557 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005558 status = psa_aead_finish( &operation, finish_ciphertext,
5559 finish_ciphertext_size,
5560 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005561 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005562
5563 TEST_EQUAL( status, expected_status );
5564
5565exit:
5566 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005567 mbedtls_free( ciphertext );
5568 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005569 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005570 psa_aead_abort( &operation );
5571 PSA_DONE( );
5572}
5573/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005574
5575/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005576void aead_multipart_verify( int key_type_arg, data_t *key_data,
5577 int alg_arg,
5578 data_t *nonce,
5579 data_t *additional_data,
5580 data_t *input_data,
5581 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005582 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005583 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005584 int expected_status_arg )
5585{
5586 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5587 psa_key_type_t key_type = key_type_arg;
5588 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005589 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005590 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5591 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5592 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005593 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005594 unsigned char *plaintext = NULL;
5595 unsigned char *finish_plaintext = NULL;
5596 size_t plaintext_size = 0;
5597 size_t plaintext_length = 0;
5598 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005599 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005600 unsigned char *tag_buffer = NULL;
5601 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005602
5603 PSA_ASSERT( psa_crypto_init( ) );
5604
5605 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5606 psa_set_key_algorithm( &attributes, alg );
5607 psa_set_key_type( &attributes, key_type );
5608
5609 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5610 &key ) );
5611
5612 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5613
5614 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5615 input_data->len );
5616
5617 ASSERT_ALLOC( plaintext, plaintext_size );
5618
5619 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5620
5621 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5622
Paul Elliott9961a662021-09-17 19:19:02 +01005623 status = psa_aead_decrypt_setup( &operation, key, alg );
5624
5625 /* If the operation is not supported, just skip and not fail in case the
5626 * encryption involves a common limitation of cryptography hardwares and
5627 * an alternative implementation. */
5628 if( status == PSA_ERROR_NOT_SUPPORTED )
5629 {
5630 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5631 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5632 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005633 TEST_EQUAL( status, expected_setup_status );
5634
5635 if( status != PSA_SUCCESS )
5636 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005637
5638 PSA_ASSERT( status );
5639
5640 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5641
Paul Elliottfec6f372021-10-06 17:15:02 +01005642 status = psa_aead_set_lengths( &operation, additional_data->len,
5643 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005644 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005645
Paul Elliott9961a662021-09-17 19:19:02 +01005646 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5647 additional_data->len ) );
5648
5649 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5650 input_data->len,
5651 plaintext, plaintext_size,
5652 &plaintext_length ) );
5653
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005654 if( tag_usage == USE_GIVEN_TAG )
5655 {
5656 tag_buffer = tag->x;
5657 tag_size = tag->len;
5658 }
5659
Paul Elliott9961a662021-09-17 19:19:02 +01005660 status = psa_aead_verify( &operation, finish_plaintext,
5661 verify_plaintext_size,
5662 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005663 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005664
5665 TEST_EQUAL( status, expected_status );
5666
5667exit:
5668 psa_destroy_key( key );
5669 mbedtls_free( plaintext );
5670 mbedtls_free( finish_plaintext );
5671 psa_aead_abort( &operation );
5672 PSA_DONE( );
5673}
5674/* END_CASE */
5675
Paul Elliott9961a662021-09-17 19:19:02 +01005676/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005677void aead_multipart_setup( int key_type_arg, data_t *key_data,
5678 int alg_arg, int expected_status_arg )
5679{
5680 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5681 psa_key_type_t key_type = key_type_arg;
5682 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005683 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5685 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5686 psa_status_t expected_status = expected_status_arg;
5687
5688 PSA_ASSERT( psa_crypto_init( ) );
5689
5690 psa_set_key_usage_flags( &attributes,
5691 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5692 psa_set_key_algorithm( &attributes, alg );
5693 psa_set_key_type( &attributes, key_type );
5694
5695 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5696 &key ) );
5697
Paul Elliott5221ef62021-09-19 17:33:03 +01005698 status = psa_aead_encrypt_setup( &operation, key, alg );
5699
5700 TEST_EQUAL( status, expected_status );
5701
5702 psa_aead_abort( &operation );
5703
Paul Elliott5221ef62021-09-19 17:33:03 +01005704 status = psa_aead_decrypt_setup( &operation, key, alg );
5705
5706 TEST_EQUAL(status, expected_status );
5707
5708exit:
5709 psa_destroy_key( key );
5710 psa_aead_abort( &operation );
5711 PSA_DONE( );
5712}
5713/* END_CASE */
5714
5715/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5717 int alg_arg,
5718 data_t *nonce,
5719 data_t *additional_data,
5720 data_t *input_data )
5721{
5722 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5723 psa_key_type_t key_type = key_type_arg;
5724 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005725 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005726 unsigned char *output_data = NULL;
5727 unsigned char *final_data = NULL;
5728 size_t output_size = 0;
5729 size_t finish_output_size = 0;
5730 size_t output_length = 0;
5731 size_t key_bits = 0;
5732 size_t tag_length = 0;
5733 size_t tag_size = 0;
5734 size_t nonce_length = 0;
5735 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5736 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5737 size_t output_part_length = 0;
5738 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5739
5740 PSA_ASSERT( psa_crypto_init( ) );
5741
5742 psa_set_key_usage_flags( & attributes,
5743 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5744 psa_set_key_algorithm( & attributes, alg );
5745 psa_set_key_type( & attributes, key_type );
5746
5747 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5748 &key ) );
5749
5750 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5751 key_bits = psa_get_key_bits( &attributes );
5752
5753 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5754
Gilles Peskine7be11a72022-04-14 00:12:57 +02005755 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756
5757 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5758
5759 ASSERT_ALLOC( output_data, output_size );
5760
5761 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5762
Gilles Peskine7be11a72022-04-14 00:12:57 +02005763 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005764
5765 ASSERT_ALLOC( final_data, finish_output_size );
5766
5767 /* Test all operations error without calling setup first. */
5768
Paul Elliottc23a9a02021-06-21 18:32:46 +01005769 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5770 PSA_ERROR_BAD_STATE );
5771
5772 psa_aead_abort( &operation );
5773
Paul Elliottc23a9a02021-06-21 18:32:46 +01005774 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5775 PSA_AEAD_NONCE_MAX_SIZE,
5776 &nonce_length ),
5777 PSA_ERROR_BAD_STATE );
5778
5779 psa_aead_abort( &operation );
5780
Paul Elliott481be342021-07-16 17:38:47 +01005781 /* ------------------------------------------------------- */
5782
Paul Elliottc23a9a02021-06-21 18:32:46 +01005783 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5784 input_data->len ),
5785 PSA_ERROR_BAD_STATE );
5786
5787 psa_aead_abort( &operation );
5788
Paul Elliott481be342021-07-16 17:38:47 +01005789 /* ------------------------------------------------------- */
5790
Paul Elliottc23a9a02021-06-21 18:32:46 +01005791 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5792 additional_data->len ),
5793 PSA_ERROR_BAD_STATE );
5794
5795 psa_aead_abort( &operation );
5796
Paul Elliott481be342021-07-16 17:38:47 +01005797 /* ------------------------------------------------------- */
5798
Paul Elliottc23a9a02021-06-21 18:32:46 +01005799 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5800 input_data->len, output_data,
5801 output_size, &output_length ),
5802 PSA_ERROR_BAD_STATE );
5803
5804 psa_aead_abort( &operation );
5805
Paul Elliott481be342021-07-16 17:38:47 +01005806 /* ------------------------------------------------------- */
5807
Paul Elliottc23a9a02021-06-21 18:32:46 +01005808 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5809 finish_output_size,
5810 &output_part_length,
5811 tag_buffer, tag_length,
5812 &tag_size ),
5813 PSA_ERROR_BAD_STATE );
5814
5815 psa_aead_abort( &operation );
5816
Paul Elliott481be342021-07-16 17:38:47 +01005817 /* ------------------------------------------------------- */
5818
Paul Elliottc23a9a02021-06-21 18:32:46 +01005819 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5820 finish_output_size,
5821 &output_part_length,
5822 tag_buffer,
5823 tag_length ),
5824 PSA_ERROR_BAD_STATE );
5825
5826 psa_aead_abort( &operation );
5827
5828 /* Test for double setups. */
5829
Paul Elliottc23a9a02021-06-21 18:32:46 +01005830 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5831
5832 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5833 PSA_ERROR_BAD_STATE );
5834
5835 psa_aead_abort( &operation );
5836
Paul Elliott481be342021-07-16 17:38:47 +01005837 /* ------------------------------------------------------- */
5838
Paul Elliottc23a9a02021-06-21 18:32:46 +01005839 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5840
5841 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5842 PSA_ERROR_BAD_STATE );
5843
5844 psa_aead_abort( &operation );
5845
Paul Elliott374a2be2021-07-16 17:53:40 +01005846 /* ------------------------------------------------------- */
5847
Paul Elliott374a2be2021-07-16 17:53:40 +01005848 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5849
5850 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5851 PSA_ERROR_BAD_STATE );
5852
5853 psa_aead_abort( &operation );
5854
5855 /* ------------------------------------------------------- */
5856
Paul Elliott374a2be2021-07-16 17:53:40 +01005857 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5858
5859 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5860 PSA_ERROR_BAD_STATE );
5861
5862 psa_aead_abort( &operation );
5863
Paul Elliottc23a9a02021-06-21 18:32:46 +01005864 /* Test for not setting a nonce. */
5865
Paul Elliottc23a9a02021-06-21 18:32:46 +01005866 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5867
5868 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5869 additional_data->len ),
5870 PSA_ERROR_BAD_STATE );
5871
5872 psa_aead_abort( &operation );
5873
Paul Elliott7f628422021-09-01 12:08:29 +01005874 /* ------------------------------------------------------- */
5875
Paul Elliott7f628422021-09-01 12:08:29 +01005876 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5877
5878 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5879 input_data->len, output_data,
5880 output_size, &output_length ),
5881 PSA_ERROR_BAD_STATE );
5882
5883 psa_aead_abort( &operation );
5884
Paul Elliottbdc2c682021-09-21 18:37:10 +01005885 /* ------------------------------------------------------- */
5886
Paul Elliottbdc2c682021-09-21 18:37:10 +01005887 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5888
5889 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5890 finish_output_size,
5891 &output_part_length,
5892 tag_buffer, tag_length,
5893 &tag_size ),
5894 PSA_ERROR_BAD_STATE );
5895
5896 psa_aead_abort( &operation );
5897
5898 /* ------------------------------------------------------- */
5899
Paul Elliottbdc2c682021-09-21 18:37:10 +01005900 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5901
5902 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5903 finish_output_size,
5904 &output_part_length,
5905 tag_buffer,
5906 tag_length ),
5907 PSA_ERROR_BAD_STATE );
5908
5909 psa_aead_abort( &operation );
5910
Paul Elliottc23a9a02021-06-21 18:32:46 +01005911 /* Test for double setting nonce. */
5912
Paul Elliottc23a9a02021-06-21 18:32:46 +01005913 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5914
5915 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5916
5917 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5918 PSA_ERROR_BAD_STATE );
5919
5920 psa_aead_abort( &operation );
5921
Paul Elliott374a2be2021-07-16 17:53:40 +01005922 /* Test for double generating nonce. */
5923
Paul Elliott374a2be2021-07-16 17:53:40 +01005924 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5925
5926 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5927 PSA_AEAD_NONCE_MAX_SIZE,
5928 &nonce_length ) );
5929
5930 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5931 PSA_AEAD_NONCE_MAX_SIZE,
5932 &nonce_length ),
5933 PSA_ERROR_BAD_STATE );
5934
5935
5936 psa_aead_abort( &operation );
5937
5938 /* Test for generate nonce then set and vice versa */
5939
Paul Elliott374a2be2021-07-16 17:53:40 +01005940 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5941
5942 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5943 PSA_AEAD_NONCE_MAX_SIZE,
5944 &nonce_length ) );
5945
5946 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5947 PSA_ERROR_BAD_STATE );
5948
5949 psa_aead_abort( &operation );
5950
Andrzej Kurekad837522021-12-15 15:28:49 +01005951 /* Test for generating nonce after calling set lengths */
5952
5953 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5954
5955 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5956 input_data->len ) );
5957
5958 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5959 PSA_AEAD_NONCE_MAX_SIZE,
5960 &nonce_length ) );
5961
5962 psa_aead_abort( &operation );
5963
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005964 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005965
5966 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5967
5968 if( operation.alg == PSA_ALG_CCM )
5969 {
5970 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5971 input_data->len ),
5972 PSA_ERROR_INVALID_ARGUMENT );
5973 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5974 PSA_AEAD_NONCE_MAX_SIZE,
5975 &nonce_length ),
5976 PSA_ERROR_BAD_STATE );
5977 }
5978 else
5979 {
5980 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5981 input_data->len ) );
5982 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5983 PSA_AEAD_NONCE_MAX_SIZE,
5984 &nonce_length ) );
5985 }
5986
5987 psa_aead_abort( &operation );
5988
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005989 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005990#if SIZE_MAX > UINT32_MAX
5991 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5992
5993 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5994 {
5995 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5996 input_data->len ),
5997 PSA_ERROR_INVALID_ARGUMENT );
5998 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5999 PSA_AEAD_NONCE_MAX_SIZE,
6000 &nonce_length ),
6001 PSA_ERROR_BAD_STATE );
6002 }
6003 else
6004 {
6005 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6006 input_data->len ) );
6007 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6008 PSA_AEAD_NONCE_MAX_SIZE,
6009 &nonce_length ) );
6010 }
6011
6012 psa_aead_abort( &operation );
6013#endif
6014
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006015 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01006016
6017 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6018
6019 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6020 PSA_AEAD_NONCE_MAX_SIZE,
6021 &nonce_length ) );
6022
6023 if( operation.alg == PSA_ALG_CCM )
6024 {
6025 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6026 input_data->len ),
6027 PSA_ERROR_INVALID_ARGUMENT );
6028 }
6029 else
6030 {
6031 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6032 input_data->len ) );
6033 }
6034
6035 psa_aead_abort( &operation );
6036
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006037 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006038 /* Test for setting nonce after calling set lengths */
6039
6040 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6041
6042 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6043 input_data->len ) );
6044
6045 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6046
6047 psa_aead_abort( &operation );
6048
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006049 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006050
6051 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6052
6053 if( operation.alg == PSA_ALG_CCM )
6054 {
6055 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6056 input_data->len ),
6057 PSA_ERROR_INVALID_ARGUMENT );
6058 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6059 PSA_ERROR_BAD_STATE );
6060 }
6061 else
6062 {
6063 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6064 input_data->len ) );
6065 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6066 }
6067
6068 psa_aead_abort( &operation );
6069
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006070 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006071#if SIZE_MAX > UINT32_MAX
6072 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6073
6074 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6075 {
6076 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6077 input_data->len ),
6078 PSA_ERROR_INVALID_ARGUMENT );
6079 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6080 PSA_ERROR_BAD_STATE );
6081 }
6082 else
6083 {
6084 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6085 input_data->len ) );
6086 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6087 }
6088
6089 psa_aead_abort( &operation );
6090#endif
6091
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006092 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006093
6094 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6095
6096 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6097
6098 if( operation.alg == PSA_ALG_CCM )
6099 {
6100 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6101 input_data->len ),
6102 PSA_ERROR_INVALID_ARGUMENT );
6103 }
6104 else
6105 {
6106 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6107 input_data->len ) );
6108 }
6109
6110 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006111
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006112 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006113#if SIZE_MAX > UINT32_MAX
6114 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6115
6116 if( operation.alg == PSA_ALG_GCM )
6117 {
6118 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6119 SIZE_MAX ),
6120 PSA_ERROR_INVALID_ARGUMENT );
6121 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6122 PSA_ERROR_BAD_STATE );
6123 }
6124 else if ( operation.alg != PSA_ALG_CCM )
6125 {
6126 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6127 SIZE_MAX ) );
6128 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6129 }
6130
6131 psa_aead_abort( &operation );
6132
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006133 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006134 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6135
6136 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6137
6138 if( operation.alg == PSA_ALG_GCM )
6139 {
6140 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6141 SIZE_MAX ),
6142 PSA_ERROR_INVALID_ARGUMENT );
6143 }
6144 else if ( operation.alg != PSA_ALG_CCM )
6145 {
6146 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6147 SIZE_MAX ) );
6148 }
6149
6150 psa_aead_abort( &operation );
6151#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006152
6153 /* ------------------------------------------------------- */
6154
Paul Elliott374a2be2021-07-16 17:53:40 +01006155 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6156
6157 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6158
6159 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6160 PSA_AEAD_NONCE_MAX_SIZE,
6161 &nonce_length ),
6162 PSA_ERROR_BAD_STATE );
6163
6164 psa_aead_abort( &operation );
6165
Paul Elliott7220cae2021-06-22 17:25:57 +01006166 /* Test for generating nonce in decrypt setup. */
6167
Paul Elliott7220cae2021-06-22 17:25:57 +01006168 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6169
6170 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6171 PSA_AEAD_NONCE_MAX_SIZE,
6172 &nonce_length ),
6173 PSA_ERROR_BAD_STATE );
6174
6175 psa_aead_abort( &operation );
6176
Paul Elliottc23a9a02021-06-21 18:32:46 +01006177 /* Test for setting lengths twice. */
6178
Paul Elliottc23a9a02021-06-21 18:32:46 +01006179 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6180
6181 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6182
6183 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6184 input_data->len ) );
6185
6186 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6187 input_data->len ),
6188 PSA_ERROR_BAD_STATE );
6189
6190 psa_aead_abort( &operation );
6191
Andrzej Kurekad837522021-12-15 15:28:49 +01006192 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006193
Paul Elliottc23a9a02021-06-21 18:32:46 +01006194 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6195
6196 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6197
Andrzej Kurekad837522021-12-15 15:28:49 +01006198 if( operation.alg == PSA_ALG_CCM )
6199 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006200
Andrzej Kurekad837522021-12-15 15:28:49 +01006201 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6202 additional_data->len ),
6203 PSA_ERROR_BAD_STATE );
6204 }
6205 else
6206 {
6207 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6208 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006209
Andrzej Kurekad837522021-12-15 15:28:49 +01006210 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6211 input_data->len ),
6212 PSA_ERROR_BAD_STATE );
6213 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006214 psa_aead_abort( &operation );
6215
6216 /* ------------------------------------------------------- */
6217
Paul Elliottf94bd992021-09-19 18:15:59 +01006218 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6219
6220 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6221
Andrzej Kurekad837522021-12-15 15:28:49 +01006222 if( operation.alg == PSA_ALG_CCM )
6223 {
6224 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6225 input_data->len, output_data,
6226 output_size, &output_length ),
6227 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006228
Andrzej Kurekad837522021-12-15 15:28:49 +01006229 }
6230 else
6231 {
6232 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6233 input_data->len, output_data,
6234 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006235
Andrzej Kurekad837522021-12-15 15:28:49 +01006236 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6237 input_data->len ),
6238 PSA_ERROR_BAD_STATE );
6239 }
6240 psa_aead_abort( &operation );
6241
6242 /* ------------------------------------------------------- */
6243
6244 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6245
6246 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6247
6248 if( operation.alg == PSA_ALG_CCM )
6249 {
6250 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6251 finish_output_size,
6252 &output_part_length,
6253 tag_buffer, tag_length,
6254 &tag_size ) );
6255 }
6256 else
6257 {
6258 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6259 finish_output_size,
6260 &output_part_length,
6261 tag_buffer, tag_length,
6262 &tag_size ) );
6263
6264 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6265 input_data->len ),
6266 PSA_ERROR_BAD_STATE );
6267 }
6268 psa_aead_abort( &operation );
6269
6270 /* Test for setting lengths after generating nonce + already starting data. */
6271
6272 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6273
6274 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6275 PSA_AEAD_NONCE_MAX_SIZE,
6276 &nonce_length ) );
6277 if( operation.alg == PSA_ALG_CCM )
6278 {
6279
6280 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6281 additional_data->len ),
6282 PSA_ERROR_BAD_STATE );
6283 }
6284 else
6285 {
6286 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6287 additional_data->len ) );
6288
6289 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6290 input_data->len ),
6291 PSA_ERROR_BAD_STATE );
6292 }
6293 psa_aead_abort( &operation );
6294
6295 /* ------------------------------------------------------- */
6296
6297 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6298
6299 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6300 PSA_AEAD_NONCE_MAX_SIZE,
6301 &nonce_length ) );
6302 if( operation.alg == PSA_ALG_CCM )
6303 {
6304 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6305 input_data->len, output_data,
6306 output_size, &output_length ),
6307 PSA_ERROR_BAD_STATE );
6308
6309 }
6310 else
6311 {
6312 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6313 input_data->len, output_data,
6314 output_size, &output_length ) );
6315
6316 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6317 input_data->len ),
6318 PSA_ERROR_BAD_STATE );
6319 }
6320 psa_aead_abort( &operation );
6321
6322 /* ------------------------------------------------------- */
6323
6324 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6325
6326 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6327 PSA_AEAD_NONCE_MAX_SIZE,
6328 &nonce_length ) );
6329 if( operation.alg == PSA_ALG_CCM )
6330 {
6331 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6332 finish_output_size,
6333 &output_part_length,
6334 tag_buffer, tag_length,
6335 &tag_size ) );
6336 }
6337 else
6338 {
6339 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6340 finish_output_size,
6341 &output_part_length,
6342 tag_buffer, tag_length,
6343 &tag_size ) );
6344
6345 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6346 input_data->len ),
6347 PSA_ERROR_BAD_STATE );
6348 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006349 psa_aead_abort( &operation );
6350
Paul Elliott243080c2021-07-21 19:01:17 +01006351 /* Test for not sending any additional data or data after setting non zero
6352 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006353
Paul Elliottc23a9a02021-06-21 18:32:46 +01006354 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6355
6356 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6357
6358 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6359 input_data->len ) );
6360
6361 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6362 finish_output_size,
6363 &output_part_length,
6364 tag_buffer, tag_length,
6365 &tag_size ),
6366 PSA_ERROR_INVALID_ARGUMENT );
6367
6368 psa_aead_abort( &operation );
6369
Paul Elliott243080c2021-07-21 19:01:17 +01006370 /* Test for not sending any additional data or data after setting non-zero
6371 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006372
Paul Elliottc23a9a02021-06-21 18:32:46 +01006373 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6374
6375 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6376
6377 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6378 input_data->len ) );
6379
6380 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6381 finish_output_size,
6382 &output_part_length,
6383 tag_buffer,
6384 tag_length ),
6385 PSA_ERROR_INVALID_ARGUMENT );
6386
6387 psa_aead_abort( &operation );
6388
Paul Elliott243080c2021-07-21 19:01:17 +01006389 /* Test for not sending any additional data after setting a non-zero length
6390 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006391
Paul Elliottc23a9a02021-06-21 18:32:46 +01006392 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6393
6394 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6395
6396 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6397 input_data->len ) );
6398
6399 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6400 input_data->len, output_data,
6401 output_size, &output_length ),
6402 PSA_ERROR_INVALID_ARGUMENT );
6403
6404 psa_aead_abort( &operation );
6405
Paul Elliottf94bd992021-09-19 18:15:59 +01006406 /* Test for not sending any data after setting a non-zero length for it.*/
6407
Paul Elliottf94bd992021-09-19 18:15:59 +01006408 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6409
6410 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6411
6412 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6413 input_data->len ) );
6414
6415 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6416 additional_data->len ) );
6417
6418 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6419 finish_output_size,
6420 &output_part_length,
6421 tag_buffer, tag_length,
6422 &tag_size ),
6423 PSA_ERROR_INVALID_ARGUMENT );
6424
6425 psa_aead_abort( &operation );
6426
Paul Elliottb0450fe2021-09-01 15:06:26 +01006427 /* Test for sending too much additional data after setting lengths. */
6428
Paul Elliottb0450fe2021-09-01 15:06:26 +01006429 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6430
6431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6432
6433 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6434
6435
6436 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6437 additional_data->len ),
6438 PSA_ERROR_INVALID_ARGUMENT );
6439
6440 psa_aead_abort( &operation );
6441
Paul Elliotta2a09b02021-09-22 14:56:40 +01006442 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006443
6444 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, additional_data->len,
6449 input_data->len ) );
6450
6451 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6452 additional_data->len ) );
6453
6454 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6455 1 ),
6456 PSA_ERROR_INVALID_ARGUMENT );
6457
6458 psa_aead_abort( &operation );
6459
Paul Elliottb0450fe2021-09-01 15:06:26 +01006460 /* Test for sending too much data after setting lengths. */
6461
Paul Elliottb0450fe2021-09-01 15:06:26 +01006462 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6463
6464 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6465
6466 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6467
6468 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6469 input_data->len, output_data,
6470 output_size, &output_length ),
6471 PSA_ERROR_INVALID_ARGUMENT );
6472
6473 psa_aead_abort( &operation );
6474
Paul Elliotta2a09b02021-09-22 14:56:40 +01006475 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006476
6477 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6478
6479 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6480
6481 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6482 input_data->len ) );
6483
6484 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6485 additional_data->len ) );
6486
6487 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6488 input_data->len, output_data,
6489 output_size, &output_length ) );
6490
6491 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6492 1, output_data,
6493 output_size, &output_length ),
6494 PSA_ERROR_INVALID_ARGUMENT );
6495
6496 psa_aead_abort( &operation );
6497
Paul Elliottc23a9a02021-06-21 18:32:46 +01006498 /* Test sending additional data after data. */
6499
Paul Elliottc23a9a02021-06-21 18:32:46 +01006500 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6501
6502 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6503
Andrzej Kurekad837522021-12-15 15:28:49 +01006504 if( operation.alg != PSA_ALG_CCM )
6505 {
6506 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6507 input_data->len, output_data,
6508 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006509
Andrzej Kurekad837522021-12-15 15:28:49 +01006510 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6511 additional_data->len ),
6512 PSA_ERROR_BAD_STATE );
6513 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006514 psa_aead_abort( &operation );
6515
Paul Elliott534d0b42021-06-22 19:15:20 +01006516 /* Test calling finish on decryption. */
6517
Paul Elliott534d0b42021-06-22 19:15:20 +01006518 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6519
6520 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6521
6522 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6523 finish_output_size,
6524 &output_part_length,
6525 tag_buffer, tag_length,
6526 &tag_size ),
6527 PSA_ERROR_BAD_STATE );
6528
6529 psa_aead_abort( &operation );
6530
6531 /* Test calling verify on encryption. */
6532
Paul Elliott534d0b42021-06-22 19:15:20 +01006533 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6534
6535 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6536
6537 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6538 finish_output_size,
6539 &output_part_length,
6540 tag_buffer,
6541 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006542 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006543
6544 psa_aead_abort( &operation );
6545
6546
Paul Elliottc23a9a02021-06-21 18:32:46 +01006547exit:
6548 psa_destroy_key( key );
6549 psa_aead_abort( &operation );
6550 mbedtls_free( output_data );
6551 mbedtls_free( final_data );
6552 PSA_DONE( );
6553}
6554/* END_CASE */
6555
6556/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006557void signature_size( int type_arg,
6558 int bits,
6559 int alg_arg,
6560 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006561{
6562 psa_key_type_t type = type_arg;
6563 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006564 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006565
Gilles Peskinefe11b722018-12-18 00:24:04 +01006566 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006567
Gilles Peskinee59236f2018-01-27 23:32:46 +01006568exit:
6569 ;
6570}
6571/* END_CASE */
6572
6573/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006574void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6575 int alg_arg, data_t *input_data,
6576 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006577{
Ronald Cron5425a212020-08-04 14:58:35 +02006578 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006579 psa_key_type_t key_type = key_type_arg;
6580 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006581 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006582 unsigned char *signature = NULL;
6583 size_t signature_size;
6584 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006586
Gilles Peskine8817f612018-12-18 00:18:46 +01006587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006588
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006590 psa_set_key_algorithm( &attributes, alg );
6591 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006592
Gilles Peskine049c7532019-05-15 20:22:09 +02006593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006594 &key ) );
6595 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006596 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006597
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006598 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006599 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006600 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006601 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006602 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006603 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006604 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006605
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006606 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006607 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006608 input_data->x, input_data->len,
6609 signature, signature_size,
6610 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006611 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006612 ASSERT_COMPARE( output_data->x, output_data->len,
6613 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006614
6615exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006616 /*
6617 * Key attributes may have been returned by psa_get_key_attributes()
6618 * thus reset them as required.
6619 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006620 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006621
Ronald Cron5425a212020-08-04 14:58:35 +02006622 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006623 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006624 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006625}
6626/* END_CASE */
6627
6628/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006629void sign_hash_fail( int key_type_arg, data_t *key_data,
6630 int alg_arg, data_t *input_data,
6631 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006632{
Ronald Cron5425a212020-08-04 14:58:35 +02006633 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006634 psa_key_type_t key_type = key_type_arg;
6635 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006636 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006637 psa_status_t actual_status;
6638 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006639 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006640 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006642
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006643 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006644
Gilles Peskine8817f612018-12-18 00:18:46 +01006645 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006646
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006648 psa_set_key_algorithm( &attributes, alg );
6649 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006650
Gilles Peskine049c7532019-05-15 20:22:09 +02006651 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006652 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006653
Ronald Cron5425a212020-08-04 14:58:35 +02006654 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006655 input_data->x, input_data->len,
6656 signature, signature_size,
6657 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006658 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006659 /* The value of *signature_length is unspecified on error, but
6660 * whatever it is, it should be less than signature_size, so that
6661 * if the caller tries to read *signature_length bytes without
6662 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006663 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006664
6665exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006666 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006667 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006668 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006669 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006670}
6671/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006672
6673/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006674void sign_verify_hash( int key_type_arg, data_t *key_data,
6675 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006676{
Ronald Cron5425a212020-08-04 14:58:35 +02006677 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006678 psa_key_type_t key_type = key_type_arg;
6679 psa_algorithm_t alg = alg_arg;
6680 size_t key_bits;
6681 unsigned char *signature = NULL;
6682 size_t signature_size;
6683 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006684 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006685
Gilles Peskine8817f612018-12-18 00:18:46 +01006686 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006687
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006688 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006689 psa_set_key_algorithm( &attributes, alg );
6690 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006691
Gilles Peskine049c7532019-05-15 20:22:09 +02006692 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006693 &key ) );
6694 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006695 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006696
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006697 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006698 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006699 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006700 key_bits, alg );
6701 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006702 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006703 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006704
6705 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006706 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006707 input_data->x, input_data->len,
6708 signature, signature_size,
6709 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006710 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006711 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006712 TEST_ASSERT( signature_length > 0 );
6713
6714 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006715 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006716 input_data->x, input_data->len,
6717 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006718
6719 if( input_data->len != 0 )
6720 {
6721 /* Flip a bit in the input and verify that the signature is now
6722 * detected as invalid. Flip a bit at the beginning, not at the end,
6723 * because ECDSA may ignore the last few bits of the input. */
6724 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006725 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006726 input_data->x, input_data->len,
6727 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006728 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006729 }
6730
6731exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006732 /*
6733 * Key attributes may have been returned by psa_get_key_attributes()
6734 * thus reset them as required.
6735 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006736 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006737
Ronald Cron5425a212020-08-04 14:58:35 +02006738 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006739 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006740 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006741}
6742/* END_CASE */
6743
6744/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006745void verify_hash( int key_type_arg, data_t *key_data,
6746 int alg_arg, data_t *hash_data,
6747 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006748{
Ronald Cron5425a212020-08-04 14:58:35 +02006749 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006750 psa_key_type_t key_type = key_type_arg;
6751 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006752 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006753
Gilles Peskine7be11a72022-04-14 00:12:57 +02006754 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006755
Gilles Peskine8817f612018-12-18 00:18:46 +01006756 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006757
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006758 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006759 psa_set_key_algorithm( &attributes, alg );
6760 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006761
Gilles Peskine049c7532019-05-15 20:22:09 +02006762 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006763 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006764
Ronald Cron5425a212020-08-04 14:58:35 +02006765 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006766 hash_data->x, hash_data->len,
6767 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006768
itayzafrir5c753392018-05-08 11:18:38 +03006769exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006770 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006771 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006772 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006773}
6774/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006775
6776/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006777void verify_hash_fail( int key_type_arg, data_t *key_data,
6778 int alg_arg, data_t *hash_data,
6779 data_t *signature_data,
6780 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006781{
Ronald Cron5425a212020-08-04 14:58:35 +02006782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006783 psa_key_type_t key_type = key_type_arg;
6784 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006785 psa_status_t actual_status;
6786 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006788
Gilles Peskine8817f612018-12-18 00:18:46 +01006789 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006790
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006791 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006792 psa_set_key_algorithm( &attributes, alg );
6793 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006794
Gilles Peskine049c7532019-05-15 20:22:09 +02006795 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006796 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006797
Ronald Cron5425a212020-08-04 14:58:35 +02006798 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006799 hash_data->x, hash_data->len,
6800 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006801 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006802
6803exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006804 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006805 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006806 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006807}
6808/* END_CASE */
6809
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006810/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006811void sign_message_deterministic( int key_type_arg,
6812 data_t *key_data,
6813 int alg_arg,
6814 data_t *input_data,
6815 data_t *output_data )
6816{
6817 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6818 psa_key_type_t key_type = key_type_arg;
6819 psa_algorithm_t alg = alg_arg;
6820 size_t key_bits;
6821 unsigned char *signature = NULL;
6822 size_t signature_size;
6823 size_t signature_length = 0xdeadbeef;
6824 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6825
6826 PSA_ASSERT( psa_crypto_init( ) );
6827
6828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6829 psa_set_key_algorithm( &attributes, alg );
6830 psa_set_key_type( &attributes, key_type );
6831
6832 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6833 &key ) );
6834 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6835 key_bits = psa_get_key_bits( &attributes );
6836
6837 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6838 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006839 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006840 ASSERT_ALLOC( signature, signature_size );
6841
6842 PSA_ASSERT( psa_sign_message( key, alg,
6843 input_data->x, input_data->len,
6844 signature, signature_size,
6845 &signature_length ) );
6846
6847 ASSERT_COMPARE( output_data->x, output_data->len,
6848 signature, signature_length );
6849
6850exit:
6851 psa_reset_key_attributes( &attributes );
6852
6853 psa_destroy_key( key );
6854 mbedtls_free( signature );
6855 PSA_DONE( );
6856
6857}
6858/* END_CASE */
6859
6860/* BEGIN_CASE */
6861void sign_message_fail( int key_type_arg,
6862 data_t *key_data,
6863 int alg_arg,
6864 data_t *input_data,
6865 int signature_size_arg,
6866 int expected_status_arg )
6867{
6868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6869 psa_key_type_t key_type = key_type_arg;
6870 psa_algorithm_t alg = alg_arg;
6871 size_t signature_size = signature_size_arg;
6872 psa_status_t actual_status;
6873 psa_status_t expected_status = expected_status_arg;
6874 unsigned char *signature = NULL;
6875 size_t signature_length = 0xdeadbeef;
6876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6877
6878 ASSERT_ALLOC( signature, signature_size );
6879
6880 PSA_ASSERT( psa_crypto_init( ) );
6881
6882 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6883 psa_set_key_algorithm( &attributes, alg );
6884 psa_set_key_type( &attributes, key_type );
6885
6886 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6887 &key ) );
6888
6889 actual_status = psa_sign_message( key, alg,
6890 input_data->x, input_data->len,
6891 signature, signature_size,
6892 &signature_length );
6893 TEST_EQUAL( actual_status, expected_status );
6894 /* The value of *signature_length is unspecified on error, but
6895 * whatever it is, it should be less than signature_size, so that
6896 * if the caller tries to read *signature_length bytes without
6897 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006898 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006899
6900exit:
6901 psa_reset_key_attributes( &attributes );
6902 psa_destroy_key( key );
6903 mbedtls_free( signature );
6904 PSA_DONE( );
6905}
6906/* END_CASE */
6907
6908/* BEGIN_CASE */
6909void sign_verify_message( int key_type_arg,
6910 data_t *key_data,
6911 int alg_arg,
6912 data_t *input_data )
6913{
6914 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6915 psa_key_type_t key_type = key_type_arg;
6916 psa_algorithm_t alg = alg_arg;
6917 size_t key_bits;
6918 unsigned char *signature = NULL;
6919 size_t signature_size;
6920 size_t signature_length = 0xdeadbeef;
6921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6922
6923 PSA_ASSERT( psa_crypto_init( ) );
6924
6925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6926 PSA_KEY_USAGE_VERIFY_MESSAGE );
6927 psa_set_key_algorithm( &attributes, alg );
6928 psa_set_key_type( &attributes, key_type );
6929
6930 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6931 &key ) );
6932 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6933 key_bits = psa_get_key_bits( &attributes );
6934
6935 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6936 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006937 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006938 ASSERT_ALLOC( signature, signature_size );
6939
6940 PSA_ASSERT( psa_sign_message( key, alg,
6941 input_data->x, input_data->len,
6942 signature, signature_size,
6943 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006944 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006945 TEST_ASSERT( signature_length > 0 );
6946
6947 PSA_ASSERT( psa_verify_message( key, alg,
6948 input_data->x, input_data->len,
6949 signature, signature_length ) );
6950
6951 if( input_data->len != 0 )
6952 {
6953 /* Flip a bit in the input and verify that the signature is now
6954 * detected as invalid. Flip a bit at the beginning, not at the end,
6955 * because ECDSA may ignore the last few bits of the input. */
6956 input_data->x[0] ^= 1;
6957 TEST_EQUAL( psa_verify_message( key, alg,
6958 input_data->x, input_data->len,
6959 signature, signature_length ),
6960 PSA_ERROR_INVALID_SIGNATURE );
6961 }
6962
6963exit:
6964 psa_reset_key_attributes( &attributes );
6965
6966 psa_destroy_key( key );
6967 mbedtls_free( signature );
6968 PSA_DONE( );
6969}
6970/* END_CASE */
6971
6972/* BEGIN_CASE */
6973void verify_message( int key_type_arg,
6974 data_t *key_data,
6975 int alg_arg,
6976 data_t *input_data,
6977 data_t *signature_data )
6978{
6979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6980 psa_key_type_t key_type = key_type_arg;
6981 psa_algorithm_t alg = alg_arg;
6982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6983
Gilles Peskine7be11a72022-04-14 00:12:57 +02006984 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006985
6986 PSA_ASSERT( psa_crypto_init( ) );
6987
6988 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6989 psa_set_key_algorithm( &attributes, alg );
6990 psa_set_key_type( &attributes, key_type );
6991
6992 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6993 &key ) );
6994
6995 PSA_ASSERT( psa_verify_message( key, alg,
6996 input_data->x, input_data->len,
6997 signature_data->x, signature_data->len ) );
6998
6999exit:
7000 psa_reset_key_attributes( &attributes );
7001 psa_destroy_key( key );
7002 PSA_DONE( );
7003}
7004/* END_CASE */
7005
7006/* BEGIN_CASE */
7007void verify_message_fail( int key_type_arg,
7008 data_t *key_data,
7009 int alg_arg,
7010 data_t *hash_data,
7011 data_t *signature_data,
7012 int expected_status_arg )
7013{
7014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7015 psa_key_type_t key_type = key_type_arg;
7016 psa_algorithm_t alg = alg_arg;
7017 psa_status_t actual_status;
7018 psa_status_t expected_status = expected_status_arg;
7019 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7020
7021 PSA_ASSERT( psa_crypto_init( ) );
7022
7023 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
7024 psa_set_key_algorithm( &attributes, alg );
7025 psa_set_key_type( &attributes, key_type );
7026
7027 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
7028 &key ) );
7029
7030 actual_status = psa_verify_message( key, alg,
7031 hash_data->x, hash_data->len,
7032 signature_data->x,
7033 signature_data->len );
7034 TEST_EQUAL( actual_status, expected_status );
7035
7036exit:
7037 psa_reset_key_attributes( &attributes );
7038 psa_destroy_key( key );
7039 PSA_DONE( );
7040}
7041/* END_CASE */
7042
7043/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007044void asymmetric_encrypt( int key_type_arg,
7045 data_t *key_data,
7046 int alg_arg,
7047 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007048 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007049 int expected_output_length_arg,
7050 int expected_status_arg )
7051{
Ronald Cron5425a212020-08-04 14:58:35 +02007052 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007053 psa_key_type_t key_type = key_type_arg;
7054 psa_algorithm_t alg = alg_arg;
7055 size_t expected_output_length = expected_output_length_arg;
7056 size_t key_bits;
7057 unsigned char *output = NULL;
7058 size_t output_size;
7059 size_t output_length = ~0;
7060 psa_status_t actual_status;
7061 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007062 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007063
Gilles Peskine8817f612018-12-18 00:18:46 +01007064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007065
Gilles Peskine656896e2018-06-29 19:12:28 +02007066 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007067 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7068 psa_set_key_algorithm( &attributes, alg );
7069 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007070 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007071 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007072
7073 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007074 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007075 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007076
Gilles Peskine656896e2018-06-29 19:12:28 +02007077 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007078 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007079 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007080
7081 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007082 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007083 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007084 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007085 output, output_size,
7086 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007087 TEST_EQUAL( actual_status, expected_status );
7088 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007089
Gilles Peskine68428122018-06-30 18:42:41 +02007090 /* If the label is empty, the test framework puts a non-null pointer
7091 * in label->x. Test that a null pointer works as well. */
7092 if( label->len == 0 )
7093 {
7094 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007095 if( output_size != 0 )
7096 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007097 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007098 input_data->x, input_data->len,
7099 NULL, label->len,
7100 output, output_size,
7101 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007102 TEST_EQUAL( actual_status, expected_status );
7103 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007104 }
7105
Gilles Peskine656896e2018-06-29 19:12:28 +02007106exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007107 /*
7108 * Key attributes may have been returned by psa_get_key_attributes()
7109 * thus reset them as required.
7110 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007111 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007112
Ronald Cron5425a212020-08-04 14:58:35 +02007113 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007114 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007115 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007116}
7117/* END_CASE */
7118
7119/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007120void asymmetric_encrypt_decrypt( int key_type_arg,
7121 data_t *key_data,
7122 int alg_arg,
7123 data_t *input_data,
7124 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007125{
Ronald Cron5425a212020-08-04 14:58:35 +02007126 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007127 psa_key_type_t key_type = key_type_arg;
7128 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007129 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007130 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007131 size_t output_size;
7132 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007133 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007134 size_t output2_size;
7135 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007136 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007137
Gilles Peskine8817f612018-12-18 00:18:46 +01007138 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007139
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7141 psa_set_key_algorithm( &attributes, alg );
7142 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007143
Gilles Peskine049c7532019-05-15 20:22:09 +02007144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007145 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007146
7147 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007148 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007149 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007150
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007151 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007152 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007153 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007154
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007155 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007156 TEST_LE_U( output2_size,
7157 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7158 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007159 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007160
Gilles Peskineeebd7382018-06-08 18:11:54 +02007161 /* We test encryption by checking that encrypt-then-decrypt gives back
7162 * the original plaintext because of the non-optional random
7163 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007164 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007165 input_data->x, input_data->len,
7166 label->x, label->len,
7167 output, output_size,
7168 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007169 /* We don't know what ciphertext length to expect, but check that
7170 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007171 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007172
Ronald Cron5425a212020-08-04 14:58:35 +02007173 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007174 output, output_length,
7175 label->x, label->len,
7176 output2, output2_size,
7177 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007178 ASSERT_COMPARE( input_data->x, input_data->len,
7179 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007180
7181exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007182 /*
7183 * Key attributes may have been returned by psa_get_key_attributes()
7184 * thus reset them as required.
7185 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007186 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007187
Ronald Cron5425a212020-08-04 14:58:35 +02007188 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007189 mbedtls_free( output );
7190 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007191 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007192}
7193/* END_CASE */
7194
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007195/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007196void asymmetric_decrypt( int key_type_arg,
7197 data_t *key_data,
7198 int alg_arg,
7199 data_t *input_data,
7200 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007201 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007202{
Ronald Cron5425a212020-08-04 14:58:35 +02007203 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007204 psa_key_type_t key_type = key_type_arg;
7205 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007206 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007207 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007208 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007209 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007211
Gilles Peskine8817f612018-12-18 00:18:46 +01007212 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007213
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007214 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7215 psa_set_key_algorithm( &attributes, alg );
7216 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007217
Gilles Peskine049c7532019-05-15 20:22:09 +02007218 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007219 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007220
gabor-mezei-armceface22021-01-21 12:26:17 +01007221 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7222 key_bits = psa_get_key_bits( &attributes );
7223
7224 /* Determine the maximum ciphertext length */
7225 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007226 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007227 ASSERT_ALLOC( output, output_size );
7228
Ronald Cron5425a212020-08-04 14:58:35 +02007229 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007230 input_data->x, input_data->len,
7231 label->x, label->len,
7232 output,
7233 output_size,
7234 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007235 ASSERT_COMPARE( expected_data->x, expected_data->len,
7236 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007237
Gilles Peskine68428122018-06-30 18:42:41 +02007238 /* If the label is empty, the test framework puts a non-null pointer
7239 * in label->x. Test that a null pointer works as well. */
7240 if( label->len == 0 )
7241 {
7242 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007243 if( output_size != 0 )
7244 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007245 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007246 input_data->x, input_data->len,
7247 NULL, label->len,
7248 output,
7249 output_size,
7250 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007251 ASSERT_COMPARE( expected_data->x, expected_data->len,
7252 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007253 }
7254
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007255exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007256 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007257 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007258 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007259 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007260}
7261/* END_CASE */
7262
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007263/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007264void asymmetric_decrypt_fail( int key_type_arg,
7265 data_t *key_data,
7266 int alg_arg,
7267 data_t *input_data,
7268 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007269 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007270 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007271{
Ronald Cron5425a212020-08-04 14:58:35 +02007272 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007273 psa_key_type_t key_type = key_type_arg;
7274 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007275 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007276 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007277 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007278 psa_status_t actual_status;
7279 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007281
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007282 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007283
Gilles Peskine8817f612018-12-18 00:18:46 +01007284 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007285
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007286 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7287 psa_set_key_algorithm( &attributes, alg );
7288 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007289
Gilles Peskine049c7532019-05-15 20:22:09 +02007290 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007291 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007292
Ronald Cron5425a212020-08-04 14:58:35 +02007293 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007294 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007295 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007296 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007297 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007298 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007299 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007300
Gilles Peskine68428122018-06-30 18:42:41 +02007301 /* If the label is empty, the test framework puts a non-null pointer
7302 * in label->x. Test that a null pointer works as well. */
7303 if( label->len == 0 )
7304 {
7305 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007306 if( output_size != 0 )
7307 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007308 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007309 input_data->x, input_data->len,
7310 NULL, label->len,
7311 output, output_size,
7312 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007313 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007314 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007315 }
7316
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007317exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007318 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007319 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007320 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007321 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007322}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007323/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007324
7325/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007326void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007327{
7328 /* Test each valid way of initializing the object, except for `= {0}`, as
7329 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7330 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007331 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007332 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007333 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7334 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7335 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007336
7337 memset( &zero, 0, sizeof( zero ) );
7338
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007339 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007340 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007341 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007342 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007343 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007344 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007345 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007346
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007347 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007348 PSA_ASSERT( psa_key_derivation_abort(&func) );
7349 PSA_ASSERT( psa_key_derivation_abort(&init) );
7350 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007351}
7352/* END_CASE */
7353
Janos Follath16de4a42019-06-13 16:32:24 +01007354/* BEGIN_CASE */
7355void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007356{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007357 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007358 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007359 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007360
Gilles Peskine8817f612018-12-18 00:18:46 +01007361 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007362
Janos Follath16de4a42019-06-13 16:32:24 +01007363 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007364 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007365
7366exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007367 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007368 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007369}
7370/* END_CASE */
7371
Janos Follathaf3c2a02019-06-12 12:34:34 +01007372/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007373void derive_set_capacity( int alg_arg, int capacity_arg,
7374 int expected_status_arg )
7375{
7376 psa_algorithm_t alg = alg_arg;
7377 size_t capacity = capacity_arg;
7378 psa_status_t expected_status = expected_status_arg;
7379 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7380
7381 PSA_ASSERT( psa_crypto_init( ) );
7382
7383 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7384
7385 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7386 expected_status );
7387
7388exit:
7389 psa_key_derivation_abort( &operation );
7390 PSA_DONE( );
7391}
7392/* END_CASE */
7393
7394/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007395void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007396 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007397 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007398 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007399 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007400 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007401 int expected_status_arg3,
7402 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007403{
7404 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007405 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7406 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007407 psa_status_t expected_statuses[] = {expected_status_arg1,
7408 expected_status_arg2,
7409 expected_status_arg3};
7410 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007411 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7412 MBEDTLS_SVC_KEY_ID_INIT,
7413 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007414 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7416 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007417 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007418 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007419 psa_status_t expected_output_status = expected_output_status_arg;
7420 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007421
7422 PSA_ASSERT( psa_crypto_init( ) );
7423
7424 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7425 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007426
7427 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7428
7429 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7430 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007431 mbedtls_test_set_step( i );
7432 if( steps[i] == 0 )
7433 {
7434 /* Skip this step */
7435 }
7436 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007437 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007438 psa_set_key_type( &attributes, key_types[i] );
7439 PSA_ASSERT( psa_import_key( &attributes,
7440 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007441 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007442 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7443 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7444 {
7445 // When taking a private key as secret input, use key agreement
7446 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007447 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7448 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007449 expected_statuses[i] );
7450 }
7451 else
7452 {
7453 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007454 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007455 expected_statuses[i] );
7456 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007457 }
7458 else
7459 {
7460 TEST_EQUAL( psa_key_derivation_input_bytes(
7461 &operation, steps[i],
7462 inputs[i]->x, inputs[i]->len ),
7463 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007464 }
7465 }
7466
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007467 if( output_key_type != PSA_KEY_TYPE_NONE )
7468 {
7469 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007470 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007471 psa_set_key_bits( &attributes, 8 );
7472 actual_output_status =
7473 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007474 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007475 }
7476 else
7477 {
7478 uint8_t buffer[1];
7479 actual_output_status =
7480 psa_key_derivation_output_bytes( &operation,
7481 buffer, sizeof( buffer ) );
7482 }
7483 TEST_EQUAL( actual_output_status, expected_output_status );
7484
Janos Follathaf3c2a02019-06-12 12:34:34 +01007485exit:
7486 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007487 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7488 psa_destroy_key( keys[i] );
7489 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007490 PSA_DONE( );
7491}
7492/* END_CASE */
7493
Janos Follathd958bb72019-07-03 15:02:16 +01007494/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007495void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007496{
Janos Follathd958bb72019-07-03 15:02:16 +01007497 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007498 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007499 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007500 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007501 unsigned char input1[] = "Input 1";
7502 size_t input1_length = sizeof( input1 );
7503 unsigned char input2[] = "Input 2";
7504 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007505 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007506 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007507 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7508 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7509 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007511
Gilles Peskine8817f612018-12-18 00:18:46 +01007512 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007513
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007514 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7515 psa_set_key_algorithm( &attributes, alg );
7516 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007517
Gilles Peskine73676cb2019-05-15 20:15:10 +02007518 PSA_ASSERT( psa_import_key( &attributes,
7519 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007520 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007521
7522 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007523 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7524 input1, input1_length,
7525 input2, input2_length,
7526 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007527 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007528
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007529 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007530 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007531 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007532
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007533 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007534
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007535 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007536 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007537
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007538exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007539 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007540 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007541 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007542}
7543/* END_CASE */
7544
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007545/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007546void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007547{
7548 uint8_t output_buffer[16];
7549 size_t buffer_size = 16;
7550 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007551 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007552
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007553 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7554 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007555 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007556
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007557 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007558 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007559
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007560 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007561
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007562 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7563 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007564 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007565
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007566 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007567 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007568
7569exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007570 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007571}
7572/* END_CASE */
7573
7574/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007575void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007576 int step1_arg, data_t *input1, int expected_status_arg1,
7577 int step2_arg, data_t *input2, int expected_status_arg2,
7578 int step3_arg, data_t *input3, int expected_status_arg3,
7579 int step4_arg, data_t *input4, int expected_status_arg4,
7580 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007581 int requested_capacity_arg,
7582 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007583 data_t *expected_output2,
7584 int other_key_input_type,
7585 int key_input_type,
7586 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007587{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007588 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007589 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7590 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007591 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7592 MBEDTLS_SVC_KEY_ID_INIT,
7593 MBEDTLS_SVC_KEY_ID_INIT,
7594 MBEDTLS_SVC_KEY_ID_INIT};
7595 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7596 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007597 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007598 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007599 uint8_t *expected_outputs[2] =
7600 {expected_output1->x, expected_output2->x};
7601 size_t output_sizes[2] =
7602 {expected_output1->len, expected_output2->len};
7603 size_t output_buffer_size = 0;
7604 uint8_t *output_buffer = NULL;
7605 size_t expected_capacity;
7606 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007607 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7608 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7609 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7610 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007611 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007612 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007613 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007614
7615 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7616 {
7617 if( output_sizes[i] > output_buffer_size )
7618 output_buffer_size = output_sizes[i];
7619 if( output_sizes[i] == 0 )
7620 expected_outputs[i] = NULL;
7621 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007622 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007623 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007624
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007625 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007626 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7627 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7628 requested_capacity ) );
7629 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007630 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007631 switch( steps[i] )
7632 {
7633 case 0:
7634 break;
7635 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007636 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007637 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007638 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007639 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007640 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007641 inputs[i]->x, inputs[i]->len ),
7642 statuses[i] );
7643
7644 if( statuses[i] != PSA_SUCCESS )
7645 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007646 break;
7647 case 1: // input key
7648 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7649 psa_set_key_algorithm( &attributes1, alg );
7650 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7651
7652 PSA_ASSERT( psa_import_key( &attributes1,
7653 inputs[i]->x, inputs[i]->len,
7654 &keys[i] ) );
7655
Przemek Stekiel38647de2022-04-19 13:27:47 +02007656 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007657 {
7658 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007659 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7660 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007661 }
7662
Przemek Stekiel38647de2022-04-19 13:27:47 +02007663 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7664 steps[i],
7665 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007666 break;
7667 default:
7668 TEST_ASSERT( ! "default case not supported" );
7669 break;
7670 }
7671 break;
7672 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007673 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007674 {
7675 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007676 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7677 steps[i],
7678 inputs[i]->x,
7679 inputs[i]->len ),
7680 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007681 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007682 case 1: // input key, type DERIVE
7683 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007684 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7685 psa_set_key_algorithm( &attributes2, alg );
7686 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7687
7688 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007689 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007690 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7691
7692 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007693 inputs[i]->x, inputs[i]->len,
7694 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007695
Przemek Stekiel38647de2022-04-19 13:27:47 +02007696 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7697 steps[i],
7698 keys[i] ),
7699 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007700 break;
7701 case 2: // key agreement
7702 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7703 psa_set_key_algorithm( &attributes3, alg );
7704 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7705
7706 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007707 inputs[i]->x, inputs[i]->len,
7708 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007709
7710 TEST_EQUAL( psa_key_derivation_key_agreement(
7711 &operation,
7712 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7713 keys[i], key_agreement_peer_key->x,
7714 key_agreement_peer_key->len ), statuses[i] );
7715 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007716 default:
7717 TEST_ASSERT( ! "default case not supported" );
7718 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007719 }
7720
Przemek Stekiel38647de2022-04-19 13:27:47 +02007721 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007722 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007723 break;
7724 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007725 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007726 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007727 inputs[i]->x, inputs[i]->len ), statuses[i] );
7728
7729 if( statuses[i] != PSA_SUCCESS )
7730 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007731 break;
7732 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007733 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007734
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007735 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007736 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007737 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007738 expected_capacity = requested_capacity;
7739
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007740 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007741 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007742 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7743
7744 /* For output key derivation secret must be provided using
7745 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007746 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007747 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007748
7749 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7750 psa_set_key_algorithm( &attributes4, alg );
7751 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007752 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007753
7754 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007755 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007756 }
7757 else // output bytes
7758 {
7759 /* Expansion phase. */
7760 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007761 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007762 /* Read some bytes. */
7763 status = psa_key_derivation_output_bytes( &operation,
7764 output_buffer, output_sizes[i] );
7765 if( expected_capacity == 0 && output_sizes[i] == 0 )
7766 {
7767 /* Reading 0 bytes when 0 bytes are available can go either way. */
7768 TEST_ASSERT( status == PSA_SUCCESS ||
7769 status == PSA_ERROR_INSUFFICIENT_DATA );
7770 continue;
7771 }
7772 else if( expected_capacity == 0 ||
7773 output_sizes[i] > expected_capacity )
7774 {
7775 /* Capacity exceeded. */
7776 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7777 expected_capacity = 0;
7778 continue;
7779 }
7780 /* Success. Check the read data. */
7781 PSA_ASSERT( status );
7782 if( output_sizes[i] != 0 )
7783 ASSERT_COMPARE( output_buffer, output_sizes[i],
7784 expected_outputs[i], output_sizes[i] );
7785 /* Check the operation status. */
7786 expected_capacity -= output_sizes[i];
7787 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7788 &current_capacity ) );
7789 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007790 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007791 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007792 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007793
7794exit:
7795 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007796 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007797 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7798 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007799 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007800 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007801}
7802/* END_CASE */
7803
7804/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007805void derive_full( int alg_arg,
7806 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007807 data_t *input1,
7808 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007809 int requested_capacity_arg )
7810{
Ronald Cron5425a212020-08-04 14:58:35 +02007811 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007812 psa_algorithm_t alg = alg_arg;
7813 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007814 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007815 unsigned char output_buffer[16];
7816 size_t expected_capacity = requested_capacity;
7817 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007819
Gilles Peskine8817f612018-12-18 00:18:46 +01007820 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007821
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007822 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7823 psa_set_key_algorithm( &attributes, alg );
7824 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007825
Gilles Peskine049c7532019-05-15 20:22:09 +02007826 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007827 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007828
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007829 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7830 input1->x, input1->len,
7831 input2->x, input2->len,
7832 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007833 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007834
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007835 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007836 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007837 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007838
7839 /* Expansion phase. */
7840 while( current_capacity > 0 )
7841 {
7842 size_t read_size = sizeof( output_buffer );
7843 if( read_size > current_capacity )
7844 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007845 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007846 output_buffer,
7847 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007848 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007849 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007850 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007851 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007852 }
7853
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007854 /* Check that the operation refuses to go over capacity. */
7855 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007856 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007857
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007858 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007859
7860exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007861 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007862 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007863 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007864}
7865/* END_CASE */
7866
Przemek Stekiel8258ea72022-10-19 12:17:19 +02007867/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007868void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007869 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007870 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007871 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007872 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007873{
7874 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7875 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007876 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007877 uint8_t *output_buffer = NULL;
7878 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007879 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7880 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7881 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007882
7883 ASSERT_ALLOC( output_buffer, expected_output->len );
7884 PSA_ASSERT( psa_crypto_init() );
7885
7886 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007887 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007888 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007889
7890 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007891 step, input->x, input->len ),
7892 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007893
7894 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7895 goto exit;
7896
7897 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7898 expected_output->len );
7899
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007900 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007901 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7902 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7903 expected_output->len );
7904
7905exit:
7906 mbedtls_free( output_buffer );
7907 psa_key_derivation_abort( &operation );
7908 PSA_DONE();
7909}
7910/* END_CASE */
7911
Janos Follathe60c9052019-07-03 13:51:30 +01007912/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007913void derive_key_exercise( int alg_arg,
7914 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007915 data_t *input1,
7916 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007917 int derived_type_arg,
7918 int derived_bits_arg,
7919 int derived_usage_arg,
7920 int derived_alg_arg )
7921{
Ronald Cron5425a212020-08-04 14:58:35 +02007922 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7923 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007924 psa_algorithm_t alg = alg_arg;
7925 psa_key_type_t derived_type = derived_type_arg;
7926 size_t derived_bits = derived_bits_arg;
7927 psa_key_usage_t derived_usage = derived_usage_arg;
7928 psa_algorithm_t derived_alg = derived_alg_arg;
7929 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007930 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007932 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007933
Gilles Peskine8817f612018-12-18 00:18:46 +01007934 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007935
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007936 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7937 psa_set_key_algorithm( &attributes, alg );
7938 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007939 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007940 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007941
7942 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007943 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7944 input1->x, input1->len,
7945 input2->x, input2->len,
7946 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007947 goto exit;
7948
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007949 psa_set_key_usage_flags( &attributes, derived_usage );
7950 psa_set_key_algorithm( &attributes, derived_alg );
7951 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007952 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007953 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007954 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007955
7956 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007957 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007958 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7959 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007960
7961 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007962 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007963 goto exit;
7964
7965exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007966 /*
7967 * Key attributes may have been returned by psa_get_key_attributes()
7968 * thus reset them as required.
7969 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007970 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007971
7972 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007973 psa_destroy_key( base_key );
7974 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007975 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007976}
7977/* END_CASE */
7978
Janos Follath42fd8882019-07-03 14:17:09 +01007979/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007980void derive_key_export( int alg_arg,
7981 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007982 data_t *input1,
7983 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007984 int bytes1_arg,
7985 int bytes2_arg )
7986{
Ronald Cron5425a212020-08-04 14:58:35 +02007987 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7988 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007989 psa_algorithm_t alg = alg_arg;
7990 size_t bytes1 = bytes1_arg;
7991 size_t bytes2 = bytes2_arg;
7992 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007993 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007994 uint8_t *output_buffer = NULL;
7995 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007996 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7997 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007998 size_t length;
7999
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008000 ASSERT_ALLOC( output_buffer, capacity );
8001 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01008002 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008003
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008004 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8005 psa_set_key_algorithm( &base_attributes, alg );
8006 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008007 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008008 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008009
8010 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008011 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8012 input1->x, input1->len,
8013 input2->x, input2->len,
8014 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008015 goto exit;
8016
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008017 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008018 output_buffer,
8019 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008020 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008021
8022 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008023 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8024 input1->x, input1->len,
8025 input2->x, input2->len,
8026 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01008027 goto exit;
8028
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008029 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8030 psa_set_key_algorithm( &derived_attributes, 0 );
8031 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008032 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008033 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008034 &derived_key ) );
8035 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008036 export_buffer, bytes1,
8037 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008038 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02008039 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008040 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008041 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008042 &derived_key ) );
8043 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008044 export_buffer + bytes1, bytes2,
8045 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008046 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008047
8048 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008049 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8050 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008051
8052exit:
8053 mbedtls_free( output_buffer );
8054 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008055 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008056 psa_destroy_key( base_key );
8057 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008058 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008059}
8060/* END_CASE */
8061
8062/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008063void derive_key_type( int alg_arg,
8064 data_t *key_data,
8065 data_t *input1,
8066 data_t *input2,
8067 int key_type_arg, int bits_arg,
8068 data_t *expected_export )
8069{
8070 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8071 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8072 const psa_algorithm_t alg = alg_arg;
8073 const psa_key_type_t key_type = key_type_arg;
8074 const size_t bits = bits_arg;
8075 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8076 const size_t export_buffer_size =
8077 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8078 uint8_t *export_buffer = NULL;
8079 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8080 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8081 size_t export_length;
8082
8083 ASSERT_ALLOC( export_buffer, export_buffer_size );
8084 PSA_ASSERT( psa_crypto_init( ) );
8085
8086 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8087 psa_set_key_algorithm( &base_attributes, alg );
8088 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8089 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8090 &base_key ) );
8091
Przemek Stekielc85f0912022-03-08 11:37:54 +01008092 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008093 &operation, base_key, alg,
8094 input1->x, input1->len,
8095 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008096 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008097 goto exit;
8098
8099 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8100 psa_set_key_algorithm( &derived_attributes, 0 );
8101 psa_set_key_type( &derived_attributes, key_type );
8102 psa_set_key_bits( &derived_attributes, bits );
8103 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8104 &derived_key ) );
8105
8106 PSA_ASSERT( psa_export_key( derived_key,
8107 export_buffer, export_buffer_size,
8108 &export_length ) );
8109 ASSERT_COMPARE( export_buffer, export_length,
8110 expected_export->x, expected_export->len );
8111
8112exit:
8113 mbedtls_free( export_buffer );
8114 psa_key_derivation_abort( &operation );
8115 psa_destroy_key( base_key );
8116 psa_destroy_key( derived_key );
8117 PSA_DONE( );
8118}
8119/* END_CASE */
8120
8121/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008122void derive_key( int alg_arg,
8123 data_t *key_data, data_t *input1, data_t *input2,
8124 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008125 int expected_status_arg,
8126 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008127{
Ronald Cron5425a212020-08-04 14:58:35 +02008128 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8129 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008130 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008131 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008132 size_t bits = bits_arg;
8133 psa_status_t expected_status = expected_status_arg;
8134 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8135 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8136 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8137
8138 PSA_ASSERT( psa_crypto_init( ) );
8139
8140 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8141 psa_set_key_algorithm( &base_attributes, alg );
8142 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8143 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008144 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008145
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008146 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8147 input1->x, input1->len,
8148 input2->x, input2->len,
8149 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008150 goto exit;
8151
8152 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8153 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008154 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008155 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008156
8157 psa_status_t status =
8158 psa_key_derivation_output_key( &derived_attributes,
8159 &operation,
8160 &derived_key );
8161 if( is_large_output > 0 )
8162 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8163 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008164
8165exit:
8166 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008167 psa_destroy_key( base_key );
8168 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008169 PSA_DONE( );
8170}
8171/* END_CASE */
8172
8173/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008174void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008175 int our_key_type_arg, int our_key_alg_arg,
8176 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008177 int expected_status_arg )
8178{
Ronald Cron5425a212020-08-04 14:58:35 +02008179 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008180 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008181 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008182 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008183 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008185 psa_status_t expected_status = expected_status_arg;
8186 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008187
Gilles Peskine8817f612018-12-18 00:18:46 +01008188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008189
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008190 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008191 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008192 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008193 PSA_ASSERT( psa_import_key( &attributes,
8194 our_key_data->x, our_key_data->len,
8195 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008196
Gilles Peskine77f40d82019-04-11 21:27:06 +02008197 /* The tests currently include inputs that should fail at either step.
8198 * Test cases that fail at the setup step should be changed to call
8199 * key_derivation_setup instead, and this function should be renamed
8200 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008201 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008202 if( status == PSA_SUCCESS )
8203 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008204 TEST_EQUAL( psa_key_derivation_key_agreement(
8205 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8206 our_key,
8207 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008208 expected_status );
8209 }
8210 else
8211 {
8212 TEST_ASSERT( status == expected_status );
8213 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008214
8215exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008216 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008217 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008218 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008219}
8220/* END_CASE */
8221
8222/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008223void raw_key_agreement( int alg_arg,
8224 int our_key_type_arg, data_t *our_key_data,
8225 data_t *peer_key_data,
8226 data_t *expected_output )
8227{
Ronald Cron5425a212020-08-04 14:58:35 +02008228 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008229 psa_algorithm_t alg = alg_arg;
8230 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008232 unsigned char *output = NULL;
8233 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008234 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008235
Gilles Peskinef0cba732019-04-11 22:12:38 +02008236 PSA_ASSERT( psa_crypto_init( ) );
8237
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008238 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8239 psa_set_key_algorithm( &attributes, alg );
8240 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008241 PSA_ASSERT( psa_import_key( &attributes,
8242 our_key_data->x, our_key_data->len,
8243 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008244
gabor-mezei-armceface22021-01-21 12:26:17 +01008245 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8246 key_bits = psa_get_key_bits( &attributes );
8247
Gilles Peskine992bee82022-04-13 23:25:52 +02008248 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008249 TEST_LE_U( expected_output->len,
8250 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8251 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8252 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008253
8254 /* Good case with exact output size */
8255 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008256 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8257 peer_key_data->x, peer_key_data->len,
8258 output, expected_output->len,
8259 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008260 ASSERT_COMPARE( output, output_length,
8261 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008262 mbedtls_free( output );
8263 output = NULL;
8264 output_length = ~0;
8265
8266 /* Larger buffer */
8267 ASSERT_ALLOC( output, expected_output->len + 1 );
8268 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8269 peer_key_data->x, peer_key_data->len,
8270 output, expected_output->len + 1,
8271 &output_length ) );
8272 ASSERT_COMPARE( output, output_length,
8273 expected_output->x, expected_output->len );
8274 mbedtls_free( output );
8275 output = NULL;
8276 output_length = ~0;
8277
8278 /* Buffer too small */
8279 ASSERT_ALLOC( output, expected_output->len - 1 );
8280 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8281 peer_key_data->x, peer_key_data->len,
8282 output, expected_output->len - 1,
8283 &output_length ),
8284 PSA_ERROR_BUFFER_TOO_SMALL );
8285 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008286 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008287 mbedtls_free( output );
8288 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008289
8290exit:
8291 mbedtls_free( output );
8292 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008293 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008294}
8295/* END_CASE */
8296
8297/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008298void key_agreement_capacity( int alg_arg,
8299 int our_key_type_arg, data_t *our_key_data,
8300 data_t *peer_key_data,
8301 int expected_capacity_arg )
8302{
Ronald Cron5425a212020-08-04 14:58:35 +02008303 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008304 psa_algorithm_t alg = alg_arg;
8305 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008306 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008308 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008309 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008310
Gilles Peskine8817f612018-12-18 00:18:46 +01008311 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008312
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008313 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8314 psa_set_key_algorithm( &attributes, alg );
8315 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008316 PSA_ASSERT( psa_import_key( &attributes,
8317 our_key_data->x, our_key_data->len,
8318 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008319
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008320 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008321 PSA_ASSERT( psa_key_derivation_key_agreement(
8322 &operation,
8323 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8324 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008325 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8326 {
8327 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008328 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008329 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008330 NULL, 0 ) );
8331 }
Gilles Peskine59685592018-09-18 12:11:34 +02008332
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008333 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008334 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008335 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008336 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008337
Gilles Peskinebf491972018-10-25 22:36:12 +02008338 /* Test the actual capacity by reading the output. */
8339 while( actual_capacity > sizeof( output ) )
8340 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008341 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008342 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008343 actual_capacity -= sizeof( output );
8344 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008345 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008346 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008347 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008348 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008349
Gilles Peskine59685592018-09-18 12:11:34 +02008350exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008351 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008352 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008353 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008354}
8355/* END_CASE */
8356
8357/* BEGIN_CASE */
8358void key_agreement_output( int alg_arg,
8359 int our_key_type_arg, data_t *our_key_data,
8360 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008361 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008362{
Ronald Cron5425a212020-08-04 14:58:35 +02008363 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008364 psa_algorithm_t alg = alg_arg;
8365 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008366 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008368 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008369
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008370 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8371 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008372
Gilles Peskine8817f612018-12-18 00:18:46 +01008373 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008374
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008375 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8376 psa_set_key_algorithm( &attributes, alg );
8377 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008378 PSA_ASSERT( psa_import_key( &attributes,
8379 our_key_data->x, our_key_data->len,
8380 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008381
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008382 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008383 PSA_ASSERT( psa_key_derivation_key_agreement(
8384 &operation,
8385 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8386 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008387 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8388 {
8389 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008390 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008391 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008392 NULL, 0 ) );
8393 }
Gilles Peskine59685592018-09-18 12:11:34 +02008394
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008395 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008396 actual_output,
8397 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008398 ASSERT_COMPARE( actual_output, expected_output1->len,
8399 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008400 if( expected_output2->len != 0 )
8401 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008402 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008403 actual_output,
8404 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008405 ASSERT_COMPARE( actual_output, expected_output2->len,
8406 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008407 }
Gilles Peskine59685592018-09-18 12:11:34 +02008408
8409exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008410 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008411 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008412 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008413 mbedtls_free( actual_output );
8414}
8415/* END_CASE */
8416
8417/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008418void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008419{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008420 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008421 unsigned char *output = NULL;
8422 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008423 size_t i;
8424 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008425
Simon Butcher49f8e312020-03-03 15:51:50 +00008426 TEST_ASSERT( bytes_arg >= 0 );
8427
Gilles Peskine91892022021-02-08 19:50:26 +01008428 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008429 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008430
Gilles Peskine8817f612018-12-18 00:18:46 +01008431 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008432
Gilles Peskinea50d7392018-06-21 10:22:13 +02008433 /* Run several times, to ensure that every output byte will be
8434 * nonzero at least once with overwhelming probability
8435 * (2^(-8*number_of_runs)). */
8436 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008437 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008438 if( bytes != 0 )
8439 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008440 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008441
Gilles Peskinea50d7392018-06-21 10:22:13 +02008442 for( i = 0; i < bytes; i++ )
8443 {
8444 if( output[i] != 0 )
8445 ++changed[i];
8446 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008447 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008448
8449 /* Check that every byte was changed to nonzero at least once. This
8450 * validates that psa_generate_random is overwriting every byte of
8451 * the output buffer. */
8452 for( i = 0; i < bytes; i++ )
8453 {
8454 TEST_ASSERT( changed[i] != 0 );
8455 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008456
8457exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008458 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008459 mbedtls_free( output );
8460 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008461}
8462/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008463
8464/* BEGIN_CASE */
8465void generate_key( int type_arg,
8466 int bits_arg,
8467 int usage_arg,
8468 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008469 int expected_status_arg,
8470 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008471{
Ronald Cron5425a212020-08-04 14:58:35 +02008472 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008473 psa_key_type_t type = type_arg;
8474 psa_key_usage_t usage = usage_arg;
8475 size_t bits = bits_arg;
8476 psa_algorithm_t alg = alg_arg;
8477 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008479 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008480
Gilles Peskine8817f612018-12-18 00:18:46 +01008481 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008482
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008483 psa_set_key_usage_flags( &attributes, usage );
8484 psa_set_key_algorithm( &attributes, alg );
8485 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008486 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008487
8488 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008489 psa_status_t status = psa_generate_key( &attributes, &key );
8490
8491 if( is_large_key > 0 )
8492 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8493 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008494 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008495 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008496
8497 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008498 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008499 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8500 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008501
Gilles Peskine818ca122018-06-20 18:16:48 +02008502 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008503 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008504 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008505
8506exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008507 /*
8508 * Key attributes may have been returned by psa_get_key_attributes()
8509 * thus reset them as required.
8510 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008511 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008512
Ronald Cron5425a212020-08-04 14:58:35 +02008513 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008514 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008515}
8516/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008517
Ronald Cronee414c72021-03-18 18:50:08 +01008518/* 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 +02008519void generate_key_rsa( int bits_arg,
8520 data_t *e_arg,
8521 int expected_status_arg )
8522{
Ronald Cron5425a212020-08-04 14:58:35 +02008523 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008524 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008525 size_t bits = bits_arg;
8526 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8527 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8528 psa_status_t expected_status = expected_status_arg;
8529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8530 uint8_t *exported = NULL;
8531 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008532 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008533 size_t exported_length = SIZE_MAX;
8534 uint8_t *e_read_buffer = NULL;
8535 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008536 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008537 size_t e_read_length = SIZE_MAX;
8538
8539 if( e_arg->len == 0 ||
8540 ( e_arg->len == 3 &&
8541 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8542 {
8543 is_default_public_exponent = 1;
8544 e_read_size = 0;
8545 }
8546 ASSERT_ALLOC( e_read_buffer, e_read_size );
8547 ASSERT_ALLOC( exported, exported_size );
8548
8549 PSA_ASSERT( psa_crypto_init( ) );
8550
8551 psa_set_key_usage_flags( &attributes, usage );
8552 psa_set_key_algorithm( &attributes, alg );
8553 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8554 e_arg->x, e_arg->len ) );
8555 psa_set_key_bits( &attributes, bits );
8556
8557 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008558 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008559 if( expected_status != PSA_SUCCESS )
8560 goto exit;
8561
8562 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008563 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008564 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8565 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8566 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8567 e_read_buffer, e_read_size,
8568 &e_read_length ) );
8569 if( is_default_public_exponent )
8570 TEST_EQUAL( e_read_length, 0 );
8571 else
8572 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8573
8574 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008575 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008576 goto exit;
8577
8578 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008579 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008580 exported, exported_size,
8581 &exported_length ) );
8582 {
8583 uint8_t *p = exported;
8584 uint8_t *end = exported + exported_length;
8585 size_t len;
8586 /* RSAPublicKey ::= SEQUENCE {
8587 * modulus INTEGER, -- n
8588 * publicExponent INTEGER } -- e
8589 */
8590 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008591 MBEDTLS_ASN1_SEQUENCE |
8592 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008593 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008594 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8595 MBEDTLS_ASN1_INTEGER ) );
8596 if( len >= 1 && p[0] == 0 )
8597 {
8598 ++p;
8599 --len;
8600 }
8601 if( e_arg->len == 0 )
8602 {
8603 TEST_EQUAL( len, 3 );
8604 TEST_EQUAL( p[0], 1 );
8605 TEST_EQUAL( p[1], 0 );
8606 TEST_EQUAL( p[2], 1 );
8607 }
8608 else
8609 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8610 }
8611
8612exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008613 /*
8614 * Key attributes may have been returned by psa_get_key_attributes() or
8615 * set by psa_set_key_domain_parameters() thus reset them as required.
8616 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008617 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008618
Ronald Cron5425a212020-08-04 14:58:35 +02008619 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008620 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008621 mbedtls_free( e_read_buffer );
8622 mbedtls_free( exported );
8623}
8624/* END_CASE */
8625
Darryl Greend49a4992018-06-18 17:27:26 +01008626/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008627void persistent_key_load_key_from_storage( data_t *data,
8628 int type_arg, int bits_arg,
8629 int usage_flags_arg, int alg_arg,
8630 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008631{
Ronald Cron71016a92020-08-28 19:01:50 +02008632 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008634 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8635 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008636 psa_key_type_t type = type_arg;
8637 size_t bits = bits_arg;
8638 psa_key_usage_t usage_flags = usage_flags_arg;
8639 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008640 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008641 unsigned char *first_export = NULL;
8642 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008643 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008644 size_t first_exported_length;
8645 size_t second_exported_length;
8646
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008647 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8648 {
8649 ASSERT_ALLOC( first_export, export_size );
8650 ASSERT_ALLOC( second_export, export_size );
8651 }
Darryl Greend49a4992018-06-18 17:27:26 +01008652
Gilles Peskine8817f612018-12-18 00:18:46 +01008653 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008654
Gilles Peskinec87af662019-05-15 16:12:22 +02008655 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008656 psa_set_key_usage_flags( &attributes, usage_flags );
8657 psa_set_key_algorithm( &attributes, alg );
8658 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008659 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008660
Darryl Green0c6575a2018-11-07 16:05:30 +00008661 switch( generation_method )
8662 {
8663 case IMPORT_KEY:
8664 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008665 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008666 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008667 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008668
Darryl Green0c6575a2018-11-07 16:05:30 +00008669 case GENERATE_KEY:
8670 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008671 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008672 break;
8673
8674 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008675#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008676 {
8677 /* Create base key */
8678 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8679 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8680 psa_set_key_usage_flags( &base_attributes,
8681 PSA_KEY_USAGE_DERIVE );
8682 psa_set_key_algorithm( &base_attributes, derive_alg );
8683 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008684 PSA_ASSERT( psa_import_key( &base_attributes,
8685 data->x, data->len,
8686 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008687 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008688 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008689 PSA_ASSERT( psa_key_derivation_input_key(
8690 &operation,
8691 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008692 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008693 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008694 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008695 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8696 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008697 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008698 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008699 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008700 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008701 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008702#else
8703 TEST_ASSUME( ! "KDF not supported in this configuration" );
8704#endif
8705 break;
8706
8707 default:
8708 TEST_ASSERT( ! "generation_method not implemented in test" );
8709 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008710 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008711 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008712
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008713 /* Export the key if permitted by the key policy. */
8714 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8715 {
Ronald Cron5425a212020-08-04 14:58:35 +02008716 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008717 first_export, export_size,
8718 &first_exported_length ) );
8719 if( generation_method == IMPORT_KEY )
8720 ASSERT_COMPARE( data->x, data->len,
8721 first_export, first_exported_length );
8722 }
Darryl Greend49a4992018-06-18 17:27:26 +01008723
8724 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008725 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008726 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008727 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008728
Darryl Greend49a4992018-06-18 17:27:26 +01008729 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008730 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008731 TEST_ASSERT( mbedtls_svc_key_id_equal(
8732 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008733 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8734 PSA_KEY_LIFETIME_PERSISTENT );
8735 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8736 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008737 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008738 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008739 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008740
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008741 /* Export the key again if permitted by the key policy. */
8742 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008743 {
Ronald Cron5425a212020-08-04 14:58:35 +02008744 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008745 second_export, export_size,
8746 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008747 ASSERT_COMPARE( first_export, first_exported_length,
8748 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008749 }
8750
8751 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008752 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008753 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008754
8755exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008756 /*
8757 * Key attributes may have been returned by psa_get_key_attributes()
8758 * thus reset them as required.
8759 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008760 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008761
Darryl Greend49a4992018-06-18 17:27:26 +01008762 mbedtls_free( first_export );
8763 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008764 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008765 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008766 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008767 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008768}
8769/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008770
Neil Armstronga557cb82022-06-10 08:58:32 +02008771/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008772void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8773 int primitive_arg, int hash_arg, int role_arg,
Valerio Setti1070aed2022-11-11 19:37:31 +01008774 int test_input, data_t *pw_data,
8775 int inj_err_type_arg,
8776 int expected_error_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008777{
8778 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8779 psa_pake_operation_t operation = psa_pake_operation_init();
8780 psa_algorithm_t alg = alg_arg;
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008781 psa_pake_primitive_t primitive = primitive_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008782 psa_key_type_t key_type_pw = key_type_pw_arg;
8783 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008784 psa_algorithm_t hash_alg = hash_arg;
8785 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008786 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8787 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01008788 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
8789 psa_status_t expected_error = expected_error_arg;
8790 psa_status_t status;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008791 unsigned char *output_buffer = NULL;
8792 size_t output_len = 0;
8793
8794 PSA_INIT( );
8795
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008796 size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8797 PSA_PAKE_STEP_KEY_SHARE);
8798 ASSERT_ALLOC( output_buffer, buf_size );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008799
8800 if( pw_data->len > 0 )
8801 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008802 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008803 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008804 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008805 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8806 &key ) );
8807 }
8808
8809 psa_pake_cs_set_algorithm( &cipher_suite, alg );
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008810 psa_pake_cs_set_primitive( &cipher_suite, primitive );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008811 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8812
Neil Armstrong645cccd2022-06-08 17:36:23 +02008813 PSA_ASSERT( psa_pake_abort( &operation ) );
8814
Valerio Setti1070aed2022-11-11 19:37:31 +01008815 if ( inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS )
8816 {
8817 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8818 expected_error );
8819 PSA_ASSERT( psa_pake_abort( &operation ) );
8820 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8821 expected_error );
8822 PSA_ASSERT( psa_pake_abort( &operation ) );
8823 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8824 expected_error );
8825 PSA_ASSERT( psa_pake_abort( &operation ) );
8826 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8827 expected_error );
8828 PSA_ASSERT( psa_pake_abort( &operation ) );
8829 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8830 NULL, 0, NULL ),
8831 expected_error );
8832 PSA_ASSERT( psa_pake_abort( &operation ) );
8833 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
8834 expected_error );
8835 PSA_ASSERT( psa_pake_abort( &operation ) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008836 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008837 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008838
Valerio Setti1070aed2022-11-11 19:37:31 +01008839 status = psa_pake_setup( &operation, &cipher_suite );
8840 if (status != PSA_SUCCESS)
8841 {
8842 TEST_EQUAL( status, expected_error );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008843 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008844 }
8845
8846 if( inj_err_type == INJECT_ERR_DUPLICATE_SETUP )
8847 {
8848 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8849 expected_error );
8850 goto exit;
8851 }
8852
8853 status = psa_pake_set_role( &operation, role);
8854 if ( status != PSA_SUCCESS )
8855 {
8856 TEST_EQUAL( status, expected_error );
8857 goto exit;
8858 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008859
8860 if( pw_data->len > 0 )
8861 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008862 status = psa_pake_set_password_key( &operation, key );
8863 if ( status != PSA_SUCCESS )
8864 {
8865 TEST_EQUAL( status, expected_error );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008866 goto exit;
Valerio Setti1070aed2022-11-11 19:37:31 +01008867 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008868 }
8869
Valerio Setti1070aed2022-11-11 19:37:31 +01008870 if ( inj_err_type == INJECT_ERR_INVALID_USER )
8871 {
8872 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8873 PSA_ERROR_INVALID_ARGUMENT );
8874 goto exit;
8875 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008876
Valerio Setti1070aed2022-11-11 19:37:31 +01008877 if ( inj_err_type == INJECT_ERR_INVALID_PEER )
8878 {
8879 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8880 PSA_ERROR_INVALID_ARGUMENT );
8881 goto exit;
8882 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008883
Valerio Setti1070aed2022-11-11 19:37:31 +01008884 if ( inj_err_type == INJECT_ERR_SET_USER )
8885 {
8886 const uint8_t unsupported_id[] = "abcd";
8887 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8888 PSA_ERROR_NOT_SUPPORTED );
8889 goto exit;
8890 }
8891
8892 if ( inj_err_type == INJECT_ERR_SET_PEER )
8893 {
8894 const uint8_t unsupported_id[] = "abcd";
8895 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8896 PSA_ERROR_NOT_SUPPORTED );
8897 goto exit;
8898 }
Neil Armstrong707d9572022-06-08 17:31:49 +02008899
Manuel Pégourié-Gonnardb63a9ef2022-10-06 10:55:19 +02008900 const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive,
8901 PSA_PAKE_STEP_KEY_SHARE );
8902 const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive,
8903 PSA_PAKE_STEP_ZK_PUBLIC );
8904 const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive,
8905 PSA_PAKE_STEP_ZK_PROOF );
8906
Valerio Setti1070aed2022-11-11 19:37:31 +01008907 if ( test_input )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008908 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008909 if ( inj_err_type == INJECT_EMPTY_IO_BUFFER )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008910 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008911 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0 ),
8912 PSA_ERROR_INVALID_ARGUMENT );
8913 goto exit;
8914 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008915
Valerio Setti1070aed2022-11-11 19:37:31 +01008916 if ( inj_err_type == INJECT_UNKNOWN_STEP )
8917 {
8918 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8919 output_buffer, size_zk_proof ),
8920 PSA_ERROR_INVALID_ARGUMENT );
8921 goto exit;
8922 }
8923
8924 if ( inj_err_type == INJECT_INVALID_FIRST_STEP )
8925 {
8926 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8927 output_buffer, size_zk_proof ),
8928 PSA_ERROR_BAD_STATE );
8929 goto exit;
8930 }
8931
8932 status = psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8933 output_buffer, size_key_share );
8934 if ( status != PSA_SUCCESS )
8935 {
8936 TEST_EQUAL( status, expected_error);
8937 goto exit;
8938 }
8939
8940 if ( inj_err_type == INJECT_WRONG_BUFFER_SIZE )
8941 {
8942 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8943 output_buffer, size_zk_public + 1 ),
8944 PSA_ERROR_INVALID_ARGUMENT );
8945 goto exit;
8946 }
8947
8948 if ( inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE )
8949 {
8950 // Just trigger any kind of error. We don't care about the result here
8951 psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8952 output_buffer, size_zk_public + 1 );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008953 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +02008954 output_buffer, size_zk_public ),
Valerio Setti1070aed2022-11-11 19:37:31 +01008955 PSA_ERROR_BAD_STATE );
8956 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008957 }
Valerio Setti1070aed2022-11-11 19:37:31 +01008958 } else {
8959 if ( inj_err_type == INJECT_EMPTY_IO_BUFFER )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008960 {
Valerio Setti1070aed2022-11-11 19:37:31 +01008961 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8962 NULL, 0, NULL ),
8963 PSA_ERROR_INVALID_ARGUMENT );
8964 goto exit;
8965 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008966
Valerio Setti1070aed2022-11-11 19:37:31 +01008967 if ( inj_err_type == INJECT_UNKNOWN_STEP )
8968 {
8969 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8970 output_buffer, buf_size, &output_len ),
8971 PSA_ERROR_INVALID_ARGUMENT );
8972 goto exit;
8973 }
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008974
Valerio Setti1070aed2022-11-11 19:37:31 +01008975 if ( inj_err_type == INJECT_INVALID_FIRST_STEP )
8976 {
8977 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8978 output_buffer, buf_size, &output_len ),
8979 PSA_ERROR_BAD_STATE );
8980 goto exit;
8981 }
8982
8983 status = psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8984 output_buffer, buf_size, &output_len );
8985 if ( status != PSA_SUCCESS )
8986 {
8987 TEST_EQUAL( status, expected_error);
8988 goto exit;
8989 }
8990
8991 TEST_ASSERT( output_len > 0 );
8992
8993 if ( inj_err_type == INJECT_WRONG_BUFFER_SIZE )
8994 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008995 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
Valerio Setti1070aed2022-11-11 19:37:31 +01008996 output_buffer, size_zk_public - 1, &output_len ),
8997 PSA_ERROR_BUFFER_TOO_SMALL );
8998 goto exit;
8999 }
9000
9001 if ( inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE )
9002 {
9003 // Just trigger any kind of error. We don't care about the result here
9004 psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
9005 output_buffer, size_zk_public - 1, &output_len );
9006 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
9007 output_buffer, buf_size, &output_len ),
9008 PSA_ERROR_BAD_STATE );
9009 goto exit;
Neil Armstrong9c8b4922022-06-08 17:59:07 +02009010 }
9011 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02009012
9013exit:
9014 PSA_ASSERT( psa_destroy_key( key ) );
9015 PSA_ASSERT( psa_pake_abort( &operation ) );
9016 mbedtls_free( output_buffer );
9017 PSA_DONE( );
9018}
9019/* END_CASE */
9020
Neil Armstronga557cb82022-06-10 08:58:32 +02009021/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009022void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
9023 int client_input_first, int inject_error,
9024 data_t *pw_data )
9025{
9026 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9027 psa_pake_operation_t server = psa_pake_operation_init();
9028 psa_pake_operation_t client = psa_pake_operation_init();
9029 psa_algorithm_t alg = alg_arg;
9030 psa_algorithm_t hash_alg = hash_arg;
9031 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9033
9034 PSA_INIT( );
9035
9036 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9037 psa_set_key_algorithm( &attributes, alg );
9038 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9039 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9040 &key ) );
9041
9042 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9043 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9044 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9045
9046
9047 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9048 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9049
9050 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9051 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9052
9053 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9054 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9055
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009056 ecjpake_do_round( alg, primitive_arg, &server, &client,
9057 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009058
9059 if( inject_error == 1 || inject_error == 2 )
9060 goto exit;
9061
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009062 ecjpake_do_round( alg, primitive_arg, &server, &client,
9063 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02009064
9065exit:
9066 psa_destroy_key( key );
9067 psa_pake_abort( &server );
9068 psa_pake_abort( &client );
9069 PSA_DONE( );
9070}
9071/* END_CASE */
9072
9073/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02009074void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02009075 int derive_alg_arg, data_t *pw_data,
Valerio Setti1070aed2022-11-11 19:37:31 +01009076 int client_input_first, int destroy_key,
9077 int client_input_first, int inj_err_type_arg )
Neil Armstrongd597bc72022-05-25 11:28:39 +02009078{
9079 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
9080 psa_pake_operation_t server = psa_pake_operation_init();
9081 psa_pake_operation_t client = psa_pake_operation_init();
9082 psa_algorithm_t alg = alg_arg;
9083 psa_algorithm_t hash_alg = hash_arg;
9084 psa_algorithm_t derive_alg = derive_alg_arg;
9085 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
9086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
9087 psa_key_derivation_operation_t server_derive =
9088 PSA_KEY_DERIVATION_OPERATION_INIT;
9089 psa_key_derivation_operation_t client_derive =
9090 PSA_KEY_DERIVATION_OPERATION_INIT;
Valerio Setti1070aed2022-11-11 19:37:31 +01009091 ecjpake_injected_failure_t inj_err_type = inj_err_type_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02009092
9093 PSA_INIT( );
9094
Neil Armstrongd597bc72022-05-25 11:28:39 +02009095 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
9096 psa_set_key_algorithm( &attributes, alg );
9097 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
9098 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
9099 &key ) );
9100
9101 psa_pake_cs_set_algorithm( &cipher_suite, alg );
9102 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
9103 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
9104
Neil Armstrong1e855602022-06-15 11:32:11 +02009105 /* Get shared key */
9106 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
9107 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
9108
9109 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
9110 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
9111 {
9112 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
9113 PSA_KEY_DERIVATION_INPUT_SEED,
9114 (const uint8_t*) "", 0) );
9115 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
9116 PSA_KEY_DERIVATION_INPUT_SEED,
9117 (const uint8_t*) "", 0) );
9118 }
9119
Neil Armstrongd597bc72022-05-25 11:28:39 +02009120 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9121 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9122
9123 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9124 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9125
9126 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9127 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9128
Przemek Stekielcd356c32022-11-20 19:05:20 +01009129 if( destroy_key == 1 )
9130 psa_destroy_key( key );
9131
Valerio Setti1070aed2022-11-11 19:37:31 +01009132 if( inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1 )
9133 {
9134 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009135 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009136 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009137 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009138 goto exit;
9139 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009140
Neil Armstrongf983caf2022-06-15 15:27:48 +02009141 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009142 ecjpake_do_round( alg, primitive_arg, &server, &client,
9143 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009144
Valerio Setti1070aed2022-11-11 19:37:31 +01009145 if ( inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2 )
9146 {
9147 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009148 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009149 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
Neil Armstrong1e855602022-06-15 11:32:11 +02009150 PSA_ERROR_BAD_STATE );
Valerio Setti1070aed2022-11-11 19:37:31 +01009151 goto exit;
9152 }
Neil Armstrong1e855602022-06-15 11:32:11 +02009153
Neil Armstrongf983caf2022-06-15 15:27:48 +02009154 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009155 ecjpake_do_round( alg, primitive_arg, &server, &client,
9156 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009157
Neil Armstrongd597bc72022-05-25 11:28:39 +02009158 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9159 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9160
9161exit:
9162 psa_key_derivation_abort( &server_derive );
9163 psa_key_derivation_abort( &client_derive );
9164 psa_destroy_key( key );
9165 psa_pake_abort( &server );
9166 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009167 PSA_DONE( );
9168}
9169/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009170
9171/* BEGIN_CASE */
9172void ecjpake_size_macros( )
9173{
9174 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9175 const size_t bits = 256;
9176 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9177 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9178 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9179 PSA_ECC_FAMILY_SECP_R1 );
9180
9181 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9182 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9183 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9184 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9185 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9186 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9187 /* The output for ZK_PROOF is the same bitsize as the curve */
9188 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9189 PSA_BITS_TO_BYTES( bits ) );
9190
9191 /* Input sizes are the same as output sizes */
9192 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9193 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9194 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9195 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9196 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9197 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9198
9199 /* These inequalities will always hold even when other PAKEs are added */
9200 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9201 PSA_PAKE_OUTPUT_MAX_SIZE );
9202 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9203 PSA_PAKE_OUTPUT_MAX_SIZE );
9204 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9205 PSA_PAKE_OUTPUT_MAX_SIZE );
9206 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9207 PSA_PAKE_INPUT_MAX_SIZE );
9208 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9209 PSA_PAKE_INPUT_MAX_SIZE );
9210 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9211 PSA_PAKE_INPUT_MAX_SIZE );
9212}
9213/* END_CASE */