blob: 67b0dd2ada84d0b0e94728b1330ea65e55e9f5d3 [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 Armstrongf983caf2022-06-15 15:27:48 +0200708static int ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
709 psa_pake_operation_t *server,
710 psa_pake_operation_t *client,
711 int client_input_first,
712 int round, int inject_error )
713{
714 unsigned char *buffer0 = NULL, *buffer1 = NULL;
715 size_t buffer_length = (
716 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
719 size_t buffer0_off = 0;
720 size_t buffer1_off = 0;
721 size_t s_g1_len, s_g2_len, s_a_len;
722 size_t s_g1_off, s_g2_off, s_a_off;
723 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
724 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
725 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
726 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
727 size_t c_g1_len, c_g2_len, c_a_len;
728 size_t c_g1_off, c_g2_off, c_a_off;
729 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
730 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
731 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
732 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
733 psa_status_t expected_status = PSA_SUCCESS;
734 int ret;
735
736 ASSERT_ALLOC( buffer0, buffer_length );
737 ASSERT_ALLOC( buffer1, buffer_length );
738
739 switch( round )
740 {
741 case 1:
742 /* Server first round Output */
743 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
744 buffer0 + buffer0_off,
745 512 - buffer0_off, &s_g1_len ) );
746 s_g1_off = buffer0_off;
747 buffer0_off += s_g1_len;
748 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
749 buffer0 + buffer0_off,
750 512 - buffer0_off, &s_x1_pk_len ) );
751 s_x1_pk_off = buffer0_off;
752 buffer0_off += s_x1_pk_len;
753 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
754 buffer0 + buffer0_off,
755 512 - buffer0_off, &s_x1_pr_len ) );
756 s_x1_pr_off = buffer0_off;
757 buffer0_off += s_x1_pr_len;
758 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
759 buffer0 + buffer0_off,
760 512 - buffer0_off, &s_g2_len ) );
761 s_g2_off = buffer0_off;
762 buffer0_off += s_g2_len;
763 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
764 buffer0 + buffer0_off,
765 512 - buffer0_off, &s_x2_pk_len ) );
766 s_x2_pk_off = buffer0_off;
767 buffer0_off += s_x2_pk_len;
768 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
769 buffer0 + buffer0_off,
770 512 - buffer0_off, &s_x2_pr_len ) );
771 s_x2_pr_off = buffer0_off;
772 buffer0_off += s_x2_pr_len;
773
774 if( inject_error == 1 )
775 {
776 buffer0[s_x1_pk_off + 12] >>= 4;
777 buffer0[s_x2_pk_off + 7] <<= 4;
778 expected_status = PSA_ERROR_DATA_INVALID;
779 }
780
781 if( client_input_first == 1 )
782 {
783 /* Client first round Input */
784 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
785 buffer0 + s_g1_off, s_g1_len ) );
786 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
787 buffer0 + s_x1_pk_off,
788 s_x1_pk_len ) );
789 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
790 buffer0 + s_x1_pr_off,
791 s_x1_pr_len ) );
792 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
793 buffer0 + s_g2_off,
794 s_g2_len ) );
795 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
796 buffer0 + s_x2_pk_off,
797 s_x2_pk_len ) );
798 TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
799 buffer0 + s_x2_pr_off,
800 s_x2_pr_len ),
801 expected_status );
802
803 if( inject_error == 1 )
804 {
805 ret = 1;
806 goto exit;
807 }
808 }
809
810 /* Client first round Output */
811 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
812 buffer1 + buffer1_off,
813 512 - buffer1_off, &c_g1_len ) );
814 c_g1_off = buffer1_off;
815 buffer1_off += c_g1_len;
816 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
817 buffer1 + buffer1_off,
818 512 - buffer1_off, &c_x1_pk_len ) );
819 c_x1_pk_off = buffer1_off;
820 buffer1_off += c_x1_pk_len;
821 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
822 buffer1 + buffer1_off,
823 512 - buffer1_off, &c_x1_pr_len ) );
824 c_x1_pr_off = buffer1_off;
825 buffer1_off += c_x1_pr_len;
826 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
827 buffer1 + buffer1_off,
828 512 - buffer1_off, &c_g2_len ) );
829 c_g2_off = buffer1_off;
830 buffer1_off += c_g2_len;
831 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
832 buffer1 + buffer1_off,
833 512 - buffer1_off, &c_x2_pk_len ) );
834 c_x2_pk_off = buffer1_off;
835 buffer1_off += c_x2_pk_len;
836 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
837 buffer1 + buffer1_off,
838 512 - buffer1_off, &c_x2_pr_len ) );
839 c_x2_pr_off = buffer1_off;
840 buffer1_off += c_x2_pr_len;
841
842 if( client_input_first == 0 )
843 {
844 /* Client first round Input */
845 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
846 buffer0 + s_g1_off, s_g1_len ) );
847 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
848 buffer0 + s_x1_pk_off,
849 s_x1_pk_len ) );
850 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
851 buffer0 + s_x1_pr_off,
852 s_x1_pr_len ) );
853 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
854 buffer0 + s_g2_off,
855 s_g2_len ) );
856 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
857 buffer0 + s_x2_pk_off,
858 s_x2_pk_len ) );
859 TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
860 buffer0 + s_x2_pr_off,
861 s_x2_pr_len ),
862 expected_status );
863
864 if( inject_error == 1 )
865 break;
866 }
867
868 if( inject_error == 2 )
869 {
870 buffer1[c_x1_pk_off + 12] >>= 4;
871 buffer1[c_x2_pk_off + 7] <<= 4;
872 expected_status = PSA_ERROR_DATA_INVALID;
873 }
874
875 /* Server first round Input */
876 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
877 buffer1 + c_g1_off, c_g1_len ) );
878 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
879 buffer1 + c_x1_pk_off, c_x1_pk_len ) );
880 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
881 buffer1 + c_x1_pr_off, c_x1_pr_len ) );
882 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
883 buffer1 + c_g2_off, c_g2_len ) );
884 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
885 buffer1 + c_x2_pk_off, c_x2_pk_len ) );
886 TEST_EQUAL( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
887 buffer1 + c_x2_pr_off, c_x2_pr_len ),
888 expected_status );
889
890 break;
891
892 case 2:
893 /* Server second round Output */
894 buffer0_off = 0;
895
896 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
897 buffer0 + buffer0_off,
898 512 - buffer0_off, &s_a_len ) );
899 s_a_off = buffer0_off;
900 buffer0_off += s_a_len;
901 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
902 buffer0 + buffer0_off,
903 512 - buffer0_off, &s_x2s_pk_len ) );
904 s_x2s_pk_off = buffer0_off;
905 buffer0_off += s_x2s_pk_len;
906 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
907 buffer0 + buffer0_off,
908 512 - buffer0_off, &s_x2s_pr_len ) );
909 s_x2s_pr_off = buffer0_off;
910 buffer0_off += s_x2s_pr_len;
911
912 if( inject_error == 3 )
913 {
914 buffer0[s_x2s_pk_off + 12] >>= 4;
915 expected_status = PSA_ERROR_DATA_INVALID;
916 }
917
918 if( client_input_first == 1 )
919 {
920 /* Client second round Input */
921 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
922 buffer0 + s_a_off, s_a_len ) );
923 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
924 buffer0 + s_x2s_pk_off,
925 s_x2s_pk_len ) );
926 TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
927 buffer0 + s_x2s_pr_off,
928 s_x2s_pr_len ),
929 expected_status );
930
931 if( inject_error == 3 )
932 break;
933 }
934
935 /* Client second round Output */
936 buffer1_off = 0;
937
938 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
939 buffer1 + buffer1_off,
940 512 - buffer1_off, &c_a_len ) );
941 c_a_off = buffer1_off;
942 buffer1_off += c_a_len;
943 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
944 buffer1 + buffer1_off,
945 512 - buffer1_off, &c_x2s_pk_len ) );
946 c_x2s_pk_off = buffer1_off;
947 buffer1_off += c_x2s_pk_len;
948 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
949 buffer1 + buffer1_off,
950 512 - buffer1_off, &c_x2s_pr_len ) );
951 c_x2s_pr_off = buffer1_off;
952 buffer1_off += c_x2s_pr_len;
953
954 if( client_input_first == 0 )
955 {
956 /* Client second round Input */
957 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
958 buffer0 + s_a_off, s_a_len ) );
959 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
960 buffer0 + s_x2s_pk_off,
961 s_x2s_pk_len ) );
962 TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
963 buffer0 + s_x2s_pr_off,
964 s_x2s_pr_len ),
965 expected_status );
966
967 if( inject_error == 3 )
968 break;
969 }
970
971 if( inject_error == 4 )
972 {
973 buffer1[c_x2s_pk_off + 12] >>= 4;
974 expected_status = PSA_ERROR_DATA_INVALID;
975 }
976
977 /* Server second round Input */
978 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
979 buffer1 + c_a_off, c_a_len ) );
980 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
981 buffer1 + c_x2s_pk_off, c_x2s_pk_len ) );
982 TEST_EQUAL( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
983 buffer1 + c_x2s_pr_off, c_x2s_pr_len ),
984 expected_status );
985
986 break;
987
988 }
989
990 ret = 1;
991
992exit:
993 mbedtls_free( buffer0 );
994 mbedtls_free( buffer1 );
995 return( ret );
996}
997
Gilles Peskinee59236f2018-01-27 23:32:46 +0100998/* END_HEADER */
999
1000/* BEGIN_DEPENDENCIES
1001 * depends_on:MBEDTLS_PSA_CRYPTO_C
1002 * END_DEPENDENCIES
1003 */
1004
1005/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001006void static_checks( )
1007{
1008 size_t max_truncated_mac_size =
1009 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1010
1011 /* Check that the length for a truncated MAC always fits in the algorithm
1012 * encoding. The shifted mask is the maximum truncated value. The
1013 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001014 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001015}
1016/* END_CASE */
1017
1018/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001019void import_with_policy( int type_arg,
1020 int usage_arg, int alg_arg,
1021 int expected_status_arg )
1022{
1023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1024 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001025 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001026 psa_key_type_t type = type_arg;
1027 psa_key_usage_t usage = usage_arg;
1028 psa_algorithm_t alg = alg_arg;
1029 psa_status_t expected_status = expected_status_arg;
1030 const uint8_t key_material[16] = {0};
1031 psa_status_t status;
1032
1033 PSA_ASSERT( psa_crypto_init( ) );
1034
1035 psa_set_key_type( &attributes, type );
1036 psa_set_key_usage_flags( &attributes, usage );
1037 psa_set_key_algorithm( &attributes, alg );
1038
1039 status = psa_import_key( &attributes,
1040 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001041 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001042 TEST_EQUAL( status, expected_status );
1043 if( status != PSA_SUCCESS )
1044 goto exit;
1045
Ronald Cron5425a212020-08-04 14:58:35 +02001046 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001047 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001048 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001049 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001050 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001051 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001052
Ronald Cron5425a212020-08-04 14:58:35 +02001053 PSA_ASSERT( psa_destroy_key( key ) );
1054 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001055
1056exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001057 /*
1058 * Key attributes may have been returned by psa_get_key_attributes()
1059 * thus reset them as required.
1060 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001061 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001062
1063 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001064 PSA_DONE( );
1065}
1066/* END_CASE */
1067
1068/* BEGIN_CASE */
1069void import_with_data( data_t *data, int type_arg,
1070 int attr_bits_arg,
1071 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001072{
1073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1074 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001076 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001077 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001078 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001079 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001080
Gilles Peskine8817f612018-12-18 00:18:46 +01001081 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001082
Gilles Peskine4747d192019-04-17 15:05:45 +02001083 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001084 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001085
Ronald Cron5425a212020-08-04 14:58:35 +02001086 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001087 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001088 if( status != PSA_SUCCESS )
1089 goto exit;
1090
Ronald Cron5425a212020-08-04 14:58:35 +02001091 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001092 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001093 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001094 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001095 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001096
Ronald Cron5425a212020-08-04 14:58:35 +02001097 PSA_ASSERT( psa_destroy_key( key ) );
1098 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001099
1100exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001101 /*
1102 * Key attributes may have been returned by psa_get_key_attributes()
1103 * thus reset them as required.
1104 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001105 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001106
1107 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001108 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001109}
1110/* END_CASE */
1111
1112/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001113void import_large_key( int type_arg, int byte_size_arg,
1114 int expected_status_arg )
1115{
1116 psa_key_type_t type = type_arg;
1117 size_t byte_size = byte_size_arg;
1118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1119 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001120 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001121 psa_status_t status;
1122 uint8_t *buffer = NULL;
1123 size_t buffer_size = byte_size + 1;
1124 size_t n;
1125
Steven Cooreman69967ce2021-01-18 18:01:08 +01001126 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001127 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001128 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001129 memset( buffer, 'K', byte_size );
1130
1131 PSA_ASSERT( psa_crypto_init( ) );
1132
1133 /* Try importing the key */
1134 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1135 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001136 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001137 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001138 TEST_EQUAL( status, expected_status );
1139
1140 if( status == PSA_SUCCESS )
1141 {
Ronald Cron5425a212020-08-04 14:58:35 +02001142 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001143 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1144 TEST_EQUAL( psa_get_key_bits( &attributes ),
1145 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001146 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001147 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001148 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001149 for( n = 0; n < byte_size; n++ )
1150 TEST_EQUAL( buffer[n], 'K' );
1151 for( n = byte_size; n < buffer_size; n++ )
1152 TEST_EQUAL( buffer[n], 0 );
1153 }
1154
1155exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001156 /*
1157 * Key attributes may have been returned by psa_get_key_attributes()
1158 * thus reset them as required.
1159 */
1160 psa_reset_key_attributes( &attributes );
1161
Ronald Cron5425a212020-08-04 14:58:35 +02001162 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001163 PSA_DONE( );
1164 mbedtls_free( buffer );
1165}
1166/* END_CASE */
1167
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001168/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001169void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1170{
Ronald Cron5425a212020-08-04 14:58:35 +02001171 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001172 size_t bits = bits_arg;
1173 psa_status_t expected_status = expected_status_arg;
1174 psa_status_t status;
1175 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001176 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001177 size_t buffer_size = /* Slight overapproximations */
1178 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001179 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001180 unsigned char *p;
1181 int ret;
1182 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001183 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001184
Gilles Peskine8817f612018-12-18 00:18:46 +01001185 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001186 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001187
1188 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1189 bits, keypair ) ) >= 0 );
1190 length = ret;
1191
1192 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001193 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001194 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001195 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001196
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001197 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001198 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001199
1200exit:
1201 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001202 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001203}
1204/* END_CASE */
1205
1206/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001207void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001208 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001209 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301210 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211 int expected_bits,
1212 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001213 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001214 int canonical_input )
1215{
Ronald Cron5425a212020-08-04 14:58:35 +02001216 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001217 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001218 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001219 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001220 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301221 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001222 unsigned char *exported = NULL;
1223 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001225 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001226 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001228 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001229
Moran Pekercb088e72018-07-17 17:36:59 +03001230 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001231 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001232 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001233 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235
Archana4d7ae1d2021-07-07 02:50:22 +05301236 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001237 psa_set_key_usage_flags( &attributes, usage_arg );
1238 psa_set_key_algorithm( &attributes, alg );
1239 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001240
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001241 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001242 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001243
1244 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001245 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001246 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1247 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001248 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001249
1250 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001251 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001252 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001253
1254 /* The exported length must be set by psa_export_key() to a value between 0
1255 * and export_size. On errors, the exported length must be 0. */
1256 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1257 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001258 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001259
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001260 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001261 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001262 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001263 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001264 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001266 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001267
Gilles Peskineea38a922021-02-13 00:05:16 +01001268 /* Run sanity checks on the exported key. For non-canonical inputs,
1269 * this validates the canonical representations. For canonical inputs,
1270 * this doesn't directly validate the implementation, but it still helps
1271 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301272 if( !psa_key_lifetime_is_external( lifetime ) )
1273 {
1274 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1275 goto exit;
1276 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001277
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001278 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001279 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280 else
1281 {
Ronald Cron5425a212020-08-04 14:58:35 +02001282 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001283 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001284 &key2 ) );
1285 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001286 reexported,
1287 export_size,
1288 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001289 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301290 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001291 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001293 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301294 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301295 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001296 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001297
1298destroy:
1299 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001300 PSA_ASSERT( psa_destroy_key( key ) );
1301 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302
1303exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001304 /*
1305 * Key attributes may have been returned by psa_get_key_attributes()
1306 * thus reset them as required.
1307 */
1308 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301309 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001310 mbedtls_free( exported );
1311 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001312 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001313}
1314/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001315
Moran Pekerf709f4a2018-06-06 17:26:04 +03001316/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001317void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001318 int type_arg,
1319 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301320 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001321 int export_size_delta,
1322 int expected_export_status_arg,
1323 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001324{
Ronald Cron5425a212020-08-04 14:58:35 +02001325 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001326 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001327 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001328 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001329 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301330 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001331 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001332 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001333 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001334 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001335
Gilles Peskine8817f612018-12-18 00:18:46 +01001336 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001337
Archana4d7ae1d2021-07-07 02:50:22 +05301338 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1340 psa_set_key_algorithm( &attributes, alg );
1341 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001342
1343 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001344 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001345
Gilles Peskine49c25912018-10-29 15:15:31 +01001346 /* Export the public key */
1347 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001348 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001349 exported, export_size,
1350 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001352 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001353 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001354 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001355 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001356 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001357 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001358 TEST_LE_U( expected_public_key->len,
1359 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1360 TEST_LE_U( expected_public_key->len,
1361 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1362 TEST_LE_U( expected_public_key->len,
1363 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001364 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1365 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001366 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001367exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001368 /*
1369 * Key attributes may have been returned by psa_get_key_attributes()
1370 * thus reset them as required.
1371 */
1372 psa_reset_key_attributes( &attributes );
1373
itayzafrir3e02b3b2018-06-12 17:06:52 +03001374 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001375 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001376 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001377}
1378/* END_CASE */
1379
Gilles Peskine20035e32018-02-03 22:44:14 +01001380/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001381void import_and_exercise_key( data_t *data,
1382 int type_arg,
1383 int bits_arg,
1384 int alg_arg )
1385{
Ronald Cron5425a212020-08-04 14:58:35 +02001386 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001387 psa_key_type_t type = type_arg;
1388 size_t bits = bits_arg;
1389 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001390 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001391 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001392 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001393
Gilles Peskine8817f612018-12-18 00:18:46 +01001394 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001395
Gilles Peskine4747d192019-04-17 15:05:45 +02001396 psa_set_key_usage_flags( &attributes, usage );
1397 psa_set_key_algorithm( &attributes, alg );
1398 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001399
1400 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001401 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001402
1403 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001404 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001405 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1406 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001407
1408 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001409 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001410 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001411
Ronald Cron5425a212020-08-04 14:58:35 +02001412 PSA_ASSERT( psa_destroy_key( key ) );
1413 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001414
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001415exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001416 /*
1417 * Key attributes may have been returned by psa_get_key_attributes()
1418 * thus reset them as required.
1419 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001420 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001421
1422 psa_reset_key_attributes( &attributes );
1423 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001424 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001425}
1426/* END_CASE */
1427
1428/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001429void effective_key_attributes( int type_arg, int expected_type_arg,
1430 int bits_arg, int expected_bits_arg,
1431 int usage_arg, int expected_usage_arg,
1432 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001433{
Ronald Cron5425a212020-08-04 14:58:35 +02001434 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001435 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001436 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001437 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001438 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001439 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001440 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001441 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001442 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001444
Gilles Peskine8817f612018-12-18 00:18:46 +01001445 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001446
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001447 psa_set_key_usage_flags( &attributes, usage );
1448 psa_set_key_algorithm( &attributes, alg );
1449 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001450 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001451
Ronald Cron5425a212020-08-04 14:58:35 +02001452 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001453 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001454
Ronald Cron5425a212020-08-04 14:58:35 +02001455 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001456 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1457 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1458 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1459 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001460
1461exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001462 /*
1463 * Key attributes may have been returned by psa_get_key_attributes()
1464 * thus reset them as required.
1465 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001466 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001467
1468 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001469 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001470}
1471/* END_CASE */
1472
1473/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001474void check_key_policy( int type_arg, int bits_arg,
1475 int usage_arg, int alg_arg )
1476{
1477 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001478 usage_arg,
1479 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001480 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001481 goto exit;
1482}
1483/* END_CASE */
1484
1485/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001486void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001487{
1488 /* Test each valid way of initializing the object, except for `= {0}`, as
1489 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1490 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001491 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001492 psa_key_attributes_t func = psa_key_attributes_init( );
1493 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1494 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001495
1496 memset( &zero, 0, sizeof( zero ) );
1497
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001498 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1499 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1500 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001501
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001502 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1503 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1504 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1505
1506 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1507 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1508 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1509
1510 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1511 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1512 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1513
1514 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1515 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1516 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001517}
1518/* END_CASE */
1519
1520/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001521void mac_key_policy( int policy_usage_arg,
1522 int policy_alg_arg,
1523 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001524 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001525 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001526 int expected_status_sign_arg,
1527 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001528{
Ronald Cron5425a212020-08-04 14:58:35 +02001529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001530 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001531 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001532 psa_key_type_t key_type = key_type_arg;
1533 psa_algorithm_t policy_alg = policy_alg_arg;
1534 psa_algorithm_t exercise_alg = exercise_alg_arg;
1535 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001536 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001537 psa_status_t expected_status_sign = expected_status_sign_arg;
1538 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001539 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001540
Gilles Peskine8817f612018-12-18 00:18:46 +01001541 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001542
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001543 psa_set_key_usage_flags( &attributes, policy_usage );
1544 psa_set_key_algorithm( &attributes, policy_alg );
1545 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001546
Gilles Peskine049c7532019-05-15 20:22:09 +02001547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001548 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001549
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001550 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1551 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001552
Ronald Cron5425a212020-08-04 14:58:35 +02001553 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001554 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001555
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001556 /* Calculate the MAC, one-shot case. */
1557 uint8_t input[128] = {0};
1558 size_t mac_len;
1559 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1560 input, 128,
1561 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1562 expected_status_sign );
1563
Neil Armstrong3af9b972022-02-07 12:20:21 +01001564 /* Calculate the MAC, multi-part case. */
1565 PSA_ASSERT( psa_mac_abort( &operation ) );
1566 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1567 if( status == PSA_SUCCESS )
1568 {
1569 status = psa_mac_update( &operation, input, 128 );
1570 if( status == PSA_SUCCESS )
1571 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1572 &mac_len ),
1573 expected_status_sign );
1574 else
1575 TEST_EQUAL( status, expected_status_sign );
1576 }
1577 else
1578 {
1579 TEST_EQUAL( status, expected_status_sign );
1580 }
1581 PSA_ASSERT( psa_mac_abort( &operation ) );
1582
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001583 /* Verify correct MAC, one-shot case. */
1584 status = psa_mac_verify( key, exercise_alg, input, 128,
1585 mac, mac_len );
1586
1587 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1588 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001589 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001590 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001591
Neil Armstrong3af9b972022-02-07 12:20:21 +01001592 /* Verify correct MAC, multi-part case. */
1593 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1594 if( status == PSA_SUCCESS )
1595 {
1596 status = psa_mac_update( &operation, input, 128 );
1597 if( status == PSA_SUCCESS )
1598 {
1599 status = psa_mac_verify_finish( &operation, mac, mac_len );
1600 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1601 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1602 else
1603 TEST_EQUAL( status, expected_status_verify );
1604 }
1605 else
1606 {
1607 TEST_EQUAL( status, expected_status_verify );
1608 }
1609 }
1610 else
1611 {
1612 TEST_EQUAL( status, expected_status_verify );
1613 }
1614
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001616
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001617 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001618 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001619 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001620
1621exit:
1622 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001623 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001624 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625}
1626/* END_CASE */
1627
1628/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001629void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630 int policy_alg,
1631 int key_type,
1632 data_t *key_data,
1633 int exercise_alg )
1634{
Ronald Cron5425a212020-08-04 14:58:35 +02001635 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001636 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001637 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001638 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001639 size_t output_buffer_size = 0;
1640 size_t input_buffer_size = 0;
1641 size_t output_length = 0;
1642 uint8_t *output = NULL;
1643 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001644 psa_status_t status;
1645
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001646 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1647 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1648 input_buffer_size );
1649
1650 ASSERT_ALLOC( input, input_buffer_size );
1651 ASSERT_ALLOC( output, output_buffer_size );
1652
Gilles Peskine8817f612018-12-18 00:18:46 +01001653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001655 psa_set_key_usage_flags( &attributes, policy_usage );
1656 psa_set_key_algorithm( &attributes, policy_alg );
1657 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
Gilles Peskine049c7532019-05-15 20:22:09 +02001659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001660 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001661
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001662 /* Check if no key usage flag implication is done */
1663 TEST_EQUAL( policy_usage,
1664 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001665
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001666 /* Encrypt check, one-shot */
1667 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1668 output, output_buffer_size,
1669 &output_length);
1670 if( policy_alg == exercise_alg &&
1671 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1672 PSA_ASSERT( status );
1673 else
1674 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1675
1676 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001677 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 if( policy_alg == exercise_alg &&
1679 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001682 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683 psa_cipher_abort( &operation );
1684
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001685 /* Decrypt check, one-shot */
1686 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1687 input, input_buffer_size,
1688 &output_length);
1689 if( policy_alg == exercise_alg &&
1690 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1691 PSA_ASSERT( status );
1692 else
1693 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1694
1695 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001696 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001697 if( policy_alg == exercise_alg &&
1698 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001699 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001700 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001701 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001702
1703exit:
1704 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001705 mbedtls_free( input );
1706 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001707 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001708 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001709}
1710/* END_CASE */
1711
1712/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001713void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001714 int policy_alg,
1715 int key_type,
1716 data_t *key_data,
1717 int nonce_length_arg,
1718 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001719 int exercise_alg,
1720 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001721{
Ronald Cron5425a212020-08-04 14:58:35 +02001722 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001724 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001725 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001726 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001727 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001728 unsigned char nonce[16] = {0};
1729 size_t nonce_length = nonce_length_arg;
1730 unsigned char tag[16];
1731 size_t tag_length = tag_length_arg;
1732 size_t output_length;
1733
Gilles Peskine7be11a72022-04-14 00:12:57 +02001734 TEST_LE_U( nonce_length, sizeof( nonce ) );
1735 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001736
Gilles Peskine8817f612018-12-18 00:18:46 +01001737 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001738
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001739 psa_set_key_usage_flags( &attributes, policy_usage );
1740 psa_set_key_algorithm( &attributes, policy_alg );
1741 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001742
Gilles Peskine049c7532019-05-15 20:22:09 +02001743 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001744 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001745
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001746 /* Check if no key usage implication is done */
1747 TEST_EQUAL( policy_usage,
1748 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001749
Neil Armstrong752d8112022-02-07 14:51:11 +01001750 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02001751 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001752 nonce, nonce_length,
1753 NULL, 0,
1754 NULL, 0,
1755 tag, tag_length,
1756 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001757 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1758 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001759 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001760 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001761
Neil Armstrong752d8112022-02-07 14:51:11 +01001762 /* Encrypt check, multi-part */
1763 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
1764 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1765 TEST_EQUAL( status, expected_status );
1766 else
1767 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1768
1769 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001771 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001772 nonce, nonce_length,
1773 NULL, 0,
1774 tag, tag_length,
1775 NULL, 0,
1776 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001777 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1778 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1779 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001780 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001781 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001782 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001783
Neil Armstrong752d8112022-02-07 14:51:11 +01001784 /* Decrypt check, multi-part */
1785 PSA_ASSERT( psa_aead_abort( &operation ) );
1786 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
1787 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1788 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1789 else
1790 TEST_EQUAL( status, expected_status );
1791
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001792exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01001793 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001794 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001795 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001796}
1797/* END_CASE */
1798
1799/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001800void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001801 int policy_alg,
1802 int key_type,
1803 data_t *key_data,
1804 int exercise_alg )
1805{
Ronald Cron5425a212020-08-04 14:58:35 +02001806 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001807 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001808 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001809 psa_status_t status;
1810 size_t key_bits;
1811 size_t buffer_length;
1812 unsigned char *buffer = NULL;
1813 size_t output_length;
1814
Gilles Peskine8817f612018-12-18 00:18:46 +01001815 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001817 psa_set_key_usage_flags( &attributes, policy_usage );
1818 psa_set_key_algorithm( &attributes, policy_alg );
1819 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001820
Gilles Peskine049c7532019-05-15 20:22:09 +02001821 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001822 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001823
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001824 /* Check if no key usage implication is done */
1825 TEST_EQUAL( policy_usage,
1826 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001827
Ronald Cron5425a212020-08-04 14:58:35 +02001828 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001829 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001830 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1831 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001832 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833
Ronald Cron5425a212020-08-04 14:58:35 +02001834 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001835 NULL, 0,
1836 NULL, 0,
1837 buffer, buffer_length,
1838 &output_length );
1839 if( policy_alg == exercise_alg &&
1840 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001841 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001843 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001844
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001845 if( buffer_length != 0 )
1846 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001847 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001848 buffer, buffer_length,
1849 NULL, 0,
1850 buffer, buffer_length,
1851 &output_length );
1852 if( policy_alg == exercise_alg &&
1853 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001854 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001855 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001856 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001857
1858exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001859 /*
1860 * Key attributes may have been returned by psa_get_key_attributes()
1861 * thus reset them as required.
1862 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001863 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001864
1865 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001866 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001867 mbedtls_free( buffer );
1868}
1869/* END_CASE */
1870
1871/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001872void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001873 int policy_alg,
1874 int key_type,
1875 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001876 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001877 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001878 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001879{
Ronald Cron5425a212020-08-04 14:58:35 +02001880 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001881 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001882 psa_key_usage_t policy_usage = policy_usage_arg;
1883 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001884 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001885 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1886 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1887 * compatible with the policy and `payload_length_arg` is supposed to be
1888 * a valid input length to sign. If `payload_length_arg <= 0`,
1889 * `exercise_alg` is supposed to be forbidden by the policy. */
1890 int compatible_alg = payload_length_arg > 0;
1891 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001892 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893 size_t signature_length;
1894
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001895 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001896 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001897 TEST_EQUAL( expected_usage,
1898 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001899
Gilles Peskine8817f612018-12-18 00:18:46 +01001900 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001901
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001902 psa_set_key_usage_flags( &attributes, policy_usage );
1903 psa_set_key_algorithm( &attributes, policy_alg );
1904 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905
Gilles Peskine049c7532019-05-15 20:22:09 +02001906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001907 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001909 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1910
Ronald Cron5425a212020-08-04 14:58:35 +02001911 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001912 payload, payload_length,
1913 signature, sizeof( signature ),
1914 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001915 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001916 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001917 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001918 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001919
1920 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001921 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001922 payload, payload_length,
1923 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001924 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001925 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001926 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001927 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001928
Gilles Peskinef7b41372021-09-22 16:15:05 +02001929 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001930 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001931 {
1932 status = psa_sign_message( key, exercise_alg,
1933 payload, payload_length,
1934 signature, sizeof( signature ),
1935 &signature_length );
1936 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1937 PSA_ASSERT( status );
1938 else
1939 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1940
1941 memset( signature, 0, sizeof( signature ) );
1942 status = psa_verify_message( key, exercise_alg,
1943 payload, payload_length,
1944 signature, sizeof( signature ) );
1945 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1946 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1947 else
1948 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1949 }
1950
Gilles Peskined5b33222018-06-18 22:20:03 +02001951exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001952 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001953 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001954}
1955/* END_CASE */
1956
Janos Follathba3fab92019-06-11 14:50:16 +01001957/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001958void derive_key_policy( int policy_usage,
1959 int policy_alg,
1960 int key_type,
1961 data_t *key_data,
1962 int exercise_alg )
1963{
Ronald Cron5425a212020-08-04 14:58:35 +02001964 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001966 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001967 psa_status_t status;
1968
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001970
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001971 psa_set_key_usage_flags( &attributes, policy_usage );
1972 psa_set_key_algorithm( &attributes, policy_alg );
1973 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001974
Gilles Peskine049c7532019-05-15 20:22:09 +02001975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001976 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001977
Janos Follathba3fab92019-06-11 14:50:16 +01001978 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1979
1980 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1981 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001982 {
Janos Follathba3fab92019-06-11 14:50:16 +01001983 PSA_ASSERT( psa_key_derivation_input_bytes(
1984 &operation,
1985 PSA_KEY_DERIVATION_INPUT_SEED,
1986 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001987 }
Janos Follathba3fab92019-06-11 14:50:16 +01001988
1989 status = psa_key_derivation_input_key( &operation,
1990 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001991 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001992
Gilles Peskineea0fb492018-07-12 17:17:20 +02001993 if( policy_alg == exercise_alg &&
1994 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001995 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001996 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001997 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001998
1999exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002000 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002001 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002002 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002003}
2004/* END_CASE */
2005
2006/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002007void agreement_key_policy( int policy_usage,
2008 int policy_alg,
2009 int key_type_arg,
2010 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002011 int exercise_alg,
2012 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002013{
Ronald Cron5425a212020-08-04 14:58:35 +02002014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002016 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002017 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002018 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002019 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002020
Gilles Peskine8817f612018-12-18 00:18:46 +01002021 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002022
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002023 psa_set_key_usage_flags( &attributes, policy_usage );
2024 psa_set_key_algorithm( &attributes, policy_alg );
2025 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002026
Gilles Peskine049c7532019-05-15 20:22:09 +02002027 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002028 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002029
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002030 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002031 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002032
Steven Cooremance48e852020-10-05 16:02:45 +02002033 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002034
2035exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002036 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002037 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002038 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002039}
2040/* END_CASE */
2041
2042/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002043void key_policy_alg2( int key_type_arg, data_t *key_data,
2044 int usage_arg, int alg_arg, int alg2_arg )
2045{
Ronald Cron5425a212020-08-04 14:58:35 +02002046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002047 psa_key_type_t key_type = key_type_arg;
2048 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2049 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2050 psa_key_usage_t usage = usage_arg;
2051 psa_algorithm_t alg = alg_arg;
2052 psa_algorithm_t alg2 = alg2_arg;
2053
2054 PSA_ASSERT( psa_crypto_init( ) );
2055
2056 psa_set_key_usage_flags( &attributes, usage );
2057 psa_set_key_algorithm( &attributes, alg );
2058 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2059 psa_set_key_type( &attributes, key_type );
2060 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002061 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002062
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002063 /* Update the usage flags to obtain implicit usage flags */
2064 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002065 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002066 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2067 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2068 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2069
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002070 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002071 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002072 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002073 goto exit;
2074
2075exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002076 /*
2077 * Key attributes may have been returned by psa_get_key_attributes()
2078 * thus reset them as required.
2079 */
2080 psa_reset_key_attributes( &got_attributes );
2081
Ronald Cron5425a212020-08-04 14:58:35 +02002082 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002083 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002084}
2085/* END_CASE */
2086
2087/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002088void raw_agreement_key_policy( int policy_usage,
2089 int policy_alg,
2090 int key_type_arg,
2091 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002092 int exercise_alg,
2093 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002094{
Ronald Cron5425a212020-08-04 14:58:35 +02002095 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002097 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002098 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002099 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002100 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002101
2102 PSA_ASSERT( psa_crypto_init( ) );
2103
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002104 psa_set_key_usage_flags( &attributes, policy_usage );
2105 psa_set_key_algorithm( &attributes, policy_alg );
2106 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002107
Gilles Peskine049c7532019-05-15 20:22:09 +02002108 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002109 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002110
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002111 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002112
Steven Cooremance48e852020-10-05 16:02:45 +02002113 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002114
2115exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002116 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002117 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002118 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002119}
2120/* END_CASE */
2121
2122/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002123void copy_success( int source_usage_arg,
2124 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302125 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002126 int type_arg, data_t *material,
2127 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002128 int target_usage_arg,
2129 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302130 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002131 int expected_usage_arg,
2132 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002133{
Gilles Peskineca25db92019-04-19 11:43:08 +02002134 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2135 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002136 psa_key_usage_t expected_usage = expected_usage_arg;
2137 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002138 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302139 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2140 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002141 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2142 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002143 uint8_t *export_buffer = NULL;
2144
Gilles Peskine57ab7212019-01-28 13:03:09 +01002145 PSA_ASSERT( psa_crypto_init( ) );
2146
Gilles Peskineca25db92019-04-19 11:43:08 +02002147 /* Prepare the source key. */
2148 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2149 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002150 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002151 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302152 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002153 PSA_ASSERT( psa_import_key( &source_attributes,
2154 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002155 &source_key ) );
2156 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002157
Gilles Peskineca25db92019-04-19 11:43:08 +02002158 /* Prepare the target attributes. */
2159 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002160 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002161 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002162 }
Archana8a180362021-07-05 02:18:48 +05302163 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002164
Gilles Peskineca25db92019-04-19 11:43:08 +02002165 if( target_usage_arg != -1 )
2166 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2167 if( target_alg_arg != -1 )
2168 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002169 if( target_alg2_arg != -1 )
2170 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002171
Archana8a180362021-07-05 02:18:48 +05302172
Gilles Peskine57ab7212019-01-28 13:03:09 +01002173 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002174 PSA_ASSERT( psa_copy_key( source_key,
2175 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002176
2177 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002178 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002179
2180 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002181 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002182 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2183 psa_get_key_type( &target_attributes ) );
2184 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2185 psa_get_key_bits( &target_attributes ) );
2186 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2187 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002188 TEST_EQUAL( expected_alg2,
2189 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002190 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2191 {
2192 size_t length;
2193 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002194 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002195 material->len, &length ) );
2196 ASSERT_COMPARE( material->x, material->len,
2197 export_buffer, length );
2198 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002199
Archana8a180362021-07-05 02:18:48 +05302200 if( !psa_key_lifetime_is_external( target_lifetime ) )
2201 {
2202 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2203 goto exit;
2204 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2205 goto exit;
2206 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002207
Ronald Cron5425a212020-08-04 14:58:35 +02002208 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002209
2210exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002211 /*
2212 * Source and target key attributes may have been returned by
2213 * psa_get_key_attributes() thus reset them as required.
2214 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002215 psa_reset_key_attributes( &source_attributes );
2216 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002217
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002218 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002219 mbedtls_free( export_buffer );
2220}
2221/* END_CASE */
2222
2223/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002224void copy_fail( int source_usage_arg,
2225 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302226 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002227 int type_arg, data_t *material,
2228 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002229 int target_usage_arg,
2230 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002231 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002232 int expected_status_arg )
2233{
2234 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2235 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002236 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2237 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002238 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002239
2240 PSA_ASSERT( psa_crypto_init( ) );
2241
2242 /* Prepare the source key. */
2243 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2244 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002245 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002246 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302247 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002248 PSA_ASSERT( psa_import_key( &source_attributes,
2249 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002250 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002251
2252 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002253 psa_set_key_id( &target_attributes, key_id );
2254 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002255 psa_set_key_type( &target_attributes, target_type_arg );
2256 psa_set_key_bits( &target_attributes, target_bits_arg );
2257 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2258 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002259 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002260
2261 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002262 TEST_EQUAL( psa_copy_key( source_key,
2263 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002264 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002265
Ronald Cron5425a212020-08-04 14:58:35 +02002266 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002267
Gilles Peskine4a644642019-05-03 17:14:08 +02002268exit:
2269 psa_reset_key_attributes( &source_attributes );
2270 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002271 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002272}
2273/* END_CASE */
2274
2275/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002276void hash_operation_init( )
2277{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002278 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002279 /* Test each valid way of initializing the object, except for `= {0}`, as
2280 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2281 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002282 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002283 psa_hash_operation_t func = psa_hash_operation_init( );
2284 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2285 psa_hash_operation_t zero;
2286
2287 memset( &zero, 0, sizeof( zero ) );
2288
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002289 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002290 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2291 PSA_ERROR_BAD_STATE );
2292 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2293 PSA_ERROR_BAD_STATE );
2294 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2295 PSA_ERROR_BAD_STATE );
2296
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002297 /* A default hash operation should be abortable without error. */
2298 PSA_ASSERT( psa_hash_abort( &func ) );
2299 PSA_ASSERT( psa_hash_abort( &init ) );
2300 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002301}
2302/* END_CASE */
2303
2304/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002305void hash_setup( int alg_arg,
2306 int expected_status_arg )
2307{
2308 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002309 uint8_t *output = NULL;
2310 size_t output_size = 0;
2311 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002312 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002313 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002314 psa_status_t status;
2315
Gilles Peskine8817f612018-12-18 00:18:46 +01002316 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002317
Neil Armstrongedb20862022-02-07 15:47:44 +01002318 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002319 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002320 ASSERT_ALLOC( output, output_size );
2321
2322 status = psa_hash_compute( alg, NULL, 0,
2323 output, output_size, &output_length );
2324 TEST_EQUAL( status, expected_status );
2325
2326 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002327 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002328 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002329
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002330 /* Whether setup succeeded or failed, abort must succeed. */
2331 PSA_ASSERT( psa_hash_abort( &operation ) );
2332
2333 /* If setup failed, reproduce the failure, so as to
2334 * test the resulting state of the operation object. */
2335 if( status != PSA_SUCCESS )
2336 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2337
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002338 /* Now the operation object should be reusable. */
2339#if defined(KNOWN_SUPPORTED_HASH_ALG)
2340 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2341 PSA_ASSERT( psa_hash_abort( &operation ) );
2342#endif
2343
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002345 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002346 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347}
2348/* END_CASE */
2349
2350/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002351void hash_compute_fail( int alg_arg, data_t *input,
2352 int output_size_arg, int expected_status_arg )
2353{
2354 psa_algorithm_t alg = alg_arg;
2355 uint8_t *output = NULL;
2356 size_t output_size = output_size_arg;
2357 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002358 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002359 psa_status_t expected_status = expected_status_arg;
2360 psa_status_t status;
2361
2362 ASSERT_ALLOC( output, output_size );
2363
2364 PSA_ASSERT( psa_crypto_init( ) );
2365
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002366 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002367 status = psa_hash_compute( alg, input->x, input->len,
2368 output, output_size, &output_length );
2369 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002370 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002371
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002372 /* Hash Compute, multi-part */
2373 status = psa_hash_setup( &operation, alg );
2374 if( status == PSA_SUCCESS )
2375 {
2376 status = psa_hash_update( &operation, input->x, input->len );
2377 if( status == PSA_SUCCESS )
2378 {
2379 status = psa_hash_finish( &operation, output, output_size,
2380 &output_length );
2381 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002382 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002383 else
2384 TEST_EQUAL( status, expected_status );
2385 }
2386 else
2387 {
2388 TEST_EQUAL( status, expected_status );
2389 }
2390 }
2391 else
2392 {
2393 TEST_EQUAL( status, expected_status );
2394 }
2395
Gilles Peskine0a749c82019-11-28 19:33:58 +01002396exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002397 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002398 mbedtls_free( output );
2399 PSA_DONE( );
2400}
2401/* END_CASE */
2402
2403/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002404void hash_compare_fail( int alg_arg, data_t *input,
2405 data_t *reference_hash,
2406 int expected_status_arg )
2407{
2408 psa_algorithm_t alg = alg_arg;
2409 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002410 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002411 psa_status_t status;
2412
2413 PSA_ASSERT( psa_crypto_init( ) );
2414
Neil Armstrong55a1be12022-02-07 11:23:20 +01002415 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002416 status = psa_hash_compare( alg, input->x, input->len,
2417 reference_hash->x, reference_hash->len );
2418 TEST_EQUAL( status, expected_status );
2419
Neil Armstrong55a1be12022-02-07 11:23:20 +01002420 /* Hash Compare, multi-part */
2421 status = psa_hash_setup( &operation, alg );
2422 if( status == PSA_SUCCESS )
2423 {
2424 status = psa_hash_update( &operation, input->x, input->len );
2425 if( status == PSA_SUCCESS )
2426 {
2427 status = psa_hash_verify( &operation, reference_hash->x,
2428 reference_hash->len );
2429 TEST_EQUAL( status, expected_status );
2430 }
2431 else
2432 {
2433 TEST_EQUAL( status, expected_status );
2434 }
2435 }
2436 else
2437 {
2438 TEST_EQUAL( status, expected_status );
2439 }
2440
Gilles Peskine88e08462020-01-28 20:43:00 +01002441exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002442 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002443 PSA_DONE( );
2444}
2445/* END_CASE */
2446
2447/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002448void hash_compute_compare( int alg_arg, data_t *input,
2449 data_t *expected_output )
2450{
2451 psa_algorithm_t alg = alg_arg;
2452 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2453 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002454 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002455 size_t i;
2456
2457 PSA_ASSERT( psa_crypto_init( ) );
2458
Neil Armstrongca30a002022-02-07 11:40:23 +01002459 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002460 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002461 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002462 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002463 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002464 ASSERT_COMPARE( output, output_length,
2465 expected_output->x, expected_output->len );
2466
Neil Armstrongca30a002022-02-07 11:40:23 +01002467 /* Compute with tight buffer, multi-part */
2468 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2469 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2470 PSA_ASSERT( psa_hash_finish( &operation, output,
2471 PSA_HASH_LENGTH( alg ),
2472 &output_length ) );
2473 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2474 ASSERT_COMPARE( output, output_length,
2475 expected_output->x, expected_output->len );
2476
2477 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002478 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2479 output, sizeof( output ),
2480 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002481 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002482 ASSERT_COMPARE( output, output_length,
2483 expected_output->x, expected_output->len );
2484
Neil Armstrongca30a002022-02-07 11:40:23 +01002485 /* Compute with larger buffer, multi-part */
2486 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2487 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2488 PSA_ASSERT( psa_hash_finish( &operation, output,
2489 sizeof( output ), &output_length ) );
2490 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2491 ASSERT_COMPARE( output, output_length,
2492 expected_output->x, expected_output->len );
2493
2494 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002495 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2496 output, output_length ) );
2497
Neil Armstrongca30a002022-02-07 11:40:23 +01002498 /* Compare with correct hash, multi-part */
2499 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2500 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2501 PSA_ASSERT( psa_hash_verify( &operation, output,
2502 output_length ) );
2503
2504 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002505 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2506 output, output_length + 1 ),
2507 PSA_ERROR_INVALID_SIGNATURE );
2508
Neil Armstrongca30a002022-02-07 11:40:23 +01002509 /* Compare with trailing garbage, multi-part */
2510 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2511 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2512 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2513 PSA_ERROR_INVALID_SIGNATURE );
2514
2515 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002516 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2517 output, output_length - 1 ),
2518 PSA_ERROR_INVALID_SIGNATURE );
2519
Neil Armstrongca30a002022-02-07 11:40:23 +01002520 /* Compare with truncated hash, multi-part */
2521 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2522 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2523 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2524 PSA_ERROR_INVALID_SIGNATURE );
2525
Gilles Peskine0a749c82019-11-28 19:33:58 +01002526 /* Compare with corrupted value */
2527 for( i = 0; i < output_length; i++ )
2528 {
Chris Jones9634bb12021-01-20 15:56:42 +00002529 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002530 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002531
2532 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002533 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2534 output, output_length ),
2535 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002536
2537 /* Multi-Part */
2538 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2539 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2540 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2541 PSA_ERROR_INVALID_SIGNATURE );
2542
Gilles Peskine0a749c82019-11-28 19:33:58 +01002543 output[i] ^= 1;
2544 }
2545
2546exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002547 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002548 PSA_DONE( );
2549}
2550/* END_CASE */
2551
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002552/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002553void hash_bad_order( )
2554{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002555 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002556 unsigned char input[] = "";
2557 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002558 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002559 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2560 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2561 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002562 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002563 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002564 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002565
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002567
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002568 /* Call setup twice in a row. */
2569 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002570 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002571 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2572 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002573 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002574 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002575 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002576
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002577 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002578 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002579 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002580 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002581
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002582 /* Check that update calls abort on error. */
2583 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002584 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002585 ASSERT_OPERATION_IS_ACTIVE( operation );
2586 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2587 PSA_ERROR_BAD_STATE );
2588 ASSERT_OPERATION_IS_INACTIVE( operation );
2589 PSA_ASSERT( psa_hash_abort( &operation ) );
2590 ASSERT_OPERATION_IS_INACTIVE( operation );
2591
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002592 /* Call update after finish. */
2593 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2594 PSA_ASSERT( psa_hash_finish( &operation,
2595 hash, sizeof( hash ), &hash_len ) );
2596 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002597 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002598 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002599
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002600 /* Call verify without calling setup beforehand. */
2601 TEST_EQUAL( psa_hash_verify( &operation,
2602 valid_hash, sizeof( valid_hash ) ),
2603 PSA_ERROR_BAD_STATE );
2604 PSA_ASSERT( psa_hash_abort( &operation ) );
2605
2606 /* Call verify after finish. */
2607 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2608 PSA_ASSERT( psa_hash_finish( &operation,
2609 hash, sizeof( hash ), &hash_len ) );
2610 TEST_EQUAL( psa_hash_verify( &operation,
2611 valid_hash, sizeof( valid_hash ) ),
2612 PSA_ERROR_BAD_STATE );
2613 PSA_ASSERT( psa_hash_abort( &operation ) );
2614
2615 /* Call verify twice in a row. */
2616 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002617 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002618 PSA_ASSERT( psa_hash_verify( &operation,
2619 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002620 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002621 TEST_EQUAL( psa_hash_verify( &operation,
2622 valid_hash, sizeof( valid_hash ) ),
2623 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002624 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002625 PSA_ASSERT( psa_hash_abort( &operation ) );
2626
2627 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002628 TEST_EQUAL( psa_hash_finish( &operation,
2629 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002630 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002631 PSA_ASSERT( psa_hash_abort( &operation ) );
2632
2633 /* Call finish twice in a row. */
2634 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2635 PSA_ASSERT( psa_hash_finish( &operation,
2636 hash, sizeof( hash ), &hash_len ) );
2637 TEST_EQUAL( psa_hash_finish( &operation,
2638 hash, sizeof( hash ), &hash_len ),
2639 PSA_ERROR_BAD_STATE );
2640 PSA_ASSERT( psa_hash_abort( &operation ) );
2641
2642 /* Call finish after calling verify. */
2643 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2644 PSA_ASSERT( psa_hash_verify( &operation,
2645 valid_hash, sizeof( valid_hash ) ) );
2646 TEST_EQUAL( psa_hash_finish( &operation,
2647 hash, sizeof( hash ), &hash_len ),
2648 PSA_ERROR_BAD_STATE );
2649 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002650
2651exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002652 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002653}
2654/* END_CASE */
2655
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002656/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002657void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002658{
2659 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002660 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2661 * appended to it */
2662 unsigned char hash[] = {
2663 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2664 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2665 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002666 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002667 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002668
Gilles Peskine8817f612018-12-18 00:18:46 +01002669 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002670
itayzafrir27e69452018-11-01 14:26:34 +02002671 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002672 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002673 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002674 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002675 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002676 ASSERT_OPERATION_IS_INACTIVE( operation );
2677 PSA_ASSERT( psa_hash_abort( &operation ) );
2678 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002679
itayzafrir27e69452018-11-01 14:26:34 +02002680 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002681 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002682 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002683 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002684
itayzafrir27e69452018-11-01 14:26:34 +02002685 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002686 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002687 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002688 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002689
itayzafrirec93d302018-10-18 18:01:10 +03002690exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002691 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002692}
2693/* END_CASE */
2694
Ronald Cronee414c72021-03-18 18:50:08 +01002695/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002696void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002697{
2698 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002699 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002700 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002701 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002702 size_t hash_len;
2703
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002705
itayzafrir58028322018-10-25 10:22:01 +03002706 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002708 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002709 hash, expected_size - 1, &hash_len ),
2710 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002711
2712exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002713 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002714}
2715/* END_CASE */
2716
Ronald Cronee414c72021-03-18 18:50:08 +01002717/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002718void hash_clone_source_state( )
2719{
2720 psa_algorithm_t alg = PSA_ALG_SHA_256;
2721 unsigned char hash[PSA_HASH_MAX_SIZE];
2722 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2723 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2724 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2725 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2726 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2727 size_t hash_len;
2728
2729 PSA_ASSERT( psa_crypto_init( ) );
2730 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2731
2732 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2733 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2734 PSA_ASSERT( psa_hash_finish( &op_finished,
2735 hash, sizeof( hash ), &hash_len ) );
2736 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2737 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2738
2739 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2740 PSA_ERROR_BAD_STATE );
2741
2742 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2743 PSA_ASSERT( psa_hash_finish( &op_init,
2744 hash, sizeof( hash ), &hash_len ) );
2745 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2746 PSA_ASSERT( psa_hash_finish( &op_finished,
2747 hash, sizeof( hash ), &hash_len ) );
2748 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2749 PSA_ASSERT( psa_hash_finish( &op_aborted,
2750 hash, sizeof( hash ), &hash_len ) );
2751
2752exit:
2753 psa_hash_abort( &op_source );
2754 psa_hash_abort( &op_init );
2755 psa_hash_abort( &op_setup );
2756 psa_hash_abort( &op_finished );
2757 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002758 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002759}
2760/* END_CASE */
2761
Ronald Cronee414c72021-03-18 18:50:08 +01002762/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002763void hash_clone_target_state( )
2764{
2765 psa_algorithm_t alg = PSA_ALG_SHA_256;
2766 unsigned char hash[PSA_HASH_MAX_SIZE];
2767 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2768 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2769 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2770 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2771 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2772 size_t hash_len;
2773
2774 PSA_ASSERT( psa_crypto_init( ) );
2775
2776 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2777 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2778 PSA_ASSERT( psa_hash_finish( &op_finished,
2779 hash, sizeof( hash ), &hash_len ) );
2780 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2781 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2782
2783 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2784 PSA_ASSERT( psa_hash_finish( &op_target,
2785 hash, sizeof( hash ), &hash_len ) );
2786
2787 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2788 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2789 PSA_ERROR_BAD_STATE );
2790 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2791 PSA_ERROR_BAD_STATE );
2792
2793exit:
2794 psa_hash_abort( &op_target );
2795 psa_hash_abort( &op_init );
2796 psa_hash_abort( &op_setup );
2797 psa_hash_abort( &op_finished );
2798 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002799 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002800}
2801/* END_CASE */
2802
itayzafrir58028322018-10-25 10:22:01 +03002803/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002804void mac_operation_init( )
2805{
Jaeden Amero252ef282019-02-15 14:05:35 +00002806 const uint8_t input[1] = { 0 };
2807
Jaeden Amero769ce272019-01-04 11:48:03 +00002808 /* Test each valid way of initializing the object, except for `= {0}`, as
2809 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2810 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002811 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00002812 psa_mac_operation_t func = psa_mac_operation_init( );
2813 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2814 psa_mac_operation_t zero;
2815
2816 memset( &zero, 0, sizeof( zero ) );
2817
Jaeden Amero252ef282019-02-15 14:05:35 +00002818 /* A freshly-initialized MAC operation should not be usable. */
2819 TEST_EQUAL( psa_mac_update( &func,
2820 input, sizeof( input ) ),
2821 PSA_ERROR_BAD_STATE );
2822 TEST_EQUAL( psa_mac_update( &init,
2823 input, sizeof( input ) ),
2824 PSA_ERROR_BAD_STATE );
2825 TEST_EQUAL( psa_mac_update( &zero,
2826 input, sizeof( input ) ),
2827 PSA_ERROR_BAD_STATE );
2828
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002829 /* A default MAC operation should be abortable without error. */
2830 PSA_ASSERT( psa_mac_abort( &func ) );
2831 PSA_ASSERT( psa_mac_abort( &init ) );
2832 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002833}
2834/* END_CASE */
2835
2836/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002837void mac_setup( int key_type_arg,
2838 data_t *key,
2839 int alg_arg,
2840 int expected_status_arg )
2841{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002842 psa_key_type_t key_type = key_type_arg;
2843 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002844 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002845 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002846 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2847#if defined(KNOWN_SUPPORTED_MAC_ALG)
2848 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2849#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002850
Gilles Peskine8817f612018-12-18 00:18:46 +01002851 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002852
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002853 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2854 &operation, &status ) )
2855 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002856 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002857
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002858 /* The operation object should be reusable. */
2859#if defined(KNOWN_SUPPORTED_MAC_ALG)
2860 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2861 smoke_test_key_data,
2862 sizeof( smoke_test_key_data ),
2863 KNOWN_SUPPORTED_MAC_ALG,
2864 &operation, &status ) )
2865 goto exit;
2866 TEST_EQUAL( status, PSA_SUCCESS );
2867#endif
2868
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002869exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002870 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002871}
2872/* END_CASE */
2873
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002874/* 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 +00002875void mac_bad_order( )
2876{
Ronald Cron5425a212020-08-04 14:58:35 +02002877 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002878 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2879 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002880 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002881 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2882 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2883 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002885 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2886 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2887 size_t sign_mac_length = 0;
2888 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2889 const uint8_t verify_mac[] = {
2890 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2891 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2892 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2893
2894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002896 psa_set_key_algorithm( &attributes, alg );
2897 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002898
Ronald Cron5425a212020-08-04 14:58:35 +02002899 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2900 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002901
Jaeden Amero252ef282019-02-15 14:05:35 +00002902 /* Call update without calling setup beforehand. */
2903 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2904 PSA_ERROR_BAD_STATE );
2905 PSA_ASSERT( psa_mac_abort( &operation ) );
2906
2907 /* Call sign finish without calling setup beforehand. */
2908 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2909 &sign_mac_length),
2910 PSA_ERROR_BAD_STATE );
2911 PSA_ASSERT( psa_mac_abort( &operation ) );
2912
2913 /* Call verify finish without calling setup beforehand. */
2914 TEST_EQUAL( psa_mac_verify_finish( &operation,
2915 verify_mac, sizeof( verify_mac ) ),
2916 PSA_ERROR_BAD_STATE );
2917 PSA_ASSERT( psa_mac_abort( &operation ) );
2918
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002919 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002920 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002921 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002922 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002923 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002924 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002925 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002926 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002927
Jaeden Amero252ef282019-02-15 14:05:35 +00002928 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002929 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002930 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2931 PSA_ASSERT( psa_mac_sign_finish( &operation,
2932 sign_mac, sizeof( sign_mac ),
2933 &sign_mac_length ) );
2934 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2935 PSA_ERROR_BAD_STATE );
2936 PSA_ASSERT( psa_mac_abort( &operation ) );
2937
2938 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002939 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002940 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2941 PSA_ASSERT( psa_mac_verify_finish( &operation,
2942 verify_mac, sizeof( verify_mac ) ) );
2943 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2944 PSA_ERROR_BAD_STATE );
2945 PSA_ASSERT( psa_mac_abort( &operation ) );
2946
2947 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002948 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002949 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2950 PSA_ASSERT( psa_mac_sign_finish( &operation,
2951 sign_mac, sizeof( sign_mac ),
2952 &sign_mac_length ) );
2953 TEST_EQUAL( psa_mac_sign_finish( &operation,
2954 sign_mac, sizeof( sign_mac ),
2955 &sign_mac_length ),
2956 PSA_ERROR_BAD_STATE );
2957 PSA_ASSERT( psa_mac_abort( &operation ) );
2958
2959 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002960 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002961 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2962 PSA_ASSERT( psa_mac_verify_finish( &operation,
2963 verify_mac, sizeof( verify_mac ) ) );
2964 TEST_EQUAL( psa_mac_verify_finish( &operation,
2965 verify_mac, sizeof( verify_mac ) ),
2966 PSA_ERROR_BAD_STATE );
2967 PSA_ASSERT( psa_mac_abort( &operation ) );
2968
2969 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002970 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002971 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002972 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002973 TEST_EQUAL( psa_mac_verify_finish( &operation,
2974 verify_mac, sizeof( verify_mac ) ),
2975 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002976 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002977 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002978 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002979
2980 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002981 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002982 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002983 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002984 TEST_EQUAL( psa_mac_sign_finish( &operation,
2985 sign_mac, sizeof( sign_mac ),
2986 &sign_mac_length ),
2987 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002988 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002989 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002990 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002991
Ronald Cron5425a212020-08-04 14:58:35 +02002992 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002993
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002994exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002995 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002996}
2997/* END_CASE */
2998
2999/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003000void mac_sign_verify_multi( int key_type_arg,
3001 data_t *key_data,
3002 int alg_arg,
3003 data_t *input,
3004 int is_verify,
3005 data_t *expected_mac )
3006{
3007 size_t data_part_len = 0;
3008
3009 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3010 {
3011 /* Split data into length(data_part_len) parts. */
3012 mbedtls_test_set_step( 2000 + data_part_len );
3013
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003014 if( mac_multipart_internal_func( key_type_arg, key_data,
3015 alg_arg,
3016 input, data_part_len,
3017 expected_mac,
3018 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003019 break;
3020
3021 /* length(0) part, length(data_part_len) part, length(0) part... */
3022 mbedtls_test_set_step( 3000 + data_part_len );
3023
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003024 if( mac_multipart_internal_func( key_type_arg, key_data,
3025 alg_arg,
3026 input, data_part_len,
3027 expected_mac,
3028 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003029 break;
3030 }
3031
3032 /* Goto is required to silence warnings about unused labels, as we
3033 * don't actually do any test assertions in this function. */
3034 goto exit;
3035}
3036/* END_CASE */
3037
3038/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003039void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003040 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003041 int alg_arg,
3042 data_t *input,
3043 data_t *expected_mac )
3044{
Ronald Cron5425a212020-08-04 14:58:35 +02003045 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003046 psa_key_type_t key_type = key_type_arg;
3047 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003048 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003050 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003051 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003052 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003053 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003054 const size_t output_sizes_to_test[] = {
3055 0,
3056 1,
3057 expected_mac->len - 1,
3058 expected_mac->len,
3059 expected_mac->len + 1,
3060 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003061
Gilles Peskine7be11a72022-04-14 00:12:57 +02003062 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003063 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003064 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003065
Gilles Peskine8817f612018-12-18 00:18:46 +01003066 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003067
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003068 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003069 psa_set_key_algorithm( &attributes, alg );
3070 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003071
Ronald Cron5425a212020-08-04 14:58:35 +02003072 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3073 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003074
Gilles Peskine8b356b52020-08-25 23:44:59 +02003075 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3076 {
3077 const size_t output_size = output_sizes_to_test[i];
3078 psa_status_t expected_status =
3079 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3080 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003081
Chris Jones9634bb12021-01-20 15:56:42 +00003082 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003083 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003084
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003085 /* Calculate the MAC, one-shot case. */
3086 TEST_EQUAL( psa_mac_compute( key, alg,
3087 input->x, input->len,
3088 actual_mac, output_size, &mac_length ),
3089 expected_status );
3090 if( expected_status == PSA_SUCCESS )
3091 {
3092 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3093 actual_mac, mac_length );
3094 }
3095
3096 if( output_size > 0 )
3097 memset( actual_mac, 0, output_size );
3098
3099 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003100 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003101 PSA_ASSERT( psa_mac_update( &operation,
3102 input->x, input->len ) );
3103 TEST_EQUAL( psa_mac_sign_finish( &operation,
3104 actual_mac, output_size,
3105 &mac_length ),
3106 expected_status );
3107 PSA_ASSERT( psa_mac_abort( &operation ) );
3108
3109 if( expected_status == PSA_SUCCESS )
3110 {
3111 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3112 actual_mac, mac_length );
3113 }
3114 mbedtls_free( actual_mac );
3115 actual_mac = NULL;
3116 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003117
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003118exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003119 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003120 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003121 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003122 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003123}
3124/* END_CASE */
3125
3126/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003127void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003128 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003129 int alg_arg,
3130 data_t *input,
3131 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003132{
Ronald Cron5425a212020-08-04 14:58:35 +02003133 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003134 psa_key_type_t key_type = key_type_arg;
3135 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003136 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003137 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003138 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003139
Gilles Peskine7be11a72022-04-14 00:12:57 +02003140 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003141
Gilles Peskine8817f612018-12-18 00:18:46 +01003142 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003143
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003144 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003145 psa_set_key_algorithm( &attributes, alg );
3146 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003147
Ronald Cron5425a212020-08-04 14:58:35 +02003148 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3149 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003150
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003151 /* Verify correct MAC, one-shot case. */
3152 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3153 expected_mac->x, expected_mac->len ) );
3154
3155 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003156 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_mac_update( &operation,
3158 input->x, input->len ) );
3159 PSA_ASSERT( psa_mac_verify_finish( &operation,
3160 expected_mac->x,
3161 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003162
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003163 /* Test a MAC that's too short, one-shot case. */
3164 TEST_EQUAL( psa_mac_verify( key, alg,
3165 input->x, input->len,
3166 expected_mac->x,
3167 expected_mac->len - 1 ),
3168 PSA_ERROR_INVALID_SIGNATURE );
3169
3170 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003171 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003172 PSA_ASSERT( psa_mac_update( &operation,
3173 input->x, input->len ) );
3174 TEST_EQUAL( psa_mac_verify_finish( &operation,
3175 expected_mac->x,
3176 expected_mac->len - 1 ),
3177 PSA_ERROR_INVALID_SIGNATURE );
3178
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003179 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003180 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3181 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003182 TEST_EQUAL( psa_mac_verify( key, alg,
3183 input->x, input->len,
3184 perturbed_mac, expected_mac->len + 1 ),
3185 PSA_ERROR_INVALID_SIGNATURE );
3186
3187 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003188 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003189 PSA_ASSERT( psa_mac_update( &operation,
3190 input->x, input->len ) );
3191 TEST_EQUAL( psa_mac_verify_finish( &operation,
3192 perturbed_mac,
3193 expected_mac->len + 1 ),
3194 PSA_ERROR_INVALID_SIGNATURE );
3195
3196 /* Test changing one byte. */
3197 for( size_t i = 0; i < expected_mac->len; i++ )
3198 {
Chris Jones9634bb12021-01-20 15:56:42 +00003199 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003200 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003201
3202 TEST_EQUAL( psa_mac_verify( key, alg,
3203 input->x, input->len,
3204 perturbed_mac, expected_mac->len ),
3205 PSA_ERROR_INVALID_SIGNATURE );
3206
Ronald Cron5425a212020-08-04 14:58:35 +02003207 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003208 PSA_ASSERT( psa_mac_update( &operation,
3209 input->x, input->len ) );
3210 TEST_EQUAL( psa_mac_verify_finish( &operation,
3211 perturbed_mac,
3212 expected_mac->len ),
3213 PSA_ERROR_INVALID_SIGNATURE );
3214 perturbed_mac[i] ^= 1;
3215 }
3216
Gilles Peskine8c9def32018-02-08 10:02:12 +01003217exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003218 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003219 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003220 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003221 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003222}
3223/* END_CASE */
3224
3225/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003226void cipher_operation_init( )
3227{
Jaeden Ameroab439972019-02-15 14:12:05 +00003228 const uint8_t input[1] = { 0 };
3229 unsigned char output[1] = { 0 };
3230 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003231 /* Test each valid way of initializing the object, except for `= {0}`, as
3232 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3233 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003234 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003235 psa_cipher_operation_t func = psa_cipher_operation_init( );
3236 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3237 psa_cipher_operation_t zero;
3238
3239 memset( &zero, 0, sizeof( zero ) );
3240
Jaeden Ameroab439972019-02-15 14:12:05 +00003241 /* A freshly-initialized cipher operation should not be usable. */
3242 TEST_EQUAL( psa_cipher_update( &func,
3243 input, sizeof( input ),
3244 output, sizeof( output ),
3245 &output_length ),
3246 PSA_ERROR_BAD_STATE );
3247 TEST_EQUAL( psa_cipher_update( &init,
3248 input, sizeof( input ),
3249 output, sizeof( output ),
3250 &output_length ),
3251 PSA_ERROR_BAD_STATE );
3252 TEST_EQUAL( psa_cipher_update( &zero,
3253 input, sizeof( input ),
3254 output, sizeof( output ),
3255 &output_length ),
3256 PSA_ERROR_BAD_STATE );
3257
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003258 /* A default cipher operation should be abortable without error. */
3259 PSA_ASSERT( psa_cipher_abort( &func ) );
3260 PSA_ASSERT( psa_cipher_abort( &init ) );
3261 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003262}
3263/* END_CASE */
3264
3265/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003266void cipher_setup( int key_type_arg,
3267 data_t *key,
3268 int alg_arg,
3269 int expected_status_arg )
3270{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003271 psa_key_type_t key_type = key_type_arg;
3272 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003273 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003274 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003275 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003276#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003277 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3278#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003279
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003281
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003282 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3283 &operation, &status ) )
3284 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003285 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003286
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003287 /* The operation object should be reusable. */
3288#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3289 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3290 smoke_test_key_data,
3291 sizeof( smoke_test_key_data ),
3292 KNOWN_SUPPORTED_CIPHER_ALG,
3293 &operation, &status ) )
3294 goto exit;
3295 TEST_EQUAL( status, PSA_SUCCESS );
3296#endif
3297
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003298exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003299 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003300 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003301}
3302/* END_CASE */
3303
Ronald Cronee414c72021-03-18 18:50:08 +01003304/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003305void cipher_bad_order( )
3306{
Ronald Cron5425a212020-08-04 14:58:35 +02003307 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003308 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3309 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003310 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003311 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003312 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003313 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003314 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3315 0xaa, 0xaa, 0xaa, 0xaa };
3316 const uint8_t text[] = {
3317 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3318 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003319 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003320 size_t length = 0;
3321
3322 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003323 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3324 psa_set_key_algorithm( &attributes, alg );
3325 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003326 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3327 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003328
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003329 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003331 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003332 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003333 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003335 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003336 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003337
3338 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003339 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003340 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003341 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003342 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003343 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003344 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003345 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003346
Jaeden Ameroab439972019-02-15 14:12:05 +00003347 /* Generate an IV without calling setup beforehand. */
3348 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3349 buffer, sizeof( buffer ),
3350 &length ),
3351 PSA_ERROR_BAD_STATE );
3352 PSA_ASSERT( psa_cipher_abort( &operation ) );
3353
3354 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003355 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003356 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3357 buffer, sizeof( buffer ),
3358 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003359 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003360 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3361 buffer, sizeof( buffer ),
3362 &length ),
3363 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003364 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003365 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003366 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003367
3368 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003369 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003370 PSA_ASSERT( psa_cipher_set_iv( &operation,
3371 iv, sizeof( iv ) ) );
3372 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3373 buffer, sizeof( buffer ),
3374 &length ),
3375 PSA_ERROR_BAD_STATE );
3376 PSA_ASSERT( psa_cipher_abort( &operation ) );
3377
3378 /* Set an IV without calling setup beforehand. */
3379 TEST_EQUAL( psa_cipher_set_iv( &operation,
3380 iv, sizeof( iv ) ),
3381 PSA_ERROR_BAD_STATE );
3382 PSA_ASSERT( psa_cipher_abort( &operation ) );
3383
3384 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003385 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003386 PSA_ASSERT( psa_cipher_set_iv( &operation,
3387 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003388 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003389 TEST_EQUAL( psa_cipher_set_iv( &operation,
3390 iv, sizeof( iv ) ),
3391 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003392 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003393 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003394 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003395
3396 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003397 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003398 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3399 buffer, sizeof( buffer ),
3400 &length ) );
3401 TEST_EQUAL( psa_cipher_set_iv( &operation,
3402 iv, sizeof( iv ) ),
3403 PSA_ERROR_BAD_STATE );
3404 PSA_ASSERT( psa_cipher_abort( &operation ) );
3405
3406 /* Call update without calling setup beforehand. */
3407 TEST_EQUAL( psa_cipher_update( &operation,
3408 text, sizeof( text ),
3409 buffer, sizeof( buffer ),
3410 &length ),
3411 PSA_ERROR_BAD_STATE );
3412 PSA_ASSERT( psa_cipher_abort( &operation ) );
3413
3414 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003415 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003416 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003417 TEST_EQUAL( psa_cipher_update( &operation,
3418 text, sizeof( text ),
3419 buffer, sizeof( buffer ),
3420 &length ),
3421 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003422 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003423 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003424 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003425
3426 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003427 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003428 PSA_ASSERT( psa_cipher_set_iv( &operation,
3429 iv, sizeof( iv ) ) );
3430 PSA_ASSERT( psa_cipher_finish( &operation,
3431 buffer, sizeof( buffer ), &length ) );
3432 TEST_EQUAL( psa_cipher_update( &operation,
3433 text, sizeof( text ),
3434 buffer, sizeof( buffer ),
3435 &length ),
3436 PSA_ERROR_BAD_STATE );
3437 PSA_ASSERT( psa_cipher_abort( &operation ) );
3438
3439 /* Call finish without calling setup beforehand. */
3440 TEST_EQUAL( psa_cipher_finish( &operation,
3441 buffer, sizeof( buffer ), &length ),
3442 PSA_ERROR_BAD_STATE );
3443 PSA_ASSERT( psa_cipher_abort( &operation ) );
3444
3445 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003446 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003447 /* Not calling update means we are encrypting an empty buffer, which is OK
3448 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003449 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003450 TEST_EQUAL( psa_cipher_finish( &operation,
3451 buffer, sizeof( buffer ), &length ),
3452 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003453 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003454 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003455 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003456
3457 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003458 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003459 PSA_ASSERT( psa_cipher_set_iv( &operation,
3460 iv, sizeof( iv ) ) );
3461 PSA_ASSERT( psa_cipher_finish( &operation,
3462 buffer, sizeof( buffer ), &length ) );
3463 TEST_EQUAL( psa_cipher_finish( &operation,
3464 buffer, sizeof( buffer ), &length ),
3465 PSA_ERROR_BAD_STATE );
3466 PSA_ASSERT( psa_cipher_abort( &operation ) );
3467
Ronald Cron5425a212020-08-04 14:58:35 +02003468 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003469
Jaeden Ameroab439972019-02-15 14:12:05 +00003470exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003471 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003472 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003473}
3474/* END_CASE */
3475
3476/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003477void cipher_encrypt_fail( int alg_arg,
3478 int key_type_arg,
3479 data_t *key_data,
3480 data_t *input,
3481 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003482{
Ronald Cron5425a212020-08-04 14:58:35 +02003483 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003484 psa_status_t status;
3485 psa_key_type_t key_type = key_type_arg;
3486 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003487 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003488 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3489 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3490 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003491 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003492 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003493 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003494 size_t function_output_length;
3495 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003496 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3497
3498 if ( PSA_ERROR_BAD_STATE != expected_status )
3499 {
3500 PSA_ASSERT( psa_crypto_init( ) );
3501
3502 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3503 psa_set_key_algorithm( &attributes, alg );
3504 psa_set_key_type( &attributes, key_type );
3505
3506 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3507 input->len );
3508 ASSERT_ALLOC( output, output_buffer_size );
3509
3510 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3511 &key ) );
3512 }
3513
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003514 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003515 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3516 output_buffer_size, &output_length );
3517
3518 TEST_EQUAL( status, expected_status );
3519
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003520 /* Encrypt, multi-part */
3521 status = psa_cipher_encrypt_setup( &operation, key, alg );
3522 if( status == PSA_SUCCESS )
3523 {
3524 if( alg != PSA_ALG_ECB_NO_PADDING )
3525 {
3526 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3527 iv, iv_size,
3528 &iv_length ) );
3529 }
3530
3531 status = psa_cipher_update( &operation, input->x, input->len,
3532 output, output_buffer_size,
3533 &function_output_length );
3534 if( status == PSA_SUCCESS )
3535 {
3536 output_length += function_output_length;
3537
3538 status = psa_cipher_finish( &operation, output + output_length,
3539 output_buffer_size - output_length,
3540 &function_output_length );
3541
3542 TEST_EQUAL( status, expected_status );
3543 }
3544 else
3545 {
3546 TEST_EQUAL( status, expected_status );
3547 }
3548 }
3549 else
3550 {
3551 TEST_EQUAL( status, expected_status );
3552 }
3553
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003554exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003555 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003556 mbedtls_free( output );
3557 psa_destroy_key( key );
3558 PSA_DONE( );
3559}
3560/* END_CASE */
3561
3562/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003563void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3564 data_t *input, int iv_length,
3565 int expected_result )
3566{
3567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3568 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3570 size_t output_buffer_size = 0;
3571 unsigned char *output = NULL;
3572
3573 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3574 ASSERT_ALLOC( output, output_buffer_size );
3575
3576 PSA_ASSERT( psa_crypto_init( ) );
3577
3578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3579 psa_set_key_algorithm( &attributes, alg );
3580 psa_set_key_type( &attributes, key_type );
3581
3582 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3583 &key ) );
3584 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3585 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3586 iv_length ) );
3587
3588exit:
3589 psa_cipher_abort( &operation );
3590 mbedtls_free( output );
3591 psa_destroy_key( key );
3592 PSA_DONE( );
3593}
3594/* END_CASE */
3595
3596/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003597void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3598 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003599{
3600 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3601 psa_key_type_t key_type = key_type_arg;
3602 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003603 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3604 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003605 unsigned char *output = NULL;
3606 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003607 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3609
3610 PSA_ASSERT( psa_crypto_init( ) );
3611
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003612 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003613 TEST_LE_U( ciphertext->len,
3614 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3615 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003616 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003617 TEST_LE_U( plaintext->len,
3618 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3619 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3620 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003621
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003622
3623 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003624 psa_set_key_usage_flags( &attributes,
3625 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003626 psa_set_key_algorithm( &attributes, alg );
3627 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003628 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3629 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003630 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3631 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003632 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003633
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003634 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003635 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3636 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3637 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003638 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3639 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003640 PSA_ERROR_BAD_STATE );
3641
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003642 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003643 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3644 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003645 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003646 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003647 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3648 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003649 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003650 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003651
Gilles Peskine286c3142022-04-20 17:09:38 +02003652 /* Multipart encryption */
3653 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3654 output_length = 0;
3655 length = ~0;
3656 PSA_ASSERT( psa_cipher_update( &operation,
3657 plaintext->x, plaintext->len,
3658 output, output_buffer_size,
3659 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003660 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003661 output_length += length;
3662 PSA_ASSERT( psa_cipher_finish( &operation,
3663 output + output_length,
3664 output_buffer_size - output_length,
3665 &length ) );
3666 output_length += length;
3667 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003668 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003669
Gilles Peskine286c3142022-04-20 17:09:38 +02003670 /* Multipart encryption */
3671 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3672 output_length = 0;
3673 length = ~0;
3674 PSA_ASSERT( psa_cipher_update( &operation,
3675 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003676 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003677 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003678 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003679 output_length += length;
3680 PSA_ASSERT( psa_cipher_finish( &operation,
3681 output + output_length,
3682 output_buffer_size - output_length,
3683 &length ) );
3684 output_length += length;
3685 ASSERT_COMPARE( plaintext->x, plaintext->len,
3686 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003687
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003688 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003689 output_length = ~0;
3690 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3691 output, output_buffer_size,
3692 &output_length ) );
3693 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3694 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003695
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003696 /* One-shot decryption */
3697 output_length = ~0;
3698 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
3699 output, output_buffer_size,
3700 &output_length ) );
3701 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003702 output, output_length );
3703
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003704exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003705 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003706 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003707 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003708 psa_destroy_key( key );
3709 PSA_DONE( );
3710}
3711/* END_CASE */
3712
3713/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003714void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3715{
3716 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3717 psa_algorithm_t alg = alg_arg;
3718 psa_key_type_t key_type = key_type_arg;
3719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3720 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3721 psa_status_t status;
3722
3723 PSA_ASSERT( psa_crypto_init( ) );
3724
3725 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3726 psa_set_key_algorithm( &attributes, alg );
3727 psa_set_key_type( &attributes, key_type );
3728
3729 /* Usage of either of these two size macros would cause divide by zero
3730 * with incorrect key types previously. Input length should be irrelevant
3731 * here. */
3732 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3733 0 );
3734 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3735
3736
3737 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3738 &key ) );
3739
3740 /* Should fail due to invalid alg type (to support invalid key type).
3741 * Encrypt or decrypt will end up in the same place. */
3742 status = psa_cipher_encrypt_setup( &operation, key, alg );
3743
3744 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3745
3746exit:
3747 psa_cipher_abort( &operation );
3748 psa_destroy_key( key );
3749 PSA_DONE( );
3750}
3751/* END_CASE */
3752
3753/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003754void cipher_encrypt_validation( int alg_arg,
3755 int key_type_arg,
3756 data_t *key_data,
3757 data_t *input )
3758{
3759 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3760 psa_key_type_t key_type = key_type_arg;
3761 psa_algorithm_t alg = alg_arg;
3762 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3763 unsigned char *output1 = NULL;
3764 size_t output1_buffer_size = 0;
3765 size_t output1_length = 0;
3766 unsigned char *output2 = NULL;
3767 size_t output2_buffer_size = 0;
3768 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003769 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003770 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003772
Gilles Peskine8817f612018-12-18 00:18:46 +01003773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003774
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003775 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3776 psa_set_key_algorithm( &attributes, alg );
3777 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003778
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003779 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3780 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3781 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3782 ASSERT_ALLOC( output1, output1_buffer_size );
3783 ASSERT_ALLOC( output2, output2_buffer_size );
3784
Ronald Cron5425a212020-08-04 14:58:35 +02003785 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3786 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003787
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003788 /* The one-shot cipher encryption uses generated iv so validating
3789 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003790 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3791 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003792 TEST_LE_U( output1_length,
3793 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3794 TEST_LE_U( output1_length,
3795 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003796
3797 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3798 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003799
Gilles Peskine8817f612018-12-18 00:18:46 +01003800 PSA_ASSERT( psa_cipher_update( &operation,
3801 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003802 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003803 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003804 TEST_LE_U( function_output_length,
3805 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3806 TEST_LE_U( function_output_length,
3807 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003808 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003809
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003810 PSA_ASSERT( psa_cipher_finish( &operation,
3811 output2 + output2_length,
3812 output2_buffer_size - output2_length,
3813 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003814 TEST_LE_U( function_output_length,
3815 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3816 TEST_LE_U( function_output_length,
3817 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003818 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003819
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820 PSA_ASSERT( psa_cipher_abort( &operation ) );
3821 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3822 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003823
Gilles Peskine50e586b2018-06-08 14:28:46 +02003824exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003825 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003826 mbedtls_free( output1 );
3827 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003828 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003829 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003830}
3831/* END_CASE */
3832
3833/* BEGIN_CASE */
3834void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003835 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003836 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003837 int first_part_size_arg,
3838 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003839 data_t *expected_output,
3840 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003841{
Ronald Cron5425a212020-08-04 14:58:35 +02003842 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003843 psa_key_type_t key_type = key_type_arg;
3844 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003845 psa_status_t status;
3846 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003847 size_t first_part_size = first_part_size_arg;
3848 size_t output1_length = output1_length_arg;
3849 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003850 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003851 size_t output_buffer_size = 0;
3852 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003853 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003854 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003856
Gilles Peskine8817f612018-12-18 00:18:46 +01003857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003859 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3860 psa_set_key_algorithm( &attributes, alg );
3861 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003862
Ronald Cron5425a212020-08-04 14:58:35 +02003863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3864 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003865
Ronald Cron5425a212020-08-04 14:58:35 +02003866 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003867
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003868 if( iv->len > 0 )
3869 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003870 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003871 }
3872
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003873 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3874 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003875 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003876
Gilles Peskine7be11a72022-04-14 00:12:57 +02003877 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003878 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3879 output, output_buffer_size,
3880 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003881 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003882 TEST_LE_U( function_output_length,
3883 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3884 TEST_LE_U( function_output_length,
3885 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003886 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003887
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003888 if( first_part_size < input->len )
3889 {
3890 PSA_ASSERT( psa_cipher_update( &operation,
3891 input->x + first_part_size,
3892 input->len - first_part_size,
3893 ( output_buffer_size == 0 ? NULL :
3894 output + total_output_length ),
3895 output_buffer_size - total_output_length,
3896 &function_output_length ) );
3897 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003898 TEST_LE_U( function_output_length,
3899 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3900 alg,
3901 input->len - first_part_size ) );
3902 TEST_LE_U( function_output_length,
3903 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003904 total_output_length += function_output_length;
3905 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003906
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003907 status = psa_cipher_finish( &operation,
3908 ( output_buffer_size == 0 ? NULL :
3909 output + total_output_length ),
3910 output_buffer_size - total_output_length,
3911 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003912 TEST_LE_U( function_output_length,
3913 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3914 TEST_LE_U( function_output_length,
3915 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003916 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003917 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003918
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003919 if( expected_status == PSA_SUCCESS )
3920 {
3921 PSA_ASSERT( psa_cipher_abort( &operation ) );
3922
3923 ASSERT_COMPARE( expected_output->x, expected_output->len,
3924 output, total_output_length );
3925 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003926
3927exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003928 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003929 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003930 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003931 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003932}
3933/* END_CASE */
3934
3935/* BEGIN_CASE */
3936void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003937 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003938 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003939 int first_part_size_arg,
3940 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003941 data_t *expected_output,
3942 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003943{
Ronald Cron5425a212020-08-04 14:58:35 +02003944 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003945 psa_key_type_t key_type = key_type_arg;
3946 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003947 psa_status_t status;
3948 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003949 size_t first_part_size = first_part_size_arg;
3950 size_t output1_length = output1_length_arg;
3951 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003952 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003953 size_t output_buffer_size = 0;
3954 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003955 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003956 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003958
Gilles Peskine8817f612018-12-18 00:18:46 +01003959 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003960
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3962 psa_set_key_algorithm( &attributes, alg );
3963 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003964
Ronald Cron5425a212020-08-04 14:58:35 +02003965 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3966 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003967
Ronald Cron5425a212020-08-04 14:58:35 +02003968 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003969
Steven Cooreman177deba2020-09-07 17:14:14 +02003970 if( iv->len > 0 )
3971 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003972 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003973 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003974
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003975 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3976 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003977 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003978
Gilles Peskine7be11a72022-04-14 00:12:57 +02003979 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003980 PSA_ASSERT( psa_cipher_update( &operation,
3981 input->x, first_part_size,
3982 output, output_buffer_size,
3983 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003984 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003985 TEST_LE_U( function_output_length,
3986 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3987 TEST_LE_U( function_output_length,
3988 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003989 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003990
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003991 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003992 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003993 PSA_ASSERT( psa_cipher_update( &operation,
3994 input->x + first_part_size,
3995 input->len - first_part_size,
3996 ( output_buffer_size == 0 ? NULL :
3997 output + total_output_length ),
3998 output_buffer_size - total_output_length,
3999 &function_output_length ) );
4000 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004001 TEST_LE_U( function_output_length,
4002 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4003 alg,
4004 input->len - first_part_size ) );
4005 TEST_LE_U( function_output_length,
4006 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004007 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004008 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004009
Gilles Peskine50e586b2018-06-08 14:28:46 +02004010 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004011 ( output_buffer_size == 0 ? NULL :
4012 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004013 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004014 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004015 TEST_LE_U( function_output_length,
4016 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4017 TEST_LE_U( function_output_length,
4018 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004019 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004020 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004021
4022 if( expected_status == PSA_SUCCESS )
4023 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004024 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004025
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004026 ASSERT_COMPARE( expected_output->x, expected_output->len,
4027 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004028 }
4029
Gilles Peskine50e586b2018-06-08 14:28:46 +02004030exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004031 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004032 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004033 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004034 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004035}
4036/* END_CASE */
4037
Gilles Peskine50e586b2018-06-08 14:28:46 +02004038/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004039void cipher_decrypt_fail( int alg_arg,
4040 int key_type_arg,
4041 data_t *key_data,
4042 data_t *iv,
4043 data_t *input_arg,
4044 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004045{
4046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4047 psa_status_t status;
4048 psa_key_type_t key_type = key_type_arg;
4049 psa_algorithm_t alg = alg_arg;
4050 psa_status_t expected_status = expected_status_arg;
4051 unsigned char *input = NULL;
4052 size_t input_buffer_size = 0;
4053 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004054 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004055 size_t output_buffer_size = 0;
4056 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004057 size_t function_output_length;
4058 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004059 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4060
4061 if ( PSA_ERROR_BAD_STATE != expected_status )
4062 {
4063 PSA_ASSERT( psa_crypto_init( ) );
4064
4065 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4066 psa_set_key_algorithm( &attributes, alg );
4067 psa_set_key_type( &attributes, key_type );
4068
4069 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4070 &key ) );
4071 }
4072
4073 /* Allocate input buffer and copy the iv and the plaintext */
4074 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4075 if ( input_buffer_size > 0 )
4076 {
4077 ASSERT_ALLOC( input, input_buffer_size );
4078 memcpy( input, iv->x, iv->len );
4079 memcpy( input + iv->len, input_arg->x, input_arg->len );
4080 }
4081
4082 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4083 ASSERT_ALLOC( output, output_buffer_size );
4084
Neil Armstrong66a479f2022-02-07 15:41:19 +01004085 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004086 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4087 output_buffer_size, &output_length );
4088 TEST_EQUAL( status, expected_status );
4089
Neil Armstrong66a479f2022-02-07 15:41:19 +01004090 /* Decrypt, multi-part */
4091 status = psa_cipher_decrypt_setup( &operation, key, alg );
4092 if( status == PSA_SUCCESS )
4093 {
4094 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4095 input_arg->len ) +
4096 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4097 ASSERT_ALLOC( output_multi, output_buffer_size );
4098
4099 if( iv->len > 0 )
4100 {
4101 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4102
4103 if( status != PSA_SUCCESS )
4104 TEST_EQUAL( status, expected_status );
4105 }
4106
4107 if( status == PSA_SUCCESS )
4108 {
4109 status = psa_cipher_update( &operation,
4110 input_arg->x, input_arg->len,
4111 output_multi, output_buffer_size,
4112 &function_output_length );
4113 if( status == PSA_SUCCESS )
4114 {
4115 output_length = function_output_length;
4116
4117 status = psa_cipher_finish( &operation,
4118 output_multi + output_length,
4119 output_buffer_size - output_length,
4120 &function_output_length );
4121
4122 TEST_EQUAL( status, expected_status );
4123 }
4124 else
4125 {
4126 TEST_EQUAL( status, expected_status );
4127 }
4128 }
4129 else
4130 {
4131 TEST_EQUAL( status, expected_status );
4132 }
4133 }
4134 else
4135 {
4136 TEST_EQUAL( status, expected_status );
4137 }
4138
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004139exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004140 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004141 mbedtls_free( input );
4142 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004143 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004144 psa_destroy_key( key );
4145 PSA_DONE( );
4146}
4147/* END_CASE */
4148
4149/* BEGIN_CASE */
4150void cipher_decrypt( int alg_arg,
4151 int key_type_arg,
4152 data_t *key_data,
4153 data_t *iv,
4154 data_t *input_arg,
4155 data_t *expected_output )
4156{
4157 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4158 psa_key_type_t key_type = key_type_arg;
4159 psa_algorithm_t alg = alg_arg;
4160 unsigned char *input = NULL;
4161 size_t input_buffer_size = 0;
4162 unsigned char *output = NULL;
4163 size_t output_buffer_size = 0;
4164 size_t output_length = 0;
4165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4166
4167 PSA_ASSERT( psa_crypto_init( ) );
4168
4169 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4170 psa_set_key_algorithm( &attributes, alg );
4171 psa_set_key_type( &attributes, key_type );
4172
4173 /* Allocate input buffer and copy the iv and the plaintext */
4174 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4175 if ( input_buffer_size > 0 )
4176 {
4177 ASSERT_ALLOC( input, input_buffer_size );
4178 memcpy( input, iv->x, iv->len );
4179 memcpy( input + iv->len, input_arg->x, input_arg->len );
4180 }
4181
4182 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4183 ASSERT_ALLOC( output, output_buffer_size );
4184
4185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4186 &key ) );
4187
4188 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4189 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004190 TEST_LE_U( output_length,
4191 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4192 TEST_LE_U( output_length,
4193 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004194
4195 ASSERT_COMPARE( expected_output->x, expected_output->len,
4196 output, output_length );
4197exit:
4198 mbedtls_free( input );
4199 mbedtls_free( output );
4200 psa_destroy_key( key );
4201 PSA_DONE( );
4202}
4203/* END_CASE */
4204
4205/* BEGIN_CASE */
4206void cipher_verify_output( int alg_arg,
4207 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004208 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004209 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004210{
Ronald Cron5425a212020-08-04 14:58:35 +02004211 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004212 psa_key_type_t key_type = key_type_arg;
4213 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004214 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004215 size_t output1_size = 0;
4216 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004217 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004218 size_t output2_size = 0;
4219 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004220 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004221
Gilles Peskine8817f612018-12-18 00:18:46 +01004222 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004223
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004224 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4225 psa_set_key_algorithm( &attributes, alg );
4226 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004227
Ronald Cron5425a212020-08-04 14:58:35 +02004228 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4229 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004230 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004231 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004232
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004233 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4234 output1, output1_size,
4235 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004236 TEST_LE_U( output1_length,
4237 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4238 TEST_LE_U( output1_length,
4239 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004240
4241 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004242 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004243
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004244 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4245 output2, output2_size,
4246 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004247 TEST_LE_U( output2_length,
4248 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4249 TEST_LE_U( output2_length,
4250 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004251
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004252 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004253
4254exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004255 mbedtls_free( output1 );
4256 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004257 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004258 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004259}
4260/* END_CASE */
4261
4262/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004263void cipher_verify_output_multipart( int alg_arg,
4264 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004265 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004266 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004267 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004268{
Ronald Cron5425a212020-08-04 14:58:35 +02004269 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004270 psa_key_type_t key_type = key_type_arg;
4271 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004272 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004273 unsigned char iv[16] = {0};
4274 size_t iv_size = 16;
4275 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004276 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004277 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004278 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004279 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004280 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004281 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004282 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004283 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4284 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004286
Gilles Peskine8817f612018-12-18 00:18:46 +01004287 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004288
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004289 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4290 psa_set_key_algorithm( &attributes, alg );
4291 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004292
Ronald Cron5425a212020-08-04 14:58:35 +02004293 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4294 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004295
Ronald Cron5425a212020-08-04 14:58:35 +02004296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4297 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004298
Steven Cooreman177deba2020-09-07 17:14:14 +02004299 if( alg != PSA_ALG_ECB_NO_PADDING )
4300 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004301 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4302 iv, iv_size,
4303 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004304 }
4305
gabor-mezei-armceface22021-01-21 12:26:17 +01004306 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004307 TEST_LE_U( output1_buffer_size,
4308 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004309 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004310
Gilles Peskine7be11a72022-04-14 00:12:57 +02004311 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004312
Gilles Peskine8817f612018-12-18 00:18:46 +01004313 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4314 output1, output1_buffer_size,
4315 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004316 TEST_LE_U( function_output_length,
4317 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4318 TEST_LE_U( function_output_length,
4319 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004320 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004321
Gilles Peskine8817f612018-12-18 00:18:46 +01004322 PSA_ASSERT( psa_cipher_update( &operation1,
4323 input->x + first_part_size,
4324 input->len - first_part_size,
4325 output1, output1_buffer_size,
4326 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004327 TEST_LE_U( function_output_length,
4328 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4329 alg,
4330 input->len - first_part_size ) );
4331 TEST_LE_U( function_output_length,
4332 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004333 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004334
Gilles Peskine8817f612018-12-18 00:18:46 +01004335 PSA_ASSERT( psa_cipher_finish( &operation1,
4336 output1 + output1_length,
4337 output1_buffer_size - output1_length,
4338 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004339 TEST_LE_U( function_output_length,
4340 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4341 TEST_LE_U( function_output_length,
4342 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004343 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004344
Gilles Peskine8817f612018-12-18 00:18:46 +01004345 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004346
Gilles Peskine048b7f02018-06-08 14:20:49 +02004347 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004348 TEST_LE_U( output2_buffer_size,
4349 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4350 TEST_LE_U( output2_buffer_size,
4351 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004352 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004353
Steven Cooreman177deba2020-09-07 17:14:14 +02004354 if( iv_length > 0 )
4355 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004356 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4357 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004358 }
Moran Pekerded84402018-06-06 16:36:50 +03004359
Gilles Peskine8817f612018-12-18 00:18:46 +01004360 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4361 output2, output2_buffer_size,
4362 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004363 TEST_LE_U( function_output_length,
4364 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4365 TEST_LE_U( function_output_length,
4366 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004367 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004368
Gilles Peskine8817f612018-12-18 00:18:46 +01004369 PSA_ASSERT( psa_cipher_update( &operation2,
4370 output1 + first_part_size,
4371 output1_length - first_part_size,
4372 output2, output2_buffer_size,
4373 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004374 TEST_LE_U( function_output_length,
4375 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4376 alg,
4377 output1_length - first_part_size ) );
4378 TEST_LE_U( function_output_length,
4379 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004380 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004381
Gilles Peskine8817f612018-12-18 00:18:46 +01004382 PSA_ASSERT( psa_cipher_finish( &operation2,
4383 output2 + output2_length,
4384 output2_buffer_size - output2_length,
4385 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004386 TEST_LE_U( function_output_length,
4387 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4388 TEST_LE_U( function_output_length,
4389 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004390 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004391
Gilles Peskine8817f612018-12-18 00:18:46 +01004392 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004393
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004394 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004395
4396exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004397 psa_cipher_abort( &operation1 );
4398 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004399 mbedtls_free( output1 );
4400 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004401 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004402 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004403}
4404/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004405
Gilles Peskine20035e32018-02-03 22:44:14 +01004406/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004407void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004408 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004409 data_t *nonce,
4410 data_t *additional_data,
4411 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004412 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004413{
Ronald Cron5425a212020-08-04 14:58:35 +02004414 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004415 psa_key_type_t key_type = key_type_arg;
4416 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004417 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004418 unsigned char *output_data = NULL;
4419 size_t output_size = 0;
4420 size_t output_length = 0;
4421 unsigned char *output_data2 = NULL;
4422 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004423 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004424 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004426
Gilles Peskine8817f612018-12-18 00:18:46 +01004427 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004428
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4430 psa_set_key_algorithm( &attributes, alg );
4431 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004432
Gilles Peskine049c7532019-05-15 20:22:09 +02004433 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004434 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004435 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4436 key_bits = psa_get_key_bits( &attributes );
4437
4438 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4439 alg );
4440 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4441 * should be exact. */
4442 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4443 expected_result != PSA_ERROR_NOT_SUPPORTED )
4444 {
4445 TEST_EQUAL( output_size,
4446 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004447 TEST_LE_U( output_size,
4448 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004449 }
4450 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004451
Steven Cooremanf49478b2021-02-15 15:19:25 +01004452 status = psa_aead_encrypt( key, alg,
4453 nonce->x, nonce->len,
4454 additional_data->x,
4455 additional_data->len,
4456 input_data->x, input_data->len,
4457 output_data, output_size,
4458 &output_length );
4459
4460 /* If the operation is not supported, just skip and not fail in case the
4461 * encryption involves a common limitation of cryptography hardwares and
4462 * an alternative implementation. */
4463 if( status == PSA_ERROR_NOT_SUPPORTED )
4464 {
4465 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4466 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4467 }
4468
4469 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004470
4471 if( PSA_SUCCESS == expected_result )
4472 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004473 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004474
Gilles Peskine003a4a92019-05-14 16:09:40 +02004475 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4476 * should be exact. */
4477 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004478 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004479
Gilles Peskine7be11a72022-04-14 00:12:57 +02004480 TEST_LE_U( input_data->len,
4481 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004482
Ronald Cron5425a212020-08-04 14:58:35 +02004483 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004484 nonce->x, nonce->len,
4485 additional_data->x,
4486 additional_data->len,
4487 output_data, output_length,
4488 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004489 &output_length2 ),
4490 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004491
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004492 ASSERT_COMPARE( input_data->x, input_data->len,
4493 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004494 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004495
Gilles Peskinea1cac842018-06-11 19:33:02 +02004496exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004497 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004498 mbedtls_free( output_data );
4499 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004500 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004501}
4502/* END_CASE */
4503
4504/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004505void aead_encrypt( int key_type_arg, data_t *key_data,
4506 int alg_arg,
4507 data_t *nonce,
4508 data_t *additional_data,
4509 data_t *input_data,
4510 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004511{
Ronald Cron5425a212020-08-04 14:58:35 +02004512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004513 psa_key_type_t key_type = key_type_arg;
4514 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004515 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004516 unsigned char *output_data = NULL;
4517 size_t output_size = 0;
4518 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004520 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004521
Gilles Peskine8817f612018-12-18 00:18:46 +01004522 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004523
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004524 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4525 psa_set_key_algorithm( &attributes, alg );
4526 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004527
Gilles Peskine049c7532019-05-15 20:22:09 +02004528 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004529 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004530 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4531 key_bits = psa_get_key_bits( &attributes );
4532
4533 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4534 alg );
4535 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4536 * should be exact. */
4537 TEST_EQUAL( output_size,
4538 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004539 TEST_LE_U( output_size,
4540 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004541 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004542
Steven Cooremand588ea12021-01-11 19:36:04 +01004543 status = psa_aead_encrypt( key, alg,
4544 nonce->x, nonce->len,
4545 additional_data->x, additional_data->len,
4546 input_data->x, input_data->len,
4547 output_data, output_size,
4548 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004549
Ronald Cron28a45ed2021-02-09 20:35:42 +01004550 /* If the operation is not supported, just skip and not fail in case the
4551 * encryption involves a common limitation of cryptography hardwares and
4552 * an alternative implementation. */
4553 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004554 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004555 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4556 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004557 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004558
4559 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004560 ASSERT_COMPARE( expected_result->x, expected_result->len,
4561 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004562
Gilles Peskinea1cac842018-06-11 19:33:02 +02004563exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004564 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004565 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004566 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004567}
4568/* END_CASE */
4569
4570/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004571void aead_decrypt( int key_type_arg, data_t *key_data,
4572 int alg_arg,
4573 data_t *nonce,
4574 data_t *additional_data,
4575 data_t *input_data,
4576 data_t *expected_data,
4577 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004578{
Ronald Cron5425a212020-08-04 14:58:35 +02004579 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004580 psa_key_type_t key_type = key_type_arg;
4581 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004582 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004583 unsigned char *output_data = NULL;
4584 size_t output_size = 0;
4585 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004587 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004588 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004589
Gilles Peskine8817f612018-12-18 00:18:46 +01004590 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004591
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004592 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4593 psa_set_key_algorithm( &attributes, alg );
4594 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004595
Gilles Peskine049c7532019-05-15 20:22:09 +02004596 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004597 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004598 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4599 key_bits = psa_get_key_bits( &attributes );
4600
4601 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4602 alg );
4603 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4604 expected_result != PSA_ERROR_NOT_SUPPORTED )
4605 {
4606 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4607 * should be exact. */
4608 TEST_EQUAL( output_size,
4609 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004610 TEST_LE_U( output_size,
4611 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004612 }
4613 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004614
Steven Cooremand588ea12021-01-11 19:36:04 +01004615 status = psa_aead_decrypt( key, alg,
4616 nonce->x, nonce->len,
4617 additional_data->x,
4618 additional_data->len,
4619 input_data->x, input_data->len,
4620 output_data, output_size,
4621 &output_length );
4622
Ronald Cron28a45ed2021-02-09 20:35:42 +01004623 /* If the operation is not supported, just skip and not fail in case the
4624 * decryption involves a common limitation of cryptography hardwares and
4625 * an alternative implementation. */
4626 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004627 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004628 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4629 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004630 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004631
4632 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004633
Gilles Peskine2d277862018-06-18 15:41:12 +02004634 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004635 ASSERT_COMPARE( expected_data->x, expected_data->len,
4636 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004637
Gilles Peskinea1cac842018-06-11 19:33:02 +02004638exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004639 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004640 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004641 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004642}
4643/* END_CASE */
4644
4645/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004646void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4647 int alg_arg,
4648 data_t *nonce,
4649 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004650 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004651 int do_set_lengths,
4652 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004653{
Paul Elliottd3f82412021-06-16 16:52:21 +01004654 size_t ad_part_len = 0;
4655 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004656 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004657
Paul Elliott32f46ba2021-09-23 18:24:36 +01004658 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004659 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004660 mbedtls_test_set_step( ad_part_len );
4661
4662 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004663 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004664 if( ad_part_len & 0x01 )
4665 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4666 else
4667 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004668 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004669
4670 /* Split ad into length(ad_part_len) parts. */
4671 if( !aead_multipart_internal_func( key_type_arg, key_data,
4672 alg_arg, nonce,
4673 additional_data,
4674 ad_part_len,
4675 input_data, -1,
4676 set_lengths_method,
4677 expected_output,
4678 1, 0 ) )
4679 break;
4680
4681 /* length(0) part, length(ad_part_len) part, length(0) part... */
4682 mbedtls_test_set_step( 1000 + ad_part_len );
4683
4684 if( !aead_multipart_internal_func( key_type_arg, key_data,
4685 alg_arg, nonce,
4686 additional_data,
4687 ad_part_len,
4688 input_data, -1,
4689 set_lengths_method,
4690 expected_output,
4691 1, 1 ) )
4692 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004693 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004694
Paul Elliott32f46ba2021-09-23 18:24:36 +01004695 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004696 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004697 /* Split data into length(data_part_len) parts. */
4698 mbedtls_test_set_step( 2000 + data_part_len );
4699
4700 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004701 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004702 if( data_part_len & 0x01 )
4703 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4704 else
4705 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004706 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004707
Paul Elliott32f46ba2021-09-23 18:24:36 +01004708 if( !aead_multipart_internal_func( key_type_arg, key_data,
4709 alg_arg, nonce,
4710 additional_data, -1,
4711 input_data, data_part_len,
4712 set_lengths_method,
4713 expected_output,
4714 1, 0 ) )
4715 break;
4716
4717 /* length(0) part, length(data_part_len) part, length(0) part... */
4718 mbedtls_test_set_step( 3000 + data_part_len );
4719
4720 if( !aead_multipart_internal_func( key_type_arg, key_data,
4721 alg_arg, nonce,
4722 additional_data, -1,
4723 input_data, data_part_len,
4724 set_lengths_method,
4725 expected_output,
4726 1, 1 ) )
4727 break;
4728 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004729
Paul Elliott8fc45162021-06-23 16:06:01 +01004730 /* Goto is required to silence warnings about unused labels, as we
4731 * don't actually do any test assertions in this function. */
4732 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004733}
4734/* END_CASE */
4735
4736/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004737void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4738 int alg_arg,
4739 data_t *nonce,
4740 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004741 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004742 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004743 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004744{
Paul Elliottd3f82412021-06-16 16:52:21 +01004745 size_t ad_part_len = 0;
4746 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004747 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004748
Paul Elliott32f46ba2021-09-23 18:24:36 +01004749 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004750 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004751 /* Split ad into length(ad_part_len) parts. */
4752 mbedtls_test_set_step( ad_part_len );
4753
4754 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004755 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004756 if( ad_part_len & 0x01 )
4757 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4758 else
4759 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004760 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004761
4762 if( !aead_multipart_internal_func( key_type_arg, key_data,
4763 alg_arg, nonce,
4764 additional_data,
4765 ad_part_len,
4766 input_data, -1,
4767 set_lengths_method,
4768 expected_output,
4769 0, 0 ) )
4770 break;
4771
4772 /* length(0) part, length(ad_part_len) part, length(0) part... */
4773 mbedtls_test_set_step( 1000 + ad_part_len );
4774
4775 if( !aead_multipart_internal_func( key_type_arg, key_data,
4776 alg_arg, nonce,
4777 additional_data,
4778 ad_part_len,
4779 input_data, -1,
4780 set_lengths_method,
4781 expected_output,
4782 0, 1 ) )
4783 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004784 }
4785
Paul Elliott32f46ba2021-09-23 18:24:36 +01004786 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004787 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004788 /* Split data into length(data_part_len) parts. */
4789 mbedtls_test_set_step( 2000 + data_part_len );
4790
4791 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004792 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004793 if( data_part_len & 0x01 )
4794 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4795 else
4796 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004797 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004798
4799 if( !aead_multipart_internal_func( key_type_arg, key_data,
4800 alg_arg, nonce,
4801 additional_data, -1,
4802 input_data, data_part_len,
4803 set_lengths_method,
4804 expected_output,
4805 0, 0 ) )
4806 break;
4807
4808 /* length(0) part, length(data_part_len) part, length(0) part... */
4809 mbedtls_test_set_step( 3000 + data_part_len );
4810
4811 if( !aead_multipart_internal_func( key_type_arg, key_data,
4812 alg_arg, nonce,
4813 additional_data, -1,
4814 input_data, data_part_len,
4815 set_lengths_method,
4816 expected_output,
4817 0, 1 ) )
4818 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004819 }
4820
Paul Elliott8fc45162021-06-23 16:06:01 +01004821 /* Goto is required to silence warnings about unused labels, as we
4822 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004823 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004824}
4825/* END_CASE */
4826
4827/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004828void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4829 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004830 int nonce_length,
4831 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004832 data_t *additional_data,
4833 data_t *input_data,
4834 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004835{
4836
4837 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4838 psa_key_type_t key_type = key_type_arg;
4839 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004840 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004841 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4842 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4843 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004844 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004845 size_t actual_nonce_length = 0;
4846 size_t expected_nonce_length = expected_nonce_length_arg;
4847 unsigned char *output = NULL;
4848 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004849 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004850 size_t ciphertext_size = 0;
4851 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004852 size_t tag_length = 0;
4853 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004854
4855 PSA_ASSERT( psa_crypto_init( ) );
4856
4857 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4858 psa_set_key_algorithm( & attributes, alg );
4859 psa_set_key_type( & attributes, key_type );
4860
4861 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4862 &key ) );
4863
4864 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4865
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004866 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4867
Paul Elliottf1277632021-08-24 18:11:37 +01004868 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004869
Paul Elliottf1277632021-08-24 18:11:37 +01004870 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004871
Gilles Peskine7be11a72022-04-14 00:12:57 +02004872 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004873
Paul Elliottf1277632021-08-24 18:11:37 +01004874 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004875
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004876 status = psa_aead_encrypt_setup( &operation, key, alg );
4877
4878 /* If the operation is not supported, just skip and not fail in case the
4879 * encryption involves a common limitation of cryptography hardwares and
4880 * an alternative implementation. */
4881 if( status == PSA_ERROR_NOT_SUPPORTED )
4882 {
4883 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004884 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004885 }
4886
4887 PSA_ASSERT( status );
4888
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004889 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004890 nonce_length,
4891 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004892
Paul Elliott693bf312021-07-23 17:40:41 +01004893 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004894
Paul Elliottf1277632021-08-24 18:11:37 +01004895 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004896
Paul Elliott88ecbe12021-09-22 17:23:03 +01004897 if( expected_status == PSA_SUCCESS )
4898 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4899 alg ) );
4900
Gilles Peskine7be11a72022-04-14 00:12:57 +02004901 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004902
Paul Elliott693bf312021-07-23 17:40:41 +01004903 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004904 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004905 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004906 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4907 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004908
4909 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4910 additional_data->len ) );
4911
4912 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004913 output, output_size,
4914 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004915
Paul Elliottf1277632021-08-24 18:11:37 +01004916 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4917 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004918 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4919 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004920
4921exit:
4922 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004923 mbedtls_free( output );
4924 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004925 psa_aead_abort( &operation );
4926 PSA_DONE( );
4927}
4928/* END_CASE */
4929
4930/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004931void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4932 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004933 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004934 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004935 data_t *additional_data,
4936 data_t *input_data,
4937 int expected_status_arg )
4938{
4939
4940 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4941 psa_key_type_t key_type = key_type_arg;
4942 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004943 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004944 uint8_t *nonce_buffer = NULL;
4945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4946 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4947 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004948 unsigned char *output = NULL;
4949 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004950 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004951 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004952 size_t ciphertext_size = 0;
4953 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004954 size_t tag_length = 0;
4955 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004956 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004957 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004958
4959 PSA_ASSERT( psa_crypto_init( ) );
4960
4961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4962 psa_set_key_algorithm( &attributes, alg );
4963 psa_set_key_type( &attributes, key_type );
4964
4965 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4966 &key ) );
4967
4968 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4969
4970 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4971
Paul Elliott6f0e7202021-08-25 12:57:18 +01004972 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004973
Paul Elliott6f0e7202021-08-25 12:57:18 +01004974 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004975
Gilles Peskine7be11a72022-04-14 00:12:57 +02004976 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004977
Paul Elliott6f0e7202021-08-25 12:57:18 +01004978 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004979
Paul Elliott863864a2021-07-23 17:28:31 +01004980 status = psa_aead_encrypt_setup( &operation, key, alg );
4981
4982 /* If the operation is not supported, just skip and not fail in case the
4983 * encryption involves a common limitation of cryptography hardwares and
4984 * an alternative implementation. */
4985 if( status == PSA_ERROR_NOT_SUPPORTED )
4986 {
4987 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004988 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004989 }
4990
4991 PSA_ASSERT( status );
4992
Paul Elliott4023ffd2021-09-10 16:21:22 +01004993 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4994 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004995 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004996 /* Arbitrary size buffer, to test zero length valid buffer. */
4997 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004998 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004999 }
5000 else
5001 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005002 /* If length is zero, then this will return NULL. */
5003 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005004 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005005
Paul Elliott4023ffd2021-09-10 16:21:22 +01005006 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005007 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005008 for( index = 0; index < nonce_length - 1; ++index )
5009 {
5010 nonce_buffer[index] = 'a' + index;
5011 }
Paul Elliott66696b52021-08-16 18:42:41 +01005012 }
Paul Elliott863864a2021-07-23 17:28:31 +01005013 }
5014
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005015 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5016 {
5017 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5018 input_data->len ) );
5019 }
5020
Paul Elliott6f0e7202021-08-25 12:57:18 +01005021 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005022
Paul Elliott693bf312021-07-23 17:40:41 +01005023 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005024
5025 if( expected_status == PSA_SUCCESS )
5026 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005027 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5028 {
5029 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5030 input_data->len ) );
5031 }
5032 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5033 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005034
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005035 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5036 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5037 additional_data->len ),
5038 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005039
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005040 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005041 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005042 &ciphertext_length ),
5043 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005044
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005045 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005046 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005047 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5048 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005049 }
5050
5051exit:
5052 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005053 mbedtls_free( output );
5054 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005055 mbedtls_free( nonce_buffer );
5056 psa_aead_abort( &operation );
5057 PSA_DONE( );
5058}
5059/* END_CASE */
5060
5061/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005062void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5063 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005064 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005065 data_t *nonce,
5066 data_t *additional_data,
5067 data_t *input_data,
5068 int expected_status_arg )
5069{
5070
5071 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5072 psa_key_type_t key_type = key_type_arg;
5073 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005074 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5076 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5077 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005078 unsigned char *output = NULL;
5079 unsigned char *ciphertext = NULL;
5080 size_t output_size = output_size_arg;
5081 size_t ciphertext_size = 0;
5082 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005083 size_t tag_length = 0;
5084 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5085
5086 PSA_ASSERT( psa_crypto_init( ) );
5087
5088 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5089 psa_set_key_algorithm( &attributes, alg );
5090 psa_set_key_type( &attributes, key_type );
5091
5092 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5093 &key ) );
5094
5095 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5096
Paul Elliottc6d11d02021-09-01 12:04:23 +01005097 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005098
Paul Elliottc6d11d02021-09-01 12:04:23 +01005099 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005100
Paul Elliottc6d11d02021-09-01 12:04:23 +01005101 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005102
Paul Elliott43fbda62021-07-23 18:30:59 +01005103 status = psa_aead_encrypt_setup( &operation, key, alg );
5104
5105 /* If the operation is not supported, just skip and not fail in case the
5106 * encryption involves a common limitation of cryptography hardwares and
5107 * an alternative implementation. */
5108 if( status == PSA_ERROR_NOT_SUPPORTED )
5109 {
5110 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5111 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5112 }
5113
5114 PSA_ASSERT( status );
5115
Paul Elliott47b9a142021-10-07 15:04:57 +01005116 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5117 input_data->len ) );
5118
Paul Elliott43fbda62021-07-23 18:30:59 +01005119 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5120
5121 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5122 additional_data->len ) );
5123
5124 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005125 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005126
5127 TEST_EQUAL( status, expected_status );
5128
5129 if( expected_status == PSA_SUCCESS )
5130 {
5131 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005132 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5133 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005134 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5135 }
5136
5137exit:
5138 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005139 mbedtls_free( output );
5140 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005141 psa_aead_abort( &operation );
5142 PSA_DONE( );
5143}
5144/* END_CASE */
5145
Paul Elliott91b021e2021-07-23 18:52:31 +01005146/* BEGIN_CASE */
5147void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5148 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005149 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005150 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005151 data_t *nonce,
5152 data_t *additional_data,
5153 data_t *input_data,
5154 int expected_status_arg )
5155{
5156
5157 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5158 psa_key_type_t key_type = key_type_arg;
5159 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005160 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005161 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5162 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5163 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005164 unsigned char *ciphertext = NULL;
5165 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005166 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005167 size_t ciphertext_size = 0;
5168 size_t ciphertext_length = 0;
5169 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005170 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005171 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005172
5173 PSA_ASSERT( psa_crypto_init( ) );
5174
5175 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5176 psa_set_key_algorithm( &attributes, alg );
5177 psa_set_key_type( &attributes, key_type );
5178
5179 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5180 &key ) );
5181
5182 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5183
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005184 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005185
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005186 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005187
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005188 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005189
Paul Elliott719c1322021-09-13 18:27:22 +01005190 ASSERT_ALLOC( tag_buffer, tag_size );
5191
Paul Elliott91b021e2021-07-23 18:52:31 +01005192 status = psa_aead_encrypt_setup( &operation, key, alg );
5193
5194 /* If the operation is not supported, just skip and not fail in case the
5195 * encryption involves a common limitation of cryptography hardwares and
5196 * an alternative implementation. */
5197 if( status == PSA_ERROR_NOT_SUPPORTED )
5198 {
5199 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5200 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5201 }
5202
5203 PSA_ASSERT( status );
5204
5205 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5206
Paul Elliott76bda482021-10-07 17:07:23 +01005207 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5208 input_data->len ) );
5209
Paul Elliott91b021e2021-07-23 18:52:31 +01005210 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 Elliotte58cb1e2021-09-10 18:36:00 +01005214 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005215
5216 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005217 status = psa_aead_finish( &operation, finish_ciphertext,
5218 finish_ciphertext_size,
5219 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005220 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005221
5222 TEST_EQUAL( status, expected_status );
5223
5224exit:
5225 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005226 mbedtls_free( ciphertext );
5227 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005228 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005229 psa_aead_abort( &operation );
5230 PSA_DONE( );
5231}
5232/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005233
5234/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005235void aead_multipart_verify( int key_type_arg, data_t *key_data,
5236 int alg_arg,
5237 data_t *nonce,
5238 data_t *additional_data,
5239 data_t *input_data,
5240 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005241 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005242 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005243 int expected_status_arg )
5244{
5245 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5246 psa_key_type_t key_type = key_type_arg;
5247 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005248 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5250 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5251 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005252 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005253 unsigned char *plaintext = NULL;
5254 unsigned char *finish_plaintext = NULL;
5255 size_t plaintext_size = 0;
5256 size_t plaintext_length = 0;
5257 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005258 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005259 unsigned char *tag_buffer = NULL;
5260 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005261
5262 PSA_ASSERT( psa_crypto_init( ) );
5263
5264 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5265 psa_set_key_algorithm( &attributes, alg );
5266 psa_set_key_type( &attributes, key_type );
5267
5268 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5269 &key ) );
5270
5271 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5272
5273 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5274 input_data->len );
5275
5276 ASSERT_ALLOC( plaintext, plaintext_size );
5277
5278 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5279
5280 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5281
Paul Elliott9961a662021-09-17 19:19:02 +01005282 status = psa_aead_decrypt_setup( &operation, key, alg );
5283
5284 /* If the operation is not supported, just skip and not fail in case the
5285 * encryption involves a common limitation of cryptography hardwares and
5286 * an alternative implementation. */
5287 if( status == PSA_ERROR_NOT_SUPPORTED )
5288 {
5289 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5290 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5291 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005292 TEST_EQUAL( status, expected_setup_status );
5293
5294 if( status != PSA_SUCCESS )
5295 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005296
5297 PSA_ASSERT( status );
5298
5299 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5300
Paul Elliottfec6f372021-10-06 17:15:02 +01005301 status = psa_aead_set_lengths( &operation, additional_data->len,
5302 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005303 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005304
Paul Elliott9961a662021-09-17 19:19:02 +01005305 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5306 additional_data->len ) );
5307
5308 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5309 input_data->len,
5310 plaintext, plaintext_size,
5311 &plaintext_length ) );
5312
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005313 if( tag_usage == USE_GIVEN_TAG )
5314 {
5315 tag_buffer = tag->x;
5316 tag_size = tag->len;
5317 }
5318
Paul Elliott9961a662021-09-17 19:19:02 +01005319 status = psa_aead_verify( &operation, finish_plaintext,
5320 verify_plaintext_size,
5321 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005322 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005323
5324 TEST_EQUAL( status, expected_status );
5325
5326exit:
5327 psa_destroy_key( key );
5328 mbedtls_free( plaintext );
5329 mbedtls_free( finish_plaintext );
5330 psa_aead_abort( &operation );
5331 PSA_DONE( );
5332}
5333/* END_CASE */
5334
Paul Elliott9961a662021-09-17 19:19:02 +01005335/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005336void aead_multipart_setup( int key_type_arg, data_t *key_data,
5337 int alg_arg, int expected_status_arg )
5338{
5339 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5340 psa_key_type_t key_type = key_type_arg;
5341 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005342 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5344 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5345 psa_status_t expected_status = expected_status_arg;
5346
5347 PSA_ASSERT( psa_crypto_init( ) );
5348
5349 psa_set_key_usage_flags( &attributes,
5350 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5351 psa_set_key_algorithm( &attributes, alg );
5352 psa_set_key_type( &attributes, key_type );
5353
5354 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5355 &key ) );
5356
Paul Elliott5221ef62021-09-19 17:33:03 +01005357 status = psa_aead_encrypt_setup( &operation, key, alg );
5358
5359 TEST_EQUAL( status, expected_status );
5360
5361 psa_aead_abort( &operation );
5362
Paul Elliott5221ef62021-09-19 17:33:03 +01005363 status = psa_aead_decrypt_setup( &operation, key, alg );
5364
5365 TEST_EQUAL(status, expected_status );
5366
5367exit:
5368 psa_destroy_key( key );
5369 psa_aead_abort( &operation );
5370 PSA_DONE( );
5371}
5372/* END_CASE */
5373
5374/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005375void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5376 int alg_arg,
5377 data_t *nonce,
5378 data_t *additional_data,
5379 data_t *input_data )
5380{
5381 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5382 psa_key_type_t key_type = key_type_arg;
5383 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005384 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005385 unsigned char *output_data = NULL;
5386 unsigned char *final_data = NULL;
5387 size_t output_size = 0;
5388 size_t finish_output_size = 0;
5389 size_t output_length = 0;
5390 size_t key_bits = 0;
5391 size_t tag_length = 0;
5392 size_t tag_size = 0;
5393 size_t nonce_length = 0;
5394 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5395 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5396 size_t output_part_length = 0;
5397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5398
5399 PSA_ASSERT( psa_crypto_init( ) );
5400
5401 psa_set_key_usage_flags( & attributes,
5402 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5403 psa_set_key_algorithm( & attributes, alg );
5404 psa_set_key_type( & attributes, key_type );
5405
5406 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5407 &key ) );
5408
5409 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5410 key_bits = psa_get_key_bits( &attributes );
5411
5412 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5413
Gilles Peskine7be11a72022-04-14 00:12:57 +02005414 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005415
5416 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5417
5418 ASSERT_ALLOC( output_data, output_size );
5419
5420 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5421
Gilles Peskine7be11a72022-04-14 00:12:57 +02005422 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005423
5424 ASSERT_ALLOC( final_data, finish_output_size );
5425
5426 /* Test all operations error without calling setup first. */
5427
Paul Elliottc23a9a02021-06-21 18:32:46 +01005428 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5429 PSA_ERROR_BAD_STATE );
5430
5431 psa_aead_abort( &operation );
5432
Paul Elliottc23a9a02021-06-21 18:32:46 +01005433 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5434 PSA_AEAD_NONCE_MAX_SIZE,
5435 &nonce_length ),
5436 PSA_ERROR_BAD_STATE );
5437
5438 psa_aead_abort( &operation );
5439
Paul Elliott481be342021-07-16 17:38:47 +01005440 /* ------------------------------------------------------- */
5441
Paul Elliottc23a9a02021-06-21 18:32:46 +01005442 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5443 input_data->len ),
5444 PSA_ERROR_BAD_STATE );
5445
5446 psa_aead_abort( &operation );
5447
Paul Elliott481be342021-07-16 17:38:47 +01005448 /* ------------------------------------------------------- */
5449
Paul Elliottc23a9a02021-06-21 18:32:46 +01005450 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5451 additional_data->len ),
5452 PSA_ERROR_BAD_STATE );
5453
5454 psa_aead_abort( &operation );
5455
Paul Elliott481be342021-07-16 17:38:47 +01005456 /* ------------------------------------------------------- */
5457
Paul Elliottc23a9a02021-06-21 18:32:46 +01005458 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5459 input_data->len, output_data,
5460 output_size, &output_length ),
5461 PSA_ERROR_BAD_STATE );
5462
5463 psa_aead_abort( &operation );
5464
Paul Elliott481be342021-07-16 17:38:47 +01005465 /* ------------------------------------------------------- */
5466
Paul Elliottc23a9a02021-06-21 18:32:46 +01005467 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5468 finish_output_size,
5469 &output_part_length,
5470 tag_buffer, tag_length,
5471 &tag_size ),
5472 PSA_ERROR_BAD_STATE );
5473
5474 psa_aead_abort( &operation );
5475
Paul Elliott481be342021-07-16 17:38:47 +01005476 /* ------------------------------------------------------- */
5477
Paul Elliottc23a9a02021-06-21 18:32:46 +01005478 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5479 finish_output_size,
5480 &output_part_length,
5481 tag_buffer,
5482 tag_length ),
5483 PSA_ERROR_BAD_STATE );
5484
5485 psa_aead_abort( &operation );
5486
5487 /* Test for double setups. */
5488
Paul Elliottc23a9a02021-06-21 18:32:46 +01005489 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5490
5491 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5492 PSA_ERROR_BAD_STATE );
5493
5494 psa_aead_abort( &operation );
5495
Paul Elliott481be342021-07-16 17:38:47 +01005496 /* ------------------------------------------------------- */
5497
Paul Elliottc23a9a02021-06-21 18:32:46 +01005498 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5499
5500 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5501 PSA_ERROR_BAD_STATE );
5502
5503 psa_aead_abort( &operation );
5504
Paul Elliott374a2be2021-07-16 17:53:40 +01005505 /* ------------------------------------------------------- */
5506
Paul Elliott374a2be2021-07-16 17:53:40 +01005507 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5508
5509 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5510 PSA_ERROR_BAD_STATE );
5511
5512 psa_aead_abort( &operation );
5513
5514 /* ------------------------------------------------------- */
5515
Paul Elliott374a2be2021-07-16 17:53:40 +01005516 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5517
5518 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5519 PSA_ERROR_BAD_STATE );
5520
5521 psa_aead_abort( &operation );
5522
Paul Elliottc23a9a02021-06-21 18:32:46 +01005523 /* Test for not setting a nonce. */
5524
Paul Elliottc23a9a02021-06-21 18:32:46 +01005525 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5526
5527 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5528 additional_data->len ),
5529 PSA_ERROR_BAD_STATE );
5530
5531 psa_aead_abort( &operation );
5532
Paul Elliott7f628422021-09-01 12:08:29 +01005533 /* ------------------------------------------------------- */
5534
Paul Elliott7f628422021-09-01 12:08:29 +01005535 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5536
5537 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5538 input_data->len, output_data,
5539 output_size, &output_length ),
5540 PSA_ERROR_BAD_STATE );
5541
5542 psa_aead_abort( &operation );
5543
Paul Elliottbdc2c682021-09-21 18:37:10 +01005544 /* ------------------------------------------------------- */
5545
Paul Elliottbdc2c682021-09-21 18:37:10 +01005546 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5547
5548 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5549 finish_output_size,
5550 &output_part_length,
5551 tag_buffer, tag_length,
5552 &tag_size ),
5553 PSA_ERROR_BAD_STATE );
5554
5555 psa_aead_abort( &operation );
5556
5557 /* ------------------------------------------------------- */
5558
Paul Elliottbdc2c682021-09-21 18:37:10 +01005559 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5560
5561 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5562 finish_output_size,
5563 &output_part_length,
5564 tag_buffer,
5565 tag_length ),
5566 PSA_ERROR_BAD_STATE );
5567
5568 psa_aead_abort( &operation );
5569
Paul Elliottc23a9a02021-06-21 18:32:46 +01005570 /* Test for double setting nonce. */
5571
Paul Elliottc23a9a02021-06-21 18:32:46 +01005572 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5573
5574 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5575
5576 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5577 PSA_ERROR_BAD_STATE );
5578
5579 psa_aead_abort( &operation );
5580
Paul Elliott374a2be2021-07-16 17:53:40 +01005581 /* Test for double generating nonce. */
5582
Paul Elliott374a2be2021-07-16 17:53:40 +01005583 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5584
5585 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5586 PSA_AEAD_NONCE_MAX_SIZE,
5587 &nonce_length ) );
5588
5589 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5590 PSA_AEAD_NONCE_MAX_SIZE,
5591 &nonce_length ),
5592 PSA_ERROR_BAD_STATE );
5593
5594
5595 psa_aead_abort( &operation );
5596
5597 /* Test for generate nonce then set and vice versa */
5598
Paul Elliott374a2be2021-07-16 17:53:40 +01005599 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5600
5601 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5602 PSA_AEAD_NONCE_MAX_SIZE,
5603 &nonce_length ) );
5604
5605 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5606 PSA_ERROR_BAD_STATE );
5607
5608 psa_aead_abort( &operation );
5609
Andrzej Kurekad837522021-12-15 15:28:49 +01005610 /* Test for generating nonce after calling set lengths */
5611
5612 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5613
5614 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5615 input_data->len ) );
5616
5617 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5618 PSA_AEAD_NONCE_MAX_SIZE,
5619 &nonce_length ) );
5620
5621 psa_aead_abort( &operation );
5622
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005623 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005624
5625 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5626
5627 if( operation.alg == PSA_ALG_CCM )
5628 {
5629 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5630 input_data->len ),
5631 PSA_ERROR_INVALID_ARGUMENT );
5632 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5633 PSA_AEAD_NONCE_MAX_SIZE,
5634 &nonce_length ),
5635 PSA_ERROR_BAD_STATE );
5636 }
5637 else
5638 {
5639 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5640 input_data->len ) );
5641 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5642 PSA_AEAD_NONCE_MAX_SIZE,
5643 &nonce_length ) );
5644 }
5645
5646 psa_aead_abort( &operation );
5647
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005648 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005649#if SIZE_MAX > UINT32_MAX
5650 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5651
5652 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5653 {
5654 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5655 input_data->len ),
5656 PSA_ERROR_INVALID_ARGUMENT );
5657 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5658 PSA_AEAD_NONCE_MAX_SIZE,
5659 &nonce_length ),
5660 PSA_ERROR_BAD_STATE );
5661 }
5662 else
5663 {
5664 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5665 input_data->len ) );
5666 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5667 PSA_AEAD_NONCE_MAX_SIZE,
5668 &nonce_length ) );
5669 }
5670
5671 psa_aead_abort( &operation );
5672#endif
5673
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005674 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005675
5676 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5677
5678 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5679 PSA_AEAD_NONCE_MAX_SIZE,
5680 &nonce_length ) );
5681
5682 if( operation.alg == PSA_ALG_CCM )
5683 {
5684 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5685 input_data->len ),
5686 PSA_ERROR_INVALID_ARGUMENT );
5687 }
5688 else
5689 {
5690 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5691 input_data->len ) );
5692 }
5693
5694 psa_aead_abort( &operation );
5695
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005696 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005697 /* Test for setting nonce after calling set lengths */
5698
5699 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5700
5701 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5702 input_data->len ) );
5703
5704 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5705
5706 psa_aead_abort( &operation );
5707
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005708 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005709
5710 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5711
5712 if( operation.alg == PSA_ALG_CCM )
5713 {
5714 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5715 input_data->len ),
5716 PSA_ERROR_INVALID_ARGUMENT );
5717 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5718 PSA_ERROR_BAD_STATE );
5719 }
5720 else
5721 {
5722 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5723 input_data->len ) );
5724 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5725 }
5726
5727 psa_aead_abort( &operation );
5728
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005729 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005730#if SIZE_MAX > UINT32_MAX
5731 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5732
5733 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5734 {
5735 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5736 input_data->len ),
5737 PSA_ERROR_INVALID_ARGUMENT );
5738 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5739 PSA_ERROR_BAD_STATE );
5740 }
5741 else
5742 {
5743 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5744 input_data->len ) );
5745 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5746 }
5747
5748 psa_aead_abort( &operation );
5749#endif
5750
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005751 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005752
5753 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5754
5755 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5756
5757 if( operation.alg == PSA_ALG_CCM )
5758 {
5759 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5760 input_data->len ),
5761 PSA_ERROR_INVALID_ARGUMENT );
5762 }
5763 else
5764 {
5765 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5766 input_data->len ) );
5767 }
5768
5769 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005770
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005771 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005772#if SIZE_MAX > UINT32_MAX
5773 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5774
5775 if( operation.alg == PSA_ALG_GCM )
5776 {
5777 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5778 SIZE_MAX ),
5779 PSA_ERROR_INVALID_ARGUMENT );
5780 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5781 PSA_ERROR_BAD_STATE );
5782 }
5783 else if ( operation.alg != PSA_ALG_CCM )
5784 {
5785 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5786 SIZE_MAX ) );
5787 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5788 }
5789
5790 psa_aead_abort( &operation );
5791
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005792 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005793 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5794
5795 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5796
5797 if( operation.alg == PSA_ALG_GCM )
5798 {
5799 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5800 SIZE_MAX ),
5801 PSA_ERROR_INVALID_ARGUMENT );
5802 }
5803 else if ( operation.alg != PSA_ALG_CCM )
5804 {
5805 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5806 SIZE_MAX ) );
5807 }
5808
5809 psa_aead_abort( &operation );
5810#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005811
5812 /* ------------------------------------------------------- */
5813
Paul Elliott374a2be2021-07-16 17:53:40 +01005814 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5815
5816 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5817
5818 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5819 PSA_AEAD_NONCE_MAX_SIZE,
5820 &nonce_length ),
5821 PSA_ERROR_BAD_STATE );
5822
5823 psa_aead_abort( &operation );
5824
Paul Elliott7220cae2021-06-22 17:25:57 +01005825 /* Test for generating nonce in decrypt setup. */
5826
Paul Elliott7220cae2021-06-22 17:25:57 +01005827 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5828
5829 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5830 PSA_AEAD_NONCE_MAX_SIZE,
5831 &nonce_length ),
5832 PSA_ERROR_BAD_STATE );
5833
5834 psa_aead_abort( &operation );
5835
Paul Elliottc23a9a02021-06-21 18:32:46 +01005836 /* Test for setting lengths twice. */
5837
Paul Elliottc23a9a02021-06-21 18:32:46 +01005838 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5839
5840 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5841
5842 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5843 input_data->len ) );
5844
5845 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5846 input_data->len ),
5847 PSA_ERROR_BAD_STATE );
5848
5849 psa_aead_abort( &operation );
5850
Andrzej Kurekad837522021-12-15 15:28:49 +01005851 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005852
Paul Elliottc23a9a02021-06-21 18:32:46 +01005853 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5854
5855 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5856
Andrzej Kurekad837522021-12-15 15:28:49 +01005857 if( operation.alg == PSA_ALG_CCM )
5858 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005859
Andrzej Kurekad837522021-12-15 15:28:49 +01005860 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5861 additional_data->len ),
5862 PSA_ERROR_BAD_STATE );
5863 }
5864 else
5865 {
5866 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5867 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005868
Andrzej Kurekad837522021-12-15 15:28:49 +01005869 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5870 input_data->len ),
5871 PSA_ERROR_BAD_STATE );
5872 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005873 psa_aead_abort( &operation );
5874
5875 /* ------------------------------------------------------- */
5876
Paul Elliottf94bd992021-09-19 18:15:59 +01005877 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5878
5879 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5880
Andrzej Kurekad837522021-12-15 15:28:49 +01005881 if( operation.alg == PSA_ALG_CCM )
5882 {
5883 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5884 input_data->len, output_data,
5885 output_size, &output_length ),
5886 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005887
Andrzej Kurekad837522021-12-15 15:28:49 +01005888 }
5889 else
5890 {
5891 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5892 input_data->len, output_data,
5893 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005894
Andrzej Kurekad837522021-12-15 15:28:49 +01005895 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5896 input_data->len ),
5897 PSA_ERROR_BAD_STATE );
5898 }
5899 psa_aead_abort( &operation );
5900
5901 /* ------------------------------------------------------- */
5902
5903 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5904
5905 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5906
5907 if( operation.alg == PSA_ALG_CCM )
5908 {
5909 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5910 finish_output_size,
5911 &output_part_length,
5912 tag_buffer, tag_length,
5913 &tag_size ) );
5914 }
5915 else
5916 {
5917 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5918 finish_output_size,
5919 &output_part_length,
5920 tag_buffer, tag_length,
5921 &tag_size ) );
5922
5923 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5924 input_data->len ),
5925 PSA_ERROR_BAD_STATE );
5926 }
5927 psa_aead_abort( &operation );
5928
5929 /* Test for setting lengths after generating nonce + already starting data. */
5930
5931 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5932
5933 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5934 PSA_AEAD_NONCE_MAX_SIZE,
5935 &nonce_length ) );
5936 if( operation.alg == PSA_ALG_CCM )
5937 {
5938
5939 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5940 additional_data->len ),
5941 PSA_ERROR_BAD_STATE );
5942 }
5943 else
5944 {
5945 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5946 additional_data->len ) );
5947
5948 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5949 input_data->len ),
5950 PSA_ERROR_BAD_STATE );
5951 }
5952 psa_aead_abort( &operation );
5953
5954 /* ------------------------------------------------------- */
5955
5956 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5957
5958 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5959 PSA_AEAD_NONCE_MAX_SIZE,
5960 &nonce_length ) );
5961 if( operation.alg == PSA_ALG_CCM )
5962 {
5963 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5964 input_data->len, output_data,
5965 output_size, &output_length ),
5966 PSA_ERROR_BAD_STATE );
5967
5968 }
5969 else
5970 {
5971 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5972 input_data->len, output_data,
5973 output_size, &output_length ) );
5974
5975 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5976 input_data->len ),
5977 PSA_ERROR_BAD_STATE );
5978 }
5979 psa_aead_abort( &operation );
5980
5981 /* ------------------------------------------------------- */
5982
5983 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5984
5985 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5986 PSA_AEAD_NONCE_MAX_SIZE,
5987 &nonce_length ) );
5988 if( operation.alg == PSA_ALG_CCM )
5989 {
5990 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5991 finish_output_size,
5992 &output_part_length,
5993 tag_buffer, tag_length,
5994 &tag_size ) );
5995 }
5996 else
5997 {
5998 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5999 finish_output_size,
6000 &output_part_length,
6001 tag_buffer, tag_length,
6002 &tag_size ) );
6003
6004 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6005 input_data->len ),
6006 PSA_ERROR_BAD_STATE );
6007 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006008 psa_aead_abort( &operation );
6009
Paul Elliott243080c2021-07-21 19:01:17 +01006010 /* Test for not sending any additional data or data after setting non zero
6011 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006012
Paul Elliottc23a9a02021-06-21 18:32:46 +01006013 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6014
6015 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6016
6017 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6018 input_data->len ) );
6019
6020 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6021 finish_output_size,
6022 &output_part_length,
6023 tag_buffer, tag_length,
6024 &tag_size ),
6025 PSA_ERROR_INVALID_ARGUMENT );
6026
6027 psa_aead_abort( &operation );
6028
Paul Elliott243080c2021-07-21 19:01:17 +01006029 /* Test for not sending any additional data or data after setting non-zero
6030 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006031
Paul Elliottc23a9a02021-06-21 18:32:46 +01006032 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6033
6034 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6035
6036 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6037 input_data->len ) );
6038
6039 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6040 finish_output_size,
6041 &output_part_length,
6042 tag_buffer,
6043 tag_length ),
6044 PSA_ERROR_INVALID_ARGUMENT );
6045
6046 psa_aead_abort( &operation );
6047
Paul Elliott243080c2021-07-21 19:01:17 +01006048 /* Test for not sending any additional data after setting a non-zero length
6049 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006050
Paul Elliottc23a9a02021-06-21 18:32:46 +01006051 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6052
6053 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6054
6055 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6056 input_data->len ) );
6057
6058 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6059 input_data->len, output_data,
6060 output_size, &output_length ),
6061 PSA_ERROR_INVALID_ARGUMENT );
6062
6063 psa_aead_abort( &operation );
6064
Paul Elliottf94bd992021-09-19 18:15:59 +01006065 /* Test for not sending any data after setting a non-zero length for it.*/
6066
Paul Elliottf94bd992021-09-19 18:15:59 +01006067 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6068
6069 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6070
6071 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6072 input_data->len ) );
6073
6074 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6075 additional_data->len ) );
6076
6077 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6078 finish_output_size,
6079 &output_part_length,
6080 tag_buffer, tag_length,
6081 &tag_size ),
6082 PSA_ERROR_INVALID_ARGUMENT );
6083
6084 psa_aead_abort( &operation );
6085
Paul Elliottb0450fe2021-09-01 15:06:26 +01006086 /* Test for sending too much additional data after setting lengths. */
6087
Paul Elliottb0450fe2021-09-01 15:06:26 +01006088 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6089
6090 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6091
6092 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6093
6094
6095 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6096 additional_data->len ),
6097 PSA_ERROR_INVALID_ARGUMENT );
6098
6099 psa_aead_abort( &operation );
6100
Paul Elliotta2a09b02021-09-22 14:56:40 +01006101 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006102
6103 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6104
6105 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6106
6107 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6108 input_data->len ) );
6109
6110 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6111 additional_data->len ) );
6112
6113 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6114 1 ),
6115 PSA_ERROR_INVALID_ARGUMENT );
6116
6117 psa_aead_abort( &operation );
6118
Paul Elliottb0450fe2021-09-01 15:06:26 +01006119 /* Test for sending too much data after setting lengths. */
6120
Paul Elliottb0450fe2021-09-01 15:06:26 +01006121 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6122
6123 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6124
6125 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6126
6127 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6128 input_data->len, output_data,
6129 output_size, &output_length ),
6130 PSA_ERROR_INVALID_ARGUMENT );
6131
6132 psa_aead_abort( &operation );
6133
Paul Elliotta2a09b02021-09-22 14:56:40 +01006134 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006135
6136 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6137
6138 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6139
6140 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6141 input_data->len ) );
6142
6143 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6144 additional_data->len ) );
6145
6146 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6147 input_data->len, output_data,
6148 output_size, &output_length ) );
6149
6150 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6151 1, output_data,
6152 output_size, &output_length ),
6153 PSA_ERROR_INVALID_ARGUMENT );
6154
6155 psa_aead_abort( &operation );
6156
Paul Elliottc23a9a02021-06-21 18:32:46 +01006157 /* Test sending additional data after data. */
6158
Paul Elliottc23a9a02021-06-21 18:32:46 +01006159 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6160
6161 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6162
Andrzej Kurekad837522021-12-15 15:28:49 +01006163 if( operation.alg != PSA_ALG_CCM )
6164 {
6165 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6166 input_data->len, output_data,
6167 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006168
Andrzej Kurekad837522021-12-15 15:28:49 +01006169 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6170 additional_data->len ),
6171 PSA_ERROR_BAD_STATE );
6172 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006173 psa_aead_abort( &operation );
6174
Paul Elliott534d0b42021-06-22 19:15:20 +01006175 /* Test calling finish on decryption. */
6176
Paul Elliott534d0b42021-06-22 19:15:20 +01006177 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6178
6179 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6180
6181 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6182 finish_output_size,
6183 &output_part_length,
6184 tag_buffer, tag_length,
6185 &tag_size ),
6186 PSA_ERROR_BAD_STATE );
6187
6188 psa_aead_abort( &operation );
6189
6190 /* Test calling verify on encryption. */
6191
Paul Elliott534d0b42021-06-22 19:15:20 +01006192 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6193
6194 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6195
6196 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6197 finish_output_size,
6198 &output_part_length,
6199 tag_buffer,
6200 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006201 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006202
6203 psa_aead_abort( &operation );
6204
6205
Paul Elliottc23a9a02021-06-21 18:32:46 +01006206exit:
6207 psa_destroy_key( key );
6208 psa_aead_abort( &operation );
6209 mbedtls_free( output_data );
6210 mbedtls_free( final_data );
6211 PSA_DONE( );
6212}
6213/* END_CASE */
6214
6215/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006216void signature_size( int type_arg,
6217 int bits,
6218 int alg_arg,
6219 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006220{
6221 psa_key_type_t type = type_arg;
6222 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006223 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006224
Gilles Peskinefe11b722018-12-18 00:24:04 +01006225 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006226
Gilles Peskinee59236f2018-01-27 23:32:46 +01006227exit:
6228 ;
6229}
6230/* END_CASE */
6231
6232/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006233void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6234 int alg_arg, data_t *input_data,
6235 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006236{
Ronald Cron5425a212020-08-04 14:58:35 +02006237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006238 psa_key_type_t key_type = key_type_arg;
6239 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006240 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006241 unsigned char *signature = NULL;
6242 size_t signature_size;
6243 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006245
Gilles Peskine8817f612018-12-18 00:18:46 +01006246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006247
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006249 psa_set_key_algorithm( &attributes, alg );
6250 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006251
Gilles Peskine049c7532019-05-15 20:22:09 +02006252 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006253 &key ) );
6254 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006255 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006256
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006257 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006258 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006259 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006260 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006261 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006262 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006263 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006264
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006265 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006266 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006267 input_data->x, input_data->len,
6268 signature, signature_size,
6269 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006270 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006271 ASSERT_COMPARE( output_data->x, output_data->len,
6272 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006273
6274exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006275 /*
6276 * Key attributes may have been returned by psa_get_key_attributes()
6277 * thus reset them as required.
6278 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006279 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006280
Ronald Cron5425a212020-08-04 14:58:35 +02006281 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006282 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006283 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006284}
6285/* END_CASE */
6286
6287/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006288void sign_hash_fail( int key_type_arg, data_t *key_data,
6289 int alg_arg, data_t *input_data,
6290 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006291{
Ronald Cron5425a212020-08-04 14:58:35 +02006292 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006293 psa_key_type_t key_type = key_type_arg;
6294 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006295 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006296 psa_status_t actual_status;
6297 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006298 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006299 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006301
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006302 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006303
Gilles Peskine8817f612018-12-18 00:18:46 +01006304 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006305
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006306 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006307 psa_set_key_algorithm( &attributes, alg );
6308 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006309
Gilles Peskine049c7532019-05-15 20:22:09 +02006310 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006311 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006312
Ronald Cron5425a212020-08-04 14:58:35 +02006313 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006314 input_data->x, input_data->len,
6315 signature, signature_size,
6316 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006317 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006318 /* The value of *signature_length is unspecified on error, but
6319 * whatever it is, it should be less than signature_size, so that
6320 * if the caller tries to read *signature_length bytes without
6321 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006322 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006323
6324exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006325 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006326 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006327 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006328 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006329}
6330/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006331
6332/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006333void sign_verify_hash( int key_type_arg, data_t *key_data,
6334 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006335{
Ronald Cron5425a212020-08-04 14:58:35 +02006336 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006337 psa_key_type_t key_type = key_type_arg;
6338 psa_algorithm_t alg = alg_arg;
6339 size_t key_bits;
6340 unsigned char *signature = NULL;
6341 size_t signature_size;
6342 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006344
Gilles Peskine8817f612018-12-18 00:18:46 +01006345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006346
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006348 psa_set_key_algorithm( &attributes, alg );
6349 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006350
Gilles Peskine049c7532019-05-15 20:22:09 +02006351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006352 &key ) );
6353 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006354 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006355
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006356 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006357 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006358 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006359 key_bits, alg );
6360 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006361 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006362 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006363
6364 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006365 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006366 input_data->x, input_data->len,
6367 signature, signature_size,
6368 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006369 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006370 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006371 TEST_ASSERT( signature_length > 0 );
6372
6373 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006374 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006375 input_data->x, input_data->len,
6376 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006377
6378 if( input_data->len != 0 )
6379 {
6380 /* Flip a bit in the input and verify that the signature is now
6381 * detected as invalid. Flip a bit at the beginning, not at the end,
6382 * because ECDSA may ignore the last few bits of the input. */
6383 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006384 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006385 input_data->x, input_data->len,
6386 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006387 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006388 }
6389
6390exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006391 /*
6392 * Key attributes may have been returned by psa_get_key_attributes()
6393 * thus reset them as required.
6394 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006395 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006396
Ronald Cron5425a212020-08-04 14:58:35 +02006397 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006398 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006399 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006400}
6401/* END_CASE */
6402
6403/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006404void verify_hash( int key_type_arg, data_t *key_data,
6405 int alg_arg, data_t *hash_data,
6406 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006407{
Ronald Cron5425a212020-08-04 14:58:35 +02006408 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006409 psa_key_type_t key_type = key_type_arg;
6410 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006412
Gilles Peskine7be11a72022-04-14 00:12:57 +02006413 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006414
Gilles Peskine8817f612018-12-18 00:18:46 +01006415 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006416
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006418 psa_set_key_algorithm( &attributes, alg );
6419 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006420
Gilles Peskine049c7532019-05-15 20:22:09 +02006421 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006422 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006423
Ronald Cron5425a212020-08-04 14:58:35 +02006424 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006425 hash_data->x, hash_data->len,
6426 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006427
itayzafrir5c753392018-05-08 11:18:38 +03006428exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006429 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006430 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006431 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006432}
6433/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006434
6435/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006436void verify_hash_fail( int key_type_arg, data_t *key_data,
6437 int alg_arg, data_t *hash_data,
6438 data_t *signature_data,
6439 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006440{
Ronald Cron5425a212020-08-04 14:58:35 +02006441 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006442 psa_key_type_t key_type = key_type_arg;
6443 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006444 psa_status_t actual_status;
6445 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006446 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006447
Gilles Peskine8817f612018-12-18 00:18:46 +01006448 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006449
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006450 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006451 psa_set_key_algorithm( &attributes, alg );
6452 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006453
Gilles Peskine049c7532019-05-15 20:22:09 +02006454 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006455 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006456
Ronald Cron5425a212020-08-04 14:58:35 +02006457 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006458 hash_data->x, hash_data->len,
6459 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006460 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006461
6462exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006463 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006464 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006465 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006466}
6467/* END_CASE */
6468
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006469/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006470void sign_message_deterministic( int key_type_arg,
6471 data_t *key_data,
6472 int alg_arg,
6473 data_t *input_data,
6474 data_t *output_data )
6475{
6476 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6477 psa_key_type_t key_type = key_type_arg;
6478 psa_algorithm_t alg = alg_arg;
6479 size_t key_bits;
6480 unsigned char *signature = NULL;
6481 size_t signature_size;
6482 size_t signature_length = 0xdeadbeef;
6483 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6484
6485 PSA_ASSERT( psa_crypto_init( ) );
6486
6487 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6488 psa_set_key_algorithm( &attributes, alg );
6489 psa_set_key_type( &attributes, key_type );
6490
6491 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6492 &key ) );
6493 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6494 key_bits = psa_get_key_bits( &attributes );
6495
6496 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6497 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006498 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006499 ASSERT_ALLOC( signature, signature_size );
6500
6501 PSA_ASSERT( psa_sign_message( key, alg,
6502 input_data->x, input_data->len,
6503 signature, signature_size,
6504 &signature_length ) );
6505
6506 ASSERT_COMPARE( output_data->x, output_data->len,
6507 signature, signature_length );
6508
6509exit:
6510 psa_reset_key_attributes( &attributes );
6511
6512 psa_destroy_key( key );
6513 mbedtls_free( signature );
6514 PSA_DONE( );
6515
6516}
6517/* END_CASE */
6518
6519/* BEGIN_CASE */
6520void sign_message_fail( int key_type_arg,
6521 data_t *key_data,
6522 int alg_arg,
6523 data_t *input_data,
6524 int signature_size_arg,
6525 int expected_status_arg )
6526{
6527 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6528 psa_key_type_t key_type = key_type_arg;
6529 psa_algorithm_t alg = alg_arg;
6530 size_t signature_size = signature_size_arg;
6531 psa_status_t actual_status;
6532 psa_status_t expected_status = expected_status_arg;
6533 unsigned char *signature = NULL;
6534 size_t signature_length = 0xdeadbeef;
6535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6536
6537 ASSERT_ALLOC( signature, signature_size );
6538
6539 PSA_ASSERT( psa_crypto_init( ) );
6540
6541 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6542 psa_set_key_algorithm( &attributes, alg );
6543 psa_set_key_type( &attributes, key_type );
6544
6545 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6546 &key ) );
6547
6548 actual_status = psa_sign_message( key, alg,
6549 input_data->x, input_data->len,
6550 signature, signature_size,
6551 &signature_length );
6552 TEST_EQUAL( actual_status, expected_status );
6553 /* The value of *signature_length is unspecified on error, but
6554 * whatever it is, it should be less than signature_size, so that
6555 * if the caller tries to read *signature_length bytes without
6556 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006557 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006558
6559exit:
6560 psa_reset_key_attributes( &attributes );
6561 psa_destroy_key( key );
6562 mbedtls_free( signature );
6563 PSA_DONE( );
6564}
6565/* END_CASE */
6566
6567/* BEGIN_CASE */
6568void sign_verify_message( int key_type_arg,
6569 data_t *key_data,
6570 int alg_arg,
6571 data_t *input_data )
6572{
6573 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6574 psa_key_type_t key_type = key_type_arg;
6575 psa_algorithm_t alg = alg_arg;
6576 size_t key_bits;
6577 unsigned char *signature = NULL;
6578 size_t signature_size;
6579 size_t signature_length = 0xdeadbeef;
6580 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6581
6582 PSA_ASSERT( psa_crypto_init( ) );
6583
6584 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6585 PSA_KEY_USAGE_VERIFY_MESSAGE );
6586 psa_set_key_algorithm( &attributes, alg );
6587 psa_set_key_type( &attributes, key_type );
6588
6589 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6590 &key ) );
6591 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6592 key_bits = psa_get_key_bits( &attributes );
6593
6594 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6595 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006596 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006597 ASSERT_ALLOC( signature, signature_size );
6598
6599 PSA_ASSERT( psa_sign_message( key, alg,
6600 input_data->x, input_data->len,
6601 signature, signature_size,
6602 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006603 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006604 TEST_ASSERT( signature_length > 0 );
6605
6606 PSA_ASSERT( psa_verify_message( key, alg,
6607 input_data->x, input_data->len,
6608 signature, signature_length ) );
6609
6610 if( input_data->len != 0 )
6611 {
6612 /* Flip a bit in the input and verify that the signature is now
6613 * detected as invalid. Flip a bit at the beginning, not at the end,
6614 * because ECDSA may ignore the last few bits of the input. */
6615 input_data->x[0] ^= 1;
6616 TEST_EQUAL( psa_verify_message( key, alg,
6617 input_data->x, input_data->len,
6618 signature, signature_length ),
6619 PSA_ERROR_INVALID_SIGNATURE );
6620 }
6621
6622exit:
6623 psa_reset_key_attributes( &attributes );
6624
6625 psa_destroy_key( key );
6626 mbedtls_free( signature );
6627 PSA_DONE( );
6628}
6629/* END_CASE */
6630
6631/* BEGIN_CASE */
6632void verify_message( int key_type_arg,
6633 data_t *key_data,
6634 int alg_arg,
6635 data_t *input_data,
6636 data_t *signature_data )
6637{
6638 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6639 psa_key_type_t key_type = key_type_arg;
6640 psa_algorithm_t alg = alg_arg;
6641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6642
Gilles Peskine7be11a72022-04-14 00:12:57 +02006643 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006644
6645 PSA_ASSERT( psa_crypto_init( ) );
6646
6647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6648 psa_set_key_algorithm( &attributes, alg );
6649 psa_set_key_type( &attributes, key_type );
6650
6651 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6652 &key ) );
6653
6654 PSA_ASSERT( psa_verify_message( key, alg,
6655 input_data->x, input_data->len,
6656 signature_data->x, signature_data->len ) );
6657
6658exit:
6659 psa_reset_key_attributes( &attributes );
6660 psa_destroy_key( key );
6661 PSA_DONE( );
6662}
6663/* END_CASE */
6664
6665/* BEGIN_CASE */
6666void verify_message_fail( int key_type_arg,
6667 data_t *key_data,
6668 int alg_arg,
6669 data_t *hash_data,
6670 data_t *signature_data,
6671 int expected_status_arg )
6672{
6673 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6674 psa_key_type_t key_type = key_type_arg;
6675 psa_algorithm_t alg = alg_arg;
6676 psa_status_t actual_status;
6677 psa_status_t expected_status = expected_status_arg;
6678 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6679
6680 PSA_ASSERT( psa_crypto_init( ) );
6681
6682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6683 psa_set_key_algorithm( &attributes, alg );
6684 psa_set_key_type( &attributes, key_type );
6685
6686 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6687 &key ) );
6688
6689 actual_status = psa_verify_message( key, alg,
6690 hash_data->x, hash_data->len,
6691 signature_data->x,
6692 signature_data->len );
6693 TEST_EQUAL( actual_status, expected_status );
6694
6695exit:
6696 psa_reset_key_attributes( &attributes );
6697 psa_destroy_key( key );
6698 PSA_DONE( );
6699}
6700/* END_CASE */
6701
6702/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006703void asymmetric_encrypt( int key_type_arg,
6704 data_t *key_data,
6705 int alg_arg,
6706 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006707 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006708 int expected_output_length_arg,
6709 int expected_status_arg )
6710{
Ronald Cron5425a212020-08-04 14:58:35 +02006711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006712 psa_key_type_t key_type = key_type_arg;
6713 psa_algorithm_t alg = alg_arg;
6714 size_t expected_output_length = expected_output_length_arg;
6715 size_t key_bits;
6716 unsigned char *output = NULL;
6717 size_t output_size;
6718 size_t output_length = ~0;
6719 psa_status_t actual_status;
6720 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006722
Gilles Peskine8817f612018-12-18 00:18:46 +01006723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006724
Gilles Peskine656896e2018-06-29 19:12:28 +02006725 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006726 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6727 psa_set_key_algorithm( &attributes, alg );
6728 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006729 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006730 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006731
6732 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006733 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006734 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006735
Gilles Peskine656896e2018-06-29 19:12:28 +02006736 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006737 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006738 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006739
6740 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006741 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006742 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006743 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006744 output, output_size,
6745 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006746 TEST_EQUAL( actual_status, expected_status );
6747 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006748
Gilles Peskine68428122018-06-30 18:42:41 +02006749 /* If the label is empty, the test framework puts a non-null pointer
6750 * in label->x. Test that a null pointer works as well. */
6751 if( label->len == 0 )
6752 {
6753 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006754 if( output_size != 0 )
6755 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006756 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006757 input_data->x, input_data->len,
6758 NULL, label->len,
6759 output, output_size,
6760 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006761 TEST_EQUAL( actual_status, expected_status );
6762 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006763 }
6764
Gilles Peskine656896e2018-06-29 19:12:28 +02006765exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006766 /*
6767 * Key attributes may have been returned by psa_get_key_attributes()
6768 * thus reset them as required.
6769 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006770 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006771
Ronald Cron5425a212020-08-04 14:58:35 +02006772 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006773 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006774 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006775}
6776/* END_CASE */
6777
6778/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006779void asymmetric_encrypt_decrypt( int key_type_arg,
6780 data_t *key_data,
6781 int alg_arg,
6782 data_t *input_data,
6783 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006784{
Ronald Cron5425a212020-08-04 14:58:35 +02006785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006786 psa_key_type_t key_type = key_type_arg;
6787 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006788 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006789 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006790 size_t output_size;
6791 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006792 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006793 size_t output2_size;
6794 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006796
Gilles Peskine8817f612018-12-18 00:18:46 +01006797 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006798
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006799 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6800 psa_set_key_algorithm( &attributes, alg );
6801 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006802
Gilles Peskine049c7532019-05-15 20:22:09 +02006803 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006804 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006805
6806 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006807 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006808 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006809
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006810 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006811 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006812 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006813
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006814 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02006815 TEST_LE_U( output2_size,
6816 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6817 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006818 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006819
Gilles Peskineeebd7382018-06-08 18:11:54 +02006820 /* We test encryption by checking that encrypt-then-decrypt gives back
6821 * the original plaintext because of the non-optional random
6822 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006823 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006824 input_data->x, input_data->len,
6825 label->x, label->len,
6826 output, output_size,
6827 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006828 /* We don't know what ciphertext length to expect, but check that
6829 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006830 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006831
Ronald Cron5425a212020-08-04 14:58:35 +02006832 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006833 output, output_length,
6834 label->x, label->len,
6835 output2, output2_size,
6836 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006837 ASSERT_COMPARE( input_data->x, input_data->len,
6838 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006839
6840exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006841 /*
6842 * Key attributes may have been returned by psa_get_key_attributes()
6843 * thus reset them as required.
6844 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006845 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006846
Ronald Cron5425a212020-08-04 14:58:35 +02006847 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006848 mbedtls_free( output );
6849 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006850 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006851}
6852/* END_CASE */
6853
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006854/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006855void asymmetric_decrypt( int key_type_arg,
6856 data_t *key_data,
6857 int alg_arg,
6858 data_t *input_data,
6859 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006860 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006861{
Ronald Cron5425a212020-08-04 14:58:35 +02006862 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006863 psa_key_type_t key_type = key_type_arg;
6864 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006865 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006866 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006867 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006868 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006869 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006870
Gilles Peskine8817f612018-12-18 00:18:46 +01006871 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006872
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006873 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6874 psa_set_key_algorithm( &attributes, alg );
6875 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006876
Gilles Peskine049c7532019-05-15 20:22:09 +02006877 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006878 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006879
gabor-mezei-armceface22021-01-21 12:26:17 +01006880 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6881 key_bits = psa_get_key_bits( &attributes );
6882
6883 /* Determine the maximum ciphertext length */
6884 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006885 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01006886 ASSERT_ALLOC( output, output_size );
6887
Ronald Cron5425a212020-08-04 14:58:35 +02006888 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006889 input_data->x, input_data->len,
6890 label->x, label->len,
6891 output,
6892 output_size,
6893 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006894 ASSERT_COMPARE( expected_data->x, expected_data->len,
6895 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006896
Gilles Peskine68428122018-06-30 18:42:41 +02006897 /* If the label is empty, the test framework puts a non-null pointer
6898 * in label->x. Test that a null pointer works as well. */
6899 if( label->len == 0 )
6900 {
6901 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006902 if( output_size != 0 )
6903 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006904 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006905 input_data->x, input_data->len,
6906 NULL, label->len,
6907 output,
6908 output_size,
6909 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006910 ASSERT_COMPARE( expected_data->x, expected_data->len,
6911 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006912 }
6913
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006914exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006915 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006916 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006917 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006918 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006919}
6920/* END_CASE */
6921
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006922/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006923void asymmetric_decrypt_fail( int key_type_arg,
6924 data_t *key_data,
6925 int alg_arg,
6926 data_t *input_data,
6927 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006928 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006929 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006930{
Ronald Cron5425a212020-08-04 14:58:35 +02006931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006932 psa_key_type_t key_type = key_type_arg;
6933 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006934 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006935 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006936 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006937 psa_status_t actual_status;
6938 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006940
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006941 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006942
Gilles Peskine8817f612018-12-18 00:18:46 +01006943 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006944
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006945 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6946 psa_set_key_algorithm( &attributes, alg );
6947 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006948
Gilles Peskine049c7532019-05-15 20:22:09 +02006949 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006950 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006951
Ronald Cron5425a212020-08-04 14:58:35 +02006952 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006953 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006954 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006955 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006956 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006957 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006958 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006959
Gilles Peskine68428122018-06-30 18:42:41 +02006960 /* If the label is empty, the test framework puts a non-null pointer
6961 * in label->x. Test that a null pointer works as well. */
6962 if( label->len == 0 )
6963 {
6964 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006965 if( output_size != 0 )
6966 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006967 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006968 input_data->x, input_data->len,
6969 NULL, label->len,
6970 output, output_size,
6971 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006972 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006973 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006974 }
6975
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006976exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006977 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006978 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006979 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006980 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006981}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006982/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006983
6984/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006985void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006986{
6987 /* Test each valid way of initializing the object, except for `= {0}`, as
6988 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6989 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006990 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006991 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006992 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6993 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6994 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006995
6996 memset( &zero, 0, sizeof( zero ) );
6997
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006998 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006999 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007000 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007001 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007002 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007003 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007004 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007005
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007006 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007007 PSA_ASSERT( psa_key_derivation_abort(&func) );
7008 PSA_ASSERT( psa_key_derivation_abort(&init) );
7009 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007010}
7011/* END_CASE */
7012
Janos Follath16de4a42019-06-13 16:32:24 +01007013/* BEGIN_CASE */
7014void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007015{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007016 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007017 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007018 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007019
Gilles Peskine8817f612018-12-18 00:18:46 +01007020 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007021
Janos Follath16de4a42019-06-13 16:32:24 +01007022 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007023 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007024
7025exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007026 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007027 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007028}
7029/* END_CASE */
7030
Janos Follathaf3c2a02019-06-12 12:34:34 +01007031/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007032void derive_set_capacity( int alg_arg, int capacity_arg,
7033 int expected_status_arg )
7034{
7035 psa_algorithm_t alg = alg_arg;
7036 size_t capacity = capacity_arg;
7037 psa_status_t expected_status = expected_status_arg;
7038 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7039
7040 PSA_ASSERT( psa_crypto_init( ) );
7041
7042 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7043
7044 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7045 expected_status );
7046
7047exit:
7048 psa_key_derivation_abort( &operation );
7049 PSA_DONE( );
7050}
7051/* END_CASE */
7052
7053/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007054void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007055 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007056 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007057 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007058 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007059 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007060 int expected_status_arg3,
7061 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007062{
7063 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007064 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7065 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007066 psa_status_t expected_statuses[] = {expected_status_arg1,
7067 expected_status_arg2,
7068 expected_status_arg3};
7069 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007070 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7071 MBEDTLS_SVC_KEY_ID_INIT,
7072 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007073 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7075 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007076 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007077 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007078 psa_status_t expected_output_status = expected_output_status_arg;
7079 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007080
7081 PSA_ASSERT( psa_crypto_init( ) );
7082
7083 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7084 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007085
7086 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7087
7088 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7089 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007090 mbedtls_test_set_step( i );
7091 if( steps[i] == 0 )
7092 {
7093 /* Skip this step */
7094 }
7095 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007096 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007097 psa_set_key_type( &attributes, key_types[i] );
7098 PSA_ASSERT( psa_import_key( &attributes,
7099 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007100 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007101 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7102 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7103 {
7104 // When taking a private key as secret input, use key agreement
7105 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007106 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7107 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007108 expected_statuses[i] );
7109 }
7110 else
7111 {
7112 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007113 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007114 expected_statuses[i] );
7115 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007116 }
7117 else
7118 {
7119 TEST_EQUAL( psa_key_derivation_input_bytes(
7120 &operation, steps[i],
7121 inputs[i]->x, inputs[i]->len ),
7122 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007123 }
7124 }
7125
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007126 if( output_key_type != PSA_KEY_TYPE_NONE )
7127 {
7128 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007129 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007130 psa_set_key_bits( &attributes, 8 );
7131 actual_output_status =
7132 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007133 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007134 }
7135 else
7136 {
7137 uint8_t buffer[1];
7138 actual_output_status =
7139 psa_key_derivation_output_bytes( &operation,
7140 buffer, sizeof( buffer ) );
7141 }
7142 TEST_EQUAL( actual_output_status, expected_output_status );
7143
Janos Follathaf3c2a02019-06-12 12:34:34 +01007144exit:
7145 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007146 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7147 psa_destroy_key( keys[i] );
7148 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007149 PSA_DONE( );
7150}
7151/* END_CASE */
7152
Janos Follathd958bb72019-07-03 15:02:16 +01007153/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007154void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007155{
Janos Follathd958bb72019-07-03 15:02:16 +01007156 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007157 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007158 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007159 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007160 unsigned char input1[] = "Input 1";
7161 size_t input1_length = sizeof( input1 );
7162 unsigned char input2[] = "Input 2";
7163 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007164 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007165 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007166 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7167 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7168 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007170
Gilles Peskine8817f612018-12-18 00:18:46 +01007171 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007172
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7174 psa_set_key_algorithm( &attributes, alg );
7175 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007176
Gilles Peskine73676cb2019-05-15 20:15:10 +02007177 PSA_ASSERT( psa_import_key( &attributes,
7178 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007179 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007180
7181 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007182 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7183 input1, input1_length,
7184 input2, input2_length,
7185 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007186 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007187
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007188 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007189 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007190 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007191
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007192 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007193
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007194 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007195 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007196
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007197exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007198 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007199 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007200 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007201}
7202/* END_CASE */
7203
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007204/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007205void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007206{
7207 uint8_t output_buffer[16];
7208 size_t buffer_size = 16;
7209 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007210 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007211
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007212 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7213 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007214 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007215
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007216 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007217 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007218
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007219 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007220
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007221 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7222 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007223 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007224
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007225 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007226 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007227
7228exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007229 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007230}
7231/* END_CASE */
7232
7233/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007234void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007235 int step1_arg, data_t *input1, int expected_status_arg1,
7236 int step2_arg, data_t *input2, int expected_status_arg2,
7237 int step3_arg, data_t *input3, int expected_status_arg3,
7238 int step4_arg, data_t *input4, int expected_status_arg4,
7239 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007240 int requested_capacity_arg,
7241 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007242 data_t *expected_output2,
7243 int other_key_input_type,
7244 int key_input_type,
7245 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007246{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007247 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007248 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7249 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007250 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7251 MBEDTLS_SVC_KEY_ID_INIT,
7252 MBEDTLS_SVC_KEY_ID_INIT,
7253 MBEDTLS_SVC_KEY_ID_INIT};
7254 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7255 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007256 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007257 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007258 uint8_t *expected_outputs[2] =
7259 {expected_output1->x, expected_output2->x};
7260 size_t output_sizes[2] =
7261 {expected_output1->len, expected_output2->len};
7262 size_t output_buffer_size = 0;
7263 uint8_t *output_buffer = NULL;
7264 size_t expected_capacity;
7265 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007266 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7267 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7268 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7269 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007270 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007271 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007272 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007273
7274 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7275 {
7276 if( output_sizes[i] > output_buffer_size )
7277 output_buffer_size = output_sizes[i];
7278 if( output_sizes[i] == 0 )
7279 expected_outputs[i] = NULL;
7280 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007281 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007283
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007284 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007285 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7286 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7287 requested_capacity ) );
7288 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007289 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007290 switch( steps[i] )
7291 {
7292 case 0:
7293 break;
7294 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007295 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007296 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007297 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007298 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007299 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007300 inputs[i]->x, inputs[i]->len ),
7301 statuses[i] );
7302
7303 if( statuses[i] != PSA_SUCCESS )
7304 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007305 break;
7306 case 1: // input key
7307 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7308 psa_set_key_algorithm( &attributes1, alg );
7309 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7310
7311 PSA_ASSERT( psa_import_key( &attributes1,
7312 inputs[i]->x, inputs[i]->len,
7313 &keys[i] ) );
7314
Przemek Stekiel38647de2022-04-19 13:27:47 +02007315 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007316 {
7317 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007318 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7319 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007320 }
7321
Przemek Stekiel38647de2022-04-19 13:27:47 +02007322 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7323 steps[i],
7324 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007325 break;
7326 default:
7327 TEST_ASSERT( ! "default case not supported" );
7328 break;
7329 }
7330 break;
7331 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007332 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007333 {
7334 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007335 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7336 steps[i],
7337 inputs[i]->x,
7338 inputs[i]->len ),
7339 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007340 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007341 case 1: // input key, type DERIVE
7342 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007343 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7344 psa_set_key_algorithm( &attributes2, alg );
7345 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7346
7347 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007348 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007349 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7350
7351 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007352 inputs[i]->x, inputs[i]->len,
7353 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007354
Przemek Stekiel38647de2022-04-19 13:27:47 +02007355 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7356 steps[i],
7357 keys[i] ),
7358 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007359 break;
7360 case 2: // key agreement
7361 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7362 psa_set_key_algorithm( &attributes3, alg );
7363 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7364
7365 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007366 inputs[i]->x, inputs[i]->len,
7367 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007368
7369 TEST_EQUAL( psa_key_derivation_key_agreement(
7370 &operation,
7371 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7372 keys[i], key_agreement_peer_key->x,
7373 key_agreement_peer_key->len ), statuses[i] );
7374 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007375 default:
7376 TEST_ASSERT( ! "default case not supported" );
7377 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007378 }
7379
Przemek Stekiel38647de2022-04-19 13:27:47 +02007380 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007381 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007382 break;
7383 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007384 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007385 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007386 inputs[i]->x, inputs[i]->len ), statuses[i] );
7387
7388 if( statuses[i] != PSA_SUCCESS )
7389 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007390 break;
7391 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007392 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007393
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007394 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007395 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007396 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007397 expected_capacity = requested_capacity;
7398
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007399 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007400 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007401 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7402
7403 /* For output key derivation secret must be provided using
7404 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007405 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007406 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007407
7408 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7409 psa_set_key_algorithm( &attributes4, alg );
7410 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007411 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007412
7413 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007414 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007415 }
7416 else // output bytes
7417 {
7418 /* Expansion phase. */
7419 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007420 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007421 /* Read some bytes. */
7422 status = psa_key_derivation_output_bytes( &operation,
7423 output_buffer, output_sizes[i] );
7424 if( expected_capacity == 0 && output_sizes[i] == 0 )
7425 {
7426 /* Reading 0 bytes when 0 bytes are available can go either way. */
7427 TEST_ASSERT( status == PSA_SUCCESS ||
7428 status == PSA_ERROR_INSUFFICIENT_DATA );
7429 continue;
7430 }
7431 else if( expected_capacity == 0 ||
7432 output_sizes[i] > expected_capacity )
7433 {
7434 /* Capacity exceeded. */
7435 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7436 expected_capacity = 0;
7437 continue;
7438 }
7439 /* Success. Check the read data. */
7440 PSA_ASSERT( status );
7441 if( output_sizes[i] != 0 )
7442 ASSERT_COMPARE( output_buffer, output_sizes[i],
7443 expected_outputs[i], output_sizes[i] );
7444 /* Check the operation status. */
7445 expected_capacity -= output_sizes[i];
7446 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7447 &current_capacity ) );
7448 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007449 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007450 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007451 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007452
7453exit:
7454 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007455 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007456 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7457 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007458 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007459 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007460}
7461/* END_CASE */
7462
7463/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007464void derive_full( int alg_arg,
7465 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007466 data_t *input1,
7467 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007468 int requested_capacity_arg )
7469{
Ronald Cron5425a212020-08-04 14:58:35 +02007470 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007471 psa_algorithm_t alg = alg_arg;
7472 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007473 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007474 unsigned char output_buffer[16];
7475 size_t expected_capacity = requested_capacity;
7476 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007477 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007478
Gilles Peskine8817f612018-12-18 00:18:46 +01007479 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007480
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007481 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7482 psa_set_key_algorithm( &attributes, alg );
7483 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007484
Gilles Peskine049c7532019-05-15 20:22:09 +02007485 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007486 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007487
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007488 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7489 input1->x, input1->len,
7490 input2->x, input2->len,
7491 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007492 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007493
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007494 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007495 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007496 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007497
7498 /* Expansion phase. */
7499 while( current_capacity > 0 )
7500 {
7501 size_t read_size = sizeof( output_buffer );
7502 if( read_size > current_capacity )
7503 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007504 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007505 output_buffer,
7506 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007507 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007508 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007509 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007510 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007511 }
7512
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007513 /* Check that the operation refuses to go over capacity. */
7514 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007515 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007516
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007517 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007518
7519exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007520 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007521 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007522 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007523}
7524/* END_CASE */
7525
Janos Follathe60c9052019-07-03 13:51:30 +01007526/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007527void derive_key_exercise( int alg_arg,
7528 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007529 data_t *input1,
7530 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007531 int derived_type_arg,
7532 int derived_bits_arg,
7533 int derived_usage_arg,
7534 int derived_alg_arg )
7535{
Ronald Cron5425a212020-08-04 14:58:35 +02007536 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7537 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007538 psa_algorithm_t alg = alg_arg;
7539 psa_key_type_t derived_type = derived_type_arg;
7540 size_t derived_bits = derived_bits_arg;
7541 psa_key_usage_t derived_usage = derived_usage_arg;
7542 psa_algorithm_t derived_alg = derived_alg_arg;
7543 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007544 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007546 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007547
Gilles Peskine8817f612018-12-18 00:18:46 +01007548 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007549
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007550 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7551 psa_set_key_algorithm( &attributes, alg );
7552 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007553 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007554 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007555
7556 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007557 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7558 input1->x, input1->len,
7559 input2->x, input2->len,
7560 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007561 goto exit;
7562
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007563 psa_set_key_usage_flags( &attributes, derived_usage );
7564 psa_set_key_algorithm( &attributes, derived_alg );
7565 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007566 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007567 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007568 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007569
7570 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007571 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007572 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7573 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007574
7575 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007576 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007577 goto exit;
7578
7579exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007580 /*
7581 * Key attributes may have been returned by psa_get_key_attributes()
7582 * thus reset them as required.
7583 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007584 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007585
7586 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007587 psa_destroy_key( base_key );
7588 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007589 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007590}
7591/* END_CASE */
7592
Janos Follath42fd8882019-07-03 14:17:09 +01007593/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007594void derive_key_export( int alg_arg,
7595 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007596 data_t *input1,
7597 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007598 int bytes1_arg,
7599 int bytes2_arg )
7600{
Ronald Cron5425a212020-08-04 14:58:35 +02007601 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7602 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007603 psa_algorithm_t alg = alg_arg;
7604 size_t bytes1 = bytes1_arg;
7605 size_t bytes2 = bytes2_arg;
7606 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007607 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007608 uint8_t *output_buffer = NULL;
7609 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007610 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7611 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007612 size_t length;
7613
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007614 ASSERT_ALLOC( output_buffer, capacity );
7615 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007616 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007617
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007618 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7619 psa_set_key_algorithm( &base_attributes, alg );
7620 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007621 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007622 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007623
7624 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007625 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7626 input1->x, input1->len,
7627 input2->x, input2->len,
7628 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007629 goto exit;
7630
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007631 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007632 output_buffer,
7633 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007634 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007635
7636 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007637 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7638 input1->x, input1->len,
7639 input2->x, input2->len,
7640 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007641 goto exit;
7642
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007643 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7644 psa_set_key_algorithm( &derived_attributes, 0 );
7645 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007646 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007647 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007648 &derived_key ) );
7649 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007650 export_buffer, bytes1,
7651 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007652 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007653 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007654 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007655 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007656 &derived_key ) );
7657 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007658 export_buffer + bytes1, bytes2,
7659 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007660 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007661
7662 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007663 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7664 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007665
7666exit:
7667 mbedtls_free( output_buffer );
7668 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007669 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007670 psa_destroy_key( base_key );
7671 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007672 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007673}
7674/* END_CASE */
7675
7676/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007677void derive_key_type( int alg_arg,
7678 data_t *key_data,
7679 data_t *input1,
7680 data_t *input2,
7681 int key_type_arg, int bits_arg,
7682 data_t *expected_export )
7683{
7684 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7685 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7686 const psa_algorithm_t alg = alg_arg;
7687 const psa_key_type_t key_type = key_type_arg;
7688 const size_t bits = bits_arg;
7689 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7690 const size_t export_buffer_size =
7691 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
7692 uint8_t *export_buffer = NULL;
7693 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7694 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7695 size_t export_length;
7696
7697 ASSERT_ALLOC( export_buffer, export_buffer_size );
7698 PSA_ASSERT( psa_crypto_init( ) );
7699
7700 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7701 psa_set_key_algorithm( &base_attributes, alg );
7702 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7703 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
7704 &base_key ) );
7705
Przemek Stekielc85f0912022-03-08 11:37:54 +01007706 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007707 &operation, base_key, alg,
7708 input1->x, input1->len,
7709 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01007710 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007711 goto exit;
7712
7713 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7714 psa_set_key_algorithm( &derived_attributes, 0 );
7715 psa_set_key_type( &derived_attributes, key_type );
7716 psa_set_key_bits( &derived_attributes, bits );
7717 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
7718 &derived_key ) );
7719
7720 PSA_ASSERT( psa_export_key( derived_key,
7721 export_buffer, export_buffer_size,
7722 &export_length ) );
7723 ASSERT_COMPARE( export_buffer, export_length,
7724 expected_export->x, expected_export->len );
7725
7726exit:
7727 mbedtls_free( export_buffer );
7728 psa_key_derivation_abort( &operation );
7729 psa_destroy_key( base_key );
7730 psa_destroy_key( derived_key );
7731 PSA_DONE( );
7732}
7733/* END_CASE */
7734
7735/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007736void derive_key( int alg_arg,
7737 data_t *key_data, data_t *input1, data_t *input2,
7738 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007739 int expected_status_arg,
7740 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02007741{
Ronald Cron5425a212020-08-04 14:58:35 +02007742 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7743 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007744 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007745 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007746 size_t bits = bits_arg;
7747 psa_status_t expected_status = expected_status_arg;
7748 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7749 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7750 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7751
7752 PSA_ASSERT( psa_crypto_init( ) );
7753
7754 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7755 psa_set_key_algorithm( &base_attributes, alg );
7756 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7757 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007758 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007759
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007760 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7761 input1->x, input1->len,
7762 input2->x, input2->len,
7763 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007764 goto exit;
7765
7766 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7767 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007768 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007769 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007770
7771 psa_status_t status =
7772 psa_key_derivation_output_key( &derived_attributes,
7773 &operation,
7774 &derived_key );
7775 if( is_large_output > 0 )
7776 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7777 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007778
7779exit:
7780 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007781 psa_destroy_key( base_key );
7782 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007783 PSA_DONE( );
7784}
7785/* END_CASE */
7786
7787/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007788void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007789 int our_key_type_arg, int our_key_alg_arg,
7790 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007791 int expected_status_arg )
7792{
Ronald Cron5425a212020-08-04 14:58:35 +02007793 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007794 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007795 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007796 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007797 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007799 psa_status_t expected_status = expected_status_arg;
7800 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007801
Gilles Peskine8817f612018-12-18 00:18:46 +01007802 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007803
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007804 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007805 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007806 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007807 PSA_ASSERT( psa_import_key( &attributes,
7808 our_key_data->x, our_key_data->len,
7809 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007810
Gilles Peskine77f40d82019-04-11 21:27:06 +02007811 /* The tests currently include inputs that should fail at either step.
7812 * Test cases that fail at the setup step should be changed to call
7813 * key_derivation_setup instead, and this function should be renamed
7814 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007815 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007816 if( status == PSA_SUCCESS )
7817 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007818 TEST_EQUAL( psa_key_derivation_key_agreement(
7819 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7820 our_key,
7821 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007822 expected_status );
7823 }
7824 else
7825 {
7826 TEST_ASSERT( status == expected_status );
7827 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007828
7829exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007830 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007831 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007832 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007833}
7834/* END_CASE */
7835
7836/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007837void raw_key_agreement( int alg_arg,
7838 int our_key_type_arg, data_t *our_key_data,
7839 data_t *peer_key_data,
7840 data_t *expected_output )
7841{
Ronald Cron5425a212020-08-04 14:58:35 +02007842 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007843 psa_algorithm_t alg = alg_arg;
7844 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007845 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007846 unsigned char *output = NULL;
7847 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007848 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007849
Gilles Peskinef0cba732019-04-11 22:12:38 +02007850 PSA_ASSERT( psa_crypto_init( ) );
7851
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007852 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7853 psa_set_key_algorithm( &attributes, alg );
7854 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007855 PSA_ASSERT( psa_import_key( &attributes,
7856 our_key_data->x, our_key_data->len,
7857 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007858
gabor-mezei-armceface22021-01-21 12:26:17 +01007859 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7860 key_bits = psa_get_key_bits( &attributes );
7861
Gilles Peskine992bee82022-04-13 23:25:52 +02007862 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007863 TEST_LE_U( expected_output->len,
7864 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7865 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
7866 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02007867
7868 /* Good case with exact output size */
7869 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02007870 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7871 peer_key_data->x, peer_key_data->len,
7872 output, expected_output->len,
7873 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007874 ASSERT_COMPARE( output, output_length,
7875 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02007876 mbedtls_free( output );
7877 output = NULL;
7878 output_length = ~0;
7879
7880 /* Larger buffer */
7881 ASSERT_ALLOC( output, expected_output->len + 1 );
7882 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7883 peer_key_data->x, peer_key_data->len,
7884 output, expected_output->len + 1,
7885 &output_length ) );
7886 ASSERT_COMPARE( output, output_length,
7887 expected_output->x, expected_output->len );
7888 mbedtls_free( output );
7889 output = NULL;
7890 output_length = ~0;
7891
7892 /* Buffer too small */
7893 ASSERT_ALLOC( output, expected_output->len - 1 );
7894 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
7895 peer_key_data->x, peer_key_data->len,
7896 output, expected_output->len - 1,
7897 &output_length ),
7898 PSA_ERROR_BUFFER_TOO_SMALL );
7899 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007900 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02007901 mbedtls_free( output );
7902 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007903
7904exit:
7905 mbedtls_free( output );
7906 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007907 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007908}
7909/* END_CASE */
7910
7911/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007912void key_agreement_capacity( int alg_arg,
7913 int our_key_type_arg, data_t *our_key_data,
7914 data_t *peer_key_data,
7915 int expected_capacity_arg )
7916{
Ronald Cron5425a212020-08-04 14:58:35 +02007917 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007918 psa_algorithm_t alg = alg_arg;
7919 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007920 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007922 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007923 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007924
Gilles Peskine8817f612018-12-18 00:18:46 +01007925 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007926
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007927 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7928 psa_set_key_algorithm( &attributes, alg );
7929 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007930 PSA_ASSERT( psa_import_key( &attributes,
7931 our_key_data->x, our_key_data->len,
7932 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007933
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007934 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007935 PSA_ASSERT( psa_key_derivation_key_agreement(
7936 &operation,
7937 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7938 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007939 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7940 {
7941 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007942 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007943 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007944 NULL, 0 ) );
7945 }
Gilles Peskine59685592018-09-18 12:11:34 +02007946
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007947 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007948 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007949 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007950 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007951
Gilles Peskinebf491972018-10-25 22:36:12 +02007952 /* Test the actual capacity by reading the output. */
7953 while( actual_capacity > sizeof( output ) )
7954 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007955 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007956 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007957 actual_capacity -= sizeof( output );
7958 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007959 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007960 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007961 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007962 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007963
Gilles Peskine59685592018-09-18 12:11:34 +02007964exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007965 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007966 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007967 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007968}
7969/* END_CASE */
7970
7971/* BEGIN_CASE */
7972void key_agreement_output( int alg_arg,
7973 int our_key_type_arg, data_t *our_key_data,
7974 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007975 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007976{
Ronald Cron5425a212020-08-04 14:58:35 +02007977 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007978 psa_algorithm_t alg = alg_arg;
7979 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007980 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007981 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007982 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007983
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007984 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7985 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007986
Gilles Peskine8817f612018-12-18 00:18:46 +01007987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007988
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7990 psa_set_key_algorithm( &attributes, alg );
7991 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007992 PSA_ASSERT( psa_import_key( &attributes,
7993 our_key_data->x, our_key_data->len,
7994 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007995
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007996 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007997 PSA_ASSERT( psa_key_derivation_key_agreement(
7998 &operation,
7999 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8000 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008001 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8002 {
8003 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008004 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008005 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008006 NULL, 0 ) );
8007 }
Gilles Peskine59685592018-09-18 12:11:34 +02008008
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008009 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008010 actual_output,
8011 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008012 ASSERT_COMPARE( actual_output, expected_output1->len,
8013 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008014 if( expected_output2->len != 0 )
8015 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008016 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008017 actual_output,
8018 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008019 ASSERT_COMPARE( actual_output, expected_output2->len,
8020 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008021 }
Gilles Peskine59685592018-09-18 12:11:34 +02008022
8023exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008024 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008025 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008026 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008027 mbedtls_free( actual_output );
8028}
8029/* END_CASE */
8030
8031/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008032void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008033{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008034 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008035 unsigned char *output = NULL;
8036 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008037 size_t i;
8038 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008039
Simon Butcher49f8e312020-03-03 15:51:50 +00008040 TEST_ASSERT( bytes_arg >= 0 );
8041
Gilles Peskine91892022021-02-08 19:50:26 +01008042 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008043 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008044
Gilles Peskine8817f612018-12-18 00:18:46 +01008045 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008046
Gilles Peskinea50d7392018-06-21 10:22:13 +02008047 /* Run several times, to ensure that every output byte will be
8048 * nonzero at least once with overwhelming probability
8049 * (2^(-8*number_of_runs)). */
8050 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008051 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008052 if( bytes != 0 )
8053 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008054 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008055
Gilles Peskinea50d7392018-06-21 10:22:13 +02008056 for( i = 0; i < bytes; i++ )
8057 {
8058 if( output[i] != 0 )
8059 ++changed[i];
8060 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008061 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008062
8063 /* Check that every byte was changed to nonzero at least once. This
8064 * validates that psa_generate_random is overwriting every byte of
8065 * the output buffer. */
8066 for( i = 0; i < bytes; i++ )
8067 {
8068 TEST_ASSERT( changed[i] != 0 );
8069 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008070
8071exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008072 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008073 mbedtls_free( output );
8074 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008075}
8076/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008077
8078/* BEGIN_CASE */
8079void generate_key( int type_arg,
8080 int bits_arg,
8081 int usage_arg,
8082 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008083 int expected_status_arg,
8084 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008085{
Ronald Cron5425a212020-08-04 14:58:35 +02008086 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008087 psa_key_type_t type = type_arg;
8088 psa_key_usage_t usage = usage_arg;
8089 size_t bits = bits_arg;
8090 psa_algorithm_t alg = alg_arg;
8091 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008093 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008094
Gilles Peskine8817f612018-12-18 00:18:46 +01008095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008096
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008097 psa_set_key_usage_flags( &attributes, usage );
8098 psa_set_key_algorithm( &attributes, alg );
8099 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008100 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008101
8102 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008103 psa_status_t status = psa_generate_key( &attributes, &key );
8104
8105 if( is_large_key > 0 )
8106 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8107 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008108 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008109 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008110
8111 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008112 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008113 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8114 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008115
Gilles Peskine818ca122018-06-20 18:16:48 +02008116 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008117 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008118 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008119
8120exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008121 /*
8122 * Key attributes may have been returned by psa_get_key_attributes()
8123 * thus reset them as required.
8124 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008125 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008126
Ronald Cron5425a212020-08-04 14:58:35 +02008127 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008128 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008129}
8130/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008131
Ronald Cronee414c72021-03-18 18:50:08 +01008132/* 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 +02008133void generate_key_rsa( int bits_arg,
8134 data_t *e_arg,
8135 int expected_status_arg )
8136{
Ronald Cron5425a212020-08-04 14:58:35 +02008137 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008138 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008139 size_t bits = bits_arg;
8140 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8141 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8142 psa_status_t expected_status = expected_status_arg;
8143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8144 uint8_t *exported = NULL;
8145 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008146 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008147 size_t exported_length = SIZE_MAX;
8148 uint8_t *e_read_buffer = NULL;
8149 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008150 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008151 size_t e_read_length = SIZE_MAX;
8152
8153 if( e_arg->len == 0 ||
8154 ( e_arg->len == 3 &&
8155 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8156 {
8157 is_default_public_exponent = 1;
8158 e_read_size = 0;
8159 }
8160 ASSERT_ALLOC( e_read_buffer, e_read_size );
8161 ASSERT_ALLOC( exported, exported_size );
8162
8163 PSA_ASSERT( psa_crypto_init( ) );
8164
8165 psa_set_key_usage_flags( &attributes, usage );
8166 psa_set_key_algorithm( &attributes, alg );
8167 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8168 e_arg->x, e_arg->len ) );
8169 psa_set_key_bits( &attributes, bits );
8170
8171 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008172 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008173 if( expected_status != PSA_SUCCESS )
8174 goto exit;
8175
8176 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008177 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008178 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8179 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8180 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8181 e_read_buffer, e_read_size,
8182 &e_read_length ) );
8183 if( is_default_public_exponent )
8184 TEST_EQUAL( e_read_length, 0 );
8185 else
8186 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8187
8188 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008189 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008190 goto exit;
8191
8192 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008193 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008194 exported, exported_size,
8195 &exported_length ) );
8196 {
8197 uint8_t *p = exported;
8198 uint8_t *end = exported + exported_length;
8199 size_t len;
8200 /* RSAPublicKey ::= SEQUENCE {
8201 * modulus INTEGER, -- n
8202 * publicExponent INTEGER } -- e
8203 */
8204 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008205 MBEDTLS_ASN1_SEQUENCE |
8206 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008207 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008208 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8209 MBEDTLS_ASN1_INTEGER ) );
8210 if( len >= 1 && p[0] == 0 )
8211 {
8212 ++p;
8213 --len;
8214 }
8215 if( e_arg->len == 0 )
8216 {
8217 TEST_EQUAL( len, 3 );
8218 TEST_EQUAL( p[0], 1 );
8219 TEST_EQUAL( p[1], 0 );
8220 TEST_EQUAL( p[2], 1 );
8221 }
8222 else
8223 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8224 }
8225
8226exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008227 /*
8228 * Key attributes may have been returned by psa_get_key_attributes() or
8229 * set by psa_set_key_domain_parameters() thus reset them as required.
8230 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008231 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008232
Ronald Cron5425a212020-08-04 14:58:35 +02008233 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008234 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008235 mbedtls_free( e_read_buffer );
8236 mbedtls_free( exported );
8237}
8238/* END_CASE */
8239
Darryl Greend49a4992018-06-18 17:27:26 +01008240/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008241void persistent_key_load_key_from_storage( data_t *data,
8242 int type_arg, int bits_arg,
8243 int usage_flags_arg, int alg_arg,
8244 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008245{
Ronald Cron71016a92020-08-28 19:01:50 +02008246 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008248 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8249 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008250 psa_key_type_t type = type_arg;
8251 size_t bits = bits_arg;
8252 psa_key_usage_t usage_flags = usage_flags_arg;
8253 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008255 unsigned char *first_export = NULL;
8256 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008257 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008258 size_t first_exported_length;
8259 size_t second_exported_length;
8260
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008261 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8262 {
8263 ASSERT_ALLOC( first_export, export_size );
8264 ASSERT_ALLOC( second_export, export_size );
8265 }
Darryl Greend49a4992018-06-18 17:27:26 +01008266
Gilles Peskine8817f612018-12-18 00:18:46 +01008267 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008268
Gilles Peskinec87af662019-05-15 16:12:22 +02008269 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008270 psa_set_key_usage_flags( &attributes, usage_flags );
8271 psa_set_key_algorithm( &attributes, alg );
8272 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008273 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008274
Darryl Green0c6575a2018-11-07 16:05:30 +00008275 switch( generation_method )
8276 {
8277 case IMPORT_KEY:
8278 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008279 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008280 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008281 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008282
Darryl Green0c6575a2018-11-07 16:05:30 +00008283 case GENERATE_KEY:
8284 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008285 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008286 break;
8287
8288 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008289#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008290 {
8291 /* Create base key */
8292 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8293 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8294 psa_set_key_usage_flags( &base_attributes,
8295 PSA_KEY_USAGE_DERIVE );
8296 psa_set_key_algorithm( &base_attributes, derive_alg );
8297 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008298 PSA_ASSERT( psa_import_key( &base_attributes,
8299 data->x, data->len,
8300 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008301 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008302 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008303 PSA_ASSERT( psa_key_derivation_input_key(
8304 &operation,
8305 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008306 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008307 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008308 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008309 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8310 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008311 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008312 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008313 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008314 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008315 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008316#else
8317 TEST_ASSUME( ! "KDF not supported in this configuration" );
8318#endif
8319 break;
8320
8321 default:
8322 TEST_ASSERT( ! "generation_method not implemented in test" );
8323 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008324 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008325 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008326
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008327 /* Export the key if permitted by the key policy. */
8328 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8329 {
Ronald Cron5425a212020-08-04 14:58:35 +02008330 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008331 first_export, export_size,
8332 &first_exported_length ) );
8333 if( generation_method == IMPORT_KEY )
8334 ASSERT_COMPARE( data->x, data->len,
8335 first_export, first_exported_length );
8336 }
Darryl Greend49a4992018-06-18 17:27:26 +01008337
8338 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008339 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008340 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008341 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008342
Darryl Greend49a4992018-06-18 17:27:26 +01008343 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008344 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008345 TEST_ASSERT( mbedtls_svc_key_id_equal(
8346 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008347 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8348 PSA_KEY_LIFETIME_PERSISTENT );
8349 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8350 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008351 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008352 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008353 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008354
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008355 /* Export the key again if permitted by the key policy. */
8356 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008357 {
Ronald Cron5425a212020-08-04 14:58:35 +02008358 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008359 second_export, export_size,
8360 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008361 ASSERT_COMPARE( first_export, first_exported_length,
8362 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008363 }
8364
8365 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008366 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008367 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008368
8369exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008370 /*
8371 * Key attributes may have been returned by psa_get_key_attributes()
8372 * thus reset them as required.
8373 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008374 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008375
Darryl Greend49a4992018-06-18 17:27:26 +01008376 mbedtls_free( first_export );
8377 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008378 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008379 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008380 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008381 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008382}
8383/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008384
Neil Armstronga557cb82022-06-10 08:58:32 +02008385/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008386void ecjpake_setup( int alg_arg, int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008387 int input_first, data_t *pw_data,
Neil Armstrongd597bc72022-05-25 11:28:39 +02008388 int expected_status_arg )
8389{
8390 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8391 psa_pake_operation_t operation = psa_pake_operation_init();
8392 psa_algorithm_t alg = alg_arg;
8393 psa_algorithm_t hash_alg = hash_arg;
8394 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008395 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8397 psa_status_t expected_status = expected_status_arg;
8398 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8399 unsigned char *output_buffer = NULL;
8400 size_t output_len = 0;
8401
8402 PSA_INIT( );
8403
8404 ASSERT_ALLOC( output_buffer,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008405 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8406 PSA_PAKE_STEP_KEY_SHARE) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008407
8408 if( pw_data->len > 0 )
8409 {
8410 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8411 psa_set_key_algorithm( &attributes, alg );
8412 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8413 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8414 &key ) );
8415 }
8416
8417 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8418 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8419 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8420
Neil Armstrong645cccd2022-06-08 17:36:23 +02008421 PSA_ASSERT( psa_pake_abort( &operation ) );
8422
8423 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8424 PSA_ERROR_BAD_STATE );
8425 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8426 PSA_ERROR_BAD_STATE );
8427 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8428 PSA_ERROR_BAD_STATE );
8429 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8430 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008431 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8432 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008433 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008434 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008435 PSA_ERROR_BAD_STATE );
8436
8437 PSA_ASSERT( psa_pake_abort( &operation ) );
8438
Neil Armstrongd597bc72022-05-25 11:28:39 +02008439 status = psa_pake_setup( &operation, &cipher_suite );
8440 if( status != PSA_SUCCESS )
8441 {
8442 TEST_EQUAL( status, expected_status );
8443 goto exit;
8444 }
8445 else
8446 PSA_ASSERT( status );
8447
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008448 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8449 PSA_ERROR_BAD_STATE );
8450
Neil Armstrongd597bc72022-05-25 11:28:39 +02008451 status = psa_pake_set_role( &operation, role );
8452 if( status != PSA_SUCCESS )
8453 {
8454 TEST_EQUAL( status, expected_status );
8455 goto exit;
8456 }
8457 else
8458 PSA_ASSERT( status );
8459
8460 if( pw_data->len > 0 )
8461 {
8462 status = psa_pake_set_password_key( &operation, key );
8463 if( status != PSA_SUCCESS )
8464 {
8465 TEST_EQUAL( status, expected_status );
8466 goto exit;
8467 }
8468 else
8469 PSA_ASSERT( status );
8470 }
8471
Neil Armstrong707d9572022-06-08 17:31:49 +02008472 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8473 PSA_ERROR_INVALID_ARGUMENT );
8474 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8475 PSA_ERROR_INVALID_ARGUMENT );
8476
8477 const uint8_t unsupported_id[] = "abcd";
8478
8479 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8480 PSA_ERROR_NOT_SUPPORTED );
8481 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8482 PSA_ERROR_NOT_SUPPORTED );
8483
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008484 /* First round */
8485 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008486 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008487 /* Invalid parameters */
8488 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8489 NULL, 0 ),
8490 PSA_ERROR_INVALID_ARGUMENT );
8491 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8492 output_buffer, 66 ),
8493 PSA_ERROR_INVALID_ARGUMENT );
8494 /* Invalid first step */
8495 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8496 output_buffer, 66 ),
8497 PSA_ERROR_BAD_STATE );
8498
8499 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8500 output_buffer, 66 ),
8501 expected_status);
8502
8503 if( expected_status == PSA_SUCCESS )
8504 {
8505 /* Buffer too large */
8506 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8507 output_buffer, 512 ),
8508 PSA_ERROR_INSUFFICIENT_MEMORY );
8509
8510 /* The operation should be aborted at this point */
8511 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8512 output_buffer, 66 ),
8513 PSA_ERROR_BAD_STATE );
8514 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008515 }
8516 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008517 {
8518 /* Invalid parameters */
8519 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8520 NULL, 0, NULL ),
8521 PSA_ERROR_INVALID_ARGUMENT );
8522 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8523 output_buffer, 512, &output_len ),
8524 PSA_ERROR_INVALID_ARGUMENT );
8525 /* Invalid first step */
8526 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8527 output_buffer, 512, &output_len ),
8528 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008529
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008530 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8531 output_buffer, 512, &output_len ),
8532 expected_status );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008533
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008534 if( expected_status == PSA_SUCCESS )
8535 {
8536 TEST_ASSERT( output_len > 0 );
8537
8538 /* Buffer too small */
8539 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8540 output_buffer, 5, &output_len ),
8541 PSA_ERROR_BUFFER_TOO_SMALL );
8542
8543 /* The operation should be aborted at this point */
8544 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8545 output_buffer, 512, &output_len ),
8546 PSA_ERROR_BAD_STATE );
8547 }
8548 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008549
8550exit:
8551 PSA_ASSERT( psa_destroy_key( key ) );
8552 PSA_ASSERT( psa_pake_abort( &operation ) );
8553 mbedtls_free( output_buffer );
8554 PSA_DONE( );
8555}
8556/* END_CASE */
8557
Neil Armstronga557cb82022-06-10 08:58:32 +02008558/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008559void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8560 int client_input_first, int inject_error,
8561 data_t *pw_data )
8562{
8563 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8564 psa_pake_operation_t server = psa_pake_operation_init();
8565 psa_pake_operation_t client = psa_pake_operation_init();
8566 psa_algorithm_t alg = alg_arg;
8567 psa_algorithm_t hash_alg = hash_arg;
8568 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8570
8571 PSA_INIT( );
8572
8573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8574 psa_set_key_algorithm( &attributes, alg );
8575 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8576 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8577 &key ) );
8578
8579 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8580 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8581 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8582
8583
8584 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8585 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8586
8587 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8588 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8589
8590 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8591 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8592
8593 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8594 client_input_first, 1,
8595 inject_error ), 1 );
8596
8597 if( inject_error == 1 || inject_error == 2 )
8598 goto exit;
8599
8600 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8601 client_input_first, 2,
8602 inject_error ), 1 );
8603
8604exit:
8605 psa_destroy_key( key );
8606 psa_pake_abort( &server );
8607 psa_pake_abort( &client );
8608 PSA_DONE( );
8609}
8610/* END_CASE */
8611
8612/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008613void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008614 int derive_alg_arg, data_t *pw_data,
8615 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008616{
8617 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8618 psa_pake_operation_t server = psa_pake_operation_init();
8619 psa_pake_operation_t client = psa_pake_operation_init();
8620 psa_algorithm_t alg = alg_arg;
8621 psa_algorithm_t hash_alg = hash_arg;
8622 psa_algorithm_t derive_alg = derive_alg_arg;
8623 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8625 psa_key_derivation_operation_t server_derive =
8626 PSA_KEY_DERIVATION_OPERATION_INIT;
8627 psa_key_derivation_operation_t client_derive =
8628 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008629
8630 PSA_INIT( );
8631
Neil Armstrongd597bc72022-05-25 11:28:39 +02008632 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8633 psa_set_key_algorithm( &attributes, alg );
8634 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8635 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8636 &key ) );
8637
8638 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8639 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8640 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8641
Neil Armstrong1e855602022-06-15 11:32:11 +02008642 /* Get shared key */
8643 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
8644 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
8645
8646 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
8647 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
8648 {
8649 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
8650 PSA_KEY_DERIVATION_INPUT_SEED,
8651 (const uint8_t*) "", 0) );
8652 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
8653 PSA_KEY_DERIVATION_INPUT_SEED,
8654 (const uint8_t*) "", 0) );
8655 }
8656
Neil Armstrongd597bc72022-05-25 11:28:39 +02008657 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8658 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8659
8660 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8661 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8662
8663 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8664 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8665
Neil Armstrong1e855602022-06-15 11:32:11 +02008666 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8667 PSA_ERROR_BAD_STATE );
8668 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8669 PSA_ERROR_BAD_STATE );
8670
Neil Armstrongf983caf2022-06-15 15:27:48 +02008671 /* First round */
8672 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8673 client_input_first, 1, 0 ), 1 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008674
Neil Armstrong1e855602022-06-15 11:32:11 +02008675 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8676 PSA_ERROR_BAD_STATE );
8677 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8678 PSA_ERROR_BAD_STATE );
8679
Neil Armstrongf983caf2022-06-15 15:27:48 +02008680 /* Second round */
8681 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8682 client_input_first, 2, 0 ), 1 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008683
Neil Armstrongd597bc72022-05-25 11:28:39 +02008684 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
8685 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
8686
8687exit:
8688 psa_key_derivation_abort( &server_derive );
8689 psa_key_derivation_abort( &client_derive );
8690 psa_destroy_key( key );
8691 psa_pake_abort( &server );
8692 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008693 PSA_DONE( );
8694}
8695/* END_CASE */