blob: 1b144df35a489d1de1629a1d9e774746192a8987 [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
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200399 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200404 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100510 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_LE_U( output_length,
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200570 TEST_LE_U( output_length,
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Neil Armstrong4766f992022-02-28 16:23:59 +0100592/*!
593 * \brief Internal Function for MAC multipart tests.
594 * \param key_type_arg Type of key passed in
595 * \param key_data The encryption / decryption key data
596 * \param alg_arg The type of algorithm used
597 * \param input_data Data to encrypt / decrypt
598 * \param data_part_len_arg If not -1, the length of chunks to feed
599 * the data in to be encrypted / decrypted. If
600 * -1, no chunking
601 * \param expected_output Expected output
602 * \param is_verify If non-zero this is an verify operation.
603 * \param do_zero_parts If non-zero, interleave zero length chunks
604 * with normal length chunks.
605 * \return int Zero on failure, non-zero on success.
606 */
607static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
608 int alg_arg,
609 data_t *input_data,
610 int data_part_len_arg,
611 data_t *expected_output,
612 int is_verify,
613 int do_zero_parts )
614{
615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
616 psa_key_type_t key_type = key_type_arg;
617 psa_algorithm_t alg = alg_arg;
618 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
619 unsigned char mac[PSA_MAC_MAX_SIZE];
620 size_t part_offset = 0;
621 size_t part_length = 0;
622 size_t data_part_len = 0;
623 size_t mac_len = 0;
624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
625 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
626
627 int test_ok = 0;
628 size_t part_count = 0;
629
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100630 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100631
632 if( is_verify )
633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
634 else
635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
636
637 psa_set_key_algorithm( &attributes, alg );
638 psa_set_key_type( &attributes, key_type );
639
640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
641 &key ) );
642
643 if( is_verify )
644 status = psa_mac_verify_setup( &operation, key, alg );
645 else
646 status = psa_mac_sign_setup( &operation, key, alg );
647
648 PSA_ASSERT( status );
649
650 if( data_part_len_arg != -1 )
651 {
652 /* Pass data in parts */
653 data_part_len = ( size_t ) data_part_len_arg;
654
655 for( part_offset = 0, part_count = 0;
656 part_offset < input_data->len;
657 part_offset += part_length, part_count++ )
658 {
659 if( do_zero_parts && ( part_count & 0x01 ) )
660 {
661 part_length = 0;
662 }
663 else if( ( input_data->len - part_offset ) < data_part_len )
664 {
665 part_length = ( input_data->len - part_offset );
666 }
667 else
668 {
669 part_length = data_part_len;
670 }
671
672 PSA_ASSERT( psa_mac_update( &operation,
673 ( input_data->x + part_offset ),
674 part_length ) );
675 }
676 }
677 else
678 {
679 /* Pass all data in one go. */
680 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
681 input_data->len ) );
682 }
683
684 if( is_verify )
685 {
686 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
687 expected_output->len ) );
688 }
689 else
690 {
691 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
692 PSA_MAC_MAX_SIZE, &mac_len ) );
693
694 ASSERT_COMPARE( expected_output->x, expected_output->len,
695 mac, mac_len );
696 }
697
698 test_ok = 1;
699
700exit:
701 psa_destroy_key( key );
702 psa_mac_abort( &operation );
703 PSA_DONE( );
704
705 return( test_ok );
706}
707
Neil Armstrong75673ab2022-06-15 17:39:01 +0200708#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200709static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
710 psa_pake_operation_t *server,
711 psa_pake_operation_t *client,
712 int client_input_first,
713 int round, int inject_error )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200714{
715 unsigned char *buffer0 = NULL, *buffer1 = NULL;
716 size_t buffer_length = (
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
719 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200720 /* The output should be exactly this size according to the spec */
721 const size_t expected_size_key_share =
722 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE);
723 /* The output should be exactly this size according to the spec */
724 const size_t expected_size_zk_public =
725 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC);
726 /* The output can be smaller: the spec allows stripping leading zeroes */
727 const size_t max_expected_size_zk_proof =
728 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF);
Neil Armstrongf983caf2022-06-15 15:27:48 +0200729 size_t buffer0_off = 0;
730 size_t buffer1_off = 0;
731 size_t s_g1_len, s_g2_len, s_a_len;
732 size_t s_g1_off, s_g2_off, s_a_off;
733 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
734 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
735 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
736 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
737 size_t c_g1_len, c_g2_len, c_a_len;
738 size_t c_g1_off, c_g2_off, c_a_off;
739 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
740 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
741 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
742 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
743 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200744 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200745
746 ASSERT_ALLOC( buffer0, buffer_length );
747 ASSERT_ALLOC( buffer1, buffer_length );
748
749 switch( round )
750 {
751 case 1:
752 /* Server first round Output */
753 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
754 buffer0 + buffer0_off,
755 512 - buffer0_off, &s_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200756 TEST_EQUAL( s_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200757 s_g1_off = buffer0_off;
758 buffer0_off += s_g1_len;
759 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
760 buffer0 + buffer0_off,
761 512 - buffer0_off, &s_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200762 TEST_EQUAL( s_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200763 s_x1_pk_off = buffer0_off;
764 buffer0_off += s_x1_pk_len;
765 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
766 buffer0 + buffer0_off,
767 512 - buffer0_off, &s_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200768 TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200769 s_x1_pr_off = buffer0_off;
770 buffer0_off += s_x1_pr_len;
771 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
772 buffer0 + buffer0_off,
773 512 - buffer0_off, &s_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200774 TEST_EQUAL( s_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200775 s_g2_off = buffer0_off;
776 buffer0_off += s_g2_len;
777 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
778 buffer0 + buffer0_off,
779 512 - buffer0_off, &s_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200780 TEST_EQUAL( s_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200781 s_x2_pk_off = buffer0_off;
782 buffer0_off += s_x2_pk_len;
783 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
784 buffer0 + buffer0_off,
785 512 - buffer0_off, &s_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200786 TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200787 s_x2_pr_off = buffer0_off;
788 buffer0_off += s_x2_pr_len;
789
790 if( inject_error == 1 )
791 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +0200792 buffer0[s_x1_pk_off + 8] >>= 4;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200793 buffer0[s_x2_pk_off + 7] <<= 4;
794 expected_status = PSA_ERROR_DATA_INVALID;
795 }
796
Neil Armstrong51009d72022-09-05 17:59:54 +0200797 /*
798 * When injecting errors in inputs, the implementation is
799 * free to detect it right away of with a delay.
800 * This permits delaying the error until the end of the input
801 * sequence, if no error appears then, this will be treated
802 * as an error.
803 */
804
Neil Armstrongf983caf2022-06-15 15:27:48 +0200805 if( client_input_first == 1 )
806 {
807 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200808 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
809 buffer0 + s_g1_off, s_g1_len );
810 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200811 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200812 TEST_EQUAL( status, expected_status );
813 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200814 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200815 else
816 {
817 TEST_EQUAL( status, PSA_SUCCESS );
818 }
819
820 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
821 buffer0 + s_x1_pk_off,
822 s_x1_pk_len );
823 if( inject_error == 1 && status != PSA_SUCCESS )
824 {
825 TEST_EQUAL( status, expected_status );
826 break;
827 }
828 else
829 {
830 TEST_EQUAL( status, PSA_SUCCESS );
831 }
832
833 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
834 buffer0 + s_x1_pr_off,
835 s_x1_pr_len );
836 if( inject_error == 1 && status != PSA_SUCCESS )
837 {
838 TEST_EQUAL( status, expected_status );
839 break;
840 }
841 else
842 {
843 TEST_EQUAL( status, PSA_SUCCESS );
844 }
845
846 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
847 buffer0 + s_g2_off,
848 s_g2_len );
849 if( inject_error == 1 && status != PSA_SUCCESS )
850 {
851 TEST_EQUAL( status, expected_status );
852 break;
853 }
854 else
855 {
856 TEST_EQUAL( status, PSA_SUCCESS );
857 }
858
859 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
860 buffer0 + s_x2_pk_off,
861 s_x2_pk_len );
862 if( inject_error == 1 && status != PSA_SUCCESS )
863 {
864 TEST_EQUAL( status, expected_status );
865 break;
866 }
867 else
868 {
869 TEST_EQUAL( status, PSA_SUCCESS );
870 }
871
872 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
873 buffer0 + s_x2_pr_off,
874 s_x2_pr_len );
875 if( inject_error == 1 && status != PSA_SUCCESS )
876 {
877 TEST_EQUAL( status, expected_status );
878 break;
879 }
880 else
881 {
882 TEST_EQUAL( status, PSA_SUCCESS );
883 }
884
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200885 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200886 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200887 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200888 }
889
890 /* Client first round Output */
891 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
892 buffer1 + buffer1_off,
893 512 - buffer1_off, &c_g1_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200894 TEST_EQUAL( c_g1_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200895 c_g1_off = buffer1_off;
896 buffer1_off += c_g1_len;
897 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
898 buffer1 + buffer1_off,
899 512 - buffer1_off, &c_x1_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200900 TEST_EQUAL( c_x1_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200901 c_x1_pk_off = buffer1_off;
902 buffer1_off += c_x1_pk_len;
903 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
904 buffer1 + buffer1_off,
905 512 - buffer1_off, &c_x1_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200906 TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200907 c_x1_pr_off = buffer1_off;
908 buffer1_off += c_x1_pr_len;
909 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
910 buffer1 + buffer1_off,
911 512 - buffer1_off, &c_g2_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200912 TEST_EQUAL( c_g2_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200913 c_g2_off = buffer1_off;
914 buffer1_off += c_g2_len;
915 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
916 buffer1 + buffer1_off,
917 512 - buffer1_off, &c_x2_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200918 TEST_EQUAL( c_x2_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200919 c_x2_pk_off = buffer1_off;
920 buffer1_off += c_x2_pk_len;
921 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
922 buffer1 + buffer1_off,
923 512 - buffer1_off, &c_x2_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200924 TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200925 c_x2_pr_off = buffer1_off;
926 buffer1_off += c_x2_pr_len;
927
928 if( client_input_first == 0 )
929 {
930 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200931 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
932 buffer0 + s_g1_off, s_g1_len );
933 if( inject_error == 1 && status != PSA_SUCCESS )
934 {
935 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200936 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200937 }
938 else
939 {
940 TEST_EQUAL( status, PSA_SUCCESS );
941 }
942
943 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
944 buffer0 + s_x1_pk_off,
945 s_x1_pk_len );
946 if( inject_error == 1 && status != PSA_SUCCESS )
947 {
948 TEST_EQUAL( status, expected_status );
949 break;
950 }
951 else
952 {
953 TEST_EQUAL( status, PSA_SUCCESS );
954 }
955
956 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
957 buffer0 + s_x1_pr_off,
958 s_x1_pr_len );
959 if( inject_error == 1 && status != PSA_SUCCESS )
960 {
961 TEST_EQUAL( status, expected_status );
962 break;
963 }
964 else
965 {
966 TEST_EQUAL( status, PSA_SUCCESS );
967 }
968
969 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
970 buffer0 + s_g2_off,
971 s_g2_len );
972 if( inject_error == 1 && status != PSA_SUCCESS )
973 {
974 TEST_EQUAL( status, expected_status );
975 break;
976 }
977 else
978 {
979 TEST_EQUAL( status, PSA_SUCCESS );
980 }
981
982 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
983 buffer0 + s_x2_pk_off,
984 s_x2_pk_len );
985 if( inject_error == 1 && status != PSA_SUCCESS )
986 {
987 TEST_EQUAL( status, expected_status );
988 break;
989 }
990 else
991 {
992 TEST_EQUAL( status, PSA_SUCCESS );
993 }
994
995 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
996 buffer0 + s_x2_pr_off,
997 s_x2_pr_len );
998 if( inject_error == 1 && status != PSA_SUCCESS )
999 {
1000 TEST_EQUAL( status, expected_status );
1001 break;
1002 }
1003 else
1004 {
1005 TEST_EQUAL( status, PSA_SUCCESS );
1006 }
1007
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001008 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001009 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001010 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001011 }
1012
1013 if( inject_error == 2 )
1014 {
1015 buffer1[c_x1_pk_off + 12] >>= 4;
1016 buffer1[c_x2_pk_off + 7] <<= 4;
1017 expected_status = PSA_ERROR_DATA_INVALID;
1018 }
1019
1020 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001021 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1022 buffer1 + c_g1_off, c_g1_len );
1023 if( inject_error == 2 && status != PSA_SUCCESS )
1024 {
1025 TEST_EQUAL( status, expected_status );
1026 break;
1027 }
1028 else
1029 {
1030 TEST_EQUAL( status, PSA_SUCCESS );
1031 }
1032
1033 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1034 buffer1 + c_x1_pk_off, c_x1_pk_len );
1035 if( inject_error == 2 && status != PSA_SUCCESS )
1036 {
1037 TEST_EQUAL( status, expected_status );
1038 break;
1039 }
1040 else
1041 {
1042 TEST_EQUAL( status, PSA_SUCCESS );
1043 }
1044
1045 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1046 buffer1 + c_x1_pr_off, c_x1_pr_len );
1047 if( inject_error == 2 && status != PSA_SUCCESS )
1048 {
1049 TEST_EQUAL( status, expected_status );
1050 break;
1051 }
1052 else
1053 {
1054 TEST_EQUAL( status, PSA_SUCCESS );
1055 }
1056
1057 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1058 buffer1 + c_g2_off, c_g2_len );
1059 if( inject_error == 2 && status != PSA_SUCCESS )
1060 {
1061 TEST_EQUAL( status, expected_status );
1062 break;
1063 }
1064 else
1065 {
1066 TEST_EQUAL( status, PSA_SUCCESS );
1067 }
1068
1069 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1070 buffer1 + c_x2_pk_off, c_x2_pk_len );
1071 if( inject_error == 2 && status != PSA_SUCCESS )
1072 {
1073 TEST_EQUAL( status, expected_status );
1074 break;
1075 }
1076 else
1077 {
1078 TEST_EQUAL( status, PSA_SUCCESS );
1079 }
1080
1081 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1082 buffer1 + c_x2_pr_off, c_x2_pr_len );
1083 if( inject_error == 2 && status != PSA_SUCCESS )
1084 {
1085 TEST_EQUAL( status, expected_status );
1086 break;
1087 }
1088 else
1089 {
1090 TEST_EQUAL( status, PSA_SUCCESS );
1091 }
1092
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001093 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001094 if( inject_error == 2 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001095 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001096
1097 break;
1098
1099 case 2:
1100 /* Server second round Output */
1101 buffer0_off = 0;
1102
1103 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1104 buffer0 + buffer0_off,
1105 512 - buffer0_off, &s_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001106 TEST_EQUAL( s_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001107 s_a_off = buffer0_off;
1108 buffer0_off += s_a_len;
1109 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1110 buffer0 + buffer0_off,
1111 512 - buffer0_off, &s_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001112 TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001113 s_x2s_pk_off = buffer0_off;
1114 buffer0_off += s_x2s_pk_len;
1115 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1116 buffer0 + buffer0_off,
1117 512 - buffer0_off, &s_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001118 TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001119 s_x2s_pr_off = buffer0_off;
1120 buffer0_off += s_x2s_pr_len;
1121
1122 if( inject_error == 3 )
1123 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001124 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001125 expected_status = PSA_ERROR_DATA_INVALID;
1126 }
1127
1128 if( client_input_first == 1 )
1129 {
1130 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001131 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1132 buffer0 + s_a_off, s_a_len );
1133 if( inject_error == 3 && status != PSA_SUCCESS )
1134 {
1135 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001136 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001137 }
1138 else
1139 {
1140 TEST_EQUAL( status, PSA_SUCCESS );
1141 }
1142
1143 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1144 buffer0 + s_x2s_pk_off,
1145 s_x2s_pk_len );
1146 if( inject_error == 3 && status != PSA_SUCCESS )
1147 {
1148 TEST_EQUAL( status, expected_status );
1149 break;
1150 }
1151 else
1152 {
1153 TEST_EQUAL( status, PSA_SUCCESS );
1154 }
1155
1156 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1157 buffer0 + s_x2s_pr_off,
1158 s_x2s_pr_len );
1159 if( inject_error == 3 && status != PSA_SUCCESS )
1160 {
1161 TEST_EQUAL( status, expected_status );
1162 break;
1163 }
1164 else
1165 {
1166 TEST_EQUAL( status, PSA_SUCCESS );
1167 }
1168
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001169 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001170 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001171 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001172 }
1173
1174 /* Client second round Output */
1175 buffer1_off = 0;
1176
1177 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1178 buffer1 + buffer1_off,
1179 512 - buffer1_off, &c_a_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001180 TEST_EQUAL( c_a_len, expected_size_key_share );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001181 c_a_off = buffer1_off;
1182 buffer1_off += c_a_len;
1183 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1184 buffer1 + buffer1_off,
1185 512 - buffer1_off, &c_x2s_pk_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001186 TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001187 c_x2s_pk_off = buffer1_off;
1188 buffer1_off += c_x2s_pk_len;
1189 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1190 buffer1 + buffer1_off,
1191 512 - buffer1_off, &c_x2s_pr_len ) );
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02001192 TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001193 c_x2s_pr_off = buffer1_off;
1194 buffer1_off += c_x2s_pr_len;
1195
1196 if( client_input_first == 0 )
1197 {
1198 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001199 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1200 buffer0 + s_a_off, s_a_len );
1201 if( inject_error == 3 && status != PSA_SUCCESS )
1202 {
1203 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001204 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001205 }
1206 else
1207 {
1208 TEST_EQUAL( status, PSA_SUCCESS );
1209 }
1210
1211 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1212 buffer0 + s_x2s_pk_off,
1213 s_x2s_pk_len );
1214 if( inject_error == 3 && status != PSA_SUCCESS )
1215 {
1216 TEST_EQUAL( status, expected_status );
1217 break;
1218 }
1219 else
1220 {
1221 TEST_EQUAL( status, PSA_SUCCESS );
1222 }
1223
1224 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1225 buffer0 + s_x2s_pr_off,
1226 s_x2s_pr_len );
1227 if( inject_error == 3 && status != PSA_SUCCESS )
1228 {
1229 TEST_EQUAL( status, expected_status );
1230 break;
1231 }
1232 else
1233 {
1234 TEST_EQUAL( status, PSA_SUCCESS );
1235 }
1236
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001237 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001238 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001239 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001240 }
1241
1242 if( inject_error == 4 )
1243 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001244 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001245 expected_status = PSA_ERROR_DATA_INVALID;
1246 }
1247
1248 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001249 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1250 buffer1 + c_a_off, c_a_len );
1251 if( inject_error == 4 && status != PSA_SUCCESS )
1252 {
1253 TEST_EQUAL( status, expected_status );
1254 break;
1255 }
1256 else
1257 {
1258 TEST_EQUAL( status, PSA_SUCCESS );
1259 }
1260
1261 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1262 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1263 if( inject_error == 4 && status != PSA_SUCCESS )
1264 {
1265 TEST_EQUAL( status, expected_status );
1266 break;
1267 }
1268 else
1269 {
1270 TEST_EQUAL( status, PSA_SUCCESS );
1271 }
1272
1273 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1274 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1275 if( inject_error == 4 && status != PSA_SUCCESS )
1276 {
1277 TEST_EQUAL( status, expected_status );
1278 break;
1279 }
1280 else
1281 {
1282 TEST_EQUAL( status, PSA_SUCCESS );
1283 }
1284
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001285 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001286 if( inject_error == 4 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001287 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001288
1289 break;
1290
1291 }
1292
Neil Armstrongf983caf2022-06-15 15:27:48 +02001293exit:
1294 mbedtls_free( buffer0 );
1295 mbedtls_free( buffer1 );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001296}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001297#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001298
Gilles Peskinee59236f2018-01-27 23:32:46 +01001299/* END_HEADER */
1300
1301/* BEGIN_DEPENDENCIES
1302 * depends_on:MBEDTLS_PSA_CRYPTO_C
1303 * END_DEPENDENCIES
1304 */
1305
1306/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001307void static_checks( )
1308{
1309 size_t max_truncated_mac_size =
1310 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1311
1312 /* Check that the length for a truncated MAC always fits in the algorithm
1313 * encoding. The shifted mask is the maximum truncated value. The
1314 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001315 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001316}
1317/* END_CASE */
1318
1319/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001320void import_with_policy( int type_arg,
1321 int usage_arg, int alg_arg,
1322 int expected_status_arg )
1323{
1324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1325 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001326 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001327 psa_key_type_t type = type_arg;
1328 psa_key_usage_t usage = usage_arg;
1329 psa_algorithm_t alg = alg_arg;
1330 psa_status_t expected_status = expected_status_arg;
1331 const uint8_t key_material[16] = {0};
1332 psa_status_t status;
1333
1334 PSA_ASSERT( psa_crypto_init( ) );
1335
1336 psa_set_key_type( &attributes, type );
1337 psa_set_key_usage_flags( &attributes, usage );
1338 psa_set_key_algorithm( &attributes, alg );
1339
1340 status = psa_import_key( &attributes,
1341 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001342 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001343 TEST_EQUAL( status, expected_status );
1344 if( status != PSA_SUCCESS )
1345 goto exit;
1346
Ronald Cron5425a212020-08-04 14:58:35 +02001347 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001348 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001349 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001350 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001351 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001352 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001353
Ronald Cron5425a212020-08-04 14:58:35 +02001354 PSA_ASSERT( psa_destroy_key( key ) );
1355 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001356
1357exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001358 /*
1359 * Key attributes may have been returned by psa_get_key_attributes()
1360 * thus reset them as required.
1361 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001362 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001363
1364 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001365 PSA_DONE( );
1366}
1367/* END_CASE */
1368
1369/* BEGIN_CASE */
1370void import_with_data( data_t *data, int type_arg,
1371 int attr_bits_arg,
1372 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001373{
1374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1375 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001376 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001377 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001378 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001379 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001380 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001381
Gilles Peskine8817f612018-12-18 00:18:46 +01001382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001383
Gilles Peskine4747d192019-04-17 15:05:45 +02001384 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001385 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001386
Ronald Cron5425a212020-08-04 14:58:35 +02001387 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001388 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001389 if( status != PSA_SUCCESS )
1390 goto exit;
1391
Ronald Cron5425a212020-08-04 14:58:35 +02001392 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001393 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001394 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001395 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001396 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001397
Ronald Cron5425a212020-08-04 14:58:35 +02001398 PSA_ASSERT( psa_destroy_key( key ) );
1399 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001400
1401exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001402 /*
1403 * Key attributes may have been returned by psa_get_key_attributes()
1404 * thus reset them as required.
1405 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001406 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001407
1408 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001409 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001414void import_large_key( int type_arg, int byte_size_arg,
1415 int expected_status_arg )
1416{
1417 psa_key_type_t type = type_arg;
1418 size_t byte_size = byte_size_arg;
1419 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1420 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001422 psa_status_t status;
1423 uint8_t *buffer = NULL;
1424 size_t buffer_size = byte_size + 1;
1425 size_t n;
1426
Steven Cooreman69967ce2021-01-18 18:01:08 +01001427 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001428 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001429 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001430 memset( buffer, 'K', byte_size );
1431
1432 PSA_ASSERT( psa_crypto_init( ) );
1433
1434 /* Try importing the key */
1435 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1436 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001437 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001438 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001439 TEST_EQUAL( status, expected_status );
1440
1441 if( status == PSA_SUCCESS )
1442 {
Ronald Cron5425a212020-08-04 14:58:35 +02001443 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001444 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1445 TEST_EQUAL( psa_get_key_bits( &attributes ),
1446 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001447 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001448 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001449 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001450 for( n = 0; n < byte_size; n++ )
1451 TEST_EQUAL( buffer[n], 'K' );
1452 for( n = byte_size; n < buffer_size; n++ )
1453 TEST_EQUAL( buffer[n], 0 );
1454 }
1455
1456exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001457 /*
1458 * Key attributes may have been returned by psa_get_key_attributes()
1459 * thus reset them as required.
1460 */
1461 psa_reset_key_attributes( &attributes );
1462
Ronald Cron5425a212020-08-04 14:58:35 +02001463 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001464 PSA_DONE( );
1465 mbedtls_free( buffer );
1466}
1467/* END_CASE */
1468
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001469/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001470void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1471{
Ronald Cron5425a212020-08-04 14:58:35 +02001472 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001473 size_t bits = bits_arg;
1474 psa_status_t expected_status = expected_status_arg;
1475 psa_status_t status;
1476 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001477 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001478 size_t buffer_size = /* Slight overapproximations */
1479 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001480 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001481 unsigned char *p;
1482 int ret;
1483 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001485
Gilles Peskine8817f612018-12-18 00:18:46 +01001486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001487 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001488
1489 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1490 bits, keypair ) ) >= 0 );
1491 length = ret;
1492
1493 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001494 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001495 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001496 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001497
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001498 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001499 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001500
1501exit:
1502 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001503 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001504}
1505/* END_CASE */
1506
1507/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001508void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001509 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001510 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301511 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001512 int expected_bits,
1513 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001514 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001515 int canonical_input )
1516{
Ronald Cron5425a212020-08-04 14:58:35 +02001517 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001518 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001519 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001520 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001521 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301522 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001523 unsigned char *exported = NULL;
1524 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001525 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001526 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001529 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001530
Moran Pekercb088e72018-07-17 17:36:59 +03001531 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001532 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001533 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001534 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001535 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536
Archana4d7ae1d2021-07-07 02:50:22 +05301537 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001538 psa_set_key_usage_flags( &attributes, usage_arg );
1539 psa_set_key_algorithm( &attributes, alg );
1540 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001541
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001542 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001543 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001544
1545 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001546 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001547 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1548 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001549 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550
1551 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001552 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001553 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001554
1555 /* The exported length must be set by psa_export_key() to a value between 0
1556 * and export_size. On errors, the exported length must be 0. */
1557 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1558 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001559 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001560
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001561 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001562 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001563 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001564 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001565 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001567 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001568
Gilles Peskineea38a922021-02-13 00:05:16 +01001569 /* Run sanity checks on the exported key. For non-canonical inputs,
1570 * this validates the canonical representations. For canonical inputs,
1571 * this doesn't directly validate the implementation, but it still helps
1572 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301573 if( !psa_key_lifetime_is_external( lifetime ) )
1574 {
1575 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1576 goto exit;
1577 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001578
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001579 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001580 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001581 else
1582 {
Ronald Cron5425a212020-08-04 14:58:35 +02001583 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001584 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001585 &key2 ) );
1586 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001587 reexported,
1588 export_size,
1589 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001590 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301591 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001592 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001593 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001594 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301595 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301596 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001597 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598
1599destroy:
1600 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001601 PSA_ASSERT( psa_destroy_key( key ) );
1602 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001603
1604exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001605 /*
1606 * Key attributes may have been returned by psa_get_key_attributes()
1607 * thus reset them as required.
1608 */
1609 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301610 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001611 mbedtls_free( exported );
1612 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001613 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001614}
1615/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001616
Moran Pekerf709f4a2018-06-06 17:26:04 +03001617/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001618void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001619 int type_arg,
1620 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301621 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001622 int export_size_delta,
1623 int expected_export_status_arg,
1624 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001625{
Ronald Cron5425a212020-08-04 14:58:35 +02001626 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001627 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001628 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001629 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001630 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301631 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001632 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001633 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001634 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001636
Gilles Peskine8817f612018-12-18 00:18:46 +01001637 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001638
Archana4d7ae1d2021-07-07 02:50:22 +05301639 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001640 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1641 psa_set_key_algorithm( &attributes, alg );
1642 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001643
1644 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001645 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001646
Gilles Peskine49c25912018-10-29 15:15:31 +01001647 /* Export the public key */
1648 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001649 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001650 exported, export_size,
1651 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001652 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001653 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001654 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001655 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001656 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001657 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001658 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001659 TEST_LE_U( expected_public_key->len,
1660 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1661 TEST_LE_U( expected_public_key->len,
1662 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1663 TEST_LE_U( expected_public_key->len,
1664 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001665 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1666 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001667 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001668exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001669 /*
1670 * Key attributes may have been returned by psa_get_key_attributes()
1671 * thus reset them as required.
1672 */
1673 psa_reset_key_attributes( &attributes );
1674
itayzafrir3e02b3b2018-06-12 17:06:52 +03001675 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001676 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001677 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001678}
1679/* END_CASE */
1680
Gilles Peskine20035e32018-02-03 22:44:14 +01001681/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001682void import_and_exercise_key( data_t *data,
1683 int type_arg,
1684 int bits_arg,
1685 int alg_arg )
1686{
Ronald Cron5425a212020-08-04 14:58:35 +02001687 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001688 psa_key_type_t type = type_arg;
1689 size_t bits = bits_arg;
1690 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001691 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001693 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001694
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001696
Gilles Peskine4747d192019-04-17 15:05:45 +02001697 psa_set_key_usage_flags( &attributes, usage );
1698 psa_set_key_algorithm( &attributes, alg );
1699 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001700
1701 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001702 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001703
1704 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001705 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001706 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1707 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001708
1709 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001710 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001711 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001712
Ronald Cron5425a212020-08-04 14:58:35 +02001713 PSA_ASSERT( psa_destroy_key( key ) );
1714 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001715
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001716exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001717 /*
1718 * Key attributes may have been returned by psa_get_key_attributes()
1719 * thus reset them as required.
1720 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001721 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001722
1723 psa_reset_key_attributes( &attributes );
1724 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001725 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001726}
1727/* END_CASE */
1728
1729/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001730void effective_key_attributes( int type_arg, int expected_type_arg,
1731 int bits_arg, int expected_bits_arg,
1732 int usage_arg, int expected_usage_arg,
1733 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001734{
Ronald Cron5425a212020-08-04 14:58:35 +02001735 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001736 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001737 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001738 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001739 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001740 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001741 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001742 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001743 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001745
Gilles Peskine8817f612018-12-18 00:18:46 +01001746 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001747
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001748 psa_set_key_usage_flags( &attributes, usage );
1749 psa_set_key_algorithm( &attributes, alg );
1750 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001751 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001752
Ronald Cron5425a212020-08-04 14:58:35 +02001753 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001754 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001755
Ronald Cron5425a212020-08-04 14:58:35 +02001756 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001757 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1758 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1759 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1760 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001761
1762exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001763 /*
1764 * Key attributes may have been returned by psa_get_key_attributes()
1765 * thus reset them as required.
1766 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001767 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001768
1769 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001770 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001771}
1772/* END_CASE */
1773
1774/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001775void check_key_policy( int type_arg, int bits_arg,
1776 int usage_arg, int alg_arg )
1777{
1778 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001779 usage_arg,
1780 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001781 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001782 goto exit;
1783}
1784/* END_CASE */
1785
1786/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001787void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001788{
1789 /* Test each valid way of initializing the object, except for `= {0}`, as
1790 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1791 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001792 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001793 psa_key_attributes_t func = psa_key_attributes_init( );
1794 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1795 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001796
1797 memset( &zero, 0, sizeof( zero ) );
1798
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001799 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1800 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1801 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001802
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001803 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1804 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1805 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1806
1807 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1808 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1809 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1810
1811 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1812 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1813 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1814
1815 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1816 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1817 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001818}
1819/* END_CASE */
1820
1821/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001822void mac_key_policy( int policy_usage_arg,
1823 int policy_alg_arg,
1824 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001825 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001826 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001827 int expected_status_sign_arg,
1828 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001829{
Ronald Cron5425a212020-08-04 14:58:35 +02001830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001832 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001833 psa_key_type_t key_type = key_type_arg;
1834 psa_algorithm_t policy_alg = policy_alg_arg;
1835 psa_algorithm_t exercise_alg = exercise_alg_arg;
1836 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001838 psa_status_t expected_status_sign = expected_status_sign_arg;
1839 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001841
Gilles Peskine8817f612018-12-18 00:18:46 +01001842 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001843
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001844 psa_set_key_usage_flags( &attributes, policy_usage );
1845 psa_set_key_algorithm( &attributes, policy_alg );
1846 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001847
Gilles Peskine049c7532019-05-15 20:22:09 +02001848 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001849 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001850
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001851 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1852 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001853
Ronald Cron5425a212020-08-04 14:58:35 +02001854 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001855 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001856
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001857 /* Calculate the MAC, one-shot case. */
1858 uint8_t input[128] = {0};
1859 size_t mac_len;
1860 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1861 input, 128,
1862 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1863 expected_status_sign );
1864
Neil Armstrong3af9b972022-02-07 12:20:21 +01001865 /* Calculate the MAC, multi-part case. */
1866 PSA_ASSERT( psa_mac_abort( &operation ) );
1867 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1868 if( status == PSA_SUCCESS )
1869 {
1870 status = psa_mac_update( &operation, input, 128 );
1871 if( status == PSA_SUCCESS )
1872 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1873 &mac_len ),
1874 expected_status_sign );
1875 else
1876 TEST_EQUAL( status, expected_status_sign );
1877 }
1878 else
1879 {
1880 TEST_EQUAL( status, expected_status_sign );
1881 }
1882 PSA_ASSERT( psa_mac_abort( &operation ) );
1883
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001884 /* Verify correct MAC, one-shot case. */
1885 status = psa_mac_verify( key, exercise_alg, input, 128,
1886 mac, mac_len );
1887
1888 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1889 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001890 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001891 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001892
Neil Armstrong3af9b972022-02-07 12:20:21 +01001893 /* Verify correct MAC, multi-part case. */
1894 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1895 if( status == PSA_SUCCESS )
1896 {
1897 status = psa_mac_update( &operation, input, 128 );
1898 if( status == PSA_SUCCESS )
1899 {
1900 status = psa_mac_verify_finish( &operation, mac, mac_len );
1901 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1902 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1903 else
1904 TEST_EQUAL( status, expected_status_verify );
1905 }
1906 else
1907 {
1908 TEST_EQUAL( status, expected_status_verify );
1909 }
1910 }
1911 else
1912 {
1913 TEST_EQUAL( status, expected_status_verify );
1914 }
1915
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001916 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001917
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001919 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001920 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921
1922exit:
1923 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001924 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001925 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001926}
1927/* END_CASE */
1928
1929/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001930void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931 int policy_alg,
1932 int key_type,
1933 data_t *key_data,
1934 int exercise_alg )
1935{
Ronald Cron5425a212020-08-04 14:58:35 +02001936 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001938 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001939 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001940 size_t output_buffer_size = 0;
1941 size_t input_buffer_size = 0;
1942 size_t output_length = 0;
1943 uint8_t *output = NULL;
1944 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001945 psa_status_t status;
1946
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001947 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1948 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1949 input_buffer_size );
1950
1951 ASSERT_ALLOC( input, input_buffer_size );
1952 ASSERT_ALLOC( output, output_buffer_size );
1953
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001956 psa_set_key_usage_flags( &attributes, policy_usage );
1957 psa_set_key_algorithm( &attributes, policy_alg );
1958 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959
Gilles Peskine049c7532019-05-15 20:22:09 +02001960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001961 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001963 /* Check if no key usage flag implication is done */
1964 TEST_EQUAL( policy_usage,
1965 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001966
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001967 /* Encrypt check, one-shot */
1968 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1969 output, output_buffer_size,
1970 &output_length);
1971 if( policy_alg == exercise_alg &&
1972 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1973 PSA_ASSERT( status );
1974 else
1975 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1976
1977 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001978 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001979 if( policy_alg == exercise_alg &&
1980 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001981 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001982 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001983 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 psa_cipher_abort( &operation );
1985
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001986 /* Decrypt check, one-shot */
1987 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1988 input, input_buffer_size,
1989 &output_length);
1990 if( policy_alg == exercise_alg &&
1991 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1992 PSA_ASSERT( status );
1993 else
1994 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1995
1996 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001997 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998 if( policy_alg == exercise_alg &&
1999 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002000 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002001 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002002 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003
2004exit:
2005 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01002006 mbedtls_free( input );
2007 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002008 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002009 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010}
2011/* END_CASE */
2012
2013/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002014void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015 int policy_alg,
2016 int key_type,
2017 data_t *key_data,
2018 int nonce_length_arg,
2019 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002020 int exercise_alg,
2021 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022{
Ronald Cron5425a212020-08-04 14:58:35 +02002023 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002024 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002025 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002026 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002027 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002028 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029 unsigned char nonce[16] = {0};
2030 size_t nonce_length = nonce_length_arg;
2031 unsigned char tag[16];
2032 size_t tag_length = tag_length_arg;
2033 size_t output_length;
2034
Gilles Peskine7be11a72022-04-14 00:12:57 +02002035 TEST_LE_U( nonce_length, sizeof( nonce ) );
2036 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037
Gilles Peskine8817f612018-12-18 00:18:46 +01002038 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002040 psa_set_key_usage_flags( &attributes, policy_usage );
2041 psa_set_key_algorithm( &attributes, policy_alg );
2042 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002043
Gilles Peskine049c7532019-05-15 20:22:09 +02002044 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002045 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002047 /* Check if no key usage implication is done */
2048 TEST_EQUAL( policy_usage,
2049 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002050
Neil Armstrong752d8112022-02-07 14:51:11 +01002051 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002052 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053 nonce, nonce_length,
2054 NULL, 0,
2055 NULL, 0,
2056 tag, tag_length,
2057 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002058 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2059 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002060 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002061 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002062
Neil Armstrong752d8112022-02-07 14:51:11 +01002063 /* Encrypt check, multi-part */
2064 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2065 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2066 TEST_EQUAL( status, expected_status );
2067 else
2068 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2069
2070 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002071 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002072 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002073 nonce, nonce_length,
2074 NULL, 0,
2075 tag, tag_length,
2076 NULL, 0,
2077 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002078 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2079 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2080 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002081 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002082 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002083 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002084
Neil Armstrong752d8112022-02-07 14:51:11 +01002085 /* Decrypt check, multi-part */
2086 PSA_ASSERT( psa_aead_abort( &operation ) );
2087 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2088 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2089 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2090 else
2091 TEST_EQUAL( status, expected_status );
2092
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002094 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002095 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002096 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097}
2098/* END_CASE */
2099
2100/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002101void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002102 int policy_alg,
2103 int key_type,
2104 data_t *key_data,
2105 int exercise_alg )
2106{
Ronald Cron5425a212020-08-04 14:58:35 +02002107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002108 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002109 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002110 psa_status_t status;
2111 size_t key_bits;
2112 size_t buffer_length;
2113 unsigned char *buffer = NULL;
2114 size_t output_length;
2115
Gilles Peskine8817f612018-12-18 00:18:46 +01002116 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002117
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002118 psa_set_key_usage_flags( &attributes, policy_usage );
2119 psa_set_key_algorithm( &attributes, policy_alg );
2120 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002121
Gilles Peskine049c7532019-05-15 20:22:09 +02002122 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002123 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002125 /* Check if no key usage implication is done */
2126 TEST_EQUAL( policy_usage,
2127 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002128
Ronald Cron5425a212020-08-04 14:58:35 +02002129 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002130 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002131 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2132 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002133 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002134
Ronald Cron5425a212020-08-04 14:58:35 +02002135 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002136 NULL, 0,
2137 NULL, 0,
2138 buffer, buffer_length,
2139 &output_length );
2140 if( policy_alg == exercise_alg &&
2141 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002142 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002144 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002146 if( buffer_length != 0 )
2147 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002148 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002149 buffer, buffer_length,
2150 NULL, 0,
2151 buffer, buffer_length,
2152 &output_length );
2153 if( policy_alg == exercise_alg &&
2154 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002155 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002156 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002157 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158
2159exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002160 /*
2161 * Key attributes may have been returned by psa_get_key_attributes()
2162 * thus reset them as required.
2163 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002164 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002165
2166 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002167 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002168 mbedtls_free( buffer );
2169}
2170/* END_CASE */
2171
2172/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002173void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002174 int policy_alg,
2175 int key_type,
2176 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002177 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002178 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002179 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002180{
Ronald Cron5425a212020-08-04 14:58:35 +02002181 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002183 psa_key_usage_t policy_usage = policy_usage_arg;
2184 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002185 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002186 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2187 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2188 * compatible with the policy and `payload_length_arg` is supposed to be
2189 * a valid input length to sign. If `payload_length_arg <= 0`,
2190 * `exercise_alg` is supposed to be forbidden by the policy. */
2191 int compatible_alg = payload_length_arg > 0;
2192 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002193 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002194 size_t signature_length;
2195
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002196 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002197 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002198 TEST_EQUAL( expected_usage,
2199 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002200
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002202
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002203 psa_set_key_usage_flags( &attributes, policy_usage );
2204 psa_set_key_algorithm( &attributes, policy_alg );
2205 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002206
Gilles Peskine049c7532019-05-15 20:22:09 +02002207 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002208 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002209
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002210 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2211
Ronald Cron5425a212020-08-04 14:58:35 +02002212 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002213 payload, payload_length,
2214 signature, sizeof( signature ),
2215 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002216 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002217 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002218 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002219 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002220
2221 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002222 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002223 payload, payload_length,
2224 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002225 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002226 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002227 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002228 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002229
Gilles Peskinef7b41372021-09-22 16:15:05 +02002230 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002231 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002232 {
2233 status = psa_sign_message( key, exercise_alg,
2234 payload, payload_length,
2235 signature, sizeof( signature ),
2236 &signature_length );
2237 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2238 PSA_ASSERT( status );
2239 else
2240 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2241
2242 memset( signature, 0, sizeof( signature ) );
2243 status = psa_verify_message( key, exercise_alg,
2244 payload, payload_length,
2245 signature, sizeof( signature ) );
2246 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2247 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2248 else
2249 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2250 }
2251
Gilles Peskined5b33222018-06-18 22:20:03 +02002252exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002253 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002254 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002255}
2256/* END_CASE */
2257
Janos Follathba3fab92019-06-11 14:50:16 +01002258/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002259void derive_key_policy( int policy_usage,
2260 int policy_alg,
2261 int key_type,
2262 data_t *key_data,
2263 int exercise_alg )
2264{
Ronald Cron5425a212020-08-04 14:58:35 +02002265 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002267 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268 psa_status_t status;
2269
Gilles Peskine8817f612018-12-18 00:18:46 +01002270 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002271
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002272 psa_set_key_usage_flags( &attributes, policy_usage );
2273 psa_set_key_algorithm( &attributes, policy_alg );
2274 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002275
Gilles Peskine049c7532019-05-15 20:22:09 +02002276 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002277 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002278
Janos Follathba3fab92019-06-11 14:50:16 +01002279 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2280
2281 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2282 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002283 {
Janos Follathba3fab92019-06-11 14:50:16 +01002284 PSA_ASSERT( psa_key_derivation_input_bytes(
2285 &operation,
2286 PSA_KEY_DERIVATION_INPUT_SEED,
2287 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002288 }
Janos Follathba3fab92019-06-11 14:50:16 +01002289
2290 status = psa_key_derivation_input_key( &operation,
2291 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002292 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002293
Gilles Peskineea0fb492018-07-12 17:17:20 +02002294 if( policy_alg == exercise_alg &&
2295 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002296 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002297 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002298 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002299
2300exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002301 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002302 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002303 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002304}
2305/* END_CASE */
2306
2307/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002308void agreement_key_policy( int policy_usage,
2309 int policy_alg,
2310 int key_type_arg,
2311 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002312 int exercise_alg,
2313 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002314{
Ronald Cron5425a212020-08-04 14:58:35 +02002315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002317 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002318 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002319 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002320 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002321
Gilles Peskine8817f612018-12-18 00:18:46 +01002322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002323
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002324 psa_set_key_usage_flags( &attributes, policy_usage );
2325 psa_set_key_algorithm( &attributes, policy_alg );
2326 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002327
Gilles Peskine049c7532019-05-15 20:22:09 +02002328 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002329 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002330
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002331 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002332 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002333
Steven Cooremance48e852020-10-05 16:02:45 +02002334 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002335
2336exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002337 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002338 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002339 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002340}
2341/* END_CASE */
2342
2343/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002344void key_policy_alg2( int key_type_arg, data_t *key_data,
2345 int usage_arg, int alg_arg, int alg2_arg )
2346{
Ronald Cron5425a212020-08-04 14:58:35 +02002347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002348 psa_key_type_t key_type = key_type_arg;
2349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2350 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2351 psa_key_usage_t usage = usage_arg;
2352 psa_algorithm_t alg = alg_arg;
2353 psa_algorithm_t alg2 = alg2_arg;
2354
2355 PSA_ASSERT( psa_crypto_init( ) );
2356
2357 psa_set_key_usage_flags( &attributes, usage );
2358 psa_set_key_algorithm( &attributes, alg );
2359 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2360 psa_set_key_type( &attributes, key_type );
2361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002362 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002363
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002364 /* Update the usage flags to obtain implicit usage flags */
2365 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002366 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002367 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2368 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2369 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2370
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002371 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002372 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002373 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002374 goto exit;
2375
2376exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002377 /*
2378 * Key attributes may have been returned by psa_get_key_attributes()
2379 * thus reset them as required.
2380 */
2381 psa_reset_key_attributes( &got_attributes );
2382
Ronald Cron5425a212020-08-04 14:58:35 +02002383 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002384 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002385}
2386/* END_CASE */
2387
2388/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002389void raw_agreement_key_policy( int policy_usage,
2390 int policy_alg,
2391 int key_type_arg,
2392 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002393 int exercise_alg,
2394 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002395{
Ronald Cron5425a212020-08-04 14:58:35 +02002396 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002398 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002399 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002400 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002401 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002402
2403 PSA_ASSERT( psa_crypto_init( ) );
2404
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002405 psa_set_key_usage_flags( &attributes, policy_usage );
2406 psa_set_key_algorithm( &attributes, policy_alg );
2407 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002408
Gilles Peskine049c7532019-05-15 20:22:09 +02002409 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002410 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002411
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002412 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002413
Steven Cooremance48e852020-10-05 16:02:45 +02002414 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002415
2416exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002417 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002418 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002419 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002420}
2421/* END_CASE */
2422
2423/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002424void copy_success( int source_usage_arg,
2425 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302426 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002427 int type_arg, data_t *material,
2428 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002429 int target_usage_arg,
2430 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302431 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002432 int expected_usage_arg,
2433 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002434{
Gilles Peskineca25db92019-04-19 11:43:08 +02002435 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2436 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002437 psa_key_usage_t expected_usage = expected_usage_arg;
2438 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002439 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302440 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2441 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002442 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2443 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002444 uint8_t *export_buffer = NULL;
2445
Gilles Peskine57ab7212019-01-28 13:03:09 +01002446 PSA_ASSERT( psa_crypto_init( ) );
2447
Gilles Peskineca25db92019-04-19 11:43:08 +02002448 /* Prepare the source key. */
2449 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2450 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002451 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002452 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302453 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002454 PSA_ASSERT( psa_import_key( &source_attributes,
2455 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002456 &source_key ) );
2457 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002458
Gilles Peskineca25db92019-04-19 11:43:08 +02002459 /* Prepare the target attributes. */
2460 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002461 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002462 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002463 }
Archana8a180362021-07-05 02:18:48 +05302464 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002465
Gilles Peskineca25db92019-04-19 11:43:08 +02002466 if( target_usage_arg != -1 )
2467 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2468 if( target_alg_arg != -1 )
2469 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002470 if( target_alg2_arg != -1 )
2471 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002472
Archana8a180362021-07-05 02:18:48 +05302473
Gilles Peskine57ab7212019-01-28 13:03:09 +01002474 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002475 PSA_ASSERT( psa_copy_key( source_key,
2476 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002477
2478 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002479 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002480
2481 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002482 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002483 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2484 psa_get_key_type( &target_attributes ) );
2485 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2486 psa_get_key_bits( &target_attributes ) );
2487 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2488 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002489 TEST_EQUAL( expected_alg2,
2490 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002491 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2492 {
2493 size_t length;
2494 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002495 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002496 material->len, &length ) );
2497 ASSERT_COMPARE( material->x, material->len,
2498 export_buffer, length );
2499 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002500
Archana8a180362021-07-05 02:18:48 +05302501 if( !psa_key_lifetime_is_external( target_lifetime ) )
2502 {
2503 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2504 goto exit;
2505 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2506 goto exit;
2507 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002508
Ronald Cron5425a212020-08-04 14:58:35 +02002509 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002510
2511exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002512 /*
2513 * Source and target key attributes may have been returned by
2514 * psa_get_key_attributes() thus reset them as required.
2515 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002516 psa_reset_key_attributes( &source_attributes );
2517 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002518
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002519 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002520 mbedtls_free( export_buffer );
2521}
2522/* END_CASE */
2523
2524/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002525void copy_fail( int source_usage_arg,
2526 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302527 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002528 int type_arg, data_t *material,
2529 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002530 int target_usage_arg,
2531 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002532 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002533 int expected_status_arg )
2534{
2535 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2536 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002537 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2538 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002539 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002540
2541 PSA_ASSERT( psa_crypto_init( ) );
2542
2543 /* Prepare the source key. */
2544 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2545 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002546 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002547 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302548 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002549 PSA_ASSERT( psa_import_key( &source_attributes,
2550 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002551 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002552
2553 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002554 psa_set_key_id( &target_attributes, key_id );
2555 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002556 psa_set_key_type( &target_attributes, target_type_arg );
2557 psa_set_key_bits( &target_attributes, target_bits_arg );
2558 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2559 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002560 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002561
2562 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002563 TEST_EQUAL( psa_copy_key( source_key,
2564 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002565 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002566
Ronald Cron5425a212020-08-04 14:58:35 +02002567 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002568
Gilles Peskine4a644642019-05-03 17:14:08 +02002569exit:
2570 psa_reset_key_attributes( &source_attributes );
2571 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002572 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002573}
2574/* END_CASE */
2575
2576/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002577void hash_operation_init( )
2578{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002579 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002580 /* Test each valid way of initializing the object, except for `= {0}`, as
2581 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2582 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002583 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002584 psa_hash_operation_t func = psa_hash_operation_init( );
2585 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2586 psa_hash_operation_t zero;
2587
2588 memset( &zero, 0, sizeof( zero ) );
2589
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002590 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002591 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2592 PSA_ERROR_BAD_STATE );
2593 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2594 PSA_ERROR_BAD_STATE );
2595 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2596 PSA_ERROR_BAD_STATE );
2597
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002598 /* A default hash operation should be abortable without error. */
2599 PSA_ASSERT( psa_hash_abort( &func ) );
2600 PSA_ASSERT( psa_hash_abort( &init ) );
2601 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002602}
2603/* END_CASE */
2604
2605/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002606void hash_setup( int alg_arg,
2607 int expected_status_arg )
2608{
2609 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002610 uint8_t *output = NULL;
2611 size_t output_size = 0;
2612 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002613 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002614 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002615 psa_status_t status;
2616
Gilles Peskine8817f612018-12-18 00:18:46 +01002617 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002618
Neil Armstrongedb20862022-02-07 15:47:44 +01002619 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002620 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002621 ASSERT_ALLOC( output, output_size );
2622
2623 status = psa_hash_compute( alg, NULL, 0,
2624 output, output_size, &output_length );
2625 TEST_EQUAL( status, expected_status );
2626
2627 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002628 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002629 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002630
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002631 /* Whether setup succeeded or failed, abort must succeed. */
2632 PSA_ASSERT( psa_hash_abort( &operation ) );
2633
2634 /* If setup failed, reproduce the failure, so as to
2635 * test the resulting state of the operation object. */
2636 if( status != PSA_SUCCESS )
2637 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2638
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002639 /* Now the operation object should be reusable. */
2640#if defined(KNOWN_SUPPORTED_HASH_ALG)
2641 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2642 PSA_ASSERT( psa_hash_abort( &operation ) );
2643#endif
2644
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002645exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002646 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002647 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002648}
2649/* END_CASE */
2650
2651/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002652void hash_compute_fail( int alg_arg, data_t *input,
2653 int output_size_arg, int expected_status_arg )
2654{
2655 psa_algorithm_t alg = alg_arg;
2656 uint8_t *output = NULL;
2657 size_t output_size = output_size_arg;
2658 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002659 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002660 psa_status_t expected_status = expected_status_arg;
2661 psa_status_t status;
2662
2663 ASSERT_ALLOC( output, output_size );
2664
2665 PSA_ASSERT( psa_crypto_init( ) );
2666
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002667 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002668 status = psa_hash_compute( alg, input->x, input->len,
2669 output, output_size, &output_length );
2670 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002671 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002672
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002673 /* Hash Compute, multi-part */
2674 status = psa_hash_setup( &operation, alg );
2675 if( status == PSA_SUCCESS )
2676 {
2677 status = psa_hash_update( &operation, input->x, input->len );
2678 if( status == PSA_SUCCESS )
2679 {
2680 status = psa_hash_finish( &operation, output, output_size,
2681 &output_length );
2682 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002683 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002684 else
2685 TEST_EQUAL( status, expected_status );
2686 }
2687 else
2688 {
2689 TEST_EQUAL( status, expected_status );
2690 }
2691 }
2692 else
2693 {
2694 TEST_EQUAL( status, expected_status );
2695 }
2696
Gilles Peskine0a749c82019-11-28 19:33:58 +01002697exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002698 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002699 mbedtls_free( output );
2700 PSA_DONE( );
2701}
2702/* END_CASE */
2703
2704/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002705void hash_compare_fail( int alg_arg, data_t *input,
2706 data_t *reference_hash,
2707 int expected_status_arg )
2708{
2709 psa_algorithm_t alg = alg_arg;
2710 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002711 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002712 psa_status_t status;
2713
2714 PSA_ASSERT( psa_crypto_init( ) );
2715
Neil Armstrong55a1be12022-02-07 11:23:20 +01002716 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002717 status = psa_hash_compare( alg, input->x, input->len,
2718 reference_hash->x, reference_hash->len );
2719 TEST_EQUAL( status, expected_status );
2720
Neil Armstrong55a1be12022-02-07 11:23:20 +01002721 /* Hash Compare, multi-part */
2722 status = psa_hash_setup( &operation, alg );
2723 if( status == PSA_SUCCESS )
2724 {
2725 status = psa_hash_update( &operation, input->x, input->len );
2726 if( status == PSA_SUCCESS )
2727 {
2728 status = psa_hash_verify( &operation, reference_hash->x,
2729 reference_hash->len );
2730 TEST_EQUAL( status, expected_status );
2731 }
2732 else
2733 {
2734 TEST_EQUAL( status, expected_status );
2735 }
2736 }
2737 else
2738 {
2739 TEST_EQUAL( status, expected_status );
2740 }
2741
Gilles Peskine88e08462020-01-28 20:43:00 +01002742exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002743 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002744 PSA_DONE( );
2745}
2746/* END_CASE */
2747
2748/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002749void hash_compute_compare( int alg_arg, data_t *input,
2750 data_t *expected_output )
2751{
2752 psa_algorithm_t alg = alg_arg;
2753 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2754 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002755 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002756 size_t i;
2757
2758 PSA_ASSERT( psa_crypto_init( ) );
2759
Neil Armstrongca30a002022-02-07 11:40:23 +01002760 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002761 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002762 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002763 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002764 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002765 ASSERT_COMPARE( output, output_length,
2766 expected_output->x, expected_output->len );
2767
Neil Armstrongca30a002022-02-07 11:40:23 +01002768 /* Compute with tight buffer, multi-part */
2769 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2770 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2771 PSA_ASSERT( psa_hash_finish( &operation, output,
2772 PSA_HASH_LENGTH( alg ),
2773 &output_length ) );
2774 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2775 ASSERT_COMPARE( output, output_length,
2776 expected_output->x, expected_output->len );
2777
2778 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002779 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2780 output, sizeof( output ),
2781 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002782 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002783 ASSERT_COMPARE( output, output_length,
2784 expected_output->x, expected_output->len );
2785
Neil Armstrongca30a002022-02-07 11:40:23 +01002786 /* Compute with larger buffer, multi-part */
2787 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2788 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2789 PSA_ASSERT( psa_hash_finish( &operation, output,
2790 sizeof( output ), &output_length ) );
2791 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2792 ASSERT_COMPARE( output, output_length,
2793 expected_output->x, expected_output->len );
2794
2795 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002796 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2797 output, output_length ) );
2798
Neil Armstrongca30a002022-02-07 11:40:23 +01002799 /* Compare with correct hash, multi-part */
2800 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2801 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2802 PSA_ASSERT( psa_hash_verify( &operation, output,
2803 output_length ) );
2804
2805 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002806 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2807 output, output_length + 1 ),
2808 PSA_ERROR_INVALID_SIGNATURE );
2809
Neil Armstrongca30a002022-02-07 11:40:23 +01002810 /* Compare with trailing garbage, multi-part */
2811 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2812 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2813 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2814 PSA_ERROR_INVALID_SIGNATURE );
2815
2816 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002817 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2818 output, output_length - 1 ),
2819 PSA_ERROR_INVALID_SIGNATURE );
2820
Neil Armstrongca30a002022-02-07 11:40:23 +01002821 /* Compare with truncated hash, multi-part */
2822 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2823 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2824 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2825 PSA_ERROR_INVALID_SIGNATURE );
2826
Gilles Peskine0a749c82019-11-28 19:33:58 +01002827 /* Compare with corrupted value */
2828 for( i = 0; i < output_length; i++ )
2829 {
Chris Jones9634bb12021-01-20 15:56:42 +00002830 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002831 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002832
2833 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002834 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2835 output, output_length ),
2836 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002837
2838 /* Multi-Part */
2839 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2840 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2841 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2842 PSA_ERROR_INVALID_SIGNATURE );
2843
Gilles Peskine0a749c82019-11-28 19:33:58 +01002844 output[i] ^= 1;
2845 }
2846
2847exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002848 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002849 PSA_DONE( );
2850}
2851/* END_CASE */
2852
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002853/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002854void hash_bad_order( )
2855{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002856 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002857 unsigned char input[] = "";
2858 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002859 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002860 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2861 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2862 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002863 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002864 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002865 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002866
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002868
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002869 /* Call setup twice in a row. */
2870 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002871 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002872 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2873 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002874 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002875 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002876 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002877
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002878 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002879 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002880 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002881 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002882
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002883 /* Check that update calls abort on error. */
2884 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002885 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002886 ASSERT_OPERATION_IS_ACTIVE( operation );
2887 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2888 PSA_ERROR_BAD_STATE );
2889 ASSERT_OPERATION_IS_INACTIVE( operation );
2890 PSA_ASSERT( psa_hash_abort( &operation ) );
2891 ASSERT_OPERATION_IS_INACTIVE( operation );
2892
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002893 /* Call update after finish. */
2894 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2895 PSA_ASSERT( psa_hash_finish( &operation,
2896 hash, sizeof( hash ), &hash_len ) );
2897 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002898 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002899 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002900
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002901 /* Call verify without calling setup beforehand. */
2902 TEST_EQUAL( psa_hash_verify( &operation,
2903 valid_hash, sizeof( valid_hash ) ),
2904 PSA_ERROR_BAD_STATE );
2905 PSA_ASSERT( psa_hash_abort( &operation ) );
2906
2907 /* Call verify after finish. */
2908 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2909 PSA_ASSERT( psa_hash_finish( &operation,
2910 hash, sizeof( hash ), &hash_len ) );
2911 TEST_EQUAL( psa_hash_verify( &operation,
2912 valid_hash, sizeof( valid_hash ) ),
2913 PSA_ERROR_BAD_STATE );
2914 PSA_ASSERT( psa_hash_abort( &operation ) );
2915
2916 /* Call verify twice in a row. */
2917 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002918 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002919 PSA_ASSERT( psa_hash_verify( &operation,
2920 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002921 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002922 TEST_EQUAL( psa_hash_verify( &operation,
2923 valid_hash, sizeof( valid_hash ) ),
2924 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002925 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002926 PSA_ASSERT( psa_hash_abort( &operation ) );
2927
2928 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002929 TEST_EQUAL( psa_hash_finish( &operation,
2930 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002931 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002932 PSA_ASSERT( psa_hash_abort( &operation ) );
2933
2934 /* Call finish twice in a row. */
2935 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2936 PSA_ASSERT( psa_hash_finish( &operation,
2937 hash, sizeof( hash ), &hash_len ) );
2938 TEST_EQUAL( psa_hash_finish( &operation,
2939 hash, sizeof( hash ), &hash_len ),
2940 PSA_ERROR_BAD_STATE );
2941 PSA_ASSERT( psa_hash_abort( &operation ) );
2942
2943 /* Call finish after calling verify. */
2944 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2945 PSA_ASSERT( psa_hash_verify( &operation,
2946 valid_hash, sizeof( valid_hash ) ) );
2947 TEST_EQUAL( psa_hash_finish( &operation,
2948 hash, sizeof( hash ), &hash_len ),
2949 PSA_ERROR_BAD_STATE );
2950 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002951
2952exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002953 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002954}
2955/* END_CASE */
2956
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002957/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002958void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002959{
2960 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002961 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2962 * appended to it */
2963 unsigned char hash[] = {
2964 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2965 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2966 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002967 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002968 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002969
Gilles Peskine8817f612018-12-18 00:18:46 +01002970 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002971
itayzafrir27e69452018-11-01 14:26:34 +02002972 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002974 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002975 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002976 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002977 ASSERT_OPERATION_IS_INACTIVE( operation );
2978 PSA_ASSERT( psa_hash_abort( &operation ) );
2979 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002980
itayzafrir27e69452018-11-01 14:26:34 +02002981 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002982 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002983 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002984 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002985
itayzafrir27e69452018-11-01 14:26:34 +02002986 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002987 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002988 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002989 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002990
itayzafrirec93d302018-10-18 18:01:10 +03002991exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002992 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002993}
2994/* END_CASE */
2995
Ronald Cronee414c72021-03-18 18:50:08 +01002996/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002997void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002998{
2999 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02003000 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003001 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00003002 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03003003 size_t hash_len;
3004
Gilles Peskine8817f612018-12-18 00:18:46 +01003005 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03003006
itayzafrir58028322018-10-25 10:22:01 +03003007 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01003008 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003009 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003010 hash, expected_size - 1, &hash_len ),
3011 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03003012
3013exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003014 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03003015}
3016/* END_CASE */
3017
Ronald Cronee414c72021-03-18 18:50:08 +01003018/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003019void hash_clone_source_state( )
3020{
3021 psa_algorithm_t alg = PSA_ALG_SHA_256;
3022 unsigned char hash[PSA_HASH_MAX_SIZE];
3023 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3024 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3025 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3026 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3027 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3028 size_t hash_len;
3029
3030 PSA_ASSERT( psa_crypto_init( ) );
3031 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3032
3033 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3034 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3035 PSA_ASSERT( psa_hash_finish( &op_finished,
3036 hash, sizeof( hash ), &hash_len ) );
3037 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3038 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3039
3040 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3041 PSA_ERROR_BAD_STATE );
3042
3043 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3044 PSA_ASSERT( psa_hash_finish( &op_init,
3045 hash, sizeof( hash ), &hash_len ) );
3046 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3047 PSA_ASSERT( psa_hash_finish( &op_finished,
3048 hash, sizeof( hash ), &hash_len ) );
3049 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3050 PSA_ASSERT( psa_hash_finish( &op_aborted,
3051 hash, sizeof( hash ), &hash_len ) );
3052
3053exit:
3054 psa_hash_abort( &op_source );
3055 psa_hash_abort( &op_init );
3056 psa_hash_abort( &op_setup );
3057 psa_hash_abort( &op_finished );
3058 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003059 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003060}
3061/* END_CASE */
3062
Ronald Cronee414c72021-03-18 18:50:08 +01003063/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003064void hash_clone_target_state( )
3065{
3066 psa_algorithm_t alg = PSA_ALG_SHA_256;
3067 unsigned char hash[PSA_HASH_MAX_SIZE];
3068 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3069 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3070 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3071 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3072 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3073 size_t hash_len;
3074
3075 PSA_ASSERT( psa_crypto_init( ) );
3076
3077 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3078 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3079 PSA_ASSERT( psa_hash_finish( &op_finished,
3080 hash, sizeof( hash ), &hash_len ) );
3081 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3082 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3083
3084 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3085 PSA_ASSERT( psa_hash_finish( &op_target,
3086 hash, sizeof( hash ), &hash_len ) );
3087
3088 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3089 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3090 PSA_ERROR_BAD_STATE );
3091 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3092 PSA_ERROR_BAD_STATE );
3093
3094exit:
3095 psa_hash_abort( &op_target );
3096 psa_hash_abort( &op_init );
3097 psa_hash_abort( &op_setup );
3098 psa_hash_abort( &op_finished );
3099 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003100 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003101}
3102/* END_CASE */
3103
itayzafrir58028322018-10-25 10:22:01 +03003104/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003105void mac_operation_init( )
3106{
Jaeden Amero252ef282019-02-15 14:05:35 +00003107 const uint8_t input[1] = { 0 };
3108
Jaeden Amero769ce272019-01-04 11:48:03 +00003109 /* Test each valid way of initializing the object, except for `= {0}`, as
3110 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3111 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003112 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003113 psa_mac_operation_t func = psa_mac_operation_init( );
3114 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3115 psa_mac_operation_t zero;
3116
3117 memset( &zero, 0, sizeof( zero ) );
3118
Jaeden Amero252ef282019-02-15 14:05:35 +00003119 /* A freshly-initialized MAC operation should not be usable. */
3120 TEST_EQUAL( psa_mac_update( &func,
3121 input, sizeof( input ) ),
3122 PSA_ERROR_BAD_STATE );
3123 TEST_EQUAL( psa_mac_update( &init,
3124 input, sizeof( input ) ),
3125 PSA_ERROR_BAD_STATE );
3126 TEST_EQUAL( psa_mac_update( &zero,
3127 input, sizeof( input ) ),
3128 PSA_ERROR_BAD_STATE );
3129
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003130 /* A default MAC operation should be abortable without error. */
3131 PSA_ASSERT( psa_mac_abort( &func ) );
3132 PSA_ASSERT( psa_mac_abort( &init ) );
3133 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003134}
3135/* END_CASE */
3136
3137/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003138void mac_setup( int key_type_arg,
3139 data_t *key,
3140 int alg_arg,
3141 int expected_status_arg )
3142{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003143 psa_key_type_t key_type = key_type_arg;
3144 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003145 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003146 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003147 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3148#if defined(KNOWN_SUPPORTED_MAC_ALG)
3149 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3150#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003151
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003153
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003154 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3155 &operation, &status ) )
3156 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003157 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003158
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003159 /* The operation object should be reusable. */
3160#if defined(KNOWN_SUPPORTED_MAC_ALG)
3161 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3162 smoke_test_key_data,
3163 sizeof( smoke_test_key_data ),
3164 KNOWN_SUPPORTED_MAC_ALG,
3165 &operation, &status ) )
3166 goto exit;
3167 TEST_EQUAL( status, PSA_SUCCESS );
3168#endif
3169
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003170exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003171 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003172}
3173/* END_CASE */
3174
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003175/* 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 +00003176void mac_bad_order( )
3177{
Ronald Cron5425a212020-08-04 14:58:35 +02003178 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003179 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3180 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003181 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003182 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3183 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3184 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003186 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3187 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3188 size_t sign_mac_length = 0;
3189 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3190 const uint8_t verify_mac[] = {
3191 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3192 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3193 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3194
3195 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003196 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003197 psa_set_key_algorithm( &attributes, alg );
3198 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003199
Ronald Cron5425a212020-08-04 14:58:35 +02003200 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3201 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003202
Jaeden Amero252ef282019-02-15 14:05:35 +00003203 /* Call update without calling setup beforehand. */
3204 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3205 PSA_ERROR_BAD_STATE );
3206 PSA_ASSERT( psa_mac_abort( &operation ) );
3207
3208 /* Call sign finish without calling setup beforehand. */
3209 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3210 &sign_mac_length),
3211 PSA_ERROR_BAD_STATE );
3212 PSA_ASSERT( psa_mac_abort( &operation ) );
3213
3214 /* Call verify finish without calling setup beforehand. */
3215 TEST_EQUAL( psa_mac_verify_finish( &operation,
3216 verify_mac, sizeof( verify_mac ) ),
3217 PSA_ERROR_BAD_STATE );
3218 PSA_ASSERT( psa_mac_abort( &operation ) );
3219
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003220 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003221 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003222 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003223 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003224 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003225 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003226 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003227 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003228
Jaeden Amero252ef282019-02-15 14:05:35 +00003229 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003230 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003231 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3232 PSA_ASSERT( psa_mac_sign_finish( &operation,
3233 sign_mac, sizeof( sign_mac ),
3234 &sign_mac_length ) );
3235 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3236 PSA_ERROR_BAD_STATE );
3237 PSA_ASSERT( psa_mac_abort( &operation ) );
3238
3239 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003240 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003241 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3242 PSA_ASSERT( psa_mac_verify_finish( &operation,
3243 verify_mac, sizeof( verify_mac ) ) );
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 twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003249 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003250 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3251 PSA_ASSERT( psa_mac_sign_finish( &operation,
3252 sign_mac, sizeof( sign_mac ),
3253 &sign_mac_length ) );
3254 TEST_EQUAL( psa_mac_sign_finish( &operation,
3255 sign_mac, sizeof( sign_mac ),
3256 &sign_mac_length ),
3257 PSA_ERROR_BAD_STATE );
3258 PSA_ASSERT( psa_mac_abort( &operation ) );
3259
3260 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003261 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003262 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3263 PSA_ASSERT( psa_mac_verify_finish( &operation,
3264 verify_mac, sizeof( verify_mac ) ) );
3265 TEST_EQUAL( psa_mac_verify_finish( &operation,
3266 verify_mac, sizeof( verify_mac ) ),
3267 PSA_ERROR_BAD_STATE );
3268 PSA_ASSERT( psa_mac_abort( &operation ) );
3269
3270 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003271 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003272 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003273 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003274 TEST_EQUAL( psa_mac_verify_finish( &operation,
3275 verify_mac, sizeof( verify_mac ) ),
3276 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003277 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003278 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003279 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003280
3281 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003282 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003283 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003284 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003285 TEST_EQUAL( psa_mac_sign_finish( &operation,
3286 sign_mac, sizeof( sign_mac ),
3287 &sign_mac_length ),
3288 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003289 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003290 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003291 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003292
Ronald Cron5425a212020-08-04 14:58:35 +02003293 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003294
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003295exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003296 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003297}
3298/* END_CASE */
3299
3300/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003301void mac_sign_verify_multi( int key_type_arg,
3302 data_t *key_data,
3303 int alg_arg,
3304 data_t *input,
3305 int is_verify,
3306 data_t *expected_mac )
3307{
3308 size_t data_part_len = 0;
3309
3310 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3311 {
3312 /* Split data into length(data_part_len) parts. */
3313 mbedtls_test_set_step( 2000 + data_part_len );
3314
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003315 if( mac_multipart_internal_func( key_type_arg, key_data,
3316 alg_arg,
3317 input, data_part_len,
3318 expected_mac,
3319 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003320 break;
3321
3322 /* length(0) part, length(data_part_len) part, length(0) part... */
3323 mbedtls_test_set_step( 3000 + data_part_len );
3324
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003325 if( mac_multipart_internal_func( key_type_arg, key_data,
3326 alg_arg,
3327 input, data_part_len,
3328 expected_mac,
3329 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003330 break;
3331 }
3332
3333 /* Goto is required to silence warnings about unused labels, as we
3334 * don't actually do any test assertions in this function. */
3335 goto exit;
3336}
3337/* END_CASE */
3338
3339/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003340void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003341 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003342 int alg_arg,
3343 data_t *input,
3344 data_t *expected_mac )
3345{
Ronald Cron5425a212020-08-04 14:58:35 +02003346 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003347 psa_key_type_t key_type = key_type_arg;
3348 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003349 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003351 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003352 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003353 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003354 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003355 const size_t output_sizes_to_test[] = {
3356 0,
3357 1,
3358 expected_mac->len - 1,
3359 expected_mac->len,
3360 expected_mac->len + 1,
3361 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003362
Gilles Peskine7be11a72022-04-14 00:12:57 +02003363 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003364 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003365 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003366
Gilles Peskine8817f612018-12-18 00:18:46 +01003367 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003368
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003369 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003370 psa_set_key_algorithm( &attributes, alg );
3371 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003372
Ronald Cron5425a212020-08-04 14:58:35 +02003373 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3374 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003375
Gilles Peskine8b356b52020-08-25 23:44:59 +02003376 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3377 {
3378 const size_t output_size = output_sizes_to_test[i];
3379 psa_status_t expected_status =
3380 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3381 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003382
Chris Jones9634bb12021-01-20 15:56:42 +00003383 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003384 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003385
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003386 /* Calculate the MAC, one-shot case. */
3387 TEST_EQUAL( psa_mac_compute( key, alg,
3388 input->x, input->len,
3389 actual_mac, output_size, &mac_length ),
3390 expected_status );
3391 if( expected_status == PSA_SUCCESS )
3392 {
3393 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3394 actual_mac, mac_length );
3395 }
3396
3397 if( output_size > 0 )
3398 memset( actual_mac, 0, output_size );
3399
3400 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003401 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003402 PSA_ASSERT( psa_mac_update( &operation,
3403 input->x, input->len ) );
3404 TEST_EQUAL( psa_mac_sign_finish( &operation,
3405 actual_mac, output_size,
3406 &mac_length ),
3407 expected_status );
3408 PSA_ASSERT( psa_mac_abort( &operation ) );
3409
3410 if( expected_status == PSA_SUCCESS )
3411 {
3412 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3413 actual_mac, mac_length );
3414 }
3415 mbedtls_free( actual_mac );
3416 actual_mac = NULL;
3417 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003418
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003419exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003420 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003421 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003422 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003423 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003424}
3425/* END_CASE */
3426
3427/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003428void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003429 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003430 int alg_arg,
3431 data_t *input,
3432 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003433{
Ronald Cron5425a212020-08-04 14:58:35 +02003434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003435 psa_key_type_t key_type = key_type_arg;
3436 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003437 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003439 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003440
Gilles Peskine7be11a72022-04-14 00:12:57 +02003441 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003444
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003445 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003446 psa_set_key_algorithm( &attributes, alg );
3447 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003448
Ronald Cron5425a212020-08-04 14:58:35 +02003449 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3450 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003451
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003452 /* Verify correct MAC, one-shot case. */
3453 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3454 expected_mac->x, expected_mac->len ) );
3455
3456 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003457 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003458 PSA_ASSERT( psa_mac_update( &operation,
3459 input->x, input->len ) );
3460 PSA_ASSERT( psa_mac_verify_finish( &operation,
3461 expected_mac->x,
3462 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003463
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003464 /* Test a MAC that's too short, one-shot case. */
3465 TEST_EQUAL( psa_mac_verify( key, alg,
3466 input->x, input->len,
3467 expected_mac->x,
3468 expected_mac->len - 1 ),
3469 PSA_ERROR_INVALID_SIGNATURE );
3470
3471 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003472 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003473 PSA_ASSERT( psa_mac_update( &operation,
3474 input->x, input->len ) );
3475 TEST_EQUAL( psa_mac_verify_finish( &operation,
3476 expected_mac->x,
3477 expected_mac->len - 1 ),
3478 PSA_ERROR_INVALID_SIGNATURE );
3479
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003480 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003481 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3482 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003483 TEST_EQUAL( psa_mac_verify( key, alg,
3484 input->x, input->len,
3485 perturbed_mac, expected_mac->len + 1 ),
3486 PSA_ERROR_INVALID_SIGNATURE );
3487
3488 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003489 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003490 PSA_ASSERT( psa_mac_update( &operation,
3491 input->x, input->len ) );
3492 TEST_EQUAL( psa_mac_verify_finish( &operation,
3493 perturbed_mac,
3494 expected_mac->len + 1 ),
3495 PSA_ERROR_INVALID_SIGNATURE );
3496
3497 /* Test changing one byte. */
3498 for( size_t i = 0; i < expected_mac->len; i++ )
3499 {
Chris Jones9634bb12021-01-20 15:56:42 +00003500 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003501 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003502
3503 TEST_EQUAL( psa_mac_verify( key, alg,
3504 input->x, input->len,
3505 perturbed_mac, expected_mac->len ),
3506 PSA_ERROR_INVALID_SIGNATURE );
3507
Ronald Cron5425a212020-08-04 14:58:35 +02003508 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003509 PSA_ASSERT( psa_mac_update( &operation,
3510 input->x, input->len ) );
3511 TEST_EQUAL( psa_mac_verify_finish( &operation,
3512 perturbed_mac,
3513 expected_mac->len ),
3514 PSA_ERROR_INVALID_SIGNATURE );
3515 perturbed_mac[i] ^= 1;
3516 }
3517
Gilles Peskine8c9def32018-02-08 10:02:12 +01003518exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003519 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003520 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003521 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003522 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003523}
3524/* END_CASE */
3525
3526/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003527void cipher_operation_init( )
3528{
Jaeden Ameroab439972019-02-15 14:12:05 +00003529 const uint8_t input[1] = { 0 };
3530 unsigned char output[1] = { 0 };
3531 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003532 /* Test each valid way of initializing the object, except for `= {0}`, as
3533 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3534 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003535 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003536 psa_cipher_operation_t func = psa_cipher_operation_init( );
3537 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3538 psa_cipher_operation_t zero;
3539
3540 memset( &zero, 0, sizeof( zero ) );
3541
Jaeden Ameroab439972019-02-15 14:12:05 +00003542 /* A freshly-initialized cipher operation should not be usable. */
3543 TEST_EQUAL( psa_cipher_update( &func,
3544 input, sizeof( input ),
3545 output, sizeof( output ),
3546 &output_length ),
3547 PSA_ERROR_BAD_STATE );
3548 TEST_EQUAL( psa_cipher_update( &init,
3549 input, sizeof( input ),
3550 output, sizeof( output ),
3551 &output_length ),
3552 PSA_ERROR_BAD_STATE );
3553 TEST_EQUAL( psa_cipher_update( &zero,
3554 input, sizeof( input ),
3555 output, sizeof( output ),
3556 &output_length ),
3557 PSA_ERROR_BAD_STATE );
3558
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003559 /* A default cipher operation should be abortable without error. */
3560 PSA_ASSERT( psa_cipher_abort( &func ) );
3561 PSA_ASSERT( psa_cipher_abort( &init ) );
3562 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003563}
3564/* END_CASE */
3565
3566/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003567void cipher_setup( int key_type_arg,
3568 data_t *key,
3569 int alg_arg,
3570 int expected_status_arg )
3571{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003572 psa_key_type_t key_type = key_type_arg;
3573 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003574 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003575 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003576 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003577#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003578 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3579#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003580
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003582
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003583 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3584 &operation, &status ) )
3585 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003586 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003587
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003588 /* The operation object should be reusable. */
3589#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3590 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3591 smoke_test_key_data,
3592 sizeof( smoke_test_key_data ),
3593 KNOWN_SUPPORTED_CIPHER_ALG,
3594 &operation, &status ) )
3595 goto exit;
3596 TEST_EQUAL( status, PSA_SUCCESS );
3597#endif
3598
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003599exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003600 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003601 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003602}
3603/* END_CASE */
3604
Ronald Cronee414c72021-03-18 18:50:08 +01003605/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003606void cipher_bad_order( )
3607{
Ronald Cron5425a212020-08-04 14:58:35 +02003608 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003609 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3610 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003612 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003613 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003614 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003615 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3616 0xaa, 0xaa, 0xaa, 0xaa };
3617 const uint8_t text[] = {
3618 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3619 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003620 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003621 size_t length = 0;
3622
3623 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003624 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3625 psa_set_key_algorithm( &attributes, alg );
3626 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003627 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3628 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003629
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003630 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003631 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003632 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003633 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003634 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003635 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003636 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003637 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003638
3639 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003640 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003641 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003642 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003643 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003644 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003645 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003646 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003647
Jaeden Ameroab439972019-02-15 14:12:05 +00003648 /* Generate an IV without calling setup beforehand. */
3649 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3650 buffer, sizeof( buffer ),
3651 &length ),
3652 PSA_ERROR_BAD_STATE );
3653 PSA_ASSERT( psa_cipher_abort( &operation ) );
3654
3655 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003656 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003657 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3658 buffer, sizeof( buffer ),
3659 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003660 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003661 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3662 buffer, sizeof( buffer ),
3663 &length ),
3664 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003665 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003666 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003667 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003668
3669 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003670 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003671 PSA_ASSERT( psa_cipher_set_iv( &operation,
3672 iv, sizeof( iv ) ) );
3673 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3674 buffer, sizeof( buffer ),
3675 &length ),
3676 PSA_ERROR_BAD_STATE );
3677 PSA_ASSERT( psa_cipher_abort( &operation ) );
3678
3679 /* Set an IV without calling setup beforehand. */
3680 TEST_EQUAL( psa_cipher_set_iv( &operation,
3681 iv, sizeof( iv ) ),
3682 PSA_ERROR_BAD_STATE );
3683 PSA_ASSERT( psa_cipher_abort( &operation ) );
3684
3685 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003686 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003687 PSA_ASSERT( psa_cipher_set_iv( &operation,
3688 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003689 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003690 TEST_EQUAL( psa_cipher_set_iv( &operation,
3691 iv, sizeof( iv ) ),
3692 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003693 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003694 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003695 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003696
3697 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003698 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003699 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3700 buffer, sizeof( buffer ),
3701 &length ) );
3702 TEST_EQUAL( psa_cipher_set_iv( &operation,
3703 iv, sizeof( iv ) ),
3704 PSA_ERROR_BAD_STATE );
3705 PSA_ASSERT( psa_cipher_abort( &operation ) );
3706
3707 /* Call update without calling setup beforehand. */
3708 TEST_EQUAL( psa_cipher_update( &operation,
3709 text, sizeof( text ),
3710 buffer, sizeof( buffer ),
3711 &length ),
3712 PSA_ERROR_BAD_STATE );
3713 PSA_ASSERT( psa_cipher_abort( &operation ) );
3714
3715 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003716 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003717 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003718 TEST_EQUAL( psa_cipher_update( &operation,
3719 text, sizeof( text ),
3720 buffer, sizeof( buffer ),
3721 &length ),
3722 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003723 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003724 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003725 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003726
3727 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003728 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003729 PSA_ASSERT( psa_cipher_set_iv( &operation,
3730 iv, sizeof( iv ) ) );
3731 PSA_ASSERT( psa_cipher_finish( &operation,
3732 buffer, sizeof( buffer ), &length ) );
3733 TEST_EQUAL( psa_cipher_update( &operation,
3734 text, sizeof( text ),
3735 buffer, sizeof( buffer ),
3736 &length ),
3737 PSA_ERROR_BAD_STATE );
3738 PSA_ASSERT( psa_cipher_abort( &operation ) );
3739
3740 /* Call finish without calling setup beforehand. */
3741 TEST_EQUAL( psa_cipher_finish( &operation,
3742 buffer, sizeof( buffer ), &length ),
3743 PSA_ERROR_BAD_STATE );
3744 PSA_ASSERT( psa_cipher_abort( &operation ) );
3745
3746 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003747 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003748 /* Not calling update means we are encrypting an empty buffer, which is OK
3749 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003750 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003751 TEST_EQUAL( psa_cipher_finish( &operation,
3752 buffer, sizeof( buffer ), &length ),
3753 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003754 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003755 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003756 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003757
3758 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003759 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003760 PSA_ASSERT( psa_cipher_set_iv( &operation,
3761 iv, sizeof( iv ) ) );
3762 PSA_ASSERT( psa_cipher_finish( &operation,
3763 buffer, sizeof( buffer ), &length ) );
3764 TEST_EQUAL( psa_cipher_finish( &operation,
3765 buffer, sizeof( buffer ), &length ),
3766 PSA_ERROR_BAD_STATE );
3767 PSA_ASSERT( psa_cipher_abort( &operation ) );
3768
Ronald Cron5425a212020-08-04 14:58:35 +02003769 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003770
Jaeden Ameroab439972019-02-15 14:12:05 +00003771exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003772 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003773 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003774}
3775/* END_CASE */
3776
3777/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003778void cipher_encrypt_fail( int alg_arg,
3779 int key_type_arg,
3780 data_t *key_data,
3781 data_t *input,
3782 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003783{
Ronald Cron5425a212020-08-04 14:58:35 +02003784 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003785 psa_status_t status;
3786 psa_key_type_t key_type = key_type_arg;
3787 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003788 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003789 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3790 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3791 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003792 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003793 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003794 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003795 size_t function_output_length;
3796 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3798
3799 if ( PSA_ERROR_BAD_STATE != expected_status )
3800 {
3801 PSA_ASSERT( psa_crypto_init( ) );
3802
3803 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3804 psa_set_key_algorithm( &attributes, alg );
3805 psa_set_key_type( &attributes, key_type );
3806
3807 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3808 input->len );
3809 ASSERT_ALLOC( output, output_buffer_size );
3810
3811 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3812 &key ) );
3813 }
3814
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003815 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003816 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3817 output_buffer_size, &output_length );
3818
3819 TEST_EQUAL( status, expected_status );
3820
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003821 /* Encrypt, multi-part */
3822 status = psa_cipher_encrypt_setup( &operation, key, alg );
3823 if( status == PSA_SUCCESS )
3824 {
3825 if( alg != PSA_ALG_ECB_NO_PADDING )
3826 {
3827 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3828 iv, iv_size,
3829 &iv_length ) );
3830 }
3831
3832 status = psa_cipher_update( &operation, input->x, input->len,
3833 output, output_buffer_size,
3834 &function_output_length );
3835 if( status == PSA_SUCCESS )
3836 {
3837 output_length += function_output_length;
3838
3839 status = psa_cipher_finish( &operation, output + output_length,
3840 output_buffer_size - output_length,
3841 &function_output_length );
3842
3843 TEST_EQUAL( status, expected_status );
3844 }
3845 else
3846 {
3847 TEST_EQUAL( status, expected_status );
3848 }
3849 }
3850 else
3851 {
3852 TEST_EQUAL( status, expected_status );
3853 }
3854
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003855exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003856 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003857 mbedtls_free( output );
3858 psa_destroy_key( key );
3859 PSA_DONE( );
3860}
3861/* END_CASE */
3862
3863/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003864void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3865 data_t *input, int iv_length,
3866 int expected_result )
3867{
3868 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3869 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3871 size_t output_buffer_size = 0;
3872 unsigned char *output = NULL;
3873
3874 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3875 ASSERT_ALLOC( output, output_buffer_size );
3876
3877 PSA_ASSERT( psa_crypto_init( ) );
3878
3879 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3880 psa_set_key_algorithm( &attributes, alg );
3881 psa_set_key_type( &attributes, key_type );
3882
3883 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3884 &key ) );
3885 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3886 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3887 iv_length ) );
3888
3889exit:
3890 psa_cipher_abort( &operation );
3891 mbedtls_free( output );
3892 psa_destroy_key( key );
3893 PSA_DONE( );
3894}
3895/* END_CASE */
3896
3897/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003898void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3899 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003900{
3901 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3902 psa_key_type_t key_type = key_type_arg;
3903 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003904 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3905 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003906 unsigned char *output = NULL;
3907 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003908 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3910
3911 PSA_ASSERT( psa_crypto_init( ) );
3912
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003913 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003914 TEST_LE_U( ciphertext->len,
3915 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3916 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003917 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003918 TEST_LE_U( plaintext->len,
3919 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3920 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3921 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003922
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003923
3924 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003925 psa_set_key_usage_flags( &attributes,
3926 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003927 psa_set_key_algorithm( &attributes, alg );
3928 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3930 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003931 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3932 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003933 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003934
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003935 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003936 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3937 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3938 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003939 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3940 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003941 PSA_ERROR_BAD_STATE );
3942
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003943 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003944 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3945 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003946 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003947 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003948 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3949 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003950 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003951 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003952
Gilles Peskine286c3142022-04-20 17:09:38 +02003953 /* Multipart encryption */
3954 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3955 output_length = 0;
3956 length = ~0;
3957 PSA_ASSERT( psa_cipher_update( &operation,
3958 plaintext->x, plaintext->len,
3959 output, output_buffer_size,
3960 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003961 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003962 output_length += length;
3963 PSA_ASSERT( psa_cipher_finish( &operation,
3964 output + output_length,
3965 output_buffer_size - output_length,
3966 &length ) );
3967 output_length += length;
3968 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003969 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003970
Gilles Peskine286c3142022-04-20 17:09:38 +02003971 /* Multipart encryption */
3972 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3973 output_length = 0;
3974 length = ~0;
3975 PSA_ASSERT( psa_cipher_update( &operation,
3976 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003977 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003978 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003979 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003980 output_length += length;
3981 PSA_ASSERT( psa_cipher_finish( &operation,
3982 output + output_length,
3983 output_buffer_size - output_length,
3984 &length ) );
3985 output_length += length;
3986 ASSERT_COMPARE( plaintext->x, plaintext->len,
3987 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003988
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003989 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003990 output_length = ~0;
3991 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3992 output, output_buffer_size,
3993 &output_length ) );
3994 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3995 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003996
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003997 /* One-shot decryption */
3998 output_length = ~0;
3999 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
4000 output, output_buffer_size,
4001 &output_length ) );
4002 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004003 output, output_length );
4004
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004005exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01004006 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004007 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02004008 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004009 psa_destroy_key( key );
4010 PSA_DONE( );
4011}
4012/* END_CASE */
4013
4014/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01004015void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
4016{
4017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4018 psa_algorithm_t alg = alg_arg;
4019 psa_key_type_t key_type = key_type_arg;
4020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4021 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
4022 psa_status_t status;
4023
4024 PSA_ASSERT( psa_crypto_init( ) );
4025
4026 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4027 psa_set_key_algorithm( &attributes, alg );
4028 psa_set_key_type( &attributes, key_type );
4029
4030 /* Usage of either of these two size macros would cause divide by zero
4031 * with incorrect key types previously. Input length should be irrelevant
4032 * here. */
4033 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4034 0 );
4035 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4036
4037
4038 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4039 &key ) );
4040
4041 /* Should fail due to invalid alg type (to support invalid key type).
4042 * Encrypt or decrypt will end up in the same place. */
4043 status = psa_cipher_encrypt_setup( &operation, key, alg );
4044
4045 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4046
4047exit:
4048 psa_cipher_abort( &operation );
4049 psa_destroy_key( key );
4050 PSA_DONE( );
4051}
4052/* END_CASE */
4053
4054/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004055void cipher_encrypt_validation( int alg_arg,
4056 int key_type_arg,
4057 data_t *key_data,
4058 data_t *input )
4059{
4060 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4061 psa_key_type_t key_type = key_type_arg;
4062 psa_algorithm_t alg = alg_arg;
4063 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4064 unsigned char *output1 = NULL;
4065 size_t output1_buffer_size = 0;
4066 size_t output1_length = 0;
4067 unsigned char *output2 = NULL;
4068 size_t output2_buffer_size = 0;
4069 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004070 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004071 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004073
Gilles Peskine8817f612018-12-18 00:18:46 +01004074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004075
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004076 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4077 psa_set_key_algorithm( &attributes, alg );
4078 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004079
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004080 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4081 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4082 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4083 ASSERT_ALLOC( output1, output1_buffer_size );
4084 ASSERT_ALLOC( output2, output2_buffer_size );
4085
Ronald Cron5425a212020-08-04 14:58:35 +02004086 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4087 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004088
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004089 /* The one-shot cipher encryption uses generated iv so validating
4090 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004091 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4092 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004093 TEST_LE_U( output1_length,
4094 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4095 TEST_LE_U( output1_length,
4096 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004097
4098 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4099 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004100
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_cipher_update( &operation,
4102 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004103 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004104 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004105 TEST_LE_U( function_output_length,
4106 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4107 TEST_LE_U( function_output_length,
4108 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004109 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004110
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004111 PSA_ASSERT( psa_cipher_finish( &operation,
4112 output2 + output2_length,
4113 output2_buffer_size - output2_length,
4114 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004115 TEST_LE_U( function_output_length,
4116 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4117 TEST_LE_U( function_output_length,
4118 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004119 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004120
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004121 PSA_ASSERT( psa_cipher_abort( &operation ) );
4122 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4123 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004124
Gilles Peskine50e586b2018-06-08 14:28:46 +02004125exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004126 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004127 mbedtls_free( output1 );
4128 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004129 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004130 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004131}
4132/* END_CASE */
4133
4134/* BEGIN_CASE */
4135void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004136 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004137 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004138 int first_part_size_arg,
4139 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004140 data_t *expected_output,
4141 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004142{
Ronald Cron5425a212020-08-04 14:58:35 +02004143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004144 psa_key_type_t key_type = key_type_arg;
4145 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004146 psa_status_t status;
4147 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004148 size_t first_part_size = first_part_size_arg;
4149 size_t output1_length = output1_length_arg;
4150 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004151 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004152 size_t output_buffer_size = 0;
4153 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004154 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004155 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004157
Gilles Peskine8817f612018-12-18 00:18:46 +01004158 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004159
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4161 psa_set_key_algorithm( &attributes, alg );
4162 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004163
Ronald Cron5425a212020-08-04 14:58:35 +02004164 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4165 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004166
Ronald Cron5425a212020-08-04 14:58:35 +02004167 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004168
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004169 if( iv->len > 0 )
4170 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004171 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004172 }
4173
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004174 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4175 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004176 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004177
Gilles Peskine7be11a72022-04-14 00:12:57 +02004178 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004179 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4180 output, output_buffer_size,
4181 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004182 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004183 TEST_LE_U( function_output_length,
4184 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4185 TEST_LE_U( function_output_length,
4186 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004187 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004188
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004189 if( first_part_size < input->len )
4190 {
4191 PSA_ASSERT( psa_cipher_update( &operation,
4192 input->x + first_part_size,
4193 input->len - first_part_size,
4194 ( output_buffer_size == 0 ? NULL :
4195 output + total_output_length ),
4196 output_buffer_size - total_output_length,
4197 &function_output_length ) );
4198 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004199 TEST_LE_U( function_output_length,
4200 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4201 alg,
4202 input->len - first_part_size ) );
4203 TEST_LE_U( function_output_length,
4204 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004205 total_output_length += function_output_length;
4206 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004207
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004208 status = psa_cipher_finish( &operation,
4209 ( output_buffer_size == 0 ? NULL :
4210 output + total_output_length ),
4211 output_buffer_size - total_output_length,
4212 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004213 TEST_LE_U( function_output_length,
4214 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4215 TEST_LE_U( function_output_length,
4216 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004217 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004218 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004219
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004220 if( expected_status == PSA_SUCCESS )
4221 {
4222 PSA_ASSERT( psa_cipher_abort( &operation ) );
4223
4224 ASSERT_COMPARE( expected_output->x, expected_output->len,
4225 output, total_output_length );
4226 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004227
4228exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004229 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004230 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004231 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004232 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004233}
4234/* END_CASE */
4235
4236/* BEGIN_CASE */
4237void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004238 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004239 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004240 int first_part_size_arg,
4241 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004242 data_t *expected_output,
4243 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004244{
Ronald Cron5425a212020-08-04 14:58:35 +02004245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004246 psa_key_type_t key_type = key_type_arg;
4247 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004248 psa_status_t status;
4249 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004250 size_t first_part_size = first_part_size_arg;
4251 size_t output1_length = output1_length_arg;
4252 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004253 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004254 size_t output_buffer_size = 0;
4255 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004256 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004257 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004259
Gilles Peskine8817f612018-12-18 00:18:46 +01004260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004261
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4263 psa_set_key_algorithm( &attributes, alg );
4264 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004265
Ronald Cron5425a212020-08-04 14:58:35 +02004266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4267 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004268
Ronald Cron5425a212020-08-04 14:58:35 +02004269 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004270
Steven Cooreman177deba2020-09-07 17:14:14 +02004271 if( iv->len > 0 )
4272 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004273 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004274 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004275
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004276 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4277 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004278 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004279
Gilles Peskine7be11a72022-04-14 00:12:57 +02004280 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004281 PSA_ASSERT( psa_cipher_update( &operation,
4282 input->x, first_part_size,
4283 output, output_buffer_size,
4284 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004285 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004286 TEST_LE_U( function_output_length,
4287 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4288 TEST_LE_U( function_output_length,
4289 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004290 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004291
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004292 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004293 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004294 PSA_ASSERT( psa_cipher_update( &operation,
4295 input->x + first_part_size,
4296 input->len - first_part_size,
4297 ( output_buffer_size == 0 ? NULL :
4298 output + total_output_length ),
4299 output_buffer_size - total_output_length,
4300 &function_output_length ) );
4301 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004302 TEST_LE_U( function_output_length,
4303 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4304 alg,
4305 input->len - first_part_size ) );
4306 TEST_LE_U( function_output_length,
4307 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004308 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004309 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004310
Gilles Peskine50e586b2018-06-08 14:28:46 +02004311 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004312 ( output_buffer_size == 0 ? NULL :
4313 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004314 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004315 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004316 TEST_LE_U( function_output_length,
4317 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4318 TEST_LE_U( function_output_length,
4319 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004320 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004321 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004322
4323 if( expected_status == PSA_SUCCESS )
4324 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004325 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004326
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004327 ASSERT_COMPARE( expected_output->x, expected_output->len,
4328 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004329 }
4330
Gilles Peskine50e586b2018-06-08 14:28:46 +02004331exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004332 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004333 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004334 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004335 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004336}
4337/* END_CASE */
4338
Gilles Peskine50e586b2018-06-08 14:28:46 +02004339/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004340void cipher_decrypt_fail( int alg_arg,
4341 int key_type_arg,
4342 data_t *key_data,
4343 data_t *iv,
4344 data_t *input_arg,
4345 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004346{
4347 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4348 psa_status_t status;
4349 psa_key_type_t key_type = key_type_arg;
4350 psa_algorithm_t alg = alg_arg;
4351 psa_status_t expected_status = expected_status_arg;
4352 unsigned char *input = NULL;
4353 size_t input_buffer_size = 0;
4354 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004356 size_t output_buffer_size = 0;
4357 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004358 size_t function_output_length;
4359 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4361
4362 if ( PSA_ERROR_BAD_STATE != expected_status )
4363 {
4364 PSA_ASSERT( psa_crypto_init( ) );
4365
4366 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4367 psa_set_key_algorithm( &attributes, alg );
4368 psa_set_key_type( &attributes, key_type );
4369
4370 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4371 &key ) );
4372 }
4373
4374 /* Allocate input buffer and copy the iv and the plaintext */
4375 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4376 if ( input_buffer_size > 0 )
4377 {
4378 ASSERT_ALLOC( input, input_buffer_size );
4379 memcpy( input, iv->x, iv->len );
4380 memcpy( input + iv->len, input_arg->x, input_arg->len );
4381 }
4382
4383 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4384 ASSERT_ALLOC( output, output_buffer_size );
4385
Neil Armstrong66a479f2022-02-07 15:41:19 +01004386 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004387 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4388 output_buffer_size, &output_length );
4389 TEST_EQUAL( status, expected_status );
4390
Neil Armstrong66a479f2022-02-07 15:41:19 +01004391 /* Decrypt, multi-part */
4392 status = psa_cipher_decrypt_setup( &operation, key, alg );
4393 if( status == PSA_SUCCESS )
4394 {
4395 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4396 input_arg->len ) +
4397 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4398 ASSERT_ALLOC( output_multi, output_buffer_size );
4399
4400 if( iv->len > 0 )
4401 {
4402 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4403
4404 if( status != PSA_SUCCESS )
4405 TEST_EQUAL( status, expected_status );
4406 }
4407
4408 if( status == PSA_SUCCESS )
4409 {
4410 status = psa_cipher_update( &operation,
4411 input_arg->x, input_arg->len,
4412 output_multi, output_buffer_size,
4413 &function_output_length );
4414 if( status == PSA_SUCCESS )
4415 {
4416 output_length = function_output_length;
4417
4418 status = psa_cipher_finish( &operation,
4419 output_multi + output_length,
4420 output_buffer_size - output_length,
4421 &function_output_length );
4422
4423 TEST_EQUAL( status, expected_status );
4424 }
4425 else
4426 {
4427 TEST_EQUAL( status, expected_status );
4428 }
4429 }
4430 else
4431 {
4432 TEST_EQUAL( status, expected_status );
4433 }
4434 }
4435 else
4436 {
4437 TEST_EQUAL( status, expected_status );
4438 }
4439
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004440exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004441 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004442 mbedtls_free( input );
4443 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004444 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004445 psa_destroy_key( key );
4446 PSA_DONE( );
4447}
4448/* END_CASE */
4449
4450/* BEGIN_CASE */
4451void cipher_decrypt( int alg_arg,
4452 int key_type_arg,
4453 data_t *key_data,
4454 data_t *iv,
4455 data_t *input_arg,
4456 data_t *expected_output )
4457{
4458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4459 psa_key_type_t key_type = key_type_arg;
4460 psa_algorithm_t alg = alg_arg;
4461 unsigned char *input = NULL;
4462 size_t input_buffer_size = 0;
4463 unsigned char *output = NULL;
4464 size_t output_buffer_size = 0;
4465 size_t output_length = 0;
4466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4467
4468 PSA_ASSERT( psa_crypto_init( ) );
4469
4470 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4471 psa_set_key_algorithm( &attributes, alg );
4472 psa_set_key_type( &attributes, key_type );
4473
4474 /* Allocate input buffer and copy the iv and the plaintext */
4475 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4476 if ( input_buffer_size > 0 )
4477 {
4478 ASSERT_ALLOC( input, input_buffer_size );
4479 memcpy( input, iv->x, iv->len );
4480 memcpy( input + iv->len, input_arg->x, input_arg->len );
4481 }
4482
4483 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4484 ASSERT_ALLOC( output, output_buffer_size );
4485
4486 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4487 &key ) );
4488
4489 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4490 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004491 TEST_LE_U( output_length,
4492 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4493 TEST_LE_U( output_length,
4494 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004495
4496 ASSERT_COMPARE( expected_output->x, expected_output->len,
4497 output, output_length );
4498exit:
4499 mbedtls_free( input );
4500 mbedtls_free( output );
4501 psa_destroy_key( key );
4502 PSA_DONE( );
4503}
4504/* END_CASE */
4505
4506/* BEGIN_CASE */
4507void cipher_verify_output( int alg_arg,
4508 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004509 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004510 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004511{
Ronald Cron5425a212020-08-04 14:58:35 +02004512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004513 psa_key_type_t key_type = key_type_arg;
4514 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004515 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004516 size_t output1_size = 0;
4517 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004518 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004519 size_t output2_size = 0;
4520 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004522
Gilles Peskine8817f612018-12-18 00:18:46 +01004523 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004524
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004525 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4526 psa_set_key_algorithm( &attributes, alg );
4527 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004528
Ronald Cron5425a212020-08-04 14:58:35 +02004529 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4530 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004531 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004532 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004533
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004534 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4535 output1, output1_size,
4536 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004537 TEST_LE_U( output1_length,
4538 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4539 TEST_LE_U( output1_length,
4540 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004541
4542 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004543 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004544
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004545 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4546 output2, output2_size,
4547 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004548 TEST_LE_U( output2_length,
4549 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4550 TEST_LE_U( output2_length,
4551 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004552
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004553 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004554
4555exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004556 mbedtls_free( output1 );
4557 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004558 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004559 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004560}
4561/* END_CASE */
4562
4563/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004564void cipher_verify_output_multipart( int alg_arg,
4565 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004566 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004567 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004568 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004569{
Ronald Cron5425a212020-08-04 14:58:35 +02004570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004571 psa_key_type_t key_type = key_type_arg;
4572 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004573 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004574 unsigned char iv[16] = {0};
4575 size_t iv_size = 16;
4576 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004577 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004578 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004579 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004580 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004581 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004582 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004583 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004584 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4585 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004587
Gilles Peskine8817f612018-12-18 00:18:46 +01004588 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004589
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004590 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4591 psa_set_key_algorithm( &attributes, alg );
4592 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004593
Ronald Cron5425a212020-08-04 14:58:35 +02004594 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4595 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004596
Ronald Cron5425a212020-08-04 14:58:35 +02004597 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4598 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004599
Steven Cooreman177deba2020-09-07 17:14:14 +02004600 if( alg != PSA_ALG_ECB_NO_PADDING )
4601 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004602 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4603 iv, iv_size,
4604 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004605 }
4606
gabor-mezei-armceface22021-01-21 12:26:17 +01004607 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004608 TEST_LE_U( output1_buffer_size,
4609 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004610 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004611
Gilles Peskine7be11a72022-04-14 00:12:57 +02004612 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004613
Gilles Peskine8817f612018-12-18 00:18:46 +01004614 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4615 output1, output1_buffer_size,
4616 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004617 TEST_LE_U( function_output_length,
4618 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4619 TEST_LE_U( function_output_length,
4620 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004621 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004622
Gilles Peskine8817f612018-12-18 00:18:46 +01004623 PSA_ASSERT( psa_cipher_update( &operation1,
4624 input->x + first_part_size,
4625 input->len - first_part_size,
4626 output1, output1_buffer_size,
4627 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004628 TEST_LE_U( function_output_length,
4629 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4630 alg,
4631 input->len - first_part_size ) );
4632 TEST_LE_U( function_output_length,
4633 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004634 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004635
Gilles Peskine8817f612018-12-18 00:18:46 +01004636 PSA_ASSERT( psa_cipher_finish( &operation1,
4637 output1 + output1_length,
4638 output1_buffer_size - output1_length,
4639 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004640 TEST_LE_U( function_output_length,
4641 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4642 TEST_LE_U( function_output_length,
4643 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004644 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004645
Gilles Peskine8817f612018-12-18 00:18:46 +01004646 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004647
Gilles Peskine048b7f02018-06-08 14:20:49 +02004648 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004649 TEST_LE_U( output2_buffer_size,
4650 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4651 TEST_LE_U( output2_buffer_size,
4652 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004653 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004654
Steven Cooreman177deba2020-09-07 17:14:14 +02004655 if( iv_length > 0 )
4656 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004657 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4658 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004659 }
Moran Pekerded84402018-06-06 16:36:50 +03004660
Gilles Peskine8817f612018-12-18 00:18:46 +01004661 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4662 output2, output2_buffer_size,
4663 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004664 TEST_LE_U( function_output_length,
4665 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4666 TEST_LE_U( function_output_length,
4667 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004668 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004669
Gilles Peskine8817f612018-12-18 00:18:46 +01004670 PSA_ASSERT( psa_cipher_update( &operation2,
4671 output1 + first_part_size,
4672 output1_length - first_part_size,
4673 output2, output2_buffer_size,
4674 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004675 TEST_LE_U( function_output_length,
4676 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4677 alg,
4678 output1_length - first_part_size ) );
4679 TEST_LE_U( function_output_length,
4680 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004681 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004682
Gilles Peskine8817f612018-12-18 00:18:46 +01004683 PSA_ASSERT( psa_cipher_finish( &operation2,
4684 output2 + output2_length,
4685 output2_buffer_size - output2_length,
4686 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004687 TEST_LE_U( function_output_length,
4688 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4689 TEST_LE_U( function_output_length,
4690 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004691 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004692
Gilles Peskine8817f612018-12-18 00:18:46 +01004693 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004694
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004695 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004696
4697exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004698 psa_cipher_abort( &operation1 );
4699 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004700 mbedtls_free( output1 );
4701 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004702 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004703 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004704}
4705/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004706
Gilles Peskine20035e32018-02-03 22:44:14 +01004707/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004708void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004709 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004710 data_t *nonce,
4711 data_t *additional_data,
4712 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004713 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004714{
Ronald Cron5425a212020-08-04 14:58:35 +02004715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004716 psa_key_type_t key_type = key_type_arg;
4717 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004718 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004719 unsigned char *output_data = NULL;
4720 size_t output_size = 0;
4721 size_t output_length = 0;
4722 unsigned char *output_data2 = NULL;
4723 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004724 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004725 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004727
Gilles Peskine8817f612018-12-18 00:18:46 +01004728 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004729
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4731 psa_set_key_algorithm( &attributes, alg );
4732 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004733
Gilles Peskine049c7532019-05-15 20:22:09 +02004734 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004735 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004736 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4737 key_bits = psa_get_key_bits( &attributes );
4738
4739 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4740 alg );
4741 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4742 * should be exact. */
4743 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4744 expected_result != PSA_ERROR_NOT_SUPPORTED )
4745 {
4746 TEST_EQUAL( output_size,
4747 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004748 TEST_LE_U( output_size,
4749 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004750 }
4751 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004752
Steven Cooremanf49478b2021-02-15 15:19:25 +01004753 status = psa_aead_encrypt( key, alg,
4754 nonce->x, nonce->len,
4755 additional_data->x,
4756 additional_data->len,
4757 input_data->x, input_data->len,
4758 output_data, output_size,
4759 &output_length );
4760
4761 /* If the operation is not supported, just skip and not fail in case the
4762 * encryption involves a common limitation of cryptography hardwares and
4763 * an alternative implementation. */
4764 if( status == PSA_ERROR_NOT_SUPPORTED )
4765 {
4766 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4767 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4768 }
4769
4770 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004771
4772 if( PSA_SUCCESS == expected_result )
4773 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004774 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775
Gilles Peskine003a4a92019-05-14 16:09:40 +02004776 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4777 * should be exact. */
4778 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004779 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004780
Gilles Peskine7be11a72022-04-14 00:12:57 +02004781 TEST_LE_U( input_data->len,
4782 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004783
Ronald Cron5425a212020-08-04 14:58:35 +02004784 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004785 nonce->x, nonce->len,
4786 additional_data->x,
4787 additional_data->len,
4788 output_data, output_length,
4789 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004790 &output_length2 ),
4791 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004792
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004793 ASSERT_COMPARE( input_data->x, input_data->len,
4794 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004796
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004798 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004799 mbedtls_free( output_data );
4800 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004801 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004802}
4803/* END_CASE */
4804
4805/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004806void aead_encrypt( int key_type_arg, data_t *key_data,
4807 int alg_arg,
4808 data_t *nonce,
4809 data_t *additional_data,
4810 data_t *input_data,
4811 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004812{
Ronald Cron5425a212020-08-04 14:58:35 +02004813 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004814 psa_key_type_t key_type = key_type_arg;
4815 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004816 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004817 unsigned char *output_data = NULL;
4818 size_t output_size = 0;
4819 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004821 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004822
Gilles Peskine8817f612018-12-18 00:18:46 +01004823 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004824
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004825 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4826 psa_set_key_algorithm( &attributes, alg );
4827 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004828
Gilles Peskine049c7532019-05-15 20:22:09 +02004829 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004830 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004831 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4832 key_bits = psa_get_key_bits( &attributes );
4833
4834 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4835 alg );
4836 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4837 * should be exact. */
4838 TEST_EQUAL( output_size,
4839 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004840 TEST_LE_U( output_size,
4841 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004842 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843
Steven Cooremand588ea12021-01-11 19:36:04 +01004844 status = psa_aead_encrypt( key, alg,
4845 nonce->x, nonce->len,
4846 additional_data->x, additional_data->len,
4847 input_data->x, input_data->len,
4848 output_data, output_size,
4849 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004850
Ronald Cron28a45ed2021-02-09 20:35:42 +01004851 /* If the operation is not supported, just skip and not fail in case the
4852 * encryption involves a common limitation of cryptography hardwares and
4853 * an alternative implementation. */
4854 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004855 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004856 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4857 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004858 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004859
4860 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004861 ASSERT_COMPARE( expected_result->x, expected_result->len,
4862 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004863
Gilles Peskinea1cac842018-06-11 19:33:02 +02004864exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004865 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004866 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004867 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004868}
4869/* END_CASE */
4870
4871/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004872void aead_decrypt( int key_type_arg, data_t *key_data,
4873 int alg_arg,
4874 data_t *nonce,
4875 data_t *additional_data,
4876 data_t *input_data,
4877 data_t *expected_data,
4878 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004879{
Ronald Cron5425a212020-08-04 14:58:35 +02004880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004881 psa_key_type_t key_type = key_type_arg;
4882 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004883 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884 unsigned char *output_data = NULL;
4885 size_t output_size = 0;
4886 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004888 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004889 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004890
Gilles Peskine8817f612018-12-18 00:18:46 +01004891 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004892
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4894 psa_set_key_algorithm( &attributes, alg );
4895 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004896
Gilles Peskine049c7532019-05-15 20:22:09 +02004897 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004898 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004899 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4900 key_bits = psa_get_key_bits( &attributes );
4901
4902 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4903 alg );
4904 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4905 expected_result != PSA_ERROR_NOT_SUPPORTED )
4906 {
4907 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4908 * should be exact. */
4909 TEST_EQUAL( output_size,
4910 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004911 TEST_LE_U( output_size,
4912 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004913 }
4914 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004915
Steven Cooremand588ea12021-01-11 19:36:04 +01004916 status = psa_aead_decrypt( key, alg,
4917 nonce->x, nonce->len,
4918 additional_data->x,
4919 additional_data->len,
4920 input_data->x, input_data->len,
4921 output_data, output_size,
4922 &output_length );
4923
Ronald Cron28a45ed2021-02-09 20:35:42 +01004924 /* If the operation is not supported, just skip and not fail in case the
4925 * decryption involves a common limitation of cryptography hardwares and
4926 * an alternative implementation. */
4927 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004928 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004929 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4930 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004931 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004932
4933 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004934
Gilles Peskine2d277862018-06-18 15:41:12 +02004935 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004936 ASSERT_COMPARE( expected_data->x, expected_data->len,
4937 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004938
Gilles Peskinea1cac842018-06-11 19:33:02 +02004939exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004940 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004941 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004942 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004943}
4944/* END_CASE */
4945
4946/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004947void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4948 int alg_arg,
4949 data_t *nonce,
4950 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004951 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004952 int do_set_lengths,
4953 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004954{
Paul Elliottd3f82412021-06-16 16:52:21 +01004955 size_t ad_part_len = 0;
4956 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004957 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004958
Paul Elliott32f46ba2021-09-23 18:24:36 +01004959 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004960 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004961 mbedtls_test_set_step( ad_part_len );
4962
4963 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004964 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004965 if( ad_part_len & 0x01 )
4966 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4967 else
4968 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004969 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004970
4971 /* Split ad into length(ad_part_len) parts. */
4972 if( !aead_multipart_internal_func( key_type_arg, key_data,
4973 alg_arg, nonce,
4974 additional_data,
4975 ad_part_len,
4976 input_data, -1,
4977 set_lengths_method,
4978 expected_output,
4979 1, 0 ) )
4980 break;
4981
4982 /* length(0) part, length(ad_part_len) part, length(0) part... */
4983 mbedtls_test_set_step( 1000 + ad_part_len );
4984
4985 if( !aead_multipart_internal_func( key_type_arg, key_data,
4986 alg_arg, nonce,
4987 additional_data,
4988 ad_part_len,
4989 input_data, -1,
4990 set_lengths_method,
4991 expected_output,
4992 1, 1 ) )
4993 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004994 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004995
Paul Elliott32f46ba2021-09-23 18:24:36 +01004996 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004997 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004998 /* Split data into length(data_part_len) parts. */
4999 mbedtls_test_set_step( 2000 + data_part_len );
5000
5001 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005002 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005003 if( data_part_len & 0x01 )
5004 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5005 else
5006 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005007 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005008
Paul Elliott32f46ba2021-09-23 18:24:36 +01005009 if( !aead_multipart_internal_func( key_type_arg, key_data,
5010 alg_arg, nonce,
5011 additional_data, -1,
5012 input_data, data_part_len,
5013 set_lengths_method,
5014 expected_output,
5015 1, 0 ) )
5016 break;
5017
5018 /* length(0) part, length(data_part_len) part, length(0) part... */
5019 mbedtls_test_set_step( 3000 + data_part_len );
5020
5021 if( !aead_multipart_internal_func( key_type_arg, key_data,
5022 alg_arg, nonce,
5023 additional_data, -1,
5024 input_data, data_part_len,
5025 set_lengths_method,
5026 expected_output,
5027 1, 1 ) )
5028 break;
5029 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005030
Paul Elliott8fc45162021-06-23 16:06:01 +01005031 /* Goto is required to silence warnings about unused labels, as we
5032 * don't actually do any test assertions in this function. */
5033 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005034}
5035/* END_CASE */
5036
5037/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5039 int alg_arg,
5040 data_t *nonce,
5041 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005042 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005043 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005044 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005045{
Paul Elliottd3f82412021-06-16 16:52:21 +01005046 size_t ad_part_len = 0;
5047 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005048 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005049
Paul Elliott32f46ba2021-09-23 18:24:36 +01005050 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005051 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005052 /* Split ad into length(ad_part_len) parts. */
5053 mbedtls_test_set_step( ad_part_len );
5054
5055 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005056 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005057 if( ad_part_len & 0x01 )
5058 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5059 else
5060 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005061 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005062
5063 if( !aead_multipart_internal_func( key_type_arg, key_data,
5064 alg_arg, nonce,
5065 additional_data,
5066 ad_part_len,
5067 input_data, -1,
5068 set_lengths_method,
5069 expected_output,
5070 0, 0 ) )
5071 break;
5072
5073 /* length(0) part, length(ad_part_len) part, length(0) part... */
5074 mbedtls_test_set_step( 1000 + ad_part_len );
5075
5076 if( !aead_multipart_internal_func( key_type_arg, key_data,
5077 alg_arg, nonce,
5078 additional_data,
5079 ad_part_len,
5080 input_data, -1,
5081 set_lengths_method,
5082 expected_output,
5083 0, 1 ) )
5084 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005085 }
5086
Paul Elliott32f46ba2021-09-23 18:24:36 +01005087 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005088 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005089 /* Split data into length(data_part_len) parts. */
5090 mbedtls_test_set_step( 2000 + data_part_len );
5091
5092 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005093 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005094 if( data_part_len & 0x01 )
5095 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5096 else
5097 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005098 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005099
5100 if( !aead_multipart_internal_func( key_type_arg, key_data,
5101 alg_arg, nonce,
5102 additional_data, -1,
5103 input_data, data_part_len,
5104 set_lengths_method,
5105 expected_output,
5106 0, 0 ) )
5107 break;
5108
5109 /* length(0) part, length(data_part_len) part, length(0) part... */
5110 mbedtls_test_set_step( 3000 + data_part_len );
5111
5112 if( !aead_multipart_internal_func( key_type_arg, key_data,
5113 alg_arg, nonce,
5114 additional_data, -1,
5115 input_data, data_part_len,
5116 set_lengths_method,
5117 expected_output,
5118 0, 1 ) )
5119 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005120 }
5121
Paul Elliott8fc45162021-06-23 16:06:01 +01005122 /* Goto is required to silence warnings about unused labels, as we
5123 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005124 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005125}
5126/* END_CASE */
5127
5128/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005129void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5130 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005131 int nonce_length,
5132 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005133 data_t *additional_data,
5134 data_t *input_data,
5135 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005136{
5137
5138 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5139 psa_key_type_t key_type = key_type_arg;
5140 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005141 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005142 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5144 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005145 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005146 size_t actual_nonce_length = 0;
5147 size_t expected_nonce_length = expected_nonce_length_arg;
5148 unsigned char *output = NULL;
5149 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005150 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005151 size_t ciphertext_size = 0;
5152 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005153 size_t tag_length = 0;
5154 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005155
5156 PSA_ASSERT( psa_crypto_init( ) );
5157
5158 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5159 psa_set_key_algorithm( & attributes, alg );
5160 psa_set_key_type( & attributes, key_type );
5161
5162 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5163 &key ) );
5164
5165 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5166
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005167 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5168
Paul Elliottf1277632021-08-24 18:11:37 +01005169 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005170
Paul Elliottf1277632021-08-24 18:11:37 +01005171 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005172
Gilles Peskine7be11a72022-04-14 00:12:57 +02005173 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005174
Paul Elliottf1277632021-08-24 18:11:37 +01005175 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005176
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005177 status = psa_aead_encrypt_setup( &operation, key, alg );
5178
5179 /* If the operation is not supported, just skip and not fail in case the
5180 * encryption involves a common limitation of cryptography hardwares and
5181 * an alternative implementation. */
5182 if( status == PSA_ERROR_NOT_SUPPORTED )
5183 {
5184 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005185 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005186 }
5187
5188 PSA_ASSERT( status );
5189
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005190 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005191 nonce_length,
5192 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005193
Paul Elliott693bf312021-07-23 17:40:41 +01005194 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005195
Paul Elliottf1277632021-08-24 18:11:37 +01005196 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005197
Paul Elliott88ecbe12021-09-22 17:23:03 +01005198 if( expected_status == PSA_SUCCESS )
5199 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5200 alg ) );
5201
Gilles Peskine7be11a72022-04-14 00:12:57 +02005202 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005203
Paul Elliott693bf312021-07-23 17:40:41 +01005204 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005205 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005206 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005207 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5208 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005209
5210 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5211 additional_data->len ) );
5212
5213 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005214 output, output_size,
5215 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005216
Paul Elliottf1277632021-08-24 18:11:37 +01005217 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5218 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005219 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5220 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005221
5222exit:
5223 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005224 mbedtls_free( output );
5225 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005226 psa_aead_abort( &operation );
5227 PSA_DONE( );
5228}
5229/* END_CASE */
5230
5231/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005232void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5233 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005234 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005235 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005236 data_t *additional_data,
5237 data_t *input_data,
5238 int expected_status_arg )
5239{
5240
5241 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5242 psa_key_type_t key_type = key_type_arg;
5243 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005244 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005245 uint8_t *nonce_buffer = NULL;
5246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5247 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5248 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005249 unsigned char *output = NULL;
5250 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005251 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005252 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005253 size_t ciphertext_size = 0;
5254 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005255 size_t tag_length = 0;
5256 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005257 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005258 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005259
5260 PSA_ASSERT( psa_crypto_init( ) );
5261
5262 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5263 psa_set_key_algorithm( &attributes, alg );
5264 psa_set_key_type( &attributes, key_type );
5265
5266 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5267 &key ) );
5268
5269 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5270
5271 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5272
Paul Elliott6f0e7202021-08-25 12:57:18 +01005273 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005274
Paul Elliott6f0e7202021-08-25 12:57:18 +01005275 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005276
Gilles Peskine7be11a72022-04-14 00:12:57 +02005277 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005278
Paul Elliott6f0e7202021-08-25 12:57:18 +01005279 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005280
Paul Elliott863864a2021-07-23 17:28:31 +01005281 status = psa_aead_encrypt_setup( &operation, key, alg );
5282
5283 /* If the operation is not supported, just skip and not fail in case the
5284 * encryption involves a common limitation of cryptography hardwares and
5285 * an alternative implementation. */
5286 if( status == PSA_ERROR_NOT_SUPPORTED )
5287 {
5288 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005289 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005290 }
5291
5292 PSA_ASSERT( status );
5293
Paul Elliott4023ffd2021-09-10 16:21:22 +01005294 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5295 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005296 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005297 /* Arbitrary size buffer, to test zero length valid buffer. */
5298 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005299 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005300 }
5301 else
5302 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005303 /* If length is zero, then this will return NULL. */
5304 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005305 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005306
Paul Elliott4023ffd2021-09-10 16:21:22 +01005307 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005308 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005309 for( index = 0; index < nonce_length - 1; ++index )
5310 {
5311 nonce_buffer[index] = 'a' + index;
5312 }
Paul Elliott66696b52021-08-16 18:42:41 +01005313 }
Paul Elliott863864a2021-07-23 17:28:31 +01005314 }
5315
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005316 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5317 {
5318 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5319 input_data->len ) );
5320 }
5321
Paul Elliott6f0e7202021-08-25 12:57:18 +01005322 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005323
Paul Elliott693bf312021-07-23 17:40:41 +01005324 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005325
5326 if( expected_status == PSA_SUCCESS )
5327 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005328 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5329 {
5330 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5331 input_data->len ) );
5332 }
5333 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5334 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005335
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005336 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5337 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5338 additional_data->len ),
5339 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005340
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005341 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005342 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005343 &ciphertext_length ),
5344 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005345
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005346 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005347 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005348 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5349 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005350 }
5351
5352exit:
5353 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005354 mbedtls_free( output );
5355 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005356 mbedtls_free( nonce_buffer );
5357 psa_aead_abort( &operation );
5358 PSA_DONE( );
5359}
5360/* END_CASE */
5361
5362/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005363void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5364 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005365 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005366 data_t *nonce,
5367 data_t *additional_data,
5368 data_t *input_data,
5369 int expected_status_arg )
5370{
5371
5372 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5373 psa_key_type_t key_type = key_type_arg;
5374 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005375 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5377 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5378 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005379 unsigned char *output = NULL;
5380 unsigned char *ciphertext = NULL;
5381 size_t output_size = output_size_arg;
5382 size_t ciphertext_size = 0;
5383 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005384 size_t tag_length = 0;
5385 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5386
5387 PSA_ASSERT( psa_crypto_init( ) );
5388
5389 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5390 psa_set_key_algorithm( &attributes, alg );
5391 psa_set_key_type( &attributes, key_type );
5392
5393 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5394 &key ) );
5395
5396 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5397
Paul Elliottc6d11d02021-09-01 12:04:23 +01005398 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005399
Paul Elliottc6d11d02021-09-01 12:04:23 +01005400 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005401
Paul Elliottc6d11d02021-09-01 12:04:23 +01005402 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005403
Paul Elliott43fbda62021-07-23 18:30:59 +01005404 status = psa_aead_encrypt_setup( &operation, key, alg );
5405
5406 /* If the operation is not supported, just skip and not fail in case the
5407 * encryption involves a common limitation of cryptography hardwares and
5408 * an alternative implementation. */
5409 if( status == PSA_ERROR_NOT_SUPPORTED )
5410 {
5411 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5412 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5413 }
5414
5415 PSA_ASSERT( status );
5416
Paul Elliott47b9a142021-10-07 15:04:57 +01005417 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5418 input_data->len ) );
5419
Paul Elliott43fbda62021-07-23 18:30:59 +01005420 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5421
5422 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5423 additional_data->len ) );
5424
5425 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005426 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005427
5428 TEST_EQUAL( status, expected_status );
5429
5430 if( expected_status == PSA_SUCCESS )
5431 {
5432 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005433 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5434 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005435 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5436 }
5437
5438exit:
5439 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005440 mbedtls_free( output );
5441 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005442 psa_aead_abort( &operation );
5443 PSA_DONE( );
5444}
5445/* END_CASE */
5446
Paul Elliott91b021e2021-07-23 18:52:31 +01005447/* BEGIN_CASE */
5448void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5449 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005450 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005451 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005452 data_t *nonce,
5453 data_t *additional_data,
5454 data_t *input_data,
5455 int expected_status_arg )
5456{
5457
5458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5459 psa_key_type_t key_type = key_type_arg;
5460 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005461 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5463 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5464 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005465 unsigned char *ciphertext = NULL;
5466 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005467 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005468 size_t ciphertext_size = 0;
5469 size_t ciphertext_length = 0;
5470 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005471 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005472 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005473
5474 PSA_ASSERT( psa_crypto_init( ) );
5475
5476 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5477 psa_set_key_algorithm( &attributes, alg );
5478 psa_set_key_type( &attributes, key_type );
5479
5480 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5481 &key ) );
5482
5483 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5484
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005485 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005486
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005487 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005488
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005489 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005490
Paul Elliott719c1322021-09-13 18:27:22 +01005491 ASSERT_ALLOC( tag_buffer, tag_size );
5492
Paul Elliott91b021e2021-07-23 18:52:31 +01005493 status = psa_aead_encrypt_setup( &operation, key, alg );
5494
5495 /* If the operation is not supported, just skip and not fail in case the
5496 * encryption involves a common limitation of cryptography hardwares and
5497 * an alternative implementation. */
5498 if( status == PSA_ERROR_NOT_SUPPORTED )
5499 {
5500 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5501 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5502 }
5503
5504 PSA_ASSERT( status );
5505
5506 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5507
Paul Elliott76bda482021-10-07 17:07:23 +01005508 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5509 input_data->len ) );
5510
Paul Elliott91b021e2021-07-23 18:52:31 +01005511 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5512 additional_data->len ) );
5513
5514 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005515 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005516
5517 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005518 status = psa_aead_finish( &operation, finish_ciphertext,
5519 finish_ciphertext_size,
5520 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005521 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005522
5523 TEST_EQUAL( status, expected_status );
5524
5525exit:
5526 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005527 mbedtls_free( ciphertext );
5528 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005529 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005530 psa_aead_abort( &operation );
5531 PSA_DONE( );
5532}
5533/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005534
5535/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005536void aead_multipart_verify( int key_type_arg, data_t *key_data,
5537 int alg_arg,
5538 data_t *nonce,
5539 data_t *additional_data,
5540 data_t *input_data,
5541 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005542 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005543 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005544 int expected_status_arg )
5545{
5546 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5547 psa_key_type_t key_type = key_type_arg;
5548 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005549 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005550 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5551 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5552 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005553 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005554 unsigned char *plaintext = NULL;
5555 unsigned char *finish_plaintext = NULL;
5556 size_t plaintext_size = 0;
5557 size_t plaintext_length = 0;
5558 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005559 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005560 unsigned char *tag_buffer = NULL;
5561 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005562
5563 PSA_ASSERT( psa_crypto_init( ) );
5564
5565 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5566 psa_set_key_algorithm( &attributes, alg );
5567 psa_set_key_type( &attributes, key_type );
5568
5569 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5570 &key ) );
5571
5572 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5573
5574 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5575 input_data->len );
5576
5577 ASSERT_ALLOC( plaintext, plaintext_size );
5578
5579 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5580
5581 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5582
Paul Elliott9961a662021-09-17 19:19:02 +01005583 status = psa_aead_decrypt_setup( &operation, key, alg );
5584
5585 /* If the operation is not supported, just skip and not fail in case the
5586 * encryption involves a common limitation of cryptography hardwares and
5587 * an alternative implementation. */
5588 if( status == PSA_ERROR_NOT_SUPPORTED )
5589 {
5590 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5591 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5592 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005593 TEST_EQUAL( status, expected_setup_status );
5594
5595 if( status != PSA_SUCCESS )
5596 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005597
5598 PSA_ASSERT( status );
5599
5600 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5601
Paul Elliottfec6f372021-10-06 17:15:02 +01005602 status = psa_aead_set_lengths( &operation, additional_data->len,
5603 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005604 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005605
Paul Elliott9961a662021-09-17 19:19:02 +01005606 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5607 additional_data->len ) );
5608
5609 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5610 input_data->len,
5611 plaintext, plaintext_size,
5612 &plaintext_length ) );
5613
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005614 if( tag_usage == USE_GIVEN_TAG )
5615 {
5616 tag_buffer = tag->x;
5617 tag_size = tag->len;
5618 }
5619
Paul Elliott9961a662021-09-17 19:19:02 +01005620 status = psa_aead_verify( &operation, finish_plaintext,
5621 verify_plaintext_size,
5622 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005623 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005624
5625 TEST_EQUAL( status, expected_status );
5626
5627exit:
5628 psa_destroy_key( key );
5629 mbedtls_free( plaintext );
5630 mbedtls_free( finish_plaintext );
5631 psa_aead_abort( &operation );
5632 PSA_DONE( );
5633}
5634/* END_CASE */
5635
Paul Elliott9961a662021-09-17 19:19:02 +01005636/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005637void aead_multipart_setup( int key_type_arg, data_t *key_data,
5638 int alg_arg, int expected_status_arg )
5639{
5640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5641 psa_key_type_t key_type = key_type_arg;
5642 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005643 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005644 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5645 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5646 psa_status_t expected_status = expected_status_arg;
5647
5648 PSA_ASSERT( psa_crypto_init( ) );
5649
5650 psa_set_key_usage_flags( &attributes,
5651 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5652 psa_set_key_algorithm( &attributes, alg );
5653 psa_set_key_type( &attributes, key_type );
5654
5655 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5656 &key ) );
5657
Paul Elliott5221ef62021-09-19 17:33:03 +01005658 status = psa_aead_encrypt_setup( &operation, key, alg );
5659
5660 TEST_EQUAL( status, expected_status );
5661
5662 psa_aead_abort( &operation );
5663
Paul Elliott5221ef62021-09-19 17:33:03 +01005664 status = psa_aead_decrypt_setup( &operation, key, alg );
5665
5666 TEST_EQUAL(status, expected_status );
5667
5668exit:
5669 psa_destroy_key( key );
5670 psa_aead_abort( &operation );
5671 PSA_DONE( );
5672}
5673/* END_CASE */
5674
5675/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005676void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5677 int alg_arg,
5678 data_t *nonce,
5679 data_t *additional_data,
5680 data_t *input_data )
5681{
5682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5683 psa_key_type_t key_type = key_type_arg;
5684 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005685 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005686 unsigned char *output_data = NULL;
5687 unsigned char *final_data = NULL;
5688 size_t output_size = 0;
5689 size_t finish_output_size = 0;
5690 size_t output_length = 0;
5691 size_t key_bits = 0;
5692 size_t tag_length = 0;
5693 size_t tag_size = 0;
5694 size_t nonce_length = 0;
5695 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5696 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5697 size_t output_part_length = 0;
5698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5699
5700 PSA_ASSERT( psa_crypto_init( ) );
5701
5702 psa_set_key_usage_flags( & attributes,
5703 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5704 psa_set_key_algorithm( & attributes, alg );
5705 psa_set_key_type( & attributes, key_type );
5706
5707 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5708 &key ) );
5709
5710 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5711 key_bits = psa_get_key_bits( &attributes );
5712
5713 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5714
Gilles Peskine7be11a72022-04-14 00:12:57 +02005715 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716
5717 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5718
5719 ASSERT_ALLOC( output_data, output_size );
5720
5721 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5722
Gilles Peskine7be11a72022-04-14 00:12:57 +02005723 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724
5725 ASSERT_ALLOC( final_data, finish_output_size );
5726
5727 /* Test all operations error without calling setup first. */
5728
Paul Elliottc23a9a02021-06-21 18:32:46 +01005729 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5730 PSA_ERROR_BAD_STATE );
5731
5732 psa_aead_abort( &operation );
5733
Paul Elliottc23a9a02021-06-21 18:32:46 +01005734 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5735 PSA_AEAD_NONCE_MAX_SIZE,
5736 &nonce_length ),
5737 PSA_ERROR_BAD_STATE );
5738
5739 psa_aead_abort( &operation );
5740
Paul Elliott481be342021-07-16 17:38:47 +01005741 /* ------------------------------------------------------- */
5742
Paul Elliottc23a9a02021-06-21 18:32:46 +01005743 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5744 input_data->len ),
5745 PSA_ERROR_BAD_STATE );
5746
5747 psa_aead_abort( &operation );
5748
Paul Elliott481be342021-07-16 17:38:47 +01005749 /* ------------------------------------------------------- */
5750
Paul Elliottc23a9a02021-06-21 18:32:46 +01005751 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5752 additional_data->len ),
5753 PSA_ERROR_BAD_STATE );
5754
5755 psa_aead_abort( &operation );
5756
Paul Elliott481be342021-07-16 17:38:47 +01005757 /* ------------------------------------------------------- */
5758
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5760 input_data->len, output_data,
5761 output_size, &output_length ),
5762 PSA_ERROR_BAD_STATE );
5763
5764 psa_aead_abort( &operation );
5765
Paul Elliott481be342021-07-16 17:38:47 +01005766 /* ------------------------------------------------------- */
5767
Paul Elliottc23a9a02021-06-21 18:32:46 +01005768 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5769 finish_output_size,
5770 &output_part_length,
5771 tag_buffer, tag_length,
5772 &tag_size ),
5773 PSA_ERROR_BAD_STATE );
5774
5775 psa_aead_abort( &operation );
5776
Paul Elliott481be342021-07-16 17:38:47 +01005777 /* ------------------------------------------------------- */
5778
Paul Elliottc23a9a02021-06-21 18:32:46 +01005779 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5780 finish_output_size,
5781 &output_part_length,
5782 tag_buffer,
5783 tag_length ),
5784 PSA_ERROR_BAD_STATE );
5785
5786 psa_aead_abort( &operation );
5787
5788 /* Test for double setups. */
5789
Paul Elliottc23a9a02021-06-21 18:32:46 +01005790 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5791
5792 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
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 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5800
5801 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5802 PSA_ERROR_BAD_STATE );
5803
5804 psa_aead_abort( &operation );
5805
Paul Elliott374a2be2021-07-16 17:53:40 +01005806 /* ------------------------------------------------------- */
5807
Paul Elliott374a2be2021-07-16 17:53:40 +01005808 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5809
5810 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5811 PSA_ERROR_BAD_STATE );
5812
5813 psa_aead_abort( &operation );
5814
5815 /* ------------------------------------------------------- */
5816
Paul Elliott374a2be2021-07-16 17:53:40 +01005817 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5818
5819 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5820 PSA_ERROR_BAD_STATE );
5821
5822 psa_aead_abort( &operation );
5823
Paul Elliottc23a9a02021-06-21 18:32:46 +01005824 /* Test for not setting a nonce. */
5825
Paul Elliottc23a9a02021-06-21 18:32:46 +01005826 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5827
5828 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5829 additional_data->len ),
5830 PSA_ERROR_BAD_STATE );
5831
5832 psa_aead_abort( &operation );
5833
Paul Elliott7f628422021-09-01 12:08:29 +01005834 /* ------------------------------------------------------- */
5835
Paul Elliott7f628422021-09-01 12:08:29 +01005836 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5837
5838 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5839 input_data->len, output_data,
5840 output_size, &output_length ),
5841 PSA_ERROR_BAD_STATE );
5842
5843 psa_aead_abort( &operation );
5844
Paul Elliottbdc2c682021-09-21 18:37:10 +01005845 /* ------------------------------------------------------- */
5846
Paul Elliottbdc2c682021-09-21 18:37:10 +01005847 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5848
5849 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5850 finish_output_size,
5851 &output_part_length,
5852 tag_buffer, tag_length,
5853 &tag_size ),
5854 PSA_ERROR_BAD_STATE );
5855
5856 psa_aead_abort( &operation );
5857
5858 /* ------------------------------------------------------- */
5859
Paul Elliottbdc2c682021-09-21 18:37:10 +01005860 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5861
5862 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5863 finish_output_size,
5864 &output_part_length,
5865 tag_buffer,
5866 tag_length ),
5867 PSA_ERROR_BAD_STATE );
5868
5869 psa_aead_abort( &operation );
5870
Paul Elliottc23a9a02021-06-21 18:32:46 +01005871 /* Test for double setting nonce. */
5872
Paul Elliottc23a9a02021-06-21 18:32:46 +01005873 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5874
5875 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5876
5877 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5878 PSA_ERROR_BAD_STATE );
5879
5880 psa_aead_abort( &operation );
5881
Paul Elliott374a2be2021-07-16 17:53:40 +01005882 /* Test for double generating nonce. */
5883
Paul Elliott374a2be2021-07-16 17:53:40 +01005884 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5885
5886 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5887 PSA_AEAD_NONCE_MAX_SIZE,
5888 &nonce_length ) );
5889
5890 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5891 PSA_AEAD_NONCE_MAX_SIZE,
5892 &nonce_length ),
5893 PSA_ERROR_BAD_STATE );
5894
5895
5896 psa_aead_abort( &operation );
5897
5898 /* Test for generate nonce then set and vice versa */
5899
Paul Elliott374a2be2021-07-16 17:53:40 +01005900 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5901
5902 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5903 PSA_AEAD_NONCE_MAX_SIZE,
5904 &nonce_length ) );
5905
5906 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5907 PSA_ERROR_BAD_STATE );
5908
5909 psa_aead_abort( &operation );
5910
Andrzej Kurekad837522021-12-15 15:28:49 +01005911 /* Test for generating nonce after calling set lengths */
5912
5913 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5914
5915 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5916 input_data->len ) );
5917
5918 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5919 PSA_AEAD_NONCE_MAX_SIZE,
5920 &nonce_length ) );
5921
5922 psa_aead_abort( &operation );
5923
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005924 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005925
5926 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5927
5928 if( operation.alg == PSA_ALG_CCM )
5929 {
5930 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5931 input_data->len ),
5932 PSA_ERROR_INVALID_ARGUMENT );
5933 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5934 PSA_AEAD_NONCE_MAX_SIZE,
5935 &nonce_length ),
5936 PSA_ERROR_BAD_STATE );
5937 }
5938 else
5939 {
5940 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5941 input_data->len ) );
5942 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5943 PSA_AEAD_NONCE_MAX_SIZE,
5944 &nonce_length ) );
5945 }
5946
5947 psa_aead_abort( &operation );
5948
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005949 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005950#if SIZE_MAX > UINT32_MAX
5951 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5952
5953 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5954 {
5955 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5956 input_data->len ),
5957 PSA_ERROR_INVALID_ARGUMENT );
5958 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5959 PSA_AEAD_NONCE_MAX_SIZE,
5960 &nonce_length ),
5961 PSA_ERROR_BAD_STATE );
5962 }
5963 else
5964 {
5965 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5966 input_data->len ) );
5967 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5968 PSA_AEAD_NONCE_MAX_SIZE,
5969 &nonce_length ) );
5970 }
5971
5972 psa_aead_abort( &operation );
5973#endif
5974
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005975 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005976
5977 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5978
5979 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5980 PSA_AEAD_NONCE_MAX_SIZE,
5981 &nonce_length ) );
5982
5983 if( operation.alg == PSA_ALG_CCM )
5984 {
5985 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5986 input_data->len ),
5987 PSA_ERROR_INVALID_ARGUMENT );
5988 }
5989 else
5990 {
5991 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5992 input_data->len ) );
5993 }
5994
5995 psa_aead_abort( &operation );
5996
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005997 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005998 /* Test for setting nonce after calling set lengths */
5999
6000 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6001
6002 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6003 input_data->len ) );
6004
6005 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6006
6007 psa_aead_abort( &operation );
6008
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006009 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006010
6011 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6012
6013 if( operation.alg == PSA_ALG_CCM )
6014 {
6015 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6016 input_data->len ),
6017 PSA_ERROR_INVALID_ARGUMENT );
6018 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6019 PSA_ERROR_BAD_STATE );
6020 }
6021 else
6022 {
6023 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6024 input_data->len ) );
6025 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6026 }
6027
6028 psa_aead_abort( &operation );
6029
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006030 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006031#if SIZE_MAX > UINT32_MAX
6032 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6033
6034 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6035 {
6036 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6037 input_data->len ),
6038 PSA_ERROR_INVALID_ARGUMENT );
6039 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6040 PSA_ERROR_BAD_STATE );
6041 }
6042 else
6043 {
6044 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6045 input_data->len ) );
6046 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6047 }
6048
6049 psa_aead_abort( &operation );
6050#endif
6051
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006052 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006053
6054 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6055
6056 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6057
6058 if( operation.alg == PSA_ALG_CCM )
6059 {
6060 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6061 input_data->len ),
6062 PSA_ERROR_INVALID_ARGUMENT );
6063 }
6064 else
6065 {
6066 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6067 input_data->len ) );
6068 }
6069
6070 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006071
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006072 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006073#if SIZE_MAX > UINT32_MAX
6074 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6075
6076 if( operation.alg == PSA_ALG_GCM )
6077 {
6078 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6079 SIZE_MAX ),
6080 PSA_ERROR_INVALID_ARGUMENT );
6081 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6082 PSA_ERROR_BAD_STATE );
6083 }
6084 else if ( operation.alg != PSA_ALG_CCM )
6085 {
6086 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6087 SIZE_MAX ) );
6088 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6089 }
6090
6091 psa_aead_abort( &operation );
6092
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006093 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006094 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_GCM )
6099 {
6100 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6101 SIZE_MAX ),
6102 PSA_ERROR_INVALID_ARGUMENT );
6103 }
6104 else if ( operation.alg != PSA_ALG_CCM )
6105 {
6106 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6107 SIZE_MAX ) );
6108 }
6109
6110 psa_aead_abort( &operation );
6111#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006112
6113 /* ------------------------------------------------------- */
6114
Paul Elliott374a2be2021-07-16 17:53:40 +01006115 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6116
6117 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6118
6119 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6120 PSA_AEAD_NONCE_MAX_SIZE,
6121 &nonce_length ),
6122 PSA_ERROR_BAD_STATE );
6123
6124 psa_aead_abort( &operation );
6125
Paul Elliott7220cae2021-06-22 17:25:57 +01006126 /* Test for generating nonce in decrypt setup. */
6127
Paul Elliott7220cae2021-06-22 17:25:57 +01006128 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6129
6130 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6131 PSA_AEAD_NONCE_MAX_SIZE,
6132 &nonce_length ),
6133 PSA_ERROR_BAD_STATE );
6134
6135 psa_aead_abort( &operation );
6136
Paul Elliottc23a9a02021-06-21 18:32:46 +01006137 /* Test for setting lengths twice. */
6138
Paul Elliottc23a9a02021-06-21 18:32:46 +01006139 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6140
6141 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6142
6143 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6144 input_data->len ) );
6145
6146 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6147 input_data->len ),
6148 PSA_ERROR_BAD_STATE );
6149
6150 psa_aead_abort( &operation );
6151
Andrzej Kurekad837522021-12-15 15:28:49 +01006152 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006153
Paul Elliottc23a9a02021-06-21 18:32:46 +01006154 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6155
6156 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6157
Andrzej Kurekad837522021-12-15 15:28:49 +01006158 if( operation.alg == PSA_ALG_CCM )
6159 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006160
Andrzej Kurekad837522021-12-15 15:28:49 +01006161 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6162 additional_data->len ),
6163 PSA_ERROR_BAD_STATE );
6164 }
6165 else
6166 {
6167 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6168 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006169
Andrzej Kurekad837522021-12-15 15:28:49 +01006170 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6171 input_data->len ),
6172 PSA_ERROR_BAD_STATE );
6173 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006174 psa_aead_abort( &operation );
6175
6176 /* ------------------------------------------------------- */
6177
Paul Elliottf94bd992021-09-19 18:15:59 +01006178 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6179
6180 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6181
Andrzej Kurekad837522021-12-15 15:28:49 +01006182 if( operation.alg == PSA_ALG_CCM )
6183 {
6184 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6185 input_data->len, output_data,
6186 output_size, &output_length ),
6187 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006188
Andrzej Kurekad837522021-12-15 15:28:49 +01006189 }
6190 else
6191 {
6192 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6193 input_data->len, output_data,
6194 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006195
Andrzej Kurekad837522021-12-15 15:28:49 +01006196 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6197 input_data->len ),
6198 PSA_ERROR_BAD_STATE );
6199 }
6200 psa_aead_abort( &operation );
6201
6202 /* ------------------------------------------------------- */
6203
6204 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6205
6206 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6207
6208 if( operation.alg == PSA_ALG_CCM )
6209 {
6210 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6211 finish_output_size,
6212 &output_part_length,
6213 tag_buffer, tag_length,
6214 &tag_size ) );
6215 }
6216 else
6217 {
6218 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6219 finish_output_size,
6220 &output_part_length,
6221 tag_buffer, tag_length,
6222 &tag_size ) );
6223
6224 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6225 input_data->len ),
6226 PSA_ERROR_BAD_STATE );
6227 }
6228 psa_aead_abort( &operation );
6229
6230 /* Test for setting lengths after generating nonce + already starting data. */
6231
6232 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6233
6234 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6235 PSA_AEAD_NONCE_MAX_SIZE,
6236 &nonce_length ) );
6237 if( operation.alg == PSA_ALG_CCM )
6238 {
6239
6240 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6241 additional_data->len ),
6242 PSA_ERROR_BAD_STATE );
6243 }
6244 else
6245 {
6246 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6247 additional_data->len ) );
6248
6249 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6250 input_data->len ),
6251 PSA_ERROR_BAD_STATE );
6252 }
6253 psa_aead_abort( &operation );
6254
6255 /* ------------------------------------------------------- */
6256
6257 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6258
6259 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6260 PSA_AEAD_NONCE_MAX_SIZE,
6261 &nonce_length ) );
6262 if( operation.alg == PSA_ALG_CCM )
6263 {
6264 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6265 input_data->len, output_data,
6266 output_size, &output_length ),
6267 PSA_ERROR_BAD_STATE );
6268
6269 }
6270 else
6271 {
6272 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6273 input_data->len, output_data,
6274 output_size, &output_length ) );
6275
6276 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6277 input_data->len ),
6278 PSA_ERROR_BAD_STATE );
6279 }
6280 psa_aead_abort( &operation );
6281
6282 /* ------------------------------------------------------- */
6283
6284 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6285
6286 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6287 PSA_AEAD_NONCE_MAX_SIZE,
6288 &nonce_length ) );
6289 if( operation.alg == PSA_ALG_CCM )
6290 {
6291 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6292 finish_output_size,
6293 &output_part_length,
6294 tag_buffer, tag_length,
6295 &tag_size ) );
6296 }
6297 else
6298 {
6299 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6300 finish_output_size,
6301 &output_part_length,
6302 tag_buffer, tag_length,
6303 &tag_size ) );
6304
6305 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6306 input_data->len ),
6307 PSA_ERROR_BAD_STATE );
6308 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006309 psa_aead_abort( &operation );
6310
Paul Elliott243080c2021-07-21 19:01:17 +01006311 /* Test for not sending any additional data or data after setting non zero
6312 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006313
Paul Elliottc23a9a02021-06-21 18:32:46 +01006314 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6315
6316 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6317
6318 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6319 input_data->len ) );
6320
6321 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6322 finish_output_size,
6323 &output_part_length,
6324 tag_buffer, tag_length,
6325 &tag_size ),
6326 PSA_ERROR_INVALID_ARGUMENT );
6327
6328 psa_aead_abort( &operation );
6329
Paul Elliott243080c2021-07-21 19:01:17 +01006330 /* Test for not sending any additional data or data after setting non-zero
6331 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006332
Paul Elliottc23a9a02021-06-21 18:32:46 +01006333 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6334
6335 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6336
6337 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6338 input_data->len ) );
6339
6340 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6341 finish_output_size,
6342 &output_part_length,
6343 tag_buffer,
6344 tag_length ),
6345 PSA_ERROR_INVALID_ARGUMENT );
6346
6347 psa_aead_abort( &operation );
6348
Paul Elliott243080c2021-07-21 19:01:17 +01006349 /* Test for not sending any additional data after setting a non-zero length
6350 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006351
Paul Elliottc23a9a02021-06-21 18:32:46 +01006352 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6353
6354 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6355
6356 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6357 input_data->len ) );
6358
6359 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6360 input_data->len, output_data,
6361 output_size, &output_length ),
6362 PSA_ERROR_INVALID_ARGUMENT );
6363
6364 psa_aead_abort( &operation );
6365
Paul Elliottf94bd992021-09-19 18:15:59 +01006366 /* Test for not sending any data after setting a non-zero length for it.*/
6367
Paul Elliottf94bd992021-09-19 18:15:59 +01006368 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6369
6370 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6371
6372 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6373 input_data->len ) );
6374
6375 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6376 additional_data->len ) );
6377
6378 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6379 finish_output_size,
6380 &output_part_length,
6381 tag_buffer, tag_length,
6382 &tag_size ),
6383 PSA_ERROR_INVALID_ARGUMENT );
6384
6385 psa_aead_abort( &operation );
6386
Paul Elliottb0450fe2021-09-01 15:06:26 +01006387 /* Test for sending too much additional data after setting lengths. */
6388
Paul Elliottb0450fe2021-09-01 15:06:26 +01006389 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6390
6391 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6392
6393 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6394
6395
6396 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6397 additional_data->len ),
6398 PSA_ERROR_INVALID_ARGUMENT );
6399
6400 psa_aead_abort( &operation );
6401
Paul Elliotta2a09b02021-09-22 14:56:40 +01006402 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006403
6404 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6405
6406 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6407
6408 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6409 input_data->len ) );
6410
6411 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6412 additional_data->len ) );
6413
6414 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6415 1 ),
6416 PSA_ERROR_INVALID_ARGUMENT );
6417
6418 psa_aead_abort( &operation );
6419
Paul Elliottb0450fe2021-09-01 15:06:26 +01006420 /* Test for sending too much data after setting lengths. */
6421
Paul Elliottb0450fe2021-09-01 15:06:26 +01006422 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6423
6424 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6425
6426 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6427
6428 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6429 input_data->len, output_data,
6430 output_size, &output_length ),
6431 PSA_ERROR_INVALID_ARGUMENT );
6432
6433 psa_aead_abort( &operation );
6434
Paul Elliotta2a09b02021-09-22 14:56:40 +01006435 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006436
6437 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6438
6439 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6440
6441 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6442 input_data->len ) );
6443
6444 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6445 additional_data->len ) );
6446
6447 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6448 input_data->len, output_data,
6449 output_size, &output_length ) );
6450
6451 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6452 1, output_data,
6453 output_size, &output_length ),
6454 PSA_ERROR_INVALID_ARGUMENT );
6455
6456 psa_aead_abort( &operation );
6457
Paul Elliottc23a9a02021-06-21 18:32:46 +01006458 /* Test sending additional data after data. */
6459
Paul Elliottc23a9a02021-06-21 18:32:46 +01006460 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6461
6462 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6463
Andrzej Kurekad837522021-12-15 15:28:49 +01006464 if( operation.alg != PSA_ALG_CCM )
6465 {
6466 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6467 input_data->len, output_data,
6468 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006469
Andrzej Kurekad837522021-12-15 15:28:49 +01006470 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6471 additional_data->len ),
6472 PSA_ERROR_BAD_STATE );
6473 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006474 psa_aead_abort( &operation );
6475
Paul Elliott534d0b42021-06-22 19:15:20 +01006476 /* Test calling finish on decryption. */
6477
Paul Elliott534d0b42021-06-22 19:15:20 +01006478 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6479
6480 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6481
6482 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6483 finish_output_size,
6484 &output_part_length,
6485 tag_buffer, tag_length,
6486 &tag_size ),
6487 PSA_ERROR_BAD_STATE );
6488
6489 psa_aead_abort( &operation );
6490
6491 /* Test calling verify on encryption. */
6492
Paul Elliott534d0b42021-06-22 19:15:20 +01006493 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6494
6495 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6496
6497 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6498 finish_output_size,
6499 &output_part_length,
6500 tag_buffer,
6501 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006502 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006503
6504 psa_aead_abort( &operation );
6505
6506
Paul Elliottc23a9a02021-06-21 18:32:46 +01006507exit:
6508 psa_destroy_key( key );
6509 psa_aead_abort( &operation );
6510 mbedtls_free( output_data );
6511 mbedtls_free( final_data );
6512 PSA_DONE( );
6513}
6514/* END_CASE */
6515
6516/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006517void signature_size( int type_arg,
6518 int bits,
6519 int alg_arg,
6520 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006521{
6522 psa_key_type_t type = type_arg;
6523 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006524 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006525
Gilles Peskinefe11b722018-12-18 00:24:04 +01006526 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006527
Gilles Peskinee59236f2018-01-27 23:32:46 +01006528exit:
6529 ;
6530}
6531/* END_CASE */
6532
6533/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006534void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6535 int alg_arg, data_t *input_data,
6536 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006537{
Ronald Cron5425a212020-08-04 14:58:35 +02006538 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006539 psa_key_type_t key_type = key_type_arg;
6540 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006541 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006542 unsigned char *signature = NULL;
6543 size_t signature_size;
6544 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006546
Gilles Peskine8817f612018-12-18 00:18:46 +01006547 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006548
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006549 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006550 psa_set_key_algorithm( &attributes, alg );
6551 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006552
Gilles Peskine049c7532019-05-15 20:22:09 +02006553 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006554 &key ) );
6555 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006556 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006557
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006558 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006559 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006560 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006561 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006562 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006563 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006564 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006565
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006566 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006567 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006568 input_data->x, input_data->len,
6569 signature, signature_size,
6570 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006571 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006572 ASSERT_COMPARE( output_data->x, output_data->len,
6573 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006574
6575exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006576 /*
6577 * Key attributes may have been returned by psa_get_key_attributes()
6578 * thus reset them as required.
6579 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006580 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006581
Ronald Cron5425a212020-08-04 14:58:35 +02006582 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006583 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006584 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006585}
6586/* END_CASE */
6587
6588/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006589void sign_hash_fail( int key_type_arg, data_t *key_data,
6590 int alg_arg, data_t *input_data,
6591 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006592{
Ronald Cron5425a212020-08-04 14:58:35 +02006593 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006594 psa_key_type_t key_type = key_type_arg;
6595 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006596 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006597 psa_status_t actual_status;
6598 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006599 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006600 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006601 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006602
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006603 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006604
Gilles Peskine8817f612018-12-18 00:18:46 +01006605 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006606
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006608 psa_set_key_algorithm( &attributes, alg );
6609 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006610
Gilles Peskine049c7532019-05-15 20:22:09 +02006611 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006612 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006613
Ronald Cron5425a212020-08-04 14:58:35 +02006614 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006615 input_data->x, input_data->len,
6616 signature, signature_size,
6617 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006618 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006619 /* The value of *signature_length is unspecified on error, but
6620 * whatever it is, it should be less than signature_size, so that
6621 * if the caller tries to read *signature_length bytes without
6622 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006623 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006624
6625exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006626 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006627 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006628 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006629 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006630}
6631/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006632
6633/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006634void sign_verify_hash( int key_type_arg, data_t *key_data,
6635 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006636{
Ronald Cron5425a212020-08-04 14:58:35 +02006637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006638 psa_key_type_t key_type = key_type_arg;
6639 psa_algorithm_t alg = alg_arg;
6640 size_t key_bits;
6641 unsigned char *signature = NULL;
6642 size_t signature_size;
6643 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006644 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006645
Gilles Peskine8817f612018-12-18 00:18:46 +01006646 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006647
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006648 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006649 psa_set_key_algorithm( &attributes, alg );
6650 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006651
Gilles Peskine049c7532019-05-15 20:22:09 +02006652 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006653 &key ) );
6654 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006655 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006656
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006657 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006658 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006659 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006660 key_bits, alg );
6661 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006662 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006663 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006664
6665 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006666 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006667 input_data->x, input_data->len,
6668 signature, signature_size,
6669 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006670 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006671 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006672 TEST_ASSERT( signature_length > 0 );
6673
6674 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006675 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006676 input_data->x, input_data->len,
6677 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006678
6679 if( input_data->len != 0 )
6680 {
6681 /* Flip a bit in the input and verify that the signature is now
6682 * detected as invalid. Flip a bit at the beginning, not at the end,
6683 * because ECDSA may ignore the last few bits of the input. */
6684 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006685 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006686 input_data->x, input_data->len,
6687 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006688 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006689 }
6690
6691exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006692 /*
6693 * Key attributes may have been returned by psa_get_key_attributes()
6694 * thus reset them as required.
6695 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006696 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006697
Ronald Cron5425a212020-08-04 14:58:35 +02006698 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006699 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006700 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006701}
6702/* END_CASE */
6703
6704/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006705void verify_hash( int key_type_arg, data_t *key_data,
6706 int alg_arg, data_t *hash_data,
6707 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006708{
Ronald Cron5425a212020-08-04 14:58:35 +02006709 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006710 psa_key_type_t key_type = key_type_arg;
6711 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006713
Gilles Peskine7be11a72022-04-14 00:12:57 +02006714 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006715
Gilles Peskine8817f612018-12-18 00:18:46 +01006716 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006717
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006718 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006719 psa_set_key_algorithm( &attributes, alg );
6720 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006721
Gilles Peskine049c7532019-05-15 20:22:09 +02006722 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006723 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006724
Ronald Cron5425a212020-08-04 14:58:35 +02006725 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006726 hash_data->x, hash_data->len,
6727 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006728
itayzafrir5c753392018-05-08 11:18:38 +03006729exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006730 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006731 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006732 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006733}
6734/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006735
6736/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006737void verify_hash_fail( int key_type_arg, data_t *key_data,
6738 int alg_arg, data_t *hash_data,
6739 data_t *signature_data,
6740 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006741{
Ronald Cron5425a212020-08-04 14:58:35 +02006742 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006743 psa_key_type_t key_type = key_type_arg;
6744 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006745 psa_status_t actual_status;
6746 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006748
Gilles Peskine8817f612018-12-18 00:18:46 +01006749 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006750
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006752 psa_set_key_algorithm( &attributes, alg );
6753 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006754
Gilles Peskine049c7532019-05-15 20:22:09 +02006755 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006756 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006757
Ronald Cron5425a212020-08-04 14:58:35 +02006758 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006759 hash_data->x, hash_data->len,
6760 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006761 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006762
6763exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006764 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006765 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006766 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006767}
6768/* END_CASE */
6769
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006770/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006771void sign_message_deterministic( int key_type_arg,
6772 data_t *key_data,
6773 int alg_arg,
6774 data_t *input_data,
6775 data_t *output_data )
6776{
6777 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6778 psa_key_type_t key_type = key_type_arg;
6779 psa_algorithm_t alg = alg_arg;
6780 size_t key_bits;
6781 unsigned char *signature = NULL;
6782 size_t signature_size;
6783 size_t signature_length = 0xdeadbeef;
6784 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6785
6786 PSA_ASSERT( psa_crypto_init( ) );
6787
6788 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6789 psa_set_key_algorithm( &attributes, alg );
6790 psa_set_key_type( &attributes, key_type );
6791
6792 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6793 &key ) );
6794 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6795 key_bits = psa_get_key_bits( &attributes );
6796
6797 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6798 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006799 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006800 ASSERT_ALLOC( signature, signature_size );
6801
6802 PSA_ASSERT( psa_sign_message( key, alg,
6803 input_data->x, input_data->len,
6804 signature, signature_size,
6805 &signature_length ) );
6806
6807 ASSERT_COMPARE( output_data->x, output_data->len,
6808 signature, signature_length );
6809
6810exit:
6811 psa_reset_key_attributes( &attributes );
6812
6813 psa_destroy_key( key );
6814 mbedtls_free( signature );
6815 PSA_DONE( );
6816
6817}
6818/* END_CASE */
6819
6820/* BEGIN_CASE */
6821void sign_message_fail( int key_type_arg,
6822 data_t *key_data,
6823 int alg_arg,
6824 data_t *input_data,
6825 int signature_size_arg,
6826 int expected_status_arg )
6827{
6828 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6829 psa_key_type_t key_type = key_type_arg;
6830 psa_algorithm_t alg = alg_arg;
6831 size_t signature_size = signature_size_arg;
6832 psa_status_t actual_status;
6833 psa_status_t expected_status = expected_status_arg;
6834 unsigned char *signature = NULL;
6835 size_t signature_length = 0xdeadbeef;
6836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6837
6838 ASSERT_ALLOC( signature, signature_size );
6839
6840 PSA_ASSERT( psa_crypto_init( ) );
6841
6842 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6843 psa_set_key_algorithm( &attributes, alg );
6844 psa_set_key_type( &attributes, key_type );
6845
6846 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6847 &key ) );
6848
6849 actual_status = psa_sign_message( key, alg,
6850 input_data->x, input_data->len,
6851 signature, signature_size,
6852 &signature_length );
6853 TEST_EQUAL( actual_status, expected_status );
6854 /* The value of *signature_length is unspecified on error, but
6855 * whatever it is, it should be less than signature_size, so that
6856 * if the caller tries to read *signature_length bytes without
6857 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006858 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006859
6860exit:
6861 psa_reset_key_attributes( &attributes );
6862 psa_destroy_key( key );
6863 mbedtls_free( signature );
6864 PSA_DONE( );
6865}
6866/* END_CASE */
6867
6868/* BEGIN_CASE */
6869void sign_verify_message( int key_type_arg,
6870 data_t *key_data,
6871 int alg_arg,
6872 data_t *input_data )
6873{
6874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6875 psa_key_type_t key_type = key_type_arg;
6876 psa_algorithm_t alg = alg_arg;
6877 size_t key_bits;
6878 unsigned char *signature = NULL;
6879 size_t signature_size;
6880 size_t signature_length = 0xdeadbeef;
6881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6882
6883 PSA_ASSERT( psa_crypto_init( ) );
6884
6885 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6886 PSA_KEY_USAGE_VERIFY_MESSAGE );
6887 psa_set_key_algorithm( &attributes, alg );
6888 psa_set_key_type( &attributes, key_type );
6889
6890 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6891 &key ) );
6892 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6893 key_bits = psa_get_key_bits( &attributes );
6894
6895 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6896 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006897 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006898 ASSERT_ALLOC( signature, signature_size );
6899
6900 PSA_ASSERT( psa_sign_message( key, alg,
6901 input_data->x, input_data->len,
6902 signature, signature_size,
6903 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006904 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006905 TEST_ASSERT( signature_length > 0 );
6906
6907 PSA_ASSERT( psa_verify_message( key, alg,
6908 input_data->x, input_data->len,
6909 signature, signature_length ) );
6910
6911 if( input_data->len != 0 )
6912 {
6913 /* Flip a bit in the input and verify that the signature is now
6914 * detected as invalid. Flip a bit at the beginning, not at the end,
6915 * because ECDSA may ignore the last few bits of the input. */
6916 input_data->x[0] ^= 1;
6917 TEST_EQUAL( psa_verify_message( key, alg,
6918 input_data->x, input_data->len,
6919 signature, signature_length ),
6920 PSA_ERROR_INVALID_SIGNATURE );
6921 }
6922
6923exit:
6924 psa_reset_key_attributes( &attributes );
6925
6926 psa_destroy_key( key );
6927 mbedtls_free( signature );
6928 PSA_DONE( );
6929}
6930/* END_CASE */
6931
6932/* BEGIN_CASE */
6933void verify_message( int key_type_arg,
6934 data_t *key_data,
6935 int alg_arg,
6936 data_t *input_data,
6937 data_t *signature_data )
6938{
6939 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6940 psa_key_type_t key_type = key_type_arg;
6941 psa_algorithm_t alg = alg_arg;
6942 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6943
Gilles Peskine7be11a72022-04-14 00:12:57 +02006944 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006945
6946 PSA_ASSERT( psa_crypto_init( ) );
6947
6948 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6949 psa_set_key_algorithm( &attributes, alg );
6950 psa_set_key_type( &attributes, key_type );
6951
6952 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6953 &key ) );
6954
6955 PSA_ASSERT( psa_verify_message( key, alg,
6956 input_data->x, input_data->len,
6957 signature_data->x, signature_data->len ) );
6958
6959exit:
6960 psa_reset_key_attributes( &attributes );
6961 psa_destroy_key( key );
6962 PSA_DONE( );
6963}
6964/* END_CASE */
6965
6966/* BEGIN_CASE */
6967void verify_message_fail( int key_type_arg,
6968 data_t *key_data,
6969 int alg_arg,
6970 data_t *hash_data,
6971 data_t *signature_data,
6972 int expected_status_arg )
6973{
6974 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6975 psa_key_type_t key_type = key_type_arg;
6976 psa_algorithm_t alg = alg_arg;
6977 psa_status_t actual_status;
6978 psa_status_t expected_status = expected_status_arg;
6979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6980
6981 PSA_ASSERT( psa_crypto_init( ) );
6982
6983 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6984 psa_set_key_algorithm( &attributes, alg );
6985 psa_set_key_type( &attributes, key_type );
6986
6987 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6988 &key ) );
6989
6990 actual_status = psa_verify_message( key, alg,
6991 hash_data->x, hash_data->len,
6992 signature_data->x,
6993 signature_data->len );
6994 TEST_EQUAL( actual_status, expected_status );
6995
6996exit:
6997 psa_reset_key_attributes( &attributes );
6998 psa_destroy_key( key );
6999 PSA_DONE( );
7000}
7001/* END_CASE */
7002
7003/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02007004void asymmetric_encrypt( int key_type_arg,
7005 data_t *key_data,
7006 int alg_arg,
7007 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02007008 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02007009 int expected_output_length_arg,
7010 int expected_status_arg )
7011{
Ronald Cron5425a212020-08-04 14:58:35 +02007012 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007013 psa_key_type_t key_type = key_type_arg;
7014 psa_algorithm_t alg = alg_arg;
7015 size_t expected_output_length = expected_output_length_arg;
7016 size_t key_bits;
7017 unsigned char *output = NULL;
7018 size_t output_size;
7019 size_t output_length = ~0;
7020 psa_status_t actual_status;
7021 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007022 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007023
Gilles Peskine8817f612018-12-18 00:18:46 +01007024 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007025
Gilles Peskine656896e2018-06-29 19:12:28 +02007026 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007027 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7028 psa_set_key_algorithm( &attributes, alg );
7029 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007030 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007031 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007032
7033 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007034 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007035 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007036
Gilles Peskine656896e2018-06-29 19:12:28 +02007037 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007038 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007039 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007040
7041 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007042 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007043 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007044 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007045 output, output_size,
7046 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007047 TEST_EQUAL( actual_status, expected_status );
7048 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007049
Gilles Peskine68428122018-06-30 18:42:41 +02007050 /* If the label is empty, the test framework puts a non-null pointer
7051 * in label->x. Test that a null pointer works as well. */
7052 if( label->len == 0 )
7053 {
7054 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007055 if( output_size != 0 )
7056 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007057 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007058 input_data->x, input_data->len,
7059 NULL, label->len,
7060 output, output_size,
7061 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007062 TEST_EQUAL( actual_status, expected_status );
7063 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007064 }
7065
Gilles Peskine656896e2018-06-29 19:12:28 +02007066exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007067 /*
7068 * Key attributes may have been returned by psa_get_key_attributes()
7069 * thus reset them as required.
7070 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007071 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007072
Ronald Cron5425a212020-08-04 14:58:35 +02007073 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007074 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007075 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007076}
7077/* END_CASE */
7078
7079/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007080void asymmetric_encrypt_decrypt( int key_type_arg,
7081 data_t *key_data,
7082 int alg_arg,
7083 data_t *input_data,
7084 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007085{
Ronald Cron5425a212020-08-04 14:58:35 +02007086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007087 psa_key_type_t key_type = key_type_arg;
7088 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007089 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007090 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007091 size_t output_size;
7092 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007093 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007094 size_t output2_size;
7095 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007097
Gilles Peskine8817f612018-12-18 00:18:46 +01007098 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007099
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7101 psa_set_key_algorithm( &attributes, alg );
7102 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007103
Gilles Peskine049c7532019-05-15 20:22:09 +02007104 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007105 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007106
7107 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007108 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007109 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007110
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007111 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007112 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007113 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007114
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007115 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007116 TEST_LE_U( output2_size,
7117 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7118 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007119 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007120
Gilles Peskineeebd7382018-06-08 18:11:54 +02007121 /* We test encryption by checking that encrypt-then-decrypt gives back
7122 * the original plaintext because of the non-optional random
7123 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007124 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007125 input_data->x, input_data->len,
7126 label->x, label->len,
7127 output, output_size,
7128 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007129 /* We don't know what ciphertext length to expect, but check that
7130 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007131 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007132
Ronald Cron5425a212020-08-04 14:58:35 +02007133 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007134 output, output_length,
7135 label->x, label->len,
7136 output2, output2_size,
7137 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007138 ASSERT_COMPARE( input_data->x, input_data->len,
7139 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007140
7141exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007142 /*
7143 * Key attributes may have been returned by psa_get_key_attributes()
7144 * thus reset them as required.
7145 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007146 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007147
Ronald Cron5425a212020-08-04 14:58:35 +02007148 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007149 mbedtls_free( output );
7150 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007151 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007152}
7153/* END_CASE */
7154
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007155/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007156void asymmetric_decrypt( int key_type_arg,
7157 data_t *key_data,
7158 int alg_arg,
7159 data_t *input_data,
7160 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007161 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007162{
Ronald Cron5425a212020-08-04 14:58:35 +02007163 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007164 psa_key_type_t key_type = key_type_arg;
7165 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007166 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007167 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007168 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007169 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007171
Gilles Peskine8817f612018-12-18 00:18:46 +01007172 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007173
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007174 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7175 psa_set_key_algorithm( &attributes, alg );
7176 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007177
Gilles Peskine049c7532019-05-15 20:22:09 +02007178 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007179 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007180
gabor-mezei-armceface22021-01-21 12:26:17 +01007181 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7182 key_bits = psa_get_key_bits( &attributes );
7183
7184 /* Determine the maximum ciphertext length */
7185 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007186 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007187 ASSERT_ALLOC( output, output_size );
7188
Ronald Cron5425a212020-08-04 14:58:35 +02007189 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007190 input_data->x, input_data->len,
7191 label->x, label->len,
7192 output,
7193 output_size,
7194 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007195 ASSERT_COMPARE( expected_data->x, expected_data->len,
7196 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007197
Gilles Peskine68428122018-06-30 18:42:41 +02007198 /* If the label is empty, the test framework puts a non-null pointer
7199 * in label->x. Test that a null pointer works as well. */
7200 if( label->len == 0 )
7201 {
7202 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007203 if( output_size != 0 )
7204 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007205 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007206 input_data->x, input_data->len,
7207 NULL, label->len,
7208 output,
7209 output_size,
7210 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007211 ASSERT_COMPARE( expected_data->x, expected_data->len,
7212 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007213 }
7214
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007215exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007216 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007217 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007218 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007219 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007220}
7221/* END_CASE */
7222
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007223/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007224void asymmetric_decrypt_fail( int key_type_arg,
7225 data_t *key_data,
7226 int alg_arg,
7227 data_t *input_data,
7228 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007229 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007230 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007231{
Ronald Cron5425a212020-08-04 14:58:35 +02007232 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007233 psa_key_type_t key_type = key_type_arg;
7234 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007235 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007236 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007237 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007238 psa_status_t actual_status;
7239 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007241
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007242 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007243
Gilles Peskine8817f612018-12-18 00:18:46 +01007244 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007245
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007246 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7247 psa_set_key_algorithm( &attributes, alg );
7248 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007249
Gilles Peskine049c7532019-05-15 20:22:09 +02007250 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007251 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007252
Ronald Cron5425a212020-08-04 14:58:35 +02007253 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007254 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007255 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007256 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007257 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007258 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007259 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007260
Gilles Peskine68428122018-06-30 18:42:41 +02007261 /* If the label is empty, the test framework puts a non-null pointer
7262 * in label->x. Test that a null pointer works as well. */
7263 if( label->len == 0 )
7264 {
7265 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007266 if( output_size != 0 )
7267 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007268 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007269 input_data->x, input_data->len,
7270 NULL, label->len,
7271 output, output_size,
7272 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007273 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007274 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007275 }
7276
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007277exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007278 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007279 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007280 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007281 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007282}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007283/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007284
7285/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007286void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007287{
7288 /* Test each valid way of initializing the object, except for `= {0}`, as
7289 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7290 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007291 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007292 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007293 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7294 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7295 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007296
7297 memset( &zero, 0, sizeof( zero ) );
7298
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007299 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007300 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007301 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007302 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007303 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007304 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007305 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007306
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007307 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007308 PSA_ASSERT( psa_key_derivation_abort(&func) );
7309 PSA_ASSERT( psa_key_derivation_abort(&init) );
7310 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007311}
7312/* END_CASE */
7313
Janos Follath16de4a42019-06-13 16:32:24 +01007314/* BEGIN_CASE */
7315void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007316{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007317 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007318 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007319 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007320
Gilles Peskine8817f612018-12-18 00:18:46 +01007321 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007322
Janos Follath16de4a42019-06-13 16:32:24 +01007323 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007324 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007325
7326exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007327 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007328 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007329}
7330/* END_CASE */
7331
Janos Follathaf3c2a02019-06-12 12:34:34 +01007332/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007333void derive_set_capacity( int alg_arg, int capacity_arg,
7334 int expected_status_arg )
7335{
7336 psa_algorithm_t alg = alg_arg;
7337 size_t capacity = capacity_arg;
7338 psa_status_t expected_status = expected_status_arg;
7339 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7340
7341 PSA_ASSERT( psa_crypto_init( ) );
7342
7343 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7344
7345 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7346 expected_status );
7347
7348exit:
7349 psa_key_derivation_abort( &operation );
7350 PSA_DONE( );
7351}
7352/* END_CASE */
7353
7354/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007355void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007356 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007357 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007358 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007359 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007360 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007361 int expected_status_arg3,
7362 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007363{
7364 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007365 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7366 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007367 psa_status_t expected_statuses[] = {expected_status_arg1,
7368 expected_status_arg2,
7369 expected_status_arg3};
7370 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007371 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7372 MBEDTLS_SVC_KEY_ID_INIT,
7373 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007374 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7375 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7376 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007377 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007378 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007379 psa_status_t expected_output_status = expected_output_status_arg;
7380 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007381
7382 PSA_ASSERT( psa_crypto_init( ) );
7383
7384 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7385 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007386
7387 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7388
7389 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7390 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007391 mbedtls_test_set_step( i );
7392 if( steps[i] == 0 )
7393 {
7394 /* Skip this step */
7395 }
7396 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007397 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007398 psa_set_key_type( &attributes, key_types[i] );
7399 PSA_ASSERT( psa_import_key( &attributes,
7400 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007401 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007402 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7403 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7404 {
7405 // When taking a private key as secret input, use key agreement
7406 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007407 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7408 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007409 expected_statuses[i] );
7410 }
7411 else
7412 {
7413 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007414 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007415 expected_statuses[i] );
7416 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007417 }
7418 else
7419 {
7420 TEST_EQUAL( psa_key_derivation_input_bytes(
7421 &operation, steps[i],
7422 inputs[i]->x, inputs[i]->len ),
7423 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007424 }
7425 }
7426
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007427 if( output_key_type != PSA_KEY_TYPE_NONE )
7428 {
7429 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007430 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007431 psa_set_key_bits( &attributes, 8 );
7432 actual_output_status =
7433 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007434 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007435 }
7436 else
7437 {
7438 uint8_t buffer[1];
7439 actual_output_status =
7440 psa_key_derivation_output_bytes( &operation,
7441 buffer, sizeof( buffer ) );
7442 }
7443 TEST_EQUAL( actual_output_status, expected_output_status );
7444
Janos Follathaf3c2a02019-06-12 12:34:34 +01007445exit:
7446 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007447 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7448 psa_destroy_key( keys[i] );
7449 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007450 PSA_DONE( );
7451}
7452/* END_CASE */
7453
Janos Follathd958bb72019-07-03 15:02:16 +01007454/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007455void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007456{
Janos Follathd958bb72019-07-03 15:02:16 +01007457 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007458 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007459 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007460 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007461 unsigned char input1[] = "Input 1";
7462 size_t input1_length = sizeof( input1 );
7463 unsigned char input2[] = "Input 2";
7464 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007465 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007466 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007467 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7468 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7469 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007470 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007471
Gilles Peskine8817f612018-12-18 00:18:46 +01007472 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007473
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007474 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7475 psa_set_key_algorithm( &attributes, alg );
7476 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007477
Gilles Peskine73676cb2019-05-15 20:15:10 +02007478 PSA_ASSERT( psa_import_key( &attributes,
7479 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007480 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007481
7482 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007483 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7484 input1, input1_length,
7485 input2, input2_length,
7486 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007487 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007488
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007489 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007490 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007491 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007493 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007495 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007496 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007497
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007498exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007499 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007500 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007501 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007502}
7503/* END_CASE */
7504
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007505/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007506void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007507{
7508 uint8_t output_buffer[16];
7509 size_t buffer_size = 16;
7510 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007511 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007512
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007513 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7514 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007515 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007516
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007517 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007518 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007519
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007520 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007521
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007522 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7523 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007524 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007525
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007526 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007527 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007528
7529exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007530 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007531}
7532/* END_CASE */
7533
7534/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007535void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007536 int step1_arg, data_t *input1, int expected_status_arg1,
7537 int step2_arg, data_t *input2, int expected_status_arg2,
7538 int step3_arg, data_t *input3, int expected_status_arg3,
7539 int step4_arg, data_t *input4, int expected_status_arg4,
7540 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007541 int requested_capacity_arg,
7542 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007543 data_t *expected_output2,
7544 int other_key_input_type,
7545 int key_input_type,
7546 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007547{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007548 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007549 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7550 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007551 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7552 MBEDTLS_SVC_KEY_ID_INIT,
7553 MBEDTLS_SVC_KEY_ID_INIT,
7554 MBEDTLS_SVC_KEY_ID_INIT};
7555 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7556 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007557 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007558 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007559 uint8_t *expected_outputs[2] =
7560 {expected_output1->x, expected_output2->x};
7561 size_t output_sizes[2] =
7562 {expected_output1->len, expected_output2->len};
7563 size_t output_buffer_size = 0;
7564 uint8_t *output_buffer = NULL;
7565 size_t expected_capacity;
7566 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007567 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7568 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7569 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7570 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007571 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007572 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007573 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007574
7575 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7576 {
7577 if( output_sizes[i] > output_buffer_size )
7578 output_buffer_size = output_sizes[i];
7579 if( output_sizes[i] == 0 )
7580 expected_outputs[i] = NULL;
7581 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007582 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007583 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007584
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007585 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007586 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7587 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7588 requested_capacity ) );
7589 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007590 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007591 switch( steps[i] )
7592 {
7593 case 0:
7594 break;
7595 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007596 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007597 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007598 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007599 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007600 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007601 inputs[i]->x, inputs[i]->len ),
7602 statuses[i] );
7603
7604 if( statuses[i] != PSA_SUCCESS )
7605 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007606 break;
7607 case 1: // input key
7608 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7609 psa_set_key_algorithm( &attributes1, alg );
7610 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7611
7612 PSA_ASSERT( psa_import_key( &attributes1,
7613 inputs[i]->x, inputs[i]->len,
7614 &keys[i] ) );
7615
Przemek Stekiel38647de2022-04-19 13:27:47 +02007616 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007617 {
7618 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007619 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7620 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007621 }
7622
Przemek Stekiel38647de2022-04-19 13:27:47 +02007623 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7624 steps[i],
7625 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007626 break;
7627 default:
7628 TEST_ASSERT( ! "default case not supported" );
7629 break;
7630 }
7631 break;
7632 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007633 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007634 {
7635 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007636 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7637 steps[i],
7638 inputs[i]->x,
7639 inputs[i]->len ),
7640 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007641 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007642 case 1: // input key, type DERIVE
7643 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007644 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7645 psa_set_key_algorithm( &attributes2, alg );
7646 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7647
7648 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007649 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007650 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7651
7652 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007653 inputs[i]->x, inputs[i]->len,
7654 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007655
Przemek Stekiel38647de2022-04-19 13:27:47 +02007656 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7657 steps[i],
7658 keys[i] ),
7659 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007660 break;
7661 case 2: // key agreement
7662 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7663 psa_set_key_algorithm( &attributes3, alg );
7664 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7665
7666 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007667 inputs[i]->x, inputs[i]->len,
7668 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007669
7670 TEST_EQUAL( psa_key_derivation_key_agreement(
7671 &operation,
7672 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7673 keys[i], key_agreement_peer_key->x,
7674 key_agreement_peer_key->len ), statuses[i] );
7675 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007676 default:
7677 TEST_ASSERT( ! "default case not supported" );
7678 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007679 }
7680
Przemek Stekiel38647de2022-04-19 13:27:47 +02007681 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007682 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007683 break;
7684 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007685 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007686 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007687 inputs[i]->x, inputs[i]->len ), statuses[i] );
7688
7689 if( statuses[i] != PSA_SUCCESS )
7690 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007691 break;
7692 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007693 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007694
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007695 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007696 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007697 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007698 expected_capacity = requested_capacity;
7699
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007700 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007701 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007702 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7703
7704 /* For output key derivation secret must be provided using
7705 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007706 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007707 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007708
7709 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7710 psa_set_key_algorithm( &attributes4, alg );
7711 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007712 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007713
7714 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007715 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007716 }
7717 else // output bytes
7718 {
7719 /* Expansion phase. */
7720 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007721 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007722 /* Read some bytes. */
7723 status = psa_key_derivation_output_bytes( &operation,
7724 output_buffer, output_sizes[i] );
7725 if( expected_capacity == 0 && output_sizes[i] == 0 )
7726 {
7727 /* Reading 0 bytes when 0 bytes are available can go either way. */
7728 TEST_ASSERT( status == PSA_SUCCESS ||
7729 status == PSA_ERROR_INSUFFICIENT_DATA );
7730 continue;
7731 }
7732 else if( expected_capacity == 0 ||
7733 output_sizes[i] > expected_capacity )
7734 {
7735 /* Capacity exceeded. */
7736 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7737 expected_capacity = 0;
7738 continue;
7739 }
7740 /* Success. Check the read data. */
7741 PSA_ASSERT( status );
7742 if( output_sizes[i] != 0 )
7743 ASSERT_COMPARE( output_buffer, output_sizes[i],
7744 expected_outputs[i], output_sizes[i] );
7745 /* Check the operation status. */
7746 expected_capacity -= output_sizes[i];
7747 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7748 &current_capacity ) );
7749 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007750 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007751 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007752 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007753
7754exit:
7755 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007756 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007757 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7758 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007759 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007760 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007761}
7762/* END_CASE */
7763
7764/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007765void derive_full( int alg_arg,
7766 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007767 data_t *input1,
7768 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007769 int requested_capacity_arg )
7770{
Ronald Cron5425a212020-08-04 14:58:35 +02007771 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007772 psa_algorithm_t alg = alg_arg;
7773 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007774 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007775 unsigned char output_buffer[16];
7776 size_t expected_capacity = requested_capacity;
7777 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007778 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007779
Gilles Peskine8817f612018-12-18 00:18:46 +01007780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007781
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7783 psa_set_key_algorithm( &attributes, alg );
7784 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007785
Gilles Peskine049c7532019-05-15 20:22:09 +02007786 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007787 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007788
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007789 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7790 input1->x, input1->len,
7791 input2->x, input2->len,
7792 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007793 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007794
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007795 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007796 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007797 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007798
7799 /* Expansion phase. */
7800 while( current_capacity > 0 )
7801 {
7802 size_t read_size = sizeof( output_buffer );
7803 if( read_size > current_capacity )
7804 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007805 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007806 output_buffer,
7807 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007808 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007809 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007810 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007811 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007812 }
7813
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007814 /* Check that the operation refuses to go over capacity. */
7815 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007816 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007817
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007818 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007819
7820exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007821 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007822 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007823 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007824}
7825/* END_CASE */
7826
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007827/* BEGIN_CASE depends_on:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS:MBEDTLS_SHA256_C */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007828void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007829 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007830 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007831 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007832 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007833{
7834 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7835 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007836 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007837 uint8_t *output_buffer = NULL;
7838 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007839 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7840 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7841 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007842
7843 ASSERT_ALLOC( output_buffer, expected_output->len );
7844 PSA_ASSERT( psa_crypto_init() );
7845
7846 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007847 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007848 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007849
7850 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007851 step, input->x, input->len ),
7852 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007853
7854 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7855 goto exit;
7856
7857 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7858 expected_output->len );
7859
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007860 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007861 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7862 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7863 expected_output->len );
7864
7865exit:
7866 mbedtls_free( output_buffer );
7867 psa_key_derivation_abort( &operation );
7868 PSA_DONE();
7869}
7870/* END_CASE */
7871
Janos Follathe60c9052019-07-03 13:51:30 +01007872/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007873void derive_key_exercise( int alg_arg,
7874 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007875 data_t *input1,
7876 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007877 int derived_type_arg,
7878 int derived_bits_arg,
7879 int derived_usage_arg,
7880 int derived_alg_arg )
7881{
Ronald Cron5425a212020-08-04 14:58:35 +02007882 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7883 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007884 psa_algorithm_t alg = alg_arg;
7885 psa_key_type_t derived_type = derived_type_arg;
7886 size_t derived_bits = derived_bits_arg;
7887 psa_key_usage_t derived_usage = derived_usage_arg;
7888 psa_algorithm_t derived_alg = derived_alg_arg;
7889 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007890 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007892 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007893
Gilles Peskine8817f612018-12-18 00:18:46 +01007894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007895
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007896 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7897 psa_set_key_algorithm( &attributes, alg );
7898 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007900 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007901
7902 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007903 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7904 input1->x, input1->len,
7905 input2->x, input2->len,
7906 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007907 goto exit;
7908
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007909 psa_set_key_usage_flags( &attributes, derived_usage );
7910 psa_set_key_algorithm( &attributes, derived_alg );
7911 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007912 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007913 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007914 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007915
7916 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007917 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007918 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7919 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007920
7921 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007922 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007923 goto exit;
7924
7925exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007926 /*
7927 * Key attributes may have been returned by psa_get_key_attributes()
7928 * thus reset them as required.
7929 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007930 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007931
7932 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007933 psa_destroy_key( base_key );
7934 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007935 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007936}
7937/* END_CASE */
7938
Janos Follath42fd8882019-07-03 14:17:09 +01007939/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007940void derive_key_export( int alg_arg,
7941 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007942 data_t *input1,
7943 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007944 int bytes1_arg,
7945 int bytes2_arg )
7946{
Ronald Cron5425a212020-08-04 14:58:35 +02007947 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7948 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007949 psa_algorithm_t alg = alg_arg;
7950 size_t bytes1 = bytes1_arg;
7951 size_t bytes2 = bytes2_arg;
7952 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007953 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007954 uint8_t *output_buffer = NULL;
7955 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007956 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7957 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007958 size_t length;
7959
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007960 ASSERT_ALLOC( output_buffer, capacity );
7961 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007962 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007963
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007964 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7965 psa_set_key_algorithm( &base_attributes, alg );
7966 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007967 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007968 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007969
7970 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007971 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7972 input1->x, input1->len,
7973 input2->x, input2->len,
7974 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007975 goto exit;
7976
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007977 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007978 output_buffer,
7979 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007980 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007981
7982 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007983 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7984 input1->x, input1->len,
7985 input2->x, input2->len,
7986 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007987 goto exit;
7988
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007989 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7990 psa_set_key_algorithm( &derived_attributes, 0 );
7991 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007992 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007993 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007994 &derived_key ) );
7995 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007996 export_buffer, bytes1,
7997 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007998 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007999 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008000 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008001 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008002 &derived_key ) );
8003 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01008004 export_buffer + bytes1, bytes2,
8005 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008006 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008007
8008 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008009 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
8010 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008011
8012exit:
8013 mbedtls_free( output_buffer );
8014 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008015 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008016 psa_destroy_key( base_key );
8017 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008018 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02008019}
8020/* END_CASE */
8021
8022/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008023void derive_key_type( int alg_arg,
8024 data_t *key_data,
8025 data_t *input1,
8026 data_t *input2,
8027 int key_type_arg, int bits_arg,
8028 data_t *expected_export )
8029{
8030 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8031 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8032 const psa_algorithm_t alg = alg_arg;
8033 const psa_key_type_t key_type = key_type_arg;
8034 const size_t bits = bits_arg;
8035 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8036 const size_t export_buffer_size =
8037 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8038 uint8_t *export_buffer = NULL;
8039 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8040 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8041 size_t export_length;
8042
8043 ASSERT_ALLOC( export_buffer, export_buffer_size );
8044 PSA_ASSERT( psa_crypto_init( ) );
8045
8046 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8047 psa_set_key_algorithm( &base_attributes, alg );
8048 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8049 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8050 &base_key ) );
8051
Przemek Stekielc85f0912022-03-08 11:37:54 +01008052 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008053 &operation, base_key, alg,
8054 input1->x, input1->len,
8055 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008056 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008057 goto exit;
8058
8059 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8060 psa_set_key_algorithm( &derived_attributes, 0 );
8061 psa_set_key_type( &derived_attributes, key_type );
8062 psa_set_key_bits( &derived_attributes, bits );
8063 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8064 &derived_key ) );
8065
8066 PSA_ASSERT( psa_export_key( derived_key,
8067 export_buffer, export_buffer_size,
8068 &export_length ) );
8069 ASSERT_COMPARE( export_buffer, export_length,
8070 expected_export->x, expected_export->len );
8071
8072exit:
8073 mbedtls_free( export_buffer );
8074 psa_key_derivation_abort( &operation );
8075 psa_destroy_key( base_key );
8076 psa_destroy_key( derived_key );
8077 PSA_DONE( );
8078}
8079/* END_CASE */
8080
8081/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008082void derive_key( int alg_arg,
8083 data_t *key_data, data_t *input1, data_t *input2,
8084 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008085 int expected_status_arg,
8086 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008087{
Ronald Cron5425a212020-08-04 14:58:35 +02008088 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8089 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008090 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008091 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008092 size_t bits = bits_arg;
8093 psa_status_t expected_status = expected_status_arg;
8094 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8095 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8096 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8097
8098 PSA_ASSERT( psa_crypto_init( ) );
8099
8100 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8101 psa_set_key_algorithm( &base_attributes, alg );
8102 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8103 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008104 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008105
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008106 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8107 input1->x, input1->len,
8108 input2->x, input2->len,
8109 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008110 goto exit;
8111
8112 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8113 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008114 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008115 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008116
8117 psa_status_t status =
8118 psa_key_derivation_output_key( &derived_attributes,
8119 &operation,
8120 &derived_key );
8121 if( is_large_output > 0 )
8122 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8123 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008124
8125exit:
8126 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008127 psa_destroy_key( base_key );
8128 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008129 PSA_DONE( );
8130}
8131/* END_CASE */
8132
8133/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008134void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008135 int our_key_type_arg, int our_key_alg_arg,
8136 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008137 int expected_status_arg )
8138{
Ronald Cron5425a212020-08-04 14:58:35 +02008139 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008140 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008141 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008142 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008143 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008145 psa_status_t expected_status = expected_status_arg;
8146 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008147
Gilles Peskine8817f612018-12-18 00:18:46 +01008148 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008149
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008150 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008151 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008152 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008153 PSA_ASSERT( psa_import_key( &attributes,
8154 our_key_data->x, our_key_data->len,
8155 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008156
Gilles Peskine77f40d82019-04-11 21:27:06 +02008157 /* The tests currently include inputs that should fail at either step.
8158 * Test cases that fail at the setup step should be changed to call
8159 * key_derivation_setup instead, and this function should be renamed
8160 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008161 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008162 if( status == PSA_SUCCESS )
8163 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008164 TEST_EQUAL( psa_key_derivation_key_agreement(
8165 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8166 our_key,
8167 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008168 expected_status );
8169 }
8170 else
8171 {
8172 TEST_ASSERT( status == expected_status );
8173 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008174
8175exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008176 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008177 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008178 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008179}
8180/* END_CASE */
8181
8182/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008183void raw_key_agreement( int alg_arg,
8184 int our_key_type_arg, data_t *our_key_data,
8185 data_t *peer_key_data,
8186 data_t *expected_output )
8187{
Ronald Cron5425a212020-08-04 14:58:35 +02008188 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008189 psa_algorithm_t alg = alg_arg;
8190 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008192 unsigned char *output = NULL;
8193 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008194 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008195
Gilles Peskinef0cba732019-04-11 22:12:38 +02008196 PSA_ASSERT( psa_crypto_init( ) );
8197
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8199 psa_set_key_algorithm( &attributes, alg );
8200 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008201 PSA_ASSERT( psa_import_key( &attributes,
8202 our_key_data->x, our_key_data->len,
8203 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008204
gabor-mezei-armceface22021-01-21 12:26:17 +01008205 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8206 key_bits = psa_get_key_bits( &attributes );
8207
Gilles Peskine992bee82022-04-13 23:25:52 +02008208 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008209 TEST_LE_U( expected_output->len,
8210 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8211 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8212 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008213
8214 /* Good case with exact output size */
8215 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008216 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8217 peer_key_data->x, peer_key_data->len,
8218 output, expected_output->len,
8219 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008220 ASSERT_COMPARE( output, output_length,
8221 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008222 mbedtls_free( output );
8223 output = NULL;
8224 output_length = ~0;
8225
8226 /* Larger buffer */
8227 ASSERT_ALLOC( output, expected_output->len + 1 );
8228 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8229 peer_key_data->x, peer_key_data->len,
8230 output, expected_output->len + 1,
8231 &output_length ) );
8232 ASSERT_COMPARE( output, output_length,
8233 expected_output->x, expected_output->len );
8234 mbedtls_free( output );
8235 output = NULL;
8236 output_length = ~0;
8237
8238 /* Buffer too small */
8239 ASSERT_ALLOC( output, expected_output->len - 1 );
8240 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8241 peer_key_data->x, peer_key_data->len,
8242 output, expected_output->len - 1,
8243 &output_length ),
8244 PSA_ERROR_BUFFER_TOO_SMALL );
8245 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008246 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008247 mbedtls_free( output );
8248 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008249
8250exit:
8251 mbedtls_free( output );
8252 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008253 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008254}
8255/* END_CASE */
8256
8257/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008258void key_agreement_capacity( int alg_arg,
8259 int our_key_type_arg, data_t *our_key_data,
8260 data_t *peer_key_data,
8261 int expected_capacity_arg )
8262{
Ronald Cron5425a212020-08-04 14:58:35 +02008263 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008264 psa_algorithm_t alg = alg_arg;
8265 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008266 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008268 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008269 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008270
Gilles Peskine8817f612018-12-18 00:18:46 +01008271 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008272
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008273 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8274 psa_set_key_algorithm( &attributes, alg );
8275 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008276 PSA_ASSERT( psa_import_key( &attributes,
8277 our_key_data->x, our_key_data->len,
8278 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008280 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008281 PSA_ASSERT( psa_key_derivation_key_agreement(
8282 &operation,
8283 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8284 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008285 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8286 {
8287 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008288 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008289 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008290 NULL, 0 ) );
8291 }
Gilles Peskine59685592018-09-18 12:11:34 +02008292
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008293 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008294 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008295 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008296 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008297
Gilles Peskinebf491972018-10-25 22:36:12 +02008298 /* Test the actual capacity by reading the output. */
8299 while( actual_capacity > sizeof( output ) )
8300 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008301 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008302 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008303 actual_capacity -= sizeof( output );
8304 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008305 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008306 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008307 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008308 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008309
Gilles Peskine59685592018-09-18 12:11:34 +02008310exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008311 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008312 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008313 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008314}
8315/* END_CASE */
8316
8317/* BEGIN_CASE */
8318void key_agreement_output( int alg_arg,
8319 int our_key_type_arg, data_t *our_key_data,
8320 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008321 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008322{
Ronald Cron5425a212020-08-04 14:58:35 +02008323 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008324 psa_algorithm_t alg = alg_arg;
8325 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008326 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008328 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008329
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008330 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8331 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008332
Gilles Peskine8817f612018-12-18 00:18:46 +01008333 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008334
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008335 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8336 psa_set_key_algorithm( &attributes, alg );
8337 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008338 PSA_ASSERT( psa_import_key( &attributes,
8339 our_key_data->x, our_key_data->len,
8340 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008341
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008342 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008343 PSA_ASSERT( psa_key_derivation_key_agreement(
8344 &operation,
8345 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8346 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008347 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8348 {
8349 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008350 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008351 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008352 NULL, 0 ) );
8353 }
Gilles Peskine59685592018-09-18 12:11:34 +02008354
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008355 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008356 actual_output,
8357 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008358 ASSERT_COMPARE( actual_output, expected_output1->len,
8359 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008360 if( expected_output2->len != 0 )
8361 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008362 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008363 actual_output,
8364 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008365 ASSERT_COMPARE( actual_output, expected_output2->len,
8366 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008367 }
Gilles Peskine59685592018-09-18 12:11:34 +02008368
8369exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008370 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008371 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008372 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008373 mbedtls_free( actual_output );
8374}
8375/* END_CASE */
8376
8377/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008378void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008379{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008380 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008381 unsigned char *output = NULL;
8382 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008383 size_t i;
8384 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008385
Simon Butcher49f8e312020-03-03 15:51:50 +00008386 TEST_ASSERT( bytes_arg >= 0 );
8387
Gilles Peskine91892022021-02-08 19:50:26 +01008388 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008389 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008390
Gilles Peskine8817f612018-12-18 00:18:46 +01008391 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008392
Gilles Peskinea50d7392018-06-21 10:22:13 +02008393 /* Run several times, to ensure that every output byte will be
8394 * nonzero at least once with overwhelming probability
8395 * (2^(-8*number_of_runs)). */
8396 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008397 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008398 if( bytes != 0 )
8399 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008400 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008401
Gilles Peskinea50d7392018-06-21 10:22:13 +02008402 for( i = 0; i < bytes; i++ )
8403 {
8404 if( output[i] != 0 )
8405 ++changed[i];
8406 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008407 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008408
8409 /* Check that every byte was changed to nonzero at least once. This
8410 * validates that psa_generate_random is overwriting every byte of
8411 * the output buffer. */
8412 for( i = 0; i < bytes; i++ )
8413 {
8414 TEST_ASSERT( changed[i] != 0 );
8415 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008416
8417exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008418 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008419 mbedtls_free( output );
8420 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008421}
8422/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008423
8424/* BEGIN_CASE */
8425void generate_key( int type_arg,
8426 int bits_arg,
8427 int usage_arg,
8428 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008429 int expected_status_arg,
8430 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008431{
Ronald Cron5425a212020-08-04 14:58:35 +02008432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008433 psa_key_type_t type = type_arg;
8434 psa_key_usage_t usage = usage_arg;
8435 size_t bits = bits_arg;
8436 psa_algorithm_t alg = alg_arg;
8437 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008438 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008439 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008440
Gilles Peskine8817f612018-12-18 00:18:46 +01008441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008442
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008443 psa_set_key_usage_flags( &attributes, usage );
8444 psa_set_key_algorithm( &attributes, alg );
8445 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008446 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008447
8448 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008449 psa_status_t status = psa_generate_key( &attributes, &key );
8450
8451 if( is_large_key > 0 )
8452 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8453 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008454 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008455 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008456
8457 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008458 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008459 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8460 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008461
Gilles Peskine818ca122018-06-20 18:16:48 +02008462 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008463 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008464 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008465
8466exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008467 /*
8468 * Key attributes may have been returned by psa_get_key_attributes()
8469 * thus reset them as required.
8470 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008471 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008472
Ronald Cron5425a212020-08-04 14:58:35 +02008473 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008474 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008475}
8476/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008477
Ronald Cronee414c72021-03-18 18:50:08 +01008478/* 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 +02008479void generate_key_rsa( int bits_arg,
8480 data_t *e_arg,
8481 int expected_status_arg )
8482{
Ronald Cron5425a212020-08-04 14:58:35 +02008483 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008484 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008485 size_t bits = bits_arg;
8486 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8487 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8488 psa_status_t expected_status = expected_status_arg;
8489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8490 uint8_t *exported = NULL;
8491 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008492 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008493 size_t exported_length = SIZE_MAX;
8494 uint8_t *e_read_buffer = NULL;
8495 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008496 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008497 size_t e_read_length = SIZE_MAX;
8498
8499 if( e_arg->len == 0 ||
8500 ( e_arg->len == 3 &&
8501 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8502 {
8503 is_default_public_exponent = 1;
8504 e_read_size = 0;
8505 }
8506 ASSERT_ALLOC( e_read_buffer, e_read_size );
8507 ASSERT_ALLOC( exported, exported_size );
8508
8509 PSA_ASSERT( psa_crypto_init( ) );
8510
8511 psa_set_key_usage_flags( &attributes, usage );
8512 psa_set_key_algorithm( &attributes, alg );
8513 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8514 e_arg->x, e_arg->len ) );
8515 psa_set_key_bits( &attributes, bits );
8516
8517 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008518 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008519 if( expected_status != PSA_SUCCESS )
8520 goto exit;
8521
8522 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008523 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008524 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8525 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8526 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8527 e_read_buffer, e_read_size,
8528 &e_read_length ) );
8529 if( is_default_public_exponent )
8530 TEST_EQUAL( e_read_length, 0 );
8531 else
8532 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8533
8534 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008535 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008536 goto exit;
8537
8538 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008539 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008540 exported, exported_size,
8541 &exported_length ) );
8542 {
8543 uint8_t *p = exported;
8544 uint8_t *end = exported + exported_length;
8545 size_t len;
8546 /* RSAPublicKey ::= SEQUENCE {
8547 * modulus INTEGER, -- n
8548 * publicExponent INTEGER } -- e
8549 */
8550 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008551 MBEDTLS_ASN1_SEQUENCE |
8552 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008553 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008554 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8555 MBEDTLS_ASN1_INTEGER ) );
8556 if( len >= 1 && p[0] == 0 )
8557 {
8558 ++p;
8559 --len;
8560 }
8561 if( e_arg->len == 0 )
8562 {
8563 TEST_EQUAL( len, 3 );
8564 TEST_EQUAL( p[0], 1 );
8565 TEST_EQUAL( p[1], 0 );
8566 TEST_EQUAL( p[2], 1 );
8567 }
8568 else
8569 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8570 }
8571
8572exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008573 /*
8574 * Key attributes may have been returned by psa_get_key_attributes() or
8575 * set by psa_set_key_domain_parameters() thus reset them as required.
8576 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008577 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008578
Ronald Cron5425a212020-08-04 14:58:35 +02008579 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008580 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008581 mbedtls_free( e_read_buffer );
8582 mbedtls_free( exported );
8583}
8584/* END_CASE */
8585
Darryl Greend49a4992018-06-18 17:27:26 +01008586/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008587void persistent_key_load_key_from_storage( data_t *data,
8588 int type_arg, int bits_arg,
8589 int usage_flags_arg, int alg_arg,
8590 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008591{
Ronald Cron71016a92020-08-28 19:01:50 +02008592 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008593 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8595 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008596 psa_key_type_t type = type_arg;
8597 size_t bits = bits_arg;
8598 psa_key_usage_t usage_flags = usage_flags_arg;
8599 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008600 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008601 unsigned char *first_export = NULL;
8602 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008603 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008604 size_t first_exported_length;
8605 size_t second_exported_length;
8606
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008607 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8608 {
8609 ASSERT_ALLOC( first_export, export_size );
8610 ASSERT_ALLOC( second_export, export_size );
8611 }
Darryl Greend49a4992018-06-18 17:27:26 +01008612
Gilles Peskine8817f612018-12-18 00:18:46 +01008613 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008614
Gilles Peskinec87af662019-05-15 16:12:22 +02008615 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008616 psa_set_key_usage_flags( &attributes, usage_flags );
8617 psa_set_key_algorithm( &attributes, alg );
8618 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008619 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008620
Darryl Green0c6575a2018-11-07 16:05:30 +00008621 switch( generation_method )
8622 {
8623 case IMPORT_KEY:
8624 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008625 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008626 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008627 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008628
Darryl Green0c6575a2018-11-07 16:05:30 +00008629 case GENERATE_KEY:
8630 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008631 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008632 break;
8633
8634 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008635#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008636 {
8637 /* Create base key */
8638 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8639 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8640 psa_set_key_usage_flags( &base_attributes,
8641 PSA_KEY_USAGE_DERIVE );
8642 psa_set_key_algorithm( &base_attributes, derive_alg );
8643 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008644 PSA_ASSERT( psa_import_key( &base_attributes,
8645 data->x, data->len,
8646 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008647 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008648 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008649 PSA_ASSERT( psa_key_derivation_input_key(
8650 &operation,
8651 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008652 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008653 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008654 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008655 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8656 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008657 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008658 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008659 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008660 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008661 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008662#else
8663 TEST_ASSUME( ! "KDF not supported in this configuration" );
8664#endif
8665 break;
8666
8667 default:
8668 TEST_ASSERT( ! "generation_method not implemented in test" );
8669 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008670 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008671 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008672
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008673 /* Export the key if permitted by the key policy. */
8674 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8675 {
Ronald Cron5425a212020-08-04 14:58:35 +02008676 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008677 first_export, export_size,
8678 &first_exported_length ) );
8679 if( generation_method == IMPORT_KEY )
8680 ASSERT_COMPARE( data->x, data->len,
8681 first_export, first_exported_length );
8682 }
Darryl Greend49a4992018-06-18 17:27:26 +01008683
8684 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008685 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008686 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008687 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008688
Darryl Greend49a4992018-06-18 17:27:26 +01008689 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008690 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008691 TEST_ASSERT( mbedtls_svc_key_id_equal(
8692 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008693 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8694 PSA_KEY_LIFETIME_PERSISTENT );
8695 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8696 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008697 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008698 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008699 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008700
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008701 /* Export the key again if permitted by the key policy. */
8702 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008703 {
Ronald Cron5425a212020-08-04 14:58:35 +02008704 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008705 second_export, export_size,
8706 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008707 ASSERT_COMPARE( first_export, first_exported_length,
8708 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008709 }
8710
8711 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008712 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008713 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008714
8715exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008716 /*
8717 * Key attributes may have been returned by psa_get_key_attributes()
8718 * thus reset them as required.
8719 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008720 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008721
Darryl Greend49a4992018-06-18 17:27:26 +01008722 mbedtls_free( first_export );
8723 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008724 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008725 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008726 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008727 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008728}
8729/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008730
Neil Armstronga557cb82022-06-10 08:58:32 +02008731/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008732void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8733 int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008734 int input_first, data_t *pw_data,
Neil Armstrong2a73f212022-09-06 11:34:54 +02008735 int expected_status_setup_arg,
8736 int expected_status_set_role_arg,
8737 int expected_status_set_password_key_arg,
8738 int expected_status_input_output_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008739{
8740 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8741 psa_pake_operation_t operation = psa_pake_operation_init();
8742 psa_algorithm_t alg = alg_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008743 psa_key_type_t key_type_pw = key_type_pw_arg;
8744 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008745 psa_algorithm_t hash_alg = hash_arg;
8746 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008749 psa_status_t expected_status_setup = expected_status_setup_arg;
8750 psa_status_t expected_status_set_role = expected_status_set_role_arg;
8751 psa_status_t expected_status_set_password_key =
8752 expected_status_set_password_key_arg;
8753 psa_status_t expected_status_input_output =
8754 expected_status_input_output_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008755 unsigned char *output_buffer = NULL;
8756 size_t output_len = 0;
8757
8758 PSA_INIT( );
8759
8760 ASSERT_ALLOC( output_buffer,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008761 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8762 PSA_PAKE_STEP_KEY_SHARE) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008763
8764 if( pw_data->len > 0 )
8765 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008766 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008767 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008768 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008769 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8770 &key ) );
8771 }
8772
8773 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8774 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8775 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8776
Neil Armstrong645cccd2022-06-08 17:36:23 +02008777 PSA_ASSERT( psa_pake_abort( &operation ) );
8778
8779 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8780 PSA_ERROR_BAD_STATE );
8781 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8782 PSA_ERROR_BAD_STATE );
8783 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8784 PSA_ERROR_BAD_STATE );
8785 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8786 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008787 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8788 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008789 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008790 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008791 PSA_ERROR_BAD_STATE );
8792
8793 PSA_ASSERT( psa_pake_abort( &operation ) );
8794
Neil Armstrong2a73f212022-09-06 11:34:54 +02008795 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8796 expected_status_setup );
8797 if( expected_status_setup != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008798 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008799
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008800 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8801 PSA_ERROR_BAD_STATE );
8802
Neil Armstrong2a73f212022-09-06 11:34:54 +02008803 TEST_EQUAL( psa_pake_set_role( &operation, role),
8804 expected_status_set_role );
8805 if( expected_status_set_role != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008806 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008807
8808 if( pw_data->len > 0 )
8809 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008810 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8811 expected_status_set_password_key );
8812 if( expected_status_set_password_key != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008813 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008814 }
8815
Neil Armstrong707d9572022-06-08 17:31:49 +02008816 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8817 PSA_ERROR_INVALID_ARGUMENT );
8818 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8819 PSA_ERROR_INVALID_ARGUMENT );
8820
8821 const uint8_t unsupported_id[] = "abcd";
8822
8823 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8824 PSA_ERROR_NOT_SUPPORTED );
8825 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8826 PSA_ERROR_NOT_SUPPORTED );
8827
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008828 /* First round */
8829 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008830 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008831 /* Invalid parameters */
8832 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8833 NULL, 0 ),
8834 PSA_ERROR_INVALID_ARGUMENT );
8835 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8836 output_buffer, 66 ),
8837 PSA_ERROR_INVALID_ARGUMENT );
8838 /* Invalid first step */
8839 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8840 output_buffer, 66 ),
8841 PSA_ERROR_BAD_STATE );
8842
8843 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8844 output_buffer, 66 ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008845 expected_status_input_output);
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008846
Neil Armstrong2a73f212022-09-06 11:34:54 +02008847 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008848 {
8849 /* Buffer too large */
8850 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8851 output_buffer, 512 ),
8852 PSA_ERROR_INSUFFICIENT_MEMORY );
8853
8854 /* The operation should be aborted at this point */
8855 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8856 output_buffer, 66 ),
8857 PSA_ERROR_BAD_STATE );
8858 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008859 }
8860 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008861 {
8862 /* Invalid parameters */
8863 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8864 NULL, 0, NULL ),
8865 PSA_ERROR_INVALID_ARGUMENT );
8866 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8867 output_buffer, 512, &output_len ),
8868 PSA_ERROR_INVALID_ARGUMENT );
8869 /* Invalid first step */
8870 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8871 output_buffer, 512, &output_len ),
8872 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008873
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008874 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8875 output_buffer, 512, &output_len ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008876 expected_status_input_output );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008877
Neil Armstrong2a73f212022-09-06 11:34:54 +02008878 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008879 {
8880 TEST_ASSERT( output_len > 0 );
8881
8882 /* Buffer too small */
8883 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8884 output_buffer, 5, &output_len ),
8885 PSA_ERROR_BUFFER_TOO_SMALL );
8886
8887 /* The operation should be aborted at this point */
8888 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8889 output_buffer, 512, &output_len ),
8890 PSA_ERROR_BAD_STATE );
8891 }
8892 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008893
8894exit:
8895 PSA_ASSERT( psa_destroy_key( key ) );
8896 PSA_ASSERT( psa_pake_abort( &operation ) );
8897 mbedtls_free( output_buffer );
8898 PSA_DONE( );
8899}
8900/* END_CASE */
8901
Neil Armstronga557cb82022-06-10 08:58:32 +02008902/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008903void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8904 int client_input_first, int inject_error,
8905 data_t *pw_data )
8906{
8907 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8908 psa_pake_operation_t server = psa_pake_operation_init();
8909 psa_pake_operation_t client = psa_pake_operation_init();
8910 psa_algorithm_t alg = alg_arg;
8911 psa_algorithm_t hash_alg = hash_arg;
8912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8914
8915 PSA_INIT( );
8916
8917 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8918 psa_set_key_algorithm( &attributes, alg );
8919 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8920 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8921 &key ) );
8922
8923 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8924 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8925 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8926
8927
8928 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8929 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8930
8931 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8932 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8933
8934 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8935 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8936
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008937 ecjpake_do_round( alg, primitive_arg, &server, &client,
8938 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008939
8940 if( inject_error == 1 || inject_error == 2 )
8941 goto exit;
8942
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008943 ecjpake_do_round( alg, primitive_arg, &server, &client,
8944 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008945
8946exit:
8947 psa_destroy_key( key );
8948 psa_pake_abort( &server );
8949 psa_pake_abort( &client );
8950 PSA_DONE( );
8951}
8952/* END_CASE */
8953
8954/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008955void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008956 int derive_alg_arg, data_t *pw_data,
8957 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008958{
8959 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8960 psa_pake_operation_t server = psa_pake_operation_init();
8961 psa_pake_operation_t client = psa_pake_operation_init();
8962 psa_algorithm_t alg = alg_arg;
8963 psa_algorithm_t hash_alg = hash_arg;
8964 psa_algorithm_t derive_alg = derive_alg_arg;
8965 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8966 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8967 psa_key_derivation_operation_t server_derive =
8968 PSA_KEY_DERIVATION_OPERATION_INIT;
8969 psa_key_derivation_operation_t client_derive =
8970 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008971
8972 PSA_INIT( );
8973
Neil Armstrongd597bc72022-05-25 11:28:39 +02008974 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8975 psa_set_key_algorithm( &attributes, alg );
8976 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8977 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8978 &key ) );
8979
8980 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8981 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8982 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8983
Neil Armstrong1e855602022-06-15 11:32:11 +02008984 /* Get shared key */
8985 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
8986 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
8987
8988 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
8989 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
8990 {
8991 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
8992 PSA_KEY_DERIVATION_INPUT_SEED,
8993 (const uint8_t*) "", 0) );
8994 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
8995 PSA_KEY_DERIVATION_INPUT_SEED,
8996 (const uint8_t*) "", 0) );
8997 }
8998
Neil Armstrongd597bc72022-05-25 11:28:39 +02008999 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
9000 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
9001
9002 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
9003 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
9004
9005 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
9006 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
9007
Neil Armstrong1e855602022-06-15 11:32:11 +02009008 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9009 PSA_ERROR_BAD_STATE );
9010 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9011 PSA_ERROR_BAD_STATE );
9012
Neil Armstrongf983caf2022-06-15 15:27:48 +02009013 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009014 ecjpake_do_round( alg, primitive_arg, &server, &client,
9015 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009016
Neil Armstrong1e855602022-06-15 11:32:11 +02009017 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
9018 PSA_ERROR_BAD_STATE );
9019 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
9020 PSA_ERROR_BAD_STATE );
9021
Neil Armstrongf983caf2022-06-15 15:27:48 +02009022 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02009023 ecjpake_do_round( alg, primitive_arg, &server, &client,
9024 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009025
Neil Armstrongd597bc72022-05-25 11:28:39 +02009026 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9027 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9028
9029exit:
9030 psa_key_derivation_abort( &server_derive );
9031 psa_key_derivation_abort( &client_derive );
9032 psa_destroy_key( key );
9033 psa_pake_abort( &server );
9034 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009035 PSA_DONE( );
9036}
9037/* END_CASE */
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +02009038
9039/* BEGIN_CASE */
9040void ecjpake_size_macros( )
9041{
9042 const psa_algorithm_t alg = PSA_ALG_JPAKE;
9043 const size_t bits = 256;
9044 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
9045 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits );
9046 const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(
9047 PSA_ECC_FAMILY_SECP_R1 );
9048
9049 // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types
9050 /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */
9051 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9052 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9053 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9054 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) );
9055 /* The output for ZK_PROOF is the same bitsize as the curve */
9056 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9057 PSA_BITS_TO_BYTES( bits ) );
9058
9059 /* Input sizes are the same as output sizes */
9060 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9061 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) );
9062 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9063 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) );
9064 TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9065 PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) );
9066
9067 /* These inequalities will always hold even when other PAKEs are added */
9068 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9069 PSA_PAKE_OUTPUT_MAX_SIZE );
9070 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9071 PSA_PAKE_OUTPUT_MAX_SIZE );
9072 TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9073 PSA_PAKE_OUTPUT_MAX_SIZE );
9074 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE),
9075 PSA_PAKE_INPUT_MAX_SIZE );
9076 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC),
9077 PSA_PAKE_INPUT_MAX_SIZE );
9078 TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF),
9079 PSA_PAKE_INPUT_MAX_SIZE );
9080}
9081/* END_CASE */