blob: 12d7a89fbabb3e74151888219dcd14b37ff574ed [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200399 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200404 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100510 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_LE_U( output_length,
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200570 TEST_LE_U( output_length,
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Neil Armstrong4766f992022-02-28 16:23:59 +0100592/*!
593 * \brief Internal Function for MAC multipart tests.
594 * \param key_type_arg Type of key passed in
595 * \param key_data The encryption / decryption key data
596 * \param alg_arg The type of algorithm used
597 * \param input_data Data to encrypt / decrypt
598 * \param data_part_len_arg If not -1, the length of chunks to feed
599 * the data in to be encrypted / decrypted. If
600 * -1, no chunking
601 * \param expected_output Expected output
602 * \param is_verify If non-zero this is an verify operation.
603 * \param do_zero_parts If non-zero, interleave zero length chunks
604 * with normal length chunks.
605 * \return int Zero on failure, non-zero on success.
606 */
607static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
608 int alg_arg,
609 data_t *input_data,
610 int data_part_len_arg,
611 data_t *expected_output,
612 int is_verify,
613 int do_zero_parts )
614{
615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
616 psa_key_type_t key_type = key_type_arg;
617 psa_algorithm_t alg = alg_arg;
618 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
619 unsigned char mac[PSA_MAC_MAX_SIZE];
620 size_t part_offset = 0;
621 size_t part_length = 0;
622 size_t data_part_len = 0;
623 size_t mac_len = 0;
624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
625 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
626
627 int test_ok = 0;
628 size_t part_count = 0;
629
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100630 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100631
632 if( is_verify )
633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
634 else
635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
636
637 psa_set_key_algorithm( &attributes, alg );
638 psa_set_key_type( &attributes, key_type );
639
640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
641 &key ) );
642
643 if( is_verify )
644 status = psa_mac_verify_setup( &operation, key, alg );
645 else
646 status = psa_mac_sign_setup( &operation, key, alg );
647
648 PSA_ASSERT( status );
649
650 if( data_part_len_arg != -1 )
651 {
652 /* Pass data in parts */
653 data_part_len = ( size_t ) data_part_len_arg;
654
655 for( part_offset = 0, part_count = 0;
656 part_offset < input_data->len;
657 part_offset += part_length, part_count++ )
658 {
659 if( do_zero_parts && ( part_count & 0x01 ) )
660 {
661 part_length = 0;
662 }
663 else if( ( input_data->len - part_offset ) < data_part_len )
664 {
665 part_length = ( input_data->len - part_offset );
666 }
667 else
668 {
669 part_length = data_part_len;
670 }
671
672 PSA_ASSERT( psa_mac_update( &operation,
673 ( input_data->x + part_offset ),
674 part_length ) );
675 }
676 }
677 else
678 {
679 /* Pass all data in one go. */
680 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
681 input_data->len ) );
682 }
683
684 if( is_verify )
685 {
686 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
687 expected_output->len ) );
688 }
689 else
690 {
691 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
692 PSA_MAC_MAX_SIZE, &mac_len ) );
693
694 ASSERT_COMPARE( expected_output->x, expected_output->len,
695 mac, mac_len );
696 }
697
698 test_ok = 1;
699
700exit:
701 psa_destroy_key( key );
702 psa_mac_abort( &operation );
703 PSA_DONE( );
704
705 return( test_ok );
706}
707
Neil Armstrong75673ab2022-06-15 17:39:01 +0200708#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200709static int ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
710 psa_pake_operation_t *server,
711 psa_pake_operation_t *client,
712 int client_input_first,
713 int round, int inject_error )
714{
715 unsigned char *buffer0 = NULL, *buffer1 = NULL;
716 size_t buffer_length = (
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
719 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
720 size_t buffer0_off = 0;
721 size_t buffer1_off = 0;
722 size_t s_g1_len, s_g2_len, s_a_len;
723 size_t s_g1_off, s_g2_off, s_a_off;
724 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
725 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
726 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
727 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
728 size_t c_g1_len, c_g2_len, c_a_len;
729 size_t c_g1_off, c_g2_off, c_a_off;
730 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
731 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
732 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
733 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
734 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200735 psa_status_t status;
736 int ret = 0;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
738 ASSERT_ALLOC( buffer0, buffer_length );
739 ASSERT_ALLOC( buffer1, buffer_length );
740
741 switch( round )
742 {
743 case 1:
744 /* Server first round Output */
745 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
746 buffer0 + buffer0_off,
747 512 - buffer0_off, &s_g1_len ) );
748 s_g1_off = buffer0_off;
749 buffer0_off += s_g1_len;
750 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
751 buffer0 + buffer0_off,
752 512 - buffer0_off, &s_x1_pk_len ) );
753 s_x1_pk_off = buffer0_off;
754 buffer0_off += s_x1_pk_len;
755 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
756 buffer0 + buffer0_off,
757 512 - buffer0_off, &s_x1_pr_len ) );
758 s_x1_pr_off = buffer0_off;
759 buffer0_off += s_x1_pr_len;
760 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
761 buffer0 + buffer0_off,
762 512 - buffer0_off, &s_g2_len ) );
763 s_g2_off = buffer0_off;
764 buffer0_off += s_g2_len;
765 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
766 buffer0 + buffer0_off,
767 512 - buffer0_off, &s_x2_pk_len ) );
768 s_x2_pk_off = buffer0_off;
769 buffer0_off += s_x2_pk_len;
770 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
771 buffer0 + buffer0_off,
772 512 - buffer0_off, &s_x2_pr_len ) );
773 s_x2_pr_off = buffer0_off;
774 buffer0_off += s_x2_pr_len;
775
776 if( inject_error == 1 )
777 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +0200778 buffer0[s_x1_pk_off + 8] >>= 4;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200779 buffer0[s_x2_pk_off + 7] <<= 4;
780 expected_status = PSA_ERROR_DATA_INVALID;
781 }
782
Neil Armstrong51009d72022-09-05 17:59:54 +0200783 /*
784 * When injecting errors in inputs, the implementation is
785 * free to detect it right away of with a delay.
786 * This permits delaying the error until the end of the input
787 * sequence, if no error appears then, this will be treated
788 * as an error.
789 */
790
Neil Armstrongf983caf2022-06-15 15:27:48 +0200791 if( client_input_first == 1 )
792 {
793 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200794 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
795 buffer0 + s_g1_off, s_g1_len );
796 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200797 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200798 TEST_EQUAL( status, expected_status );
799 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200800 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200801 else
802 {
803 TEST_EQUAL( status, PSA_SUCCESS );
804 }
805
806 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
807 buffer0 + s_x1_pk_off,
808 s_x1_pk_len );
809 if( inject_error == 1 && status != PSA_SUCCESS )
810 {
811 TEST_EQUAL( status, expected_status );
812 break;
813 }
814 else
815 {
816 TEST_EQUAL( status, PSA_SUCCESS );
817 }
818
819 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
820 buffer0 + s_x1_pr_off,
821 s_x1_pr_len );
822 if( inject_error == 1 && status != PSA_SUCCESS )
823 {
824 TEST_EQUAL( status, expected_status );
825 break;
826 }
827 else
828 {
829 TEST_EQUAL( status, PSA_SUCCESS );
830 }
831
832 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
833 buffer0 + s_g2_off,
834 s_g2_len );
835 if( inject_error == 1 && status != PSA_SUCCESS )
836 {
837 TEST_EQUAL( status, expected_status );
838 break;
839 }
840 else
841 {
842 TEST_EQUAL( status, PSA_SUCCESS );
843 }
844
845 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
846 buffer0 + s_x2_pk_off,
847 s_x2_pk_len );
848 if( inject_error == 1 && status != PSA_SUCCESS )
849 {
850 TEST_EQUAL( status, expected_status );
851 break;
852 }
853 else
854 {
855 TEST_EQUAL( status, PSA_SUCCESS );
856 }
857
858 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
859 buffer0 + s_x2_pr_off,
860 s_x2_pr_len );
861 if( inject_error == 1 && status != PSA_SUCCESS )
862 {
863 TEST_EQUAL( status, expected_status );
864 break;
865 }
866 else
867 {
868 TEST_EQUAL( status, PSA_SUCCESS );
869 }
870
871 /* Error didn't trigger, exit with error */
872 if( inject_error == 1 )
873 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200874 }
875
876 /* Client first round Output */
877 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
878 buffer1 + buffer1_off,
879 512 - buffer1_off, &c_g1_len ) );
880 c_g1_off = buffer1_off;
881 buffer1_off += c_g1_len;
882 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
883 buffer1 + buffer1_off,
884 512 - buffer1_off, &c_x1_pk_len ) );
885 c_x1_pk_off = buffer1_off;
886 buffer1_off += c_x1_pk_len;
887 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
888 buffer1 + buffer1_off,
889 512 - buffer1_off, &c_x1_pr_len ) );
890 c_x1_pr_off = buffer1_off;
891 buffer1_off += c_x1_pr_len;
892 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
893 buffer1 + buffer1_off,
894 512 - buffer1_off, &c_g2_len ) );
895 c_g2_off = buffer1_off;
896 buffer1_off += c_g2_len;
897 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
898 buffer1 + buffer1_off,
899 512 - buffer1_off, &c_x2_pk_len ) );
900 c_x2_pk_off = buffer1_off;
901 buffer1_off += c_x2_pk_len;
902 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
903 buffer1 + buffer1_off,
904 512 - buffer1_off, &c_x2_pr_len ) );
905 c_x2_pr_off = buffer1_off;
906 buffer1_off += c_x2_pr_len;
907
908 if( client_input_first == 0 )
909 {
910 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200911 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
912 buffer0 + s_g1_off, s_g1_len );
913 if( inject_error == 1 && status != PSA_SUCCESS )
914 {
915 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200916 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200917 }
918 else
919 {
920 TEST_EQUAL( status, PSA_SUCCESS );
921 }
922
923 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
924 buffer0 + s_x1_pk_off,
925 s_x1_pk_len );
926 if( inject_error == 1 && status != PSA_SUCCESS )
927 {
928 TEST_EQUAL( status, expected_status );
929 break;
930 }
931 else
932 {
933 TEST_EQUAL( status, PSA_SUCCESS );
934 }
935
936 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
937 buffer0 + s_x1_pr_off,
938 s_x1_pr_len );
939 if( inject_error == 1 && status != PSA_SUCCESS )
940 {
941 TEST_EQUAL( status, expected_status );
942 break;
943 }
944 else
945 {
946 TEST_EQUAL( status, PSA_SUCCESS );
947 }
948
949 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
950 buffer0 + s_g2_off,
951 s_g2_len );
952 if( inject_error == 1 && status != PSA_SUCCESS )
953 {
954 TEST_EQUAL( status, expected_status );
955 break;
956 }
957 else
958 {
959 TEST_EQUAL( status, PSA_SUCCESS );
960 }
961
962 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
963 buffer0 + s_x2_pk_off,
964 s_x2_pk_len );
965 if( inject_error == 1 && status != PSA_SUCCESS )
966 {
967 TEST_EQUAL( status, expected_status );
968 break;
969 }
970 else
971 {
972 TEST_EQUAL( status, PSA_SUCCESS );
973 }
974
975 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
976 buffer0 + s_x2_pr_off,
977 s_x2_pr_len );
978 if( inject_error == 1 && status != PSA_SUCCESS )
979 {
980 TEST_EQUAL( status, expected_status );
981 break;
982 }
983 else
984 {
985 TEST_EQUAL( status, PSA_SUCCESS );
986 }
987
988 /* Error didn't trigger, exit with error */
989 if( inject_error == 1 )
990 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200991 }
992
993 if( inject_error == 2 )
994 {
995 buffer1[c_x1_pk_off + 12] >>= 4;
996 buffer1[c_x2_pk_off + 7] <<= 4;
997 expected_status = PSA_ERROR_DATA_INVALID;
998 }
999
1000 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001001 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1002 buffer1 + c_g1_off, c_g1_len );
1003 if( inject_error == 2 && status != PSA_SUCCESS )
1004 {
1005 TEST_EQUAL( status, expected_status );
1006 break;
1007 }
1008 else
1009 {
1010 TEST_EQUAL( status, PSA_SUCCESS );
1011 }
1012
1013 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1014 buffer1 + c_x1_pk_off, c_x1_pk_len );
1015 if( inject_error == 2 && status != PSA_SUCCESS )
1016 {
1017 TEST_EQUAL( status, expected_status );
1018 break;
1019 }
1020 else
1021 {
1022 TEST_EQUAL( status, PSA_SUCCESS );
1023 }
1024
1025 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1026 buffer1 + c_x1_pr_off, c_x1_pr_len );
1027 if( inject_error == 2 && status != PSA_SUCCESS )
1028 {
1029 TEST_EQUAL( status, expected_status );
1030 break;
1031 }
1032 else
1033 {
1034 TEST_EQUAL( status, PSA_SUCCESS );
1035 }
1036
1037 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1038 buffer1 + c_g2_off, c_g2_len );
1039 if( inject_error == 2 && status != PSA_SUCCESS )
1040 {
1041 TEST_EQUAL( status, expected_status );
1042 break;
1043 }
1044 else
1045 {
1046 TEST_EQUAL( status, PSA_SUCCESS );
1047 }
1048
1049 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1050 buffer1 + c_x2_pk_off, c_x2_pk_len );
1051 if( inject_error == 2 && status != PSA_SUCCESS )
1052 {
1053 TEST_EQUAL( status, expected_status );
1054 break;
1055 }
1056 else
1057 {
1058 TEST_EQUAL( status, PSA_SUCCESS );
1059 }
1060
1061 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1062 buffer1 + c_x2_pr_off, c_x2_pr_len );
1063 if( inject_error == 2 && status != PSA_SUCCESS )
1064 {
1065 TEST_EQUAL( status, expected_status );
1066 break;
1067 }
1068 else
1069 {
1070 TEST_EQUAL( status, PSA_SUCCESS );
1071 }
1072
1073 /* Error didn't trigger, exit with error */
1074 if( inject_error == 2 )
1075 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001076
1077 break;
1078
1079 case 2:
1080 /* Server second round Output */
1081 buffer0_off = 0;
1082
1083 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1084 buffer0 + buffer0_off,
1085 512 - buffer0_off, &s_a_len ) );
1086 s_a_off = buffer0_off;
1087 buffer0_off += s_a_len;
1088 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1089 buffer0 + buffer0_off,
1090 512 - buffer0_off, &s_x2s_pk_len ) );
1091 s_x2s_pk_off = buffer0_off;
1092 buffer0_off += s_x2s_pk_len;
1093 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1094 buffer0 + buffer0_off,
1095 512 - buffer0_off, &s_x2s_pr_len ) );
1096 s_x2s_pr_off = buffer0_off;
1097 buffer0_off += s_x2s_pr_len;
1098
1099 if( inject_error == 3 )
1100 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001101 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001102 expected_status = PSA_ERROR_DATA_INVALID;
1103 }
1104
1105 if( client_input_first == 1 )
1106 {
1107 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001108 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1109 buffer0 + s_a_off, s_a_len );
1110 if( inject_error == 3 && status != PSA_SUCCESS )
1111 {
1112 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001113 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001114 }
1115 else
1116 {
1117 TEST_EQUAL( status, PSA_SUCCESS );
1118 }
1119
1120 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1121 buffer0 + s_x2s_pk_off,
1122 s_x2s_pk_len );
1123 if( inject_error == 3 && status != PSA_SUCCESS )
1124 {
1125 TEST_EQUAL( status, expected_status );
1126 break;
1127 }
1128 else
1129 {
1130 TEST_EQUAL( status, PSA_SUCCESS );
1131 }
1132
1133 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1134 buffer0 + s_x2s_pr_off,
1135 s_x2s_pr_len );
1136 if( inject_error == 3 && status != PSA_SUCCESS )
1137 {
1138 TEST_EQUAL( status, expected_status );
1139 break;
1140 }
1141 else
1142 {
1143 TEST_EQUAL( status, PSA_SUCCESS );
1144 }
1145
1146 /* Error didn't trigger, exit with error */
1147 if( inject_error == 3 )
1148 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001149 }
1150
1151 /* Client second round Output */
1152 buffer1_off = 0;
1153
1154 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1155 buffer1 + buffer1_off,
1156 512 - buffer1_off, &c_a_len ) );
1157 c_a_off = buffer1_off;
1158 buffer1_off += c_a_len;
1159 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1160 buffer1 + buffer1_off,
1161 512 - buffer1_off, &c_x2s_pk_len ) );
1162 c_x2s_pk_off = buffer1_off;
1163 buffer1_off += c_x2s_pk_len;
1164 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1165 buffer1 + buffer1_off,
1166 512 - buffer1_off, &c_x2s_pr_len ) );
1167 c_x2s_pr_off = buffer1_off;
1168 buffer1_off += c_x2s_pr_len;
1169
1170 if( client_input_first == 0 )
1171 {
1172 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001173 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1174 buffer0 + s_a_off, s_a_len );
1175 if( inject_error == 3 && status != PSA_SUCCESS )
1176 {
1177 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001178 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001179 }
1180 else
1181 {
1182 TEST_EQUAL( status, PSA_SUCCESS );
1183 }
1184
1185 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1186 buffer0 + s_x2s_pk_off,
1187 s_x2s_pk_len );
1188 if( inject_error == 3 && status != PSA_SUCCESS )
1189 {
1190 TEST_EQUAL( status, expected_status );
1191 break;
1192 }
1193 else
1194 {
1195 TEST_EQUAL( status, PSA_SUCCESS );
1196 }
1197
1198 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1199 buffer0 + s_x2s_pr_off,
1200 s_x2s_pr_len );
1201 if( inject_error == 3 && status != PSA_SUCCESS )
1202 {
1203 TEST_EQUAL( status, expected_status );
1204 break;
1205 }
1206 else
1207 {
1208 TEST_EQUAL( status, PSA_SUCCESS );
1209 }
1210
1211 /* Error didn't trigger, exit with error */
1212 if( inject_error == 3 )
1213 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001214 }
1215
1216 if( inject_error == 4 )
1217 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001218 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001219 expected_status = PSA_ERROR_DATA_INVALID;
1220 }
1221
1222 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001223 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1224 buffer1 + c_a_off, c_a_len );
1225 if( inject_error == 4 && status != PSA_SUCCESS )
1226 {
1227 TEST_EQUAL( status, expected_status );
1228 break;
1229 }
1230 else
1231 {
1232 TEST_EQUAL( status, PSA_SUCCESS );
1233 }
1234
1235 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1236 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1237 if( inject_error == 4 && status != PSA_SUCCESS )
1238 {
1239 TEST_EQUAL( status, expected_status );
1240 break;
1241 }
1242 else
1243 {
1244 TEST_EQUAL( status, PSA_SUCCESS );
1245 }
1246
1247 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1248 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1249 if( inject_error == 4 && status != PSA_SUCCESS )
1250 {
1251 TEST_EQUAL( status, expected_status );
1252 break;
1253 }
1254 else
1255 {
1256 TEST_EQUAL( status, PSA_SUCCESS );
1257 }
1258
1259 /* Error didn't trigger, exit with error */
1260 if( inject_error == 4 )
1261 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001262
1263 break;
1264
1265 }
1266
1267 ret = 1;
1268
1269exit:
1270 mbedtls_free( buffer0 );
1271 mbedtls_free( buffer1 );
1272 return( ret );
1273}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001274#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001275
Gilles Peskinee59236f2018-01-27 23:32:46 +01001276/* END_HEADER */
1277
1278/* BEGIN_DEPENDENCIES
1279 * depends_on:MBEDTLS_PSA_CRYPTO_C
1280 * END_DEPENDENCIES
1281 */
1282
1283/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001284void static_checks( )
1285{
1286 size_t max_truncated_mac_size =
1287 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1288
1289 /* Check that the length for a truncated MAC always fits in the algorithm
1290 * encoding. The shifted mask is the maximum truncated value. The
1291 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001292 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001293}
1294/* END_CASE */
1295
1296/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001297void import_with_policy( int type_arg,
1298 int usage_arg, int alg_arg,
1299 int expected_status_arg )
1300{
1301 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1302 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001303 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001304 psa_key_type_t type = type_arg;
1305 psa_key_usage_t usage = usage_arg;
1306 psa_algorithm_t alg = alg_arg;
1307 psa_status_t expected_status = expected_status_arg;
1308 const uint8_t key_material[16] = {0};
1309 psa_status_t status;
1310
1311 PSA_ASSERT( psa_crypto_init( ) );
1312
1313 psa_set_key_type( &attributes, type );
1314 psa_set_key_usage_flags( &attributes, usage );
1315 psa_set_key_algorithm( &attributes, alg );
1316
1317 status = psa_import_key( &attributes,
1318 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001319 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001320 TEST_EQUAL( status, expected_status );
1321 if( status != PSA_SUCCESS )
1322 goto exit;
1323
Ronald Cron5425a212020-08-04 14:58:35 +02001324 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001325 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001326 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001327 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001328 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001329 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001330
Ronald Cron5425a212020-08-04 14:58:35 +02001331 PSA_ASSERT( psa_destroy_key( key ) );
1332 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001333
1334exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001335 /*
1336 * Key attributes may have been returned by psa_get_key_attributes()
1337 * thus reset them as required.
1338 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001339 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001340
1341 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001342 PSA_DONE( );
1343}
1344/* END_CASE */
1345
1346/* BEGIN_CASE */
1347void import_with_data( data_t *data, int type_arg,
1348 int attr_bits_arg,
1349 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001350{
1351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1352 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001353 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001354 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001355 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001356 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001357 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001358
Gilles Peskine8817f612018-12-18 00:18:46 +01001359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360
Gilles Peskine4747d192019-04-17 15:05:45 +02001361 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001362 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001363
Ronald Cron5425a212020-08-04 14:58:35 +02001364 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001365 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001366 if( status != PSA_SUCCESS )
1367 goto exit;
1368
Ronald Cron5425a212020-08-04 14:58:35 +02001369 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001370 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001371 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001372 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001373 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001374
Ronald Cron5425a212020-08-04 14:58:35 +02001375 PSA_ASSERT( psa_destroy_key( key ) );
1376 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001377
1378exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001379 /*
1380 * Key attributes may have been returned by psa_get_key_attributes()
1381 * thus reset them as required.
1382 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001383 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001384
1385 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001386 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001387}
1388/* END_CASE */
1389
1390/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001391void import_large_key( int type_arg, int byte_size_arg,
1392 int expected_status_arg )
1393{
1394 psa_key_type_t type = type_arg;
1395 size_t byte_size = byte_size_arg;
1396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1397 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001398 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001399 psa_status_t status;
1400 uint8_t *buffer = NULL;
1401 size_t buffer_size = byte_size + 1;
1402 size_t n;
1403
Steven Cooreman69967ce2021-01-18 18:01:08 +01001404 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001405 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001406 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001407 memset( buffer, 'K', byte_size );
1408
1409 PSA_ASSERT( psa_crypto_init( ) );
1410
1411 /* Try importing the key */
1412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1413 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001414 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001415 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001416 TEST_EQUAL( status, expected_status );
1417
1418 if( status == PSA_SUCCESS )
1419 {
Ronald Cron5425a212020-08-04 14:58:35 +02001420 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001421 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1422 TEST_EQUAL( psa_get_key_bits( &attributes ),
1423 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001424 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001425 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001426 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001427 for( n = 0; n < byte_size; n++ )
1428 TEST_EQUAL( buffer[n], 'K' );
1429 for( n = byte_size; n < buffer_size; n++ )
1430 TEST_EQUAL( buffer[n], 0 );
1431 }
1432
1433exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001434 /*
1435 * Key attributes may have been returned by psa_get_key_attributes()
1436 * thus reset them as required.
1437 */
1438 psa_reset_key_attributes( &attributes );
1439
Ronald Cron5425a212020-08-04 14:58:35 +02001440 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001441 PSA_DONE( );
1442 mbedtls_free( buffer );
1443}
1444/* END_CASE */
1445
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001446/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001447void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1448{
Ronald Cron5425a212020-08-04 14:58:35 +02001449 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001450 size_t bits = bits_arg;
1451 psa_status_t expected_status = expected_status_arg;
1452 psa_status_t status;
1453 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001454 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001455 size_t buffer_size = /* Slight overapproximations */
1456 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001457 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001458 unsigned char *p;
1459 int ret;
1460 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001461 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001462
Gilles Peskine8817f612018-12-18 00:18:46 +01001463 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001464 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001465
1466 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1467 bits, keypair ) ) >= 0 );
1468 length = ret;
1469
1470 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001471 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001472 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001473 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001474
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001475 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001476 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001477
1478exit:
1479 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001480 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001481}
1482/* END_CASE */
1483
1484/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001485void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001486 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001487 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301488 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489 int expected_bits,
1490 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001491 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492 int canonical_input )
1493{
Ronald Cron5425a212020-08-04 14:58:35 +02001494 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001495 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001496 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001497 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001498 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301499 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500 unsigned char *exported = NULL;
1501 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001503 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001506 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001507
Moran Pekercb088e72018-07-17 17:36:59 +03001508 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001509 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001510 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001511 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513
Archana4d7ae1d2021-07-07 02:50:22 +05301514 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001515 psa_set_key_usage_flags( &attributes, usage_arg );
1516 psa_set_key_algorithm( &attributes, alg );
1517 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001518
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001520 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001521
1522 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001523 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001524 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1525 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001526 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001527
1528 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001529 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001530 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001531
1532 /* The exported length must be set by psa_export_key() to a value between 0
1533 * and export_size. On errors, the exported length must be 0. */
1534 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1535 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001536 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001537
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001538 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001539 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001541 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001542 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001543 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001544 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001545
Gilles Peskineea38a922021-02-13 00:05:16 +01001546 /* Run sanity checks on the exported key. For non-canonical inputs,
1547 * this validates the canonical representations. For canonical inputs,
1548 * this doesn't directly validate the implementation, but it still helps
1549 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301550 if( !psa_key_lifetime_is_external( lifetime ) )
1551 {
1552 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1553 goto exit;
1554 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001555
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001556 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001557 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001558 else
1559 {
Ronald Cron5425a212020-08-04 14:58:35 +02001560 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001561 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001562 &key2 ) );
1563 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001564 reexported,
1565 export_size,
1566 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001567 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301568 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001569 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001570 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001571 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301572 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301573 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001574 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001575
1576destroy:
1577 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001578 PSA_ASSERT( psa_destroy_key( key ) );
1579 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001580
1581exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001582 /*
1583 * Key attributes may have been returned by psa_get_key_attributes()
1584 * thus reset them as required.
1585 */
1586 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301587 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001588 mbedtls_free( exported );
1589 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001590 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591}
1592/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001593
Moran Pekerf709f4a2018-06-06 17:26:04 +03001594/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001595void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001596 int type_arg,
1597 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301598 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001599 int export_size_delta,
1600 int expected_export_status_arg,
1601 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001602{
Ronald Cron5425a212020-08-04 14:58:35 +02001603 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001604 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001605 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001606 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001607 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301608 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001609 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001610 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001611 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001612 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001613
Gilles Peskine8817f612018-12-18 00:18:46 +01001614 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001615
Archana4d7ae1d2021-07-07 02:50:22 +05301616 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1618 psa_set_key_algorithm( &attributes, alg );
1619 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001620
1621 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001622 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001623
Gilles Peskine49c25912018-10-29 15:15:31 +01001624 /* Export the public key */
1625 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001626 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001627 exported, export_size,
1628 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001629 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001630 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001631 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001632 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001633 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001634 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001635 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001636 TEST_LE_U( expected_public_key->len,
1637 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1638 TEST_LE_U( expected_public_key->len,
1639 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1640 TEST_LE_U( expected_public_key->len,
1641 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001642 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1643 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001644 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001645exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001646 /*
1647 * Key attributes may have been returned by psa_get_key_attributes()
1648 * thus reset them as required.
1649 */
1650 psa_reset_key_attributes( &attributes );
1651
itayzafrir3e02b3b2018-06-12 17:06:52 +03001652 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001653 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001654 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001655}
1656/* END_CASE */
1657
Gilles Peskine20035e32018-02-03 22:44:14 +01001658/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001659void import_and_exercise_key( data_t *data,
1660 int type_arg,
1661 int bits_arg,
1662 int alg_arg )
1663{
Ronald Cron5425a212020-08-04 14:58:35 +02001664 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001665 psa_key_type_t type = type_arg;
1666 size_t bits = bits_arg;
1667 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001668 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001670 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001671
Gilles Peskine8817f612018-12-18 00:18:46 +01001672 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001673
Gilles Peskine4747d192019-04-17 15:05:45 +02001674 psa_set_key_usage_flags( &attributes, usage );
1675 psa_set_key_algorithm( &attributes, alg );
1676 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001677
1678 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001679 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001680
1681 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001682 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001683 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1684 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001685
1686 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001687 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001688 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689
Ronald Cron5425a212020-08-04 14:58:35 +02001690 PSA_ASSERT( psa_destroy_key( key ) );
1691 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001692
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001693exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001694 /*
1695 * Key attributes may have been returned by psa_get_key_attributes()
1696 * thus reset them as required.
1697 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001698 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001699
1700 psa_reset_key_attributes( &attributes );
1701 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001702 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001703}
1704/* END_CASE */
1705
1706/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001707void effective_key_attributes( int type_arg, int expected_type_arg,
1708 int bits_arg, int expected_bits_arg,
1709 int usage_arg, int expected_usage_arg,
1710 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001711{
Ronald Cron5425a212020-08-04 14:58:35 +02001712 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001713 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001714 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001715 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001716 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001717 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001718 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001719 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001720 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001722
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001724
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001725 psa_set_key_usage_flags( &attributes, usage );
1726 psa_set_key_algorithm( &attributes, alg );
1727 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001728 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001729
Ronald Cron5425a212020-08-04 14:58:35 +02001730 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001731 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001732
Ronald Cron5425a212020-08-04 14:58:35 +02001733 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001734 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1735 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1736 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1737 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001738
1739exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001740 /*
1741 * Key attributes may have been returned by psa_get_key_attributes()
1742 * thus reset them as required.
1743 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001744 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001745
1746 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001747 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001752void check_key_policy( int type_arg, int bits_arg,
1753 int usage_arg, int alg_arg )
1754{
1755 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001756 usage_arg,
1757 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001758 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001759 goto exit;
1760}
1761/* END_CASE */
1762
1763/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001764void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001765{
1766 /* Test each valid way of initializing the object, except for `= {0}`, as
1767 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1768 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001769 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001770 psa_key_attributes_t func = psa_key_attributes_init( );
1771 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1772 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001773
1774 memset( &zero, 0, sizeof( zero ) );
1775
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001776 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1777 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1778 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001779
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001780 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1781 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1782 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1783
1784 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1785 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1786 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1787
1788 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1789 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1790 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1791
1792 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1793 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1794 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001795}
1796/* END_CASE */
1797
1798/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001799void mac_key_policy( int policy_usage_arg,
1800 int policy_alg_arg,
1801 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001802 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001803 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001804 int expected_status_sign_arg,
1805 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001806{
Ronald Cron5425a212020-08-04 14:58:35 +02001807 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001808 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001809 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001810 psa_key_type_t key_type = key_type_arg;
1811 psa_algorithm_t policy_alg = policy_alg_arg;
1812 psa_algorithm_t exercise_alg = exercise_alg_arg;
1813 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001814 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001815 psa_status_t expected_status_sign = expected_status_sign_arg;
1816 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001817 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001818
Gilles Peskine8817f612018-12-18 00:18:46 +01001819 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001820
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001821 psa_set_key_usage_flags( &attributes, policy_usage );
1822 psa_set_key_algorithm( &attributes, policy_alg );
1823 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001824
Gilles Peskine049c7532019-05-15 20:22:09 +02001825 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001826 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001827
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001828 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1829 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001830
Ronald Cron5425a212020-08-04 14:58:35 +02001831 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001832 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001833
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001834 /* Calculate the MAC, one-shot case. */
1835 uint8_t input[128] = {0};
1836 size_t mac_len;
1837 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1838 input, 128,
1839 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1840 expected_status_sign );
1841
Neil Armstrong3af9b972022-02-07 12:20:21 +01001842 /* Calculate the MAC, multi-part case. */
1843 PSA_ASSERT( psa_mac_abort( &operation ) );
1844 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1845 if( status == PSA_SUCCESS )
1846 {
1847 status = psa_mac_update( &operation, input, 128 );
1848 if( status == PSA_SUCCESS )
1849 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1850 &mac_len ),
1851 expected_status_sign );
1852 else
1853 TEST_EQUAL( status, expected_status_sign );
1854 }
1855 else
1856 {
1857 TEST_EQUAL( status, expected_status_sign );
1858 }
1859 PSA_ASSERT( psa_mac_abort( &operation ) );
1860
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001861 /* Verify correct MAC, one-shot case. */
1862 status = psa_mac_verify( key, exercise_alg, input, 128,
1863 mac, mac_len );
1864
1865 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1866 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001867 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001868 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001869
Neil Armstrong3af9b972022-02-07 12:20:21 +01001870 /* Verify correct MAC, multi-part case. */
1871 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1872 if( status == PSA_SUCCESS )
1873 {
1874 status = psa_mac_update( &operation, input, 128 );
1875 if( status == PSA_SUCCESS )
1876 {
1877 status = psa_mac_verify_finish( &operation, mac, mac_len );
1878 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1879 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1880 else
1881 TEST_EQUAL( status, expected_status_verify );
1882 }
1883 else
1884 {
1885 TEST_EQUAL( status, expected_status_verify );
1886 }
1887 }
1888 else
1889 {
1890 TEST_EQUAL( status, expected_status_verify );
1891 }
1892
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001894
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001896 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001897 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001898
1899exit:
1900 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001901 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001902 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001903}
1904/* END_CASE */
1905
1906/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001907void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908 int policy_alg,
1909 int key_type,
1910 data_t *key_data,
1911 int exercise_alg )
1912{
Ronald Cron5425a212020-08-04 14:58:35 +02001913 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001915 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001916 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001917 size_t output_buffer_size = 0;
1918 size_t input_buffer_size = 0;
1919 size_t output_length = 0;
1920 uint8_t *output = NULL;
1921 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922 psa_status_t status;
1923
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001924 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1925 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1926 input_buffer_size );
1927
1928 ASSERT_ALLOC( input, input_buffer_size );
1929 ASSERT_ALLOC( output, output_buffer_size );
1930
Gilles Peskine8817f612018-12-18 00:18:46 +01001931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933 psa_set_key_usage_flags( &attributes, policy_usage );
1934 psa_set_key_algorithm( &attributes, policy_alg );
1935 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001936
Gilles Peskine049c7532019-05-15 20:22:09 +02001937 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001938 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001939
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001940 /* Check if no key usage flag implication is done */
1941 TEST_EQUAL( policy_usage,
1942 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001943
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001944 /* Encrypt check, one-shot */
1945 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1946 output, output_buffer_size,
1947 &output_length);
1948 if( policy_alg == exercise_alg &&
1949 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1950 PSA_ASSERT( status );
1951 else
1952 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1953
1954 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001955 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956 if( policy_alg == exercise_alg &&
1957 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001958 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001960 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961 psa_cipher_abort( &operation );
1962
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001963 /* Decrypt check, one-shot */
1964 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1965 input, input_buffer_size,
1966 &output_length);
1967 if( policy_alg == exercise_alg &&
1968 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1969 PSA_ASSERT( status );
1970 else
1971 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1972
1973 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001974 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975 if( policy_alg == exercise_alg &&
1976 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001977 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001978 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001979 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001980
1981exit:
1982 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001983 mbedtls_free( input );
1984 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001985 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001986 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001987}
1988/* END_CASE */
1989
1990/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001991void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992 int policy_alg,
1993 int key_type,
1994 data_t *key_data,
1995 int nonce_length_arg,
1996 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001997 int exercise_alg,
1998 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001999{
Ronald Cron5425a212020-08-04 14:58:35 +02002000 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002001 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01002002 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002003 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002005 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006 unsigned char nonce[16] = {0};
2007 size_t nonce_length = nonce_length_arg;
2008 unsigned char tag[16];
2009 size_t tag_length = tag_length_arg;
2010 size_t output_length;
2011
Gilles Peskine7be11a72022-04-14 00:12:57 +02002012 TEST_LE_U( nonce_length, sizeof( nonce ) );
2013 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002014
Gilles Peskine8817f612018-12-18 00:18:46 +01002015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002017 psa_set_key_usage_flags( &attributes, policy_usage );
2018 psa_set_key_algorithm( &attributes, policy_alg );
2019 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002020
Gilles Peskine049c7532019-05-15 20:22:09 +02002021 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002022 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002023
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002024 /* Check if no key usage implication is done */
2025 TEST_EQUAL( policy_usage,
2026 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002027
Neil Armstrong752d8112022-02-07 14:51:11 +01002028 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002029 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002030 nonce, nonce_length,
2031 NULL, 0,
2032 NULL, 0,
2033 tag, tag_length,
2034 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002035 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2036 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002037 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002038 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002039
Neil Armstrong752d8112022-02-07 14:51:11 +01002040 /* Encrypt check, multi-part */
2041 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2042 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2043 TEST_EQUAL( status, expected_status );
2044 else
2045 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2046
2047 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002048 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002049 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050 nonce, nonce_length,
2051 NULL, 0,
2052 tag, tag_length,
2053 NULL, 0,
2054 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002055 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2056 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2057 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002058 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002059 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002060 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061
Neil Armstrong752d8112022-02-07 14:51:11 +01002062 /* Decrypt check, multi-part */
2063 PSA_ASSERT( psa_aead_abort( &operation ) );
2064 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2065 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2066 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2067 else
2068 TEST_EQUAL( status, expected_status );
2069
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002071 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002072 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002073 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002074}
2075/* END_CASE */
2076
2077/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002078void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079 int policy_alg,
2080 int key_type,
2081 data_t *key_data,
2082 int exercise_alg )
2083{
Ronald Cron5425a212020-08-04 14:58:35 +02002084 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002086 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002087 psa_status_t status;
2088 size_t key_bits;
2089 size_t buffer_length;
2090 unsigned char *buffer = NULL;
2091 size_t output_length;
2092
Gilles Peskine8817f612018-12-18 00:18:46 +01002093 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002095 psa_set_key_usage_flags( &attributes, policy_usage );
2096 psa_set_key_algorithm( &attributes, policy_alg );
2097 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002098
Gilles Peskine049c7532019-05-15 20:22:09 +02002099 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002100 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002101
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002102 /* Check if no key usage implication is done */
2103 TEST_EQUAL( policy_usage,
2104 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002105
Ronald Cron5425a212020-08-04 14:58:35 +02002106 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2109 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002110 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002111
Ronald Cron5425a212020-08-04 14:58:35 +02002112 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113 NULL, 0,
2114 NULL, 0,
2115 buffer, buffer_length,
2116 &output_length );
2117 if( policy_alg == exercise_alg &&
2118 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002119 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002120 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002121 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002123 if( buffer_length != 0 )
2124 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002125 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002126 buffer, buffer_length,
2127 NULL, 0,
2128 buffer, buffer_length,
2129 &output_length );
2130 if( policy_alg == exercise_alg &&
2131 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002132 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002133 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002134 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002135
2136exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002137 /*
2138 * Key attributes may have been returned by psa_get_key_attributes()
2139 * thus reset them as required.
2140 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002141 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002142
2143 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002144 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002145 mbedtls_free( buffer );
2146}
2147/* END_CASE */
2148
2149/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002150void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002151 int policy_alg,
2152 int key_type,
2153 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002154 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002155 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002156 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002157{
Ronald Cron5425a212020-08-04 14:58:35 +02002158 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002160 psa_key_usage_t policy_usage = policy_usage_arg;
2161 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002162 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002163 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2164 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2165 * compatible with the policy and `payload_length_arg` is supposed to be
2166 * a valid input length to sign. If `payload_length_arg <= 0`,
2167 * `exercise_alg` is supposed to be forbidden by the policy. */
2168 int compatible_alg = payload_length_arg > 0;
2169 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002170 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002171 size_t signature_length;
2172
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002173 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002174 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002175 TEST_EQUAL( expected_usage,
2176 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002177
Gilles Peskine8817f612018-12-18 00:18:46 +01002178 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002180 psa_set_key_usage_flags( &attributes, policy_usage );
2181 psa_set_key_algorithm( &attributes, policy_alg );
2182 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002183
Gilles Peskine049c7532019-05-15 20:22:09 +02002184 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002185 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002186
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002187 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2188
Ronald Cron5425a212020-08-04 14:58:35 +02002189 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002190 payload, payload_length,
2191 signature, sizeof( signature ),
2192 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002193 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002194 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002195 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002196 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002197
2198 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002199 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002200 payload, payload_length,
2201 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002202 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002203 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002204 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002205 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002206
Gilles Peskinef7b41372021-09-22 16:15:05 +02002207 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002208 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002209 {
2210 status = psa_sign_message( key, exercise_alg,
2211 payload, payload_length,
2212 signature, sizeof( signature ),
2213 &signature_length );
2214 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2215 PSA_ASSERT( status );
2216 else
2217 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2218
2219 memset( signature, 0, sizeof( signature ) );
2220 status = psa_verify_message( key, exercise_alg,
2221 payload, payload_length,
2222 signature, sizeof( signature ) );
2223 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2224 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2225 else
2226 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2227 }
2228
Gilles Peskined5b33222018-06-18 22:20:03 +02002229exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002230 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002231 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002232}
2233/* END_CASE */
2234
Janos Follathba3fab92019-06-11 14:50:16 +01002235/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002236void derive_key_policy( int policy_usage,
2237 int policy_alg,
2238 int key_type,
2239 data_t *key_data,
2240 int exercise_alg )
2241{
Ronald Cron5425a212020-08-04 14:58:35 +02002242 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002244 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002245 psa_status_t status;
2246
Gilles Peskine8817f612018-12-18 00:18:46 +01002247 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002249 psa_set_key_usage_flags( &attributes, policy_usage );
2250 psa_set_key_algorithm( &attributes, policy_alg );
2251 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002252
Gilles Peskine049c7532019-05-15 20:22:09 +02002253 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002254 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002255
Janos Follathba3fab92019-06-11 14:50:16 +01002256 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2257
2258 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2259 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002260 {
Janos Follathba3fab92019-06-11 14:50:16 +01002261 PSA_ASSERT( psa_key_derivation_input_bytes(
2262 &operation,
2263 PSA_KEY_DERIVATION_INPUT_SEED,
2264 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002265 }
Janos Follathba3fab92019-06-11 14:50:16 +01002266
2267 status = psa_key_derivation_input_key( &operation,
2268 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002269 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002270
Gilles Peskineea0fb492018-07-12 17:17:20 +02002271 if( policy_alg == exercise_alg &&
2272 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002273 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002274 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002275 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002276
2277exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002278 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002279 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002280 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002285void agreement_key_policy( int policy_usage,
2286 int policy_alg,
2287 int key_type_arg,
2288 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002289 int exercise_alg,
2290 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002291{
Ronald Cron5425a212020-08-04 14:58:35 +02002292 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002294 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002295 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002297 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002298
Gilles Peskine8817f612018-12-18 00:18:46 +01002299 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002301 psa_set_key_usage_flags( &attributes, policy_usage );
2302 psa_set_key_algorithm( &attributes, policy_alg );
2303 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304
Gilles Peskine049c7532019-05-15 20:22:09 +02002305 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002306 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002307
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002308 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002309 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002310
Steven Cooremance48e852020-10-05 16:02:45 +02002311 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002312
2313exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002314 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002315 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002316 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002317}
2318/* END_CASE */
2319
2320/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002321void key_policy_alg2( int key_type_arg, data_t *key_data,
2322 int usage_arg, int alg_arg, int alg2_arg )
2323{
Ronald Cron5425a212020-08-04 14:58:35 +02002324 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002325 psa_key_type_t key_type = key_type_arg;
2326 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2327 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2328 psa_key_usage_t usage = usage_arg;
2329 psa_algorithm_t alg = alg_arg;
2330 psa_algorithm_t alg2 = alg2_arg;
2331
2332 PSA_ASSERT( psa_crypto_init( ) );
2333
2334 psa_set_key_usage_flags( &attributes, usage );
2335 psa_set_key_algorithm( &attributes, alg );
2336 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2337 psa_set_key_type( &attributes, key_type );
2338 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002339 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002340
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002341 /* Update the usage flags to obtain implicit usage flags */
2342 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002343 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002344 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2345 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2346 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2347
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002348 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002349 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002350 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002351 goto exit;
2352
2353exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002354 /*
2355 * Key attributes may have been returned by psa_get_key_attributes()
2356 * thus reset them as required.
2357 */
2358 psa_reset_key_attributes( &got_attributes );
2359
Ronald Cron5425a212020-08-04 14:58:35 +02002360 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002361 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002362}
2363/* END_CASE */
2364
2365/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002366void raw_agreement_key_policy( int policy_usage,
2367 int policy_alg,
2368 int key_type_arg,
2369 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002370 int exercise_alg,
2371 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002372{
Ronald Cron5425a212020-08-04 14:58:35 +02002373 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002374 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002376 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002377 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002378 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002379
2380 PSA_ASSERT( psa_crypto_init( ) );
2381
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002382 psa_set_key_usage_flags( &attributes, policy_usage );
2383 psa_set_key_algorithm( &attributes, policy_alg );
2384 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002385
Gilles Peskine049c7532019-05-15 20:22:09 +02002386 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002387 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002389 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002390
Steven Cooremance48e852020-10-05 16:02:45 +02002391 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002392
2393exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002394 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002395 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002396 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401void copy_success( int source_usage_arg,
2402 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302403 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002404 int type_arg, data_t *material,
2405 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002406 int target_usage_arg,
2407 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302408 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002409 int expected_usage_arg,
2410 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002411{
Gilles Peskineca25db92019-04-19 11:43:08 +02002412 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2413 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002414 psa_key_usage_t expected_usage = expected_usage_arg;
2415 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002416 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302417 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2418 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002419 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2420 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002421 uint8_t *export_buffer = NULL;
2422
Gilles Peskine57ab7212019-01-28 13:03:09 +01002423 PSA_ASSERT( psa_crypto_init( ) );
2424
Gilles Peskineca25db92019-04-19 11:43:08 +02002425 /* Prepare the source key. */
2426 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2427 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002428 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002429 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302430 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002431 PSA_ASSERT( psa_import_key( &source_attributes,
2432 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002433 &source_key ) );
2434 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002435
Gilles Peskineca25db92019-04-19 11:43:08 +02002436 /* Prepare the target attributes. */
2437 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002438 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002439 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002440 }
Archana8a180362021-07-05 02:18:48 +05302441 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002442
Gilles Peskineca25db92019-04-19 11:43:08 +02002443 if( target_usage_arg != -1 )
2444 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2445 if( target_alg_arg != -1 )
2446 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002447 if( target_alg2_arg != -1 )
2448 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449
Archana8a180362021-07-05 02:18:48 +05302450
Gilles Peskine57ab7212019-01-28 13:03:09 +01002451 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002452 PSA_ASSERT( psa_copy_key( source_key,
2453 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002454
2455 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002456 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002457
2458 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002459 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002460 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2461 psa_get_key_type( &target_attributes ) );
2462 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2463 psa_get_key_bits( &target_attributes ) );
2464 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2465 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002466 TEST_EQUAL( expected_alg2,
2467 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002468 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2469 {
2470 size_t length;
2471 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002472 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002473 material->len, &length ) );
2474 ASSERT_COMPARE( material->x, material->len,
2475 export_buffer, length );
2476 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002477
Archana8a180362021-07-05 02:18:48 +05302478 if( !psa_key_lifetime_is_external( target_lifetime ) )
2479 {
2480 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2481 goto exit;
2482 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2483 goto exit;
2484 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002485
Ronald Cron5425a212020-08-04 14:58:35 +02002486 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002487
2488exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002489 /*
2490 * Source and target key attributes may have been returned by
2491 * psa_get_key_attributes() thus reset them as required.
2492 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002493 psa_reset_key_attributes( &source_attributes );
2494 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002495
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002496 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002497 mbedtls_free( export_buffer );
2498}
2499/* END_CASE */
2500
2501/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002502void copy_fail( int source_usage_arg,
2503 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302504 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002505 int type_arg, data_t *material,
2506 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002507 int target_usage_arg,
2508 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002509 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002510 int expected_status_arg )
2511{
2512 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2513 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002514 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2515 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002516 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002517
2518 PSA_ASSERT( psa_crypto_init( ) );
2519
2520 /* Prepare the source key. */
2521 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2522 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002523 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002524 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302525 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002526 PSA_ASSERT( psa_import_key( &source_attributes,
2527 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002528 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002529
2530 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002531 psa_set_key_id( &target_attributes, key_id );
2532 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002533 psa_set_key_type( &target_attributes, target_type_arg );
2534 psa_set_key_bits( &target_attributes, target_bits_arg );
2535 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2536 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002537 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002538
2539 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002540 TEST_EQUAL( psa_copy_key( source_key,
2541 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002542 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002543
Ronald Cron5425a212020-08-04 14:58:35 +02002544 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002545
Gilles Peskine4a644642019-05-03 17:14:08 +02002546exit:
2547 psa_reset_key_attributes( &source_attributes );
2548 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002549 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002550}
2551/* END_CASE */
2552
2553/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002554void hash_operation_init( )
2555{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002556 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002557 /* Test each valid way of initializing the object, except for `= {0}`, as
2558 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2559 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002560 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002561 psa_hash_operation_t func = psa_hash_operation_init( );
2562 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2563 psa_hash_operation_t zero;
2564
2565 memset( &zero, 0, sizeof( zero ) );
2566
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002567 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002568 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2569 PSA_ERROR_BAD_STATE );
2570 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2571 PSA_ERROR_BAD_STATE );
2572 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2573 PSA_ERROR_BAD_STATE );
2574
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002575 /* A default hash operation should be abortable without error. */
2576 PSA_ASSERT( psa_hash_abort( &func ) );
2577 PSA_ASSERT( psa_hash_abort( &init ) );
2578 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002579}
2580/* END_CASE */
2581
2582/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002583void hash_setup( int alg_arg,
2584 int expected_status_arg )
2585{
2586 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002587 uint8_t *output = NULL;
2588 size_t output_size = 0;
2589 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002590 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002591 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002592 psa_status_t status;
2593
Gilles Peskine8817f612018-12-18 00:18:46 +01002594 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002595
Neil Armstrongedb20862022-02-07 15:47:44 +01002596 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002597 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002598 ASSERT_ALLOC( output, output_size );
2599
2600 status = psa_hash_compute( alg, NULL, 0,
2601 output, output_size, &output_length );
2602 TEST_EQUAL( status, expected_status );
2603
2604 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002605 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002606 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002608 /* Whether setup succeeded or failed, abort must succeed. */
2609 PSA_ASSERT( psa_hash_abort( &operation ) );
2610
2611 /* If setup failed, reproduce the failure, so as to
2612 * test the resulting state of the operation object. */
2613 if( status != PSA_SUCCESS )
2614 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2615
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002616 /* Now the operation object should be reusable. */
2617#if defined(KNOWN_SUPPORTED_HASH_ALG)
2618 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2619 PSA_ASSERT( psa_hash_abort( &operation ) );
2620#endif
2621
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002622exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002623 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002624 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625}
2626/* END_CASE */
2627
2628/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002629void hash_compute_fail( int alg_arg, data_t *input,
2630 int output_size_arg, int expected_status_arg )
2631{
2632 psa_algorithm_t alg = alg_arg;
2633 uint8_t *output = NULL;
2634 size_t output_size = output_size_arg;
2635 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002636 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002637 psa_status_t expected_status = expected_status_arg;
2638 psa_status_t status;
2639
2640 ASSERT_ALLOC( output, output_size );
2641
2642 PSA_ASSERT( psa_crypto_init( ) );
2643
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002644 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002645 status = psa_hash_compute( alg, input->x, input->len,
2646 output, output_size, &output_length );
2647 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002648 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002649
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002650 /* Hash Compute, multi-part */
2651 status = psa_hash_setup( &operation, alg );
2652 if( status == PSA_SUCCESS )
2653 {
2654 status = psa_hash_update( &operation, input->x, input->len );
2655 if( status == PSA_SUCCESS )
2656 {
2657 status = psa_hash_finish( &operation, output, output_size,
2658 &output_length );
2659 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002660 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002661 else
2662 TEST_EQUAL( status, expected_status );
2663 }
2664 else
2665 {
2666 TEST_EQUAL( status, expected_status );
2667 }
2668 }
2669 else
2670 {
2671 TEST_EQUAL( status, expected_status );
2672 }
2673
Gilles Peskine0a749c82019-11-28 19:33:58 +01002674exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002675 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002676 mbedtls_free( output );
2677 PSA_DONE( );
2678}
2679/* END_CASE */
2680
2681/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002682void hash_compare_fail( int alg_arg, data_t *input,
2683 data_t *reference_hash,
2684 int expected_status_arg )
2685{
2686 psa_algorithm_t alg = alg_arg;
2687 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002688 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002689 psa_status_t status;
2690
2691 PSA_ASSERT( psa_crypto_init( ) );
2692
Neil Armstrong55a1be12022-02-07 11:23:20 +01002693 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002694 status = psa_hash_compare( alg, input->x, input->len,
2695 reference_hash->x, reference_hash->len );
2696 TEST_EQUAL( status, expected_status );
2697
Neil Armstrong55a1be12022-02-07 11:23:20 +01002698 /* Hash Compare, multi-part */
2699 status = psa_hash_setup( &operation, alg );
2700 if( status == PSA_SUCCESS )
2701 {
2702 status = psa_hash_update( &operation, input->x, input->len );
2703 if( status == PSA_SUCCESS )
2704 {
2705 status = psa_hash_verify( &operation, reference_hash->x,
2706 reference_hash->len );
2707 TEST_EQUAL( status, expected_status );
2708 }
2709 else
2710 {
2711 TEST_EQUAL( status, expected_status );
2712 }
2713 }
2714 else
2715 {
2716 TEST_EQUAL( status, expected_status );
2717 }
2718
Gilles Peskine88e08462020-01-28 20:43:00 +01002719exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002720 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002721 PSA_DONE( );
2722}
2723/* END_CASE */
2724
2725/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002726void hash_compute_compare( int alg_arg, data_t *input,
2727 data_t *expected_output )
2728{
2729 psa_algorithm_t alg = alg_arg;
2730 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2731 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002732 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002733 size_t i;
2734
2735 PSA_ASSERT( psa_crypto_init( ) );
2736
Neil Armstrongca30a002022-02-07 11:40:23 +01002737 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002738 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002739 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002740 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002741 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002742 ASSERT_COMPARE( output, output_length,
2743 expected_output->x, expected_output->len );
2744
Neil Armstrongca30a002022-02-07 11:40:23 +01002745 /* Compute with tight buffer, multi-part */
2746 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2747 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2748 PSA_ASSERT( psa_hash_finish( &operation, output,
2749 PSA_HASH_LENGTH( alg ),
2750 &output_length ) );
2751 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2752 ASSERT_COMPARE( output, output_length,
2753 expected_output->x, expected_output->len );
2754
2755 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002756 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2757 output, sizeof( output ),
2758 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002759 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002760 ASSERT_COMPARE( output, output_length,
2761 expected_output->x, expected_output->len );
2762
Neil Armstrongca30a002022-02-07 11:40:23 +01002763 /* Compute with larger buffer, multi-part */
2764 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2765 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2766 PSA_ASSERT( psa_hash_finish( &operation, output,
2767 sizeof( output ), &output_length ) );
2768 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2769 ASSERT_COMPARE( output, output_length,
2770 expected_output->x, expected_output->len );
2771
2772 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002773 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2774 output, output_length ) );
2775
Neil Armstrongca30a002022-02-07 11:40:23 +01002776 /* Compare with correct hash, multi-part */
2777 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2778 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2779 PSA_ASSERT( psa_hash_verify( &operation, output,
2780 output_length ) );
2781
2782 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002783 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2784 output, output_length + 1 ),
2785 PSA_ERROR_INVALID_SIGNATURE );
2786
Neil Armstrongca30a002022-02-07 11:40:23 +01002787 /* Compare with trailing garbage, multi-part */
2788 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2789 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2790 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2791 PSA_ERROR_INVALID_SIGNATURE );
2792
2793 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002794 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2795 output, output_length - 1 ),
2796 PSA_ERROR_INVALID_SIGNATURE );
2797
Neil Armstrongca30a002022-02-07 11:40:23 +01002798 /* Compare with truncated hash, multi-part */
2799 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2800 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2801 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2802 PSA_ERROR_INVALID_SIGNATURE );
2803
Gilles Peskine0a749c82019-11-28 19:33:58 +01002804 /* Compare with corrupted value */
2805 for( i = 0; i < output_length; i++ )
2806 {
Chris Jones9634bb12021-01-20 15:56:42 +00002807 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002808 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002809
2810 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002811 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2812 output, output_length ),
2813 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002814
2815 /* Multi-Part */
2816 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2817 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2818 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2819 PSA_ERROR_INVALID_SIGNATURE );
2820
Gilles Peskine0a749c82019-11-28 19:33:58 +01002821 output[i] ^= 1;
2822 }
2823
2824exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002825 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002826 PSA_DONE( );
2827}
2828/* END_CASE */
2829
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002830/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002831void hash_bad_order( )
2832{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002833 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002834 unsigned char input[] = "";
2835 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002836 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002837 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2838 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2839 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002840 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002841 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002842 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002843
Gilles Peskine8817f612018-12-18 00:18:46 +01002844 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002845
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002846 /* Call setup twice in a row. */
2847 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002848 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002849 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2850 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002851 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002852 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002853 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002854
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002855 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002856 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002857 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002858 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002859
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002860 /* Check that update calls abort on error. */
2861 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002862 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002863 ASSERT_OPERATION_IS_ACTIVE( operation );
2864 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2865 PSA_ERROR_BAD_STATE );
2866 ASSERT_OPERATION_IS_INACTIVE( operation );
2867 PSA_ASSERT( psa_hash_abort( &operation ) );
2868 ASSERT_OPERATION_IS_INACTIVE( operation );
2869
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002870 /* Call update after finish. */
2871 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2872 PSA_ASSERT( psa_hash_finish( &operation,
2873 hash, sizeof( hash ), &hash_len ) );
2874 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002875 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002876 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002877
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002878 /* Call verify without calling setup beforehand. */
2879 TEST_EQUAL( psa_hash_verify( &operation,
2880 valid_hash, sizeof( valid_hash ) ),
2881 PSA_ERROR_BAD_STATE );
2882 PSA_ASSERT( psa_hash_abort( &operation ) );
2883
2884 /* Call verify after finish. */
2885 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2886 PSA_ASSERT( psa_hash_finish( &operation,
2887 hash, sizeof( hash ), &hash_len ) );
2888 TEST_EQUAL( psa_hash_verify( &operation,
2889 valid_hash, sizeof( valid_hash ) ),
2890 PSA_ERROR_BAD_STATE );
2891 PSA_ASSERT( psa_hash_abort( &operation ) );
2892
2893 /* Call verify twice in a row. */
2894 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002895 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002896 PSA_ASSERT( psa_hash_verify( &operation,
2897 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002898 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002899 TEST_EQUAL( psa_hash_verify( &operation,
2900 valid_hash, sizeof( valid_hash ) ),
2901 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002902 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002903 PSA_ASSERT( psa_hash_abort( &operation ) );
2904
2905 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002906 TEST_EQUAL( psa_hash_finish( &operation,
2907 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002908 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002909 PSA_ASSERT( psa_hash_abort( &operation ) );
2910
2911 /* Call finish twice in a row. */
2912 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2913 PSA_ASSERT( psa_hash_finish( &operation,
2914 hash, sizeof( hash ), &hash_len ) );
2915 TEST_EQUAL( psa_hash_finish( &operation,
2916 hash, sizeof( hash ), &hash_len ),
2917 PSA_ERROR_BAD_STATE );
2918 PSA_ASSERT( psa_hash_abort( &operation ) );
2919
2920 /* Call finish after calling verify. */
2921 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2922 PSA_ASSERT( psa_hash_verify( &operation,
2923 valid_hash, sizeof( valid_hash ) ) );
2924 TEST_EQUAL( psa_hash_finish( &operation,
2925 hash, sizeof( hash ), &hash_len ),
2926 PSA_ERROR_BAD_STATE );
2927 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002928
2929exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002930 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002931}
2932/* END_CASE */
2933
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002934/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002935void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002936{
2937 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002938 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2939 * appended to it */
2940 unsigned char hash[] = {
2941 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2942 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2943 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002944 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002945 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002946
Gilles Peskine8817f612018-12-18 00:18:46 +01002947 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002948
itayzafrir27e69452018-11-01 14:26:34 +02002949 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002950 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002951 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002952 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002953 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002954 ASSERT_OPERATION_IS_INACTIVE( operation );
2955 PSA_ASSERT( psa_hash_abort( &operation ) );
2956 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002957
itayzafrir27e69452018-11-01 14:26:34 +02002958 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002960 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002961 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002962
itayzafrir27e69452018-11-01 14:26:34 +02002963 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002964 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002965 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002966 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002967
itayzafrirec93d302018-10-18 18:01:10 +03002968exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002969 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002970}
2971/* END_CASE */
2972
Ronald Cronee414c72021-03-18 18:50:08 +01002973/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002974void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002975{
2976 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002977 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002978 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002979 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002980 size_t hash_len;
2981
Gilles Peskine8817f612018-12-18 00:18:46 +01002982 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002983
itayzafrir58028322018-10-25 10:22:01 +03002984 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002985 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002986 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002987 hash, expected_size - 1, &hash_len ),
2988 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002989
2990exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002991 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002992}
2993/* END_CASE */
2994
Ronald Cronee414c72021-03-18 18:50:08 +01002995/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002996void hash_clone_source_state( )
2997{
2998 psa_algorithm_t alg = PSA_ALG_SHA_256;
2999 unsigned char hash[PSA_HASH_MAX_SIZE];
3000 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
3001 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3002 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3003 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3004 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3005 size_t hash_len;
3006
3007 PSA_ASSERT( psa_crypto_init( ) );
3008 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3009
3010 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3011 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3012 PSA_ASSERT( psa_hash_finish( &op_finished,
3013 hash, sizeof( hash ), &hash_len ) );
3014 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3015 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3016
3017 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3018 PSA_ERROR_BAD_STATE );
3019
3020 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3021 PSA_ASSERT( psa_hash_finish( &op_init,
3022 hash, sizeof( hash ), &hash_len ) );
3023 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3024 PSA_ASSERT( psa_hash_finish( &op_finished,
3025 hash, sizeof( hash ), &hash_len ) );
3026 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3027 PSA_ASSERT( psa_hash_finish( &op_aborted,
3028 hash, sizeof( hash ), &hash_len ) );
3029
3030exit:
3031 psa_hash_abort( &op_source );
3032 psa_hash_abort( &op_init );
3033 psa_hash_abort( &op_setup );
3034 psa_hash_abort( &op_finished );
3035 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003036 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037}
3038/* END_CASE */
3039
Ronald Cronee414c72021-03-18 18:50:08 +01003040/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003041void hash_clone_target_state( )
3042{
3043 psa_algorithm_t alg = PSA_ALG_SHA_256;
3044 unsigned char hash[PSA_HASH_MAX_SIZE];
3045 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3046 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3047 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3048 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3049 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3050 size_t hash_len;
3051
3052 PSA_ASSERT( psa_crypto_init( ) );
3053
3054 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3055 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3056 PSA_ASSERT( psa_hash_finish( &op_finished,
3057 hash, sizeof( hash ), &hash_len ) );
3058 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3059 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3060
3061 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3062 PSA_ASSERT( psa_hash_finish( &op_target,
3063 hash, sizeof( hash ), &hash_len ) );
3064
3065 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3066 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3067 PSA_ERROR_BAD_STATE );
3068 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3069 PSA_ERROR_BAD_STATE );
3070
3071exit:
3072 psa_hash_abort( &op_target );
3073 psa_hash_abort( &op_init );
3074 psa_hash_abort( &op_setup );
3075 psa_hash_abort( &op_finished );
3076 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003077 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003078}
3079/* END_CASE */
3080
itayzafrir58028322018-10-25 10:22:01 +03003081/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003082void mac_operation_init( )
3083{
Jaeden Amero252ef282019-02-15 14:05:35 +00003084 const uint8_t input[1] = { 0 };
3085
Jaeden Amero769ce272019-01-04 11:48:03 +00003086 /* Test each valid way of initializing the object, except for `= {0}`, as
3087 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3088 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003089 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003090 psa_mac_operation_t func = psa_mac_operation_init( );
3091 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3092 psa_mac_operation_t zero;
3093
3094 memset( &zero, 0, sizeof( zero ) );
3095
Jaeden Amero252ef282019-02-15 14:05:35 +00003096 /* A freshly-initialized MAC operation should not be usable. */
3097 TEST_EQUAL( psa_mac_update( &func,
3098 input, sizeof( input ) ),
3099 PSA_ERROR_BAD_STATE );
3100 TEST_EQUAL( psa_mac_update( &init,
3101 input, sizeof( input ) ),
3102 PSA_ERROR_BAD_STATE );
3103 TEST_EQUAL( psa_mac_update( &zero,
3104 input, sizeof( input ) ),
3105 PSA_ERROR_BAD_STATE );
3106
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003107 /* A default MAC operation should be abortable without error. */
3108 PSA_ASSERT( psa_mac_abort( &func ) );
3109 PSA_ASSERT( psa_mac_abort( &init ) );
3110 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003111}
3112/* END_CASE */
3113
3114/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003115void mac_setup( int key_type_arg,
3116 data_t *key,
3117 int alg_arg,
3118 int expected_status_arg )
3119{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003120 psa_key_type_t key_type = key_type_arg;
3121 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003122 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003123 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003124 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3125#if defined(KNOWN_SUPPORTED_MAC_ALG)
3126 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3127#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003128
Gilles Peskine8817f612018-12-18 00:18:46 +01003129 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003130
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003131 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3132 &operation, &status ) )
3133 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003134 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003135
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003136 /* The operation object should be reusable. */
3137#if defined(KNOWN_SUPPORTED_MAC_ALG)
3138 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3139 smoke_test_key_data,
3140 sizeof( smoke_test_key_data ),
3141 KNOWN_SUPPORTED_MAC_ALG,
3142 &operation, &status ) )
3143 goto exit;
3144 TEST_EQUAL( status, PSA_SUCCESS );
3145#endif
3146
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003147exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003148 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003149}
3150/* END_CASE */
3151
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003152/* 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 +00003153void mac_bad_order( )
3154{
Ronald Cron5425a212020-08-04 14:58:35 +02003155 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003156 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3157 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003158 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003159 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3160 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3161 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003163 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3164 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3165 size_t sign_mac_length = 0;
3166 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3167 const uint8_t verify_mac[] = {
3168 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3169 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3170 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3171
3172 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003173 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003174 psa_set_key_algorithm( &attributes, alg );
3175 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003176
Ronald Cron5425a212020-08-04 14:58:35 +02003177 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3178 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003179
Jaeden Amero252ef282019-02-15 14:05:35 +00003180 /* Call update without calling setup beforehand. */
3181 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3182 PSA_ERROR_BAD_STATE );
3183 PSA_ASSERT( psa_mac_abort( &operation ) );
3184
3185 /* Call sign finish without calling setup beforehand. */
3186 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3187 &sign_mac_length),
3188 PSA_ERROR_BAD_STATE );
3189 PSA_ASSERT( psa_mac_abort( &operation ) );
3190
3191 /* Call verify finish without calling setup beforehand. */
3192 TEST_EQUAL( psa_mac_verify_finish( &operation,
3193 verify_mac, sizeof( verify_mac ) ),
3194 PSA_ERROR_BAD_STATE );
3195 PSA_ASSERT( psa_mac_abort( &operation ) );
3196
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003197 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003198 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003199 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003200 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003201 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003202 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003203 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003204 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003205
Jaeden Amero252ef282019-02-15 14:05:35 +00003206 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003207 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003208 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3209 PSA_ASSERT( psa_mac_sign_finish( &operation,
3210 sign_mac, sizeof( sign_mac ),
3211 &sign_mac_length ) );
3212 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3213 PSA_ERROR_BAD_STATE );
3214 PSA_ASSERT( psa_mac_abort( &operation ) );
3215
3216 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003217 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003218 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3219 PSA_ASSERT( psa_mac_verify_finish( &operation,
3220 verify_mac, sizeof( verify_mac ) ) );
3221 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3222 PSA_ERROR_BAD_STATE );
3223 PSA_ASSERT( psa_mac_abort( &operation ) );
3224
3225 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003226 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003227 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3228 PSA_ASSERT( psa_mac_sign_finish( &operation,
3229 sign_mac, sizeof( sign_mac ),
3230 &sign_mac_length ) );
3231 TEST_EQUAL( psa_mac_sign_finish( &operation,
3232 sign_mac, sizeof( sign_mac ),
3233 &sign_mac_length ),
3234 PSA_ERROR_BAD_STATE );
3235 PSA_ASSERT( psa_mac_abort( &operation ) );
3236
3237 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003238 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003239 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3240 PSA_ASSERT( psa_mac_verify_finish( &operation,
3241 verify_mac, sizeof( verify_mac ) ) );
3242 TEST_EQUAL( psa_mac_verify_finish( &operation,
3243 verify_mac, sizeof( verify_mac ) ),
3244 PSA_ERROR_BAD_STATE );
3245 PSA_ASSERT( psa_mac_abort( &operation ) );
3246
3247 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003248 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003249 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003250 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003251 TEST_EQUAL( psa_mac_verify_finish( &operation,
3252 verify_mac, sizeof( verify_mac ) ),
3253 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003254 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003255 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003256 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003257
3258 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003259 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003260 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003261 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003262 TEST_EQUAL( psa_mac_sign_finish( &operation,
3263 sign_mac, sizeof( sign_mac ),
3264 &sign_mac_length ),
3265 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003266 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003267 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003268 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003269
Ronald Cron5425a212020-08-04 14:58:35 +02003270 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003271
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003272exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003273 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003274}
3275/* END_CASE */
3276
3277/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003278void mac_sign_verify_multi( int key_type_arg,
3279 data_t *key_data,
3280 int alg_arg,
3281 data_t *input,
3282 int is_verify,
3283 data_t *expected_mac )
3284{
3285 size_t data_part_len = 0;
3286
3287 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3288 {
3289 /* Split data into length(data_part_len) parts. */
3290 mbedtls_test_set_step( 2000 + data_part_len );
3291
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003292 if( mac_multipart_internal_func( key_type_arg, key_data,
3293 alg_arg,
3294 input, data_part_len,
3295 expected_mac,
3296 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003297 break;
3298
3299 /* length(0) part, length(data_part_len) part, length(0) part... */
3300 mbedtls_test_set_step( 3000 + data_part_len );
3301
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003302 if( mac_multipart_internal_func( key_type_arg, key_data,
3303 alg_arg,
3304 input, data_part_len,
3305 expected_mac,
3306 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003307 break;
3308 }
3309
3310 /* Goto is required to silence warnings about unused labels, as we
3311 * don't actually do any test assertions in this function. */
3312 goto exit;
3313}
3314/* END_CASE */
3315
3316/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003317void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003318 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003319 int alg_arg,
3320 data_t *input,
3321 data_t *expected_mac )
3322{
Ronald Cron5425a212020-08-04 14:58:35 +02003323 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003324 psa_key_type_t key_type = key_type_arg;
3325 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003326 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003327 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003328 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003329 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003330 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003331 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003332 const size_t output_sizes_to_test[] = {
3333 0,
3334 1,
3335 expected_mac->len - 1,
3336 expected_mac->len,
3337 expected_mac->len + 1,
3338 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003339
Gilles Peskine7be11a72022-04-14 00:12:57 +02003340 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003341 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003342 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003343
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003345
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003347 psa_set_key_algorithm( &attributes, alg );
3348 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003349
Ronald Cron5425a212020-08-04 14:58:35 +02003350 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3351 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003352
Gilles Peskine8b356b52020-08-25 23:44:59 +02003353 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3354 {
3355 const size_t output_size = output_sizes_to_test[i];
3356 psa_status_t expected_status =
3357 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3358 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003359
Chris Jones9634bb12021-01-20 15:56:42 +00003360 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003361 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003362
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003363 /* Calculate the MAC, one-shot case. */
3364 TEST_EQUAL( psa_mac_compute( key, alg,
3365 input->x, input->len,
3366 actual_mac, output_size, &mac_length ),
3367 expected_status );
3368 if( expected_status == PSA_SUCCESS )
3369 {
3370 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3371 actual_mac, mac_length );
3372 }
3373
3374 if( output_size > 0 )
3375 memset( actual_mac, 0, output_size );
3376
3377 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003378 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003379 PSA_ASSERT( psa_mac_update( &operation,
3380 input->x, input->len ) );
3381 TEST_EQUAL( psa_mac_sign_finish( &operation,
3382 actual_mac, output_size,
3383 &mac_length ),
3384 expected_status );
3385 PSA_ASSERT( psa_mac_abort( &operation ) );
3386
3387 if( expected_status == PSA_SUCCESS )
3388 {
3389 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3390 actual_mac, mac_length );
3391 }
3392 mbedtls_free( actual_mac );
3393 actual_mac = NULL;
3394 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003395
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003396exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003397 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003398 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003399 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003400 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003401}
3402/* END_CASE */
3403
3404/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003405void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003406 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003407 int alg_arg,
3408 data_t *input,
3409 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003410{
Ronald Cron5425a212020-08-04 14:58:35 +02003411 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003412 psa_key_type_t key_type = key_type_arg;
3413 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003414 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003416 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003417
Gilles Peskine7be11a72022-04-14 00:12:57 +02003418 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003419
Gilles Peskine8817f612018-12-18 00:18:46 +01003420 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003421
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003422 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003423 psa_set_key_algorithm( &attributes, alg );
3424 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003425
Ronald Cron5425a212020-08-04 14:58:35 +02003426 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3427 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003428
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003429 /* Verify correct MAC, one-shot case. */
3430 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3431 expected_mac->x, expected_mac->len ) );
3432
3433 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003434 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003435 PSA_ASSERT( psa_mac_update( &operation,
3436 input->x, input->len ) );
3437 PSA_ASSERT( psa_mac_verify_finish( &operation,
3438 expected_mac->x,
3439 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003440
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003441 /* Test a MAC that's too short, one-shot case. */
3442 TEST_EQUAL( psa_mac_verify( key, alg,
3443 input->x, input->len,
3444 expected_mac->x,
3445 expected_mac->len - 1 ),
3446 PSA_ERROR_INVALID_SIGNATURE );
3447
3448 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003449 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003450 PSA_ASSERT( psa_mac_update( &operation,
3451 input->x, input->len ) );
3452 TEST_EQUAL( psa_mac_verify_finish( &operation,
3453 expected_mac->x,
3454 expected_mac->len - 1 ),
3455 PSA_ERROR_INVALID_SIGNATURE );
3456
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003457 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003458 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3459 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003460 TEST_EQUAL( psa_mac_verify( key, alg,
3461 input->x, input->len,
3462 perturbed_mac, expected_mac->len + 1 ),
3463 PSA_ERROR_INVALID_SIGNATURE );
3464
3465 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003466 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003467 PSA_ASSERT( psa_mac_update( &operation,
3468 input->x, input->len ) );
3469 TEST_EQUAL( psa_mac_verify_finish( &operation,
3470 perturbed_mac,
3471 expected_mac->len + 1 ),
3472 PSA_ERROR_INVALID_SIGNATURE );
3473
3474 /* Test changing one byte. */
3475 for( size_t i = 0; i < expected_mac->len; i++ )
3476 {
Chris Jones9634bb12021-01-20 15:56:42 +00003477 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003478 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003479
3480 TEST_EQUAL( psa_mac_verify( key, alg,
3481 input->x, input->len,
3482 perturbed_mac, expected_mac->len ),
3483 PSA_ERROR_INVALID_SIGNATURE );
3484
Ronald Cron5425a212020-08-04 14:58:35 +02003485 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003486 PSA_ASSERT( psa_mac_update( &operation,
3487 input->x, input->len ) );
3488 TEST_EQUAL( psa_mac_verify_finish( &operation,
3489 perturbed_mac,
3490 expected_mac->len ),
3491 PSA_ERROR_INVALID_SIGNATURE );
3492 perturbed_mac[i] ^= 1;
3493 }
3494
Gilles Peskine8c9def32018-02-08 10:02:12 +01003495exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003496 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003497 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003498 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003499 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003500}
3501/* END_CASE */
3502
3503/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003504void cipher_operation_init( )
3505{
Jaeden Ameroab439972019-02-15 14:12:05 +00003506 const uint8_t input[1] = { 0 };
3507 unsigned char output[1] = { 0 };
3508 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003509 /* Test each valid way of initializing the object, except for `= {0}`, as
3510 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3511 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003512 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003513 psa_cipher_operation_t func = psa_cipher_operation_init( );
3514 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3515 psa_cipher_operation_t zero;
3516
3517 memset( &zero, 0, sizeof( zero ) );
3518
Jaeden Ameroab439972019-02-15 14:12:05 +00003519 /* A freshly-initialized cipher operation should not be usable. */
3520 TEST_EQUAL( psa_cipher_update( &func,
3521 input, sizeof( input ),
3522 output, sizeof( output ),
3523 &output_length ),
3524 PSA_ERROR_BAD_STATE );
3525 TEST_EQUAL( psa_cipher_update( &init,
3526 input, sizeof( input ),
3527 output, sizeof( output ),
3528 &output_length ),
3529 PSA_ERROR_BAD_STATE );
3530 TEST_EQUAL( psa_cipher_update( &zero,
3531 input, sizeof( input ),
3532 output, sizeof( output ),
3533 &output_length ),
3534 PSA_ERROR_BAD_STATE );
3535
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003536 /* A default cipher operation should be abortable without error. */
3537 PSA_ASSERT( psa_cipher_abort( &func ) );
3538 PSA_ASSERT( psa_cipher_abort( &init ) );
3539 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003540}
3541/* END_CASE */
3542
3543/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003544void cipher_setup( int key_type_arg,
3545 data_t *key,
3546 int alg_arg,
3547 int expected_status_arg )
3548{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003549 psa_key_type_t key_type = key_type_arg;
3550 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003551 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003552 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003553 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003554#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003555 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3556#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003557
Gilles Peskine8817f612018-12-18 00:18:46 +01003558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003559
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003560 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3561 &operation, &status ) )
3562 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003563 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003564
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003565 /* The operation object should be reusable. */
3566#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3567 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3568 smoke_test_key_data,
3569 sizeof( smoke_test_key_data ),
3570 KNOWN_SUPPORTED_CIPHER_ALG,
3571 &operation, &status ) )
3572 goto exit;
3573 TEST_EQUAL( status, PSA_SUCCESS );
3574#endif
3575
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003576exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003577 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003578 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003579}
3580/* END_CASE */
3581
Ronald Cronee414c72021-03-18 18:50:08 +01003582/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003583void cipher_bad_order( )
3584{
Ronald Cron5425a212020-08-04 14:58:35 +02003585 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003586 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3587 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003589 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003590 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003591 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003592 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3593 0xaa, 0xaa, 0xaa, 0xaa };
3594 const uint8_t text[] = {
3595 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3596 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003597 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003598 size_t length = 0;
3599
3600 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003601 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3602 psa_set_key_algorithm( &attributes, alg );
3603 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003604 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3605 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003606
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003607 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003608 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003609 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003610 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003611 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003612 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003613 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003614 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003615
3616 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003617 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003618 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003619 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003620 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003621 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003622 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003623 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003624
Jaeden Ameroab439972019-02-15 14:12:05 +00003625 /* Generate an IV without calling setup beforehand. */
3626 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3627 buffer, sizeof( buffer ),
3628 &length ),
3629 PSA_ERROR_BAD_STATE );
3630 PSA_ASSERT( psa_cipher_abort( &operation ) );
3631
3632 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003633 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003634 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3635 buffer, sizeof( buffer ),
3636 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003637 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003638 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3639 buffer, sizeof( buffer ),
3640 &length ),
3641 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003642 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003643 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003644 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003645
3646 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003647 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003648 PSA_ASSERT( psa_cipher_set_iv( &operation,
3649 iv, sizeof( iv ) ) );
3650 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3651 buffer, sizeof( buffer ),
3652 &length ),
3653 PSA_ERROR_BAD_STATE );
3654 PSA_ASSERT( psa_cipher_abort( &operation ) );
3655
3656 /* Set an IV without calling setup beforehand. */
3657 TEST_EQUAL( psa_cipher_set_iv( &operation,
3658 iv, sizeof( iv ) ),
3659 PSA_ERROR_BAD_STATE );
3660 PSA_ASSERT( psa_cipher_abort( &operation ) );
3661
3662 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003663 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003664 PSA_ASSERT( psa_cipher_set_iv( &operation,
3665 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003666 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003667 TEST_EQUAL( psa_cipher_set_iv( &operation,
3668 iv, sizeof( iv ) ),
3669 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003670 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003671 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003672 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003673
3674 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003675 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003676 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3677 buffer, sizeof( buffer ),
3678 &length ) );
3679 TEST_EQUAL( psa_cipher_set_iv( &operation,
3680 iv, sizeof( iv ) ),
3681 PSA_ERROR_BAD_STATE );
3682 PSA_ASSERT( psa_cipher_abort( &operation ) );
3683
3684 /* Call update without calling setup beforehand. */
3685 TEST_EQUAL( psa_cipher_update( &operation,
3686 text, sizeof( text ),
3687 buffer, sizeof( buffer ),
3688 &length ),
3689 PSA_ERROR_BAD_STATE );
3690 PSA_ASSERT( psa_cipher_abort( &operation ) );
3691
3692 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003693 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003694 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003695 TEST_EQUAL( psa_cipher_update( &operation,
3696 text, sizeof( text ),
3697 buffer, sizeof( buffer ),
3698 &length ),
3699 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003700 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003701 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003702 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003703
3704 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003705 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003706 PSA_ASSERT( psa_cipher_set_iv( &operation,
3707 iv, sizeof( iv ) ) );
3708 PSA_ASSERT( psa_cipher_finish( &operation,
3709 buffer, sizeof( buffer ), &length ) );
3710 TEST_EQUAL( psa_cipher_update( &operation,
3711 text, sizeof( text ),
3712 buffer, sizeof( buffer ),
3713 &length ),
3714 PSA_ERROR_BAD_STATE );
3715 PSA_ASSERT( psa_cipher_abort( &operation ) );
3716
3717 /* Call finish without calling setup beforehand. */
3718 TEST_EQUAL( psa_cipher_finish( &operation,
3719 buffer, sizeof( buffer ), &length ),
3720 PSA_ERROR_BAD_STATE );
3721 PSA_ASSERT( psa_cipher_abort( &operation ) );
3722
3723 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003724 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003725 /* Not calling update means we are encrypting an empty buffer, which is OK
3726 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003727 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003728 TEST_EQUAL( psa_cipher_finish( &operation,
3729 buffer, sizeof( buffer ), &length ),
3730 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003731 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003732 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003733 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003734
3735 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003736 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003737 PSA_ASSERT( psa_cipher_set_iv( &operation,
3738 iv, sizeof( iv ) ) );
3739 PSA_ASSERT( psa_cipher_finish( &operation,
3740 buffer, sizeof( buffer ), &length ) );
3741 TEST_EQUAL( psa_cipher_finish( &operation,
3742 buffer, sizeof( buffer ), &length ),
3743 PSA_ERROR_BAD_STATE );
3744 PSA_ASSERT( psa_cipher_abort( &operation ) );
3745
Ronald Cron5425a212020-08-04 14:58:35 +02003746 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003747
Jaeden Ameroab439972019-02-15 14:12:05 +00003748exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003749 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003750 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003751}
3752/* END_CASE */
3753
3754/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003755void cipher_encrypt_fail( int alg_arg,
3756 int key_type_arg,
3757 data_t *key_data,
3758 data_t *input,
3759 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003760{
Ronald Cron5425a212020-08-04 14:58:35 +02003761 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003762 psa_status_t status;
3763 psa_key_type_t key_type = key_type_arg;
3764 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003765 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003766 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3767 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3768 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003769 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003770 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003771 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003772 size_t function_output_length;
3773 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003774 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3775
3776 if ( PSA_ERROR_BAD_STATE != expected_status )
3777 {
3778 PSA_ASSERT( psa_crypto_init( ) );
3779
3780 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3781 psa_set_key_algorithm( &attributes, alg );
3782 psa_set_key_type( &attributes, key_type );
3783
3784 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3785 input->len );
3786 ASSERT_ALLOC( output, output_buffer_size );
3787
3788 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3789 &key ) );
3790 }
3791
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003792 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003793 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3794 output_buffer_size, &output_length );
3795
3796 TEST_EQUAL( status, expected_status );
3797
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003798 /* Encrypt, multi-part */
3799 status = psa_cipher_encrypt_setup( &operation, key, alg );
3800 if( status == PSA_SUCCESS )
3801 {
3802 if( alg != PSA_ALG_ECB_NO_PADDING )
3803 {
3804 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3805 iv, iv_size,
3806 &iv_length ) );
3807 }
3808
3809 status = psa_cipher_update( &operation, input->x, input->len,
3810 output, output_buffer_size,
3811 &function_output_length );
3812 if( status == PSA_SUCCESS )
3813 {
3814 output_length += function_output_length;
3815
3816 status = psa_cipher_finish( &operation, output + output_length,
3817 output_buffer_size - output_length,
3818 &function_output_length );
3819
3820 TEST_EQUAL( status, expected_status );
3821 }
3822 else
3823 {
3824 TEST_EQUAL( status, expected_status );
3825 }
3826 }
3827 else
3828 {
3829 TEST_EQUAL( status, expected_status );
3830 }
3831
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003832exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003833 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003834 mbedtls_free( output );
3835 psa_destroy_key( key );
3836 PSA_DONE( );
3837}
3838/* END_CASE */
3839
3840/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003841void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3842 data_t *input, int iv_length,
3843 int expected_result )
3844{
3845 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3846 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3848 size_t output_buffer_size = 0;
3849 unsigned char *output = NULL;
3850
3851 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3852 ASSERT_ALLOC( output, output_buffer_size );
3853
3854 PSA_ASSERT( psa_crypto_init( ) );
3855
3856 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3857 psa_set_key_algorithm( &attributes, alg );
3858 psa_set_key_type( &attributes, key_type );
3859
3860 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3861 &key ) );
3862 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3863 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3864 iv_length ) );
3865
3866exit:
3867 psa_cipher_abort( &operation );
3868 mbedtls_free( output );
3869 psa_destroy_key( key );
3870 PSA_DONE( );
3871}
3872/* END_CASE */
3873
3874/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003875void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3876 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003877{
3878 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3879 psa_key_type_t key_type = key_type_arg;
3880 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003881 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3882 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003883 unsigned char *output = NULL;
3884 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003885 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3887
3888 PSA_ASSERT( psa_crypto_init( ) );
3889
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003890 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003891 TEST_LE_U( ciphertext->len,
3892 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3893 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003894 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003895 TEST_LE_U( plaintext->len,
3896 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3897 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3898 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003899
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003900
3901 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003902 psa_set_key_usage_flags( &attributes,
3903 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003904 psa_set_key_algorithm( &attributes, alg );
3905 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003906 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3907 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003908 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3909 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003910 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003911
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003912 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003913 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3914 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3915 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003916 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3917 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003918 PSA_ERROR_BAD_STATE );
3919
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003920 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003921 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3922 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003923 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003924 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003925 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3926 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003927 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003928 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003929
Gilles Peskine286c3142022-04-20 17:09:38 +02003930 /* Multipart encryption */
3931 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3932 output_length = 0;
3933 length = ~0;
3934 PSA_ASSERT( psa_cipher_update( &operation,
3935 plaintext->x, plaintext->len,
3936 output, output_buffer_size,
3937 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003938 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003939 output_length += length;
3940 PSA_ASSERT( psa_cipher_finish( &operation,
3941 output + output_length,
3942 output_buffer_size - output_length,
3943 &length ) );
3944 output_length += length;
3945 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003946 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003947
Gilles Peskine286c3142022-04-20 17:09:38 +02003948 /* Multipart encryption */
3949 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3950 output_length = 0;
3951 length = ~0;
3952 PSA_ASSERT( psa_cipher_update( &operation,
3953 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003954 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003955 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003956 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003957 output_length += length;
3958 PSA_ASSERT( psa_cipher_finish( &operation,
3959 output + output_length,
3960 output_buffer_size - output_length,
3961 &length ) );
3962 output_length += length;
3963 ASSERT_COMPARE( plaintext->x, plaintext->len,
3964 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003965
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003966 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003967 output_length = ~0;
3968 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3969 output, output_buffer_size,
3970 &output_length ) );
3971 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3972 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003973
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003974 /* One-shot decryption */
3975 output_length = ~0;
3976 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
3977 output, output_buffer_size,
3978 &output_length ) );
3979 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003980 output, output_length );
3981
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003982exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003983 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003984 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003985 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003986 psa_destroy_key( key );
3987 PSA_DONE( );
3988}
3989/* END_CASE */
3990
3991/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003992void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3993{
3994 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3995 psa_algorithm_t alg = alg_arg;
3996 psa_key_type_t key_type = key_type_arg;
3997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3998 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3999 psa_status_t status;
4000
4001 PSA_ASSERT( psa_crypto_init( ) );
4002
4003 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4004 psa_set_key_algorithm( &attributes, alg );
4005 psa_set_key_type( &attributes, key_type );
4006
4007 /* Usage of either of these two size macros would cause divide by zero
4008 * with incorrect key types previously. Input length should be irrelevant
4009 * here. */
4010 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4011 0 );
4012 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4013
4014
4015 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4016 &key ) );
4017
4018 /* Should fail due to invalid alg type (to support invalid key type).
4019 * Encrypt or decrypt will end up in the same place. */
4020 status = psa_cipher_encrypt_setup( &operation, key, alg );
4021
4022 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4023
4024exit:
4025 psa_cipher_abort( &operation );
4026 psa_destroy_key( key );
4027 PSA_DONE( );
4028}
4029/* END_CASE */
4030
4031/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004032void cipher_encrypt_validation( int alg_arg,
4033 int key_type_arg,
4034 data_t *key_data,
4035 data_t *input )
4036{
4037 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4038 psa_key_type_t key_type = key_type_arg;
4039 psa_algorithm_t alg = alg_arg;
4040 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4041 unsigned char *output1 = NULL;
4042 size_t output1_buffer_size = 0;
4043 size_t output1_length = 0;
4044 unsigned char *output2 = NULL;
4045 size_t output2_buffer_size = 0;
4046 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004047 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004048 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004050
Gilles Peskine8817f612018-12-18 00:18:46 +01004051 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004052
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004053 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4054 psa_set_key_algorithm( &attributes, alg );
4055 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004056
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004057 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4058 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4059 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4060 ASSERT_ALLOC( output1, output1_buffer_size );
4061 ASSERT_ALLOC( output2, output2_buffer_size );
4062
Ronald Cron5425a212020-08-04 14:58:35 +02004063 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4064 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004065
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004066 /* The one-shot cipher encryption uses generated iv so validating
4067 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004068 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4069 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004070 TEST_LE_U( output1_length,
4071 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4072 TEST_LE_U( output1_length,
4073 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004074
4075 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4076 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004077
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 PSA_ASSERT( psa_cipher_update( &operation,
4079 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004080 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004081 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004082 TEST_LE_U( function_output_length,
4083 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4084 TEST_LE_U( function_output_length,
4085 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004086 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004087
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004088 PSA_ASSERT( psa_cipher_finish( &operation,
4089 output2 + output2_length,
4090 output2_buffer_size - output2_length,
4091 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004092 TEST_LE_U( function_output_length,
4093 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4094 TEST_LE_U( function_output_length,
4095 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004096 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004097
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004098 PSA_ASSERT( psa_cipher_abort( &operation ) );
4099 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4100 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004101
Gilles Peskine50e586b2018-06-08 14:28:46 +02004102exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004103 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004104 mbedtls_free( output1 );
4105 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004106 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004107 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004108}
4109/* END_CASE */
4110
4111/* BEGIN_CASE */
4112void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004113 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004114 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004115 int first_part_size_arg,
4116 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004117 data_t *expected_output,
4118 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004119{
Ronald Cron5425a212020-08-04 14:58:35 +02004120 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004121 psa_key_type_t key_type = key_type_arg;
4122 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004123 psa_status_t status;
4124 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004125 size_t first_part_size = first_part_size_arg;
4126 size_t output1_length = output1_length_arg;
4127 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004128 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004129 size_t output_buffer_size = 0;
4130 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004131 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004132 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004134
Gilles Peskine8817f612018-12-18 00:18:46 +01004135 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4138 psa_set_key_algorithm( &attributes, alg );
4139 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004140
Ronald Cron5425a212020-08-04 14:58:35 +02004141 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4142 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004143
Ronald Cron5425a212020-08-04 14:58:35 +02004144 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004145
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004146 if( iv->len > 0 )
4147 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004148 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004149 }
4150
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004151 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4152 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004153 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004154
Gilles Peskine7be11a72022-04-14 00:12:57 +02004155 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004156 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4157 output, output_buffer_size,
4158 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004159 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004160 TEST_LE_U( function_output_length,
4161 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4162 TEST_LE_U( function_output_length,
4163 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004164 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004165
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004166 if( first_part_size < input->len )
4167 {
4168 PSA_ASSERT( psa_cipher_update( &operation,
4169 input->x + first_part_size,
4170 input->len - first_part_size,
4171 ( output_buffer_size == 0 ? NULL :
4172 output + total_output_length ),
4173 output_buffer_size - total_output_length,
4174 &function_output_length ) );
4175 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004176 TEST_LE_U( function_output_length,
4177 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4178 alg,
4179 input->len - first_part_size ) );
4180 TEST_LE_U( function_output_length,
4181 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004182 total_output_length += function_output_length;
4183 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004184
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004185 status = psa_cipher_finish( &operation,
4186 ( output_buffer_size == 0 ? NULL :
4187 output + total_output_length ),
4188 output_buffer_size - total_output_length,
4189 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004190 TEST_LE_U( function_output_length,
4191 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4192 TEST_LE_U( function_output_length,
4193 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004194 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004195 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004196
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004197 if( expected_status == PSA_SUCCESS )
4198 {
4199 PSA_ASSERT( psa_cipher_abort( &operation ) );
4200
4201 ASSERT_COMPARE( expected_output->x, expected_output->len,
4202 output, total_output_length );
4203 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004204
4205exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004206 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004207 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004208 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004209 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004210}
4211/* END_CASE */
4212
4213/* BEGIN_CASE */
4214void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004215 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004216 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004217 int first_part_size_arg,
4218 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004219 data_t *expected_output,
4220 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004221{
Ronald Cron5425a212020-08-04 14:58:35 +02004222 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004223 psa_key_type_t key_type = key_type_arg;
4224 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004225 psa_status_t status;
4226 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004227 size_t first_part_size = first_part_size_arg;
4228 size_t output1_length = output1_length_arg;
4229 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004230 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004231 size_t output_buffer_size = 0;
4232 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004233 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004234 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004236
Gilles Peskine8817f612018-12-18 00:18:46 +01004237 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004238
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004239 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4240 psa_set_key_algorithm( &attributes, alg );
4241 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004242
Ronald Cron5425a212020-08-04 14:58:35 +02004243 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4244 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004245
Ronald Cron5425a212020-08-04 14:58:35 +02004246 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004247
Steven Cooreman177deba2020-09-07 17:14:14 +02004248 if( iv->len > 0 )
4249 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004250 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004251 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004252
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004253 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4254 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004255 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004256
Gilles Peskine7be11a72022-04-14 00:12:57 +02004257 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004258 PSA_ASSERT( psa_cipher_update( &operation,
4259 input->x, first_part_size,
4260 output, output_buffer_size,
4261 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004262 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004263 TEST_LE_U( function_output_length,
4264 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4265 TEST_LE_U( function_output_length,
4266 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004267 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004268
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004269 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004270 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004271 PSA_ASSERT( psa_cipher_update( &operation,
4272 input->x + first_part_size,
4273 input->len - first_part_size,
4274 ( output_buffer_size == 0 ? NULL :
4275 output + total_output_length ),
4276 output_buffer_size - total_output_length,
4277 &function_output_length ) );
4278 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004279 TEST_LE_U( function_output_length,
4280 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4281 alg,
4282 input->len - first_part_size ) );
4283 TEST_LE_U( function_output_length,
4284 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004285 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004286 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004287
Gilles Peskine50e586b2018-06-08 14:28:46 +02004288 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004289 ( output_buffer_size == 0 ? NULL :
4290 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004291 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004292 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004293 TEST_LE_U( function_output_length,
4294 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4295 TEST_LE_U( function_output_length,
4296 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004297 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004298 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004299
4300 if( expected_status == PSA_SUCCESS )
4301 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004302 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004303
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004304 ASSERT_COMPARE( expected_output->x, expected_output->len,
4305 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004306 }
4307
Gilles Peskine50e586b2018-06-08 14:28:46 +02004308exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004309 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004310 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004311 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004312 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004313}
4314/* END_CASE */
4315
Gilles Peskine50e586b2018-06-08 14:28:46 +02004316/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004317void cipher_decrypt_fail( int alg_arg,
4318 int key_type_arg,
4319 data_t *key_data,
4320 data_t *iv,
4321 data_t *input_arg,
4322 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004323{
4324 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4325 psa_status_t status;
4326 psa_key_type_t key_type = key_type_arg;
4327 psa_algorithm_t alg = alg_arg;
4328 psa_status_t expected_status = expected_status_arg;
4329 unsigned char *input = NULL;
4330 size_t input_buffer_size = 0;
4331 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004332 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004333 size_t output_buffer_size = 0;
4334 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004335 size_t function_output_length;
4336 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4338
4339 if ( PSA_ERROR_BAD_STATE != expected_status )
4340 {
4341 PSA_ASSERT( psa_crypto_init( ) );
4342
4343 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4344 psa_set_key_algorithm( &attributes, alg );
4345 psa_set_key_type( &attributes, key_type );
4346
4347 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4348 &key ) );
4349 }
4350
4351 /* Allocate input buffer and copy the iv and the plaintext */
4352 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4353 if ( input_buffer_size > 0 )
4354 {
4355 ASSERT_ALLOC( input, input_buffer_size );
4356 memcpy( input, iv->x, iv->len );
4357 memcpy( input + iv->len, input_arg->x, input_arg->len );
4358 }
4359
4360 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4361 ASSERT_ALLOC( output, output_buffer_size );
4362
Neil Armstrong66a479f2022-02-07 15:41:19 +01004363 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004364 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4365 output_buffer_size, &output_length );
4366 TEST_EQUAL( status, expected_status );
4367
Neil Armstrong66a479f2022-02-07 15:41:19 +01004368 /* Decrypt, multi-part */
4369 status = psa_cipher_decrypt_setup( &operation, key, alg );
4370 if( status == PSA_SUCCESS )
4371 {
4372 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4373 input_arg->len ) +
4374 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4375 ASSERT_ALLOC( output_multi, output_buffer_size );
4376
4377 if( iv->len > 0 )
4378 {
4379 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4380
4381 if( status != PSA_SUCCESS )
4382 TEST_EQUAL( status, expected_status );
4383 }
4384
4385 if( status == PSA_SUCCESS )
4386 {
4387 status = psa_cipher_update( &operation,
4388 input_arg->x, input_arg->len,
4389 output_multi, output_buffer_size,
4390 &function_output_length );
4391 if( status == PSA_SUCCESS )
4392 {
4393 output_length = function_output_length;
4394
4395 status = psa_cipher_finish( &operation,
4396 output_multi + output_length,
4397 output_buffer_size - output_length,
4398 &function_output_length );
4399
4400 TEST_EQUAL( status, expected_status );
4401 }
4402 else
4403 {
4404 TEST_EQUAL( status, expected_status );
4405 }
4406 }
4407 else
4408 {
4409 TEST_EQUAL( status, expected_status );
4410 }
4411 }
4412 else
4413 {
4414 TEST_EQUAL( status, expected_status );
4415 }
4416
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004417exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004418 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004419 mbedtls_free( input );
4420 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004421 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004422 psa_destroy_key( key );
4423 PSA_DONE( );
4424}
4425/* END_CASE */
4426
4427/* BEGIN_CASE */
4428void cipher_decrypt( int alg_arg,
4429 int key_type_arg,
4430 data_t *key_data,
4431 data_t *iv,
4432 data_t *input_arg,
4433 data_t *expected_output )
4434{
4435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4436 psa_key_type_t key_type = key_type_arg;
4437 psa_algorithm_t alg = alg_arg;
4438 unsigned char *input = NULL;
4439 size_t input_buffer_size = 0;
4440 unsigned char *output = NULL;
4441 size_t output_buffer_size = 0;
4442 size_t output_length = 0;
4443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4444
4445 PSA_ASSERT( psa_crypto_init( ) );
4446
4447 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4448 psa_set_key_algorithm( &attributes, alg );
4449 psa_set_key_type( &attributes, key_type );
4450
4451 /* Allocate input buffer and copy the iv and the plaintext */
4452 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4453 if ( input_buffer_size > 0 )
4454 {
4455 ASSERT_ALLOC( input, input_buffer_size );
4456 memcpy( input, iv->x, iv->len );
4457 memcpy( input + iv->len, input_arg->x, input_arg->len );
4458 }
4459
4460 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4461 ASSERT_ALLOC( output, output_buffer_size );
4462
4463 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4464 &key ) );
4465
4466 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4467 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004468 TEST_LE_U( output_length,
4469 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4470 TEST_LE_U( output_length,
4471 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004472
4473 ASSERT_COMPARE( expected_output->x, expected_output->len,
4474 output, output_length );
4475exit:
4476 mbedtls_free( input );
4477 mbedtls_free( output );
4478 psa_destroy_key( key );
4479 PSA_DONE( );
4480}
4481/* END_CASE */
4482
4483/* BEGIN_CASE */
4484void cipher_verify_output( int alg_arg,
4485 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004486 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004487 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004488{
Ronald Cron5425a212020-08-04 14:58:35 +02004489 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004490 psa_key_type_t key_type = key_type_arg;
4491 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004492 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004493 size_t output1_size = 0;
4494 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004495 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004496 size_t output2_size = 0;
4497 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004499
Gilles Peskine8817f612018-12-18 00:18:46 +01004500 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004501
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004502 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4503 psa_set_key_algorithm( &attributes, alg );
4504 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004505
Ronald Cron5425a212020-08-04 14:58:35 +02004506 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4507 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004508 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004509 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004510
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004511 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4512 output1, output1_size,
4513 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004514 TEST_LE_U( output1_length,
4515 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4516 TEST_LE_U( output1_length,
4517 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004518
4519 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004520 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004521
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004522 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4523 output2, output2_size,
4524 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004525 TEST_LE_U( output2_length,
4526 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4527 TEST_LE_U( output2_length,
4528 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004529
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004530 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004531
4532exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004533 mbedtls_free( output1 );
4534 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004535 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004536 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004537}
4538/* END_CASE */
4539
4540/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004541void cipher_verify_output_multipart( int alg_arg,
4542 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004543 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004544 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004545 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004546{
Ronald Cron5425a212020-08-04 14:58:35 +02004547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004548 psa_key_type_t key_type = key_type_arg;
4549 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004550 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004551 unsigned char iv[16] = {0};
4552 size_t iv_size = 16;
4553 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004554 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004555 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004556 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004557 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004558 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004559 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004560 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004561 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4562 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004564
Gilles Peskine8817f612018-12-18 00:18:46 +01004565 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004566
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4568 psa_set_key_algorithm( &attributes, alg );
4569 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004570
Ronald Cron5425a212020-08-04 14:58:35 +02004571 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4572 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004573
Ronald Cron5425a212020-08-04 14:58:35 +02004574 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4575 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004576
Steven Cooreman177deba2020-09-07 17:14:14 +02004577 if( alg != PSA_ALG_ECB_NO_PADDING )
4578 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004579 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4580 iv, iv_size,
4581 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004582 }
4583
gabor-mezei-armceface22021-01-21 12:26:17 +01004584 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004585 TEST_LE_U( output1_buffer_size,
4586 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004587 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004588
Gilles Peskine7be11a72022-04-14 00:12:57 +02004589 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004590
Gilles Peskine8817f612018-12-18 00:18:46 +01004591 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4592 output1, output1_buffer_size,
4593 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004594 TEST_LE_U( function_output_length,
4595 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4596 TEST_LE_U( function_output_length,
4597 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004598 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004599
Gilles Peskine8817f612018-12-18 00:18:46 +01004600 PSA_ASSERT( psa_cipher_update( &operation1,
4601 input->x + first_part_size,
4602 input->len - first_part_size,
4603 output1, output1_buffer_size,
4604 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004605 TEST_LE_U( function_output_length,
4606 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4607 alg,
4608 input->len - first_part_size ) );
4609 TEST_LE_U( function_output_length,
4610 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004611 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004612
Gilles Peskine8817f612018-12-18 00:18:46 +01004613 PSA_ASSERT( psa_cipher_finish( &operation1,
4614 output1 + output1_length,
4615 output1_buffer_size - output1_length,
4616 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004617 TEST_LE_U( function_output_length,
4618 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4619 TEST_LE_U( function_output_length,
4620 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004621 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004622
Gilles Peskine8817f612018-12-18 00:18:46 +01004623 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004624
Gilles Peskine048b7f02018-06-08 14:20:49 +02004625 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004626 TEST_LE_U( output2_buffer_size,
4627 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4628 TEST_LE_U( output2_buffer_size,
4629 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004630 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004631
Steven Cooreman177deba2020-09-07 17:14:14 +02004632 if( iv_length > 0 )
4633 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004634 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4635 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004636 }
Moran Pekerded84402018-06-06 16:36:50 +03004637
Gilles Peskine8817f612018-12-18 00:18:46 +01004638 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4639 output2, output2_buffer_size,
4640 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004641 TEST_LE_U( function_output_length,
4642 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4643 TEST_LE_U( function_output_length,
4644 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004645 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004646
Gilles Peskine8817f612018-12-18 00:18:46 +01004647 PSA_ASSERT( psa_cipher_update( &operation2,
4648 output1 + first_part_size,
4649 output1_length - first_part_size,
4650 output2, output2_buffer_size,
4651 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004652 TEST_LE_U( function_output_length,
4653 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4654 alg,
4655 output1_length - first_part_size ) );
4656 TEST_LE_U( function_output_length,
4657 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004658 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004659
Gilles Peskine8817f612018-12-18 00:18:46 +01004660 PSA_ASSERT( psa_cipher_finish( &operation2,
4661 output2 + output2_length,
4662 output2_buffer_size - output2_length,
4663 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004664 TEST_LE_U( function_output_length,
4665 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4666 TEST_LE_U( function_output_length,
4667 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004668 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004669
Gilles Peskine8817f612018-12-18 00:18:46 +01004670 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004671
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004672 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004673
4674exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004675 psa_cipher_abort( &operation1 );
4676 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004677 mbedtls_free( output1 );
4678 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004679 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004680 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004681}
4682/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004683
Gilles Peskine20035e32018-02-03 22:44:14 +01004684/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004685void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004686 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004687 data_t *nonce,
4688 data_t *additional_data,
4689 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004690 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004691{
Ronald Cron5425a212020-08-04 14:58:35 +02004692 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004693 psa_key_type_t key_type = key_type_arg;
4694 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004695 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004696 unsigned char *output_data = NULL;
4697 size_t output_size = 0;
4698 size_t output_length = 0;
4699 unsigned char *output_data2 = NULL;
4700 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004701 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004702 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004703 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004704
Gilles Peskine8817f612018-12-18 00:18:46 +01004705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004706
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004707 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4708 psa_set_key_algorithm( &attributes, alg );
4709 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004710
Gilles Peskine049c7532019-05-15 20:22:09 +02004711 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004712 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004713 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4714 key_bits = psa_get_key_bits( &attributes );
4715
4716 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4717 alg );
4718 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4719 * should be exact. */
4720 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4721 expected_result != PSA_ERROR_NOT_SUPPORTED )
4722 {
4723 TEST_EQUAL( output_size,
4724 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004725 TEST_LE_U( output_size,
4726 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004727 }
4728 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004729
Steven Cooremanf49478b2021-02-15 15:19:25 +01004730 status = psa_aead_encrypt( key, alg,
4731 nonce->x, nonce->len,
4732 additional_data->x,
4733 additional_data->len,
4734 input_data->x, input_data->len,
4735 output_data, output_size,
4736 &output_length );
4737
4738 /* If the operation is not supported, just skip and not fail in case the
4739 * encryption involves a common limitation of cryptography hardwares and
4740 * an alternative implementation. */
4741 if( status == PSA_ERROR_NOT_SUPPORTED )
4742 {
4743 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4744 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4745 }
4746
4747 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004748
4749 if( PSA_SUCCESS == expected_result )
4750 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004751 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004752
Gilles Peskine003a4a92019-05-14 16:09:40 +02004753 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4754 * should be exact. */
4755 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004756 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004757
Gilles Peskine7be11a72022-04-14 00:12:57 +02004758 TEST_LE_U( input_data->len,
4759 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004760
Ronald Cron5425a212020-08-04 14:58:35 +02004761 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004762 nonce->x, nonce->len,
4763 additional_data->x,
4764 additional_data->len,
4765 output_data, output_length,
4766 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004767 &output_length2 ),
4768 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004769
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004770 ASSERT_COMPARE( input_data->x, input_data->len,
4771 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004772 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004773
Gilles Peskinea1cac842018-06-11 19:33:02 +02004774exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004775 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004776 mbedtls_free( output_data );
4777 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004778 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004779}
4780/* END_CASE */
4781
4782/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004783void aead_encrypt( int key_type_arg, data_t *key_data,
4784 int alg_arg,
4785 data_t *nonce,
4786 data_t *additional_data,
4787 data_t *input_data,
4788 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004789{
Ronald Cron5425a212020-08-04 14:58:35 +02004790 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791 psa_key_type_t key_type = key_type_arg;
4792 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004793 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004794 unsigned char *output_data = NULL;
4795 size_t output_size = 0;
4796 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004798 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004799
Gilles Peskine8817f612018-12-18 00:18:46 +01004800 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004801
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004802 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4803 psa_set_key_algorithm( &attributes, alg );
4804 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004805
Gilles Peskine049c7532019-05-15 20:22:09 +02004806 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004807 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004808 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4809 key_bits = psa_get_key_bits( &attributes );
4810
4811 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4812 alg );
4813 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4814 * should be exact. */
4815 TEST_EQUAL( output_size,
4816 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004817 TEST_LE_U( output_size,
4818 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004819 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004820
Steven Cooremand588ea12021-01-11 19:36:04 +01004821 status = psa_aead_encrypt( key, alg,
4822 nonce->x, nonce->len,
4823 additional_data->x, additional_data->len,
4824 input_data->x, input_data->len,
4825 output_data, output_size,
4826 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004827
Ronald Cron28a45ed2021-02-09 20:35:42 +01004828 /* If the operation is not supported, just skip and not fail in case the
4829 * encryption involves a common limitation of cryptography hardwares and
4830 * an alternative implementation. */
4831 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004832 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004833 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4834 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004835 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004836
4837 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004838 ASSERT_COMPARE( expected_result->x, expected_result->len,
4839 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004840
Gilles Peskinea1cac842018-06-11 19:33:02 +02004841exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004842 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004843 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004844 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004845}
4846/* END_CASE */
4847
4848/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004849void aead_decrypt( int key_type_arg, data_t *key_data,
4850 int alg_arg,
4851 data_t *nonce,
4852 data_t *additional_data,
4853 data_t *input_data,
4854 data_t *expected_data,
4855 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004856{
Ronald Cron5425a212020-08-04 14:58:35 +02004857 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004858 psa_key_type_t key_type = key_type_arg;
4859 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004860 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861 unsigned char *output_data = NULL;
4862 size_t output_size = 0;
4863 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004865 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004866 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004867
Gilles Peskine8817f612018-12-18 00:18:46 +01004868 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004870 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4871 psa_set_key_algorithm( &attributes, alg );
4872 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004873
Gilles Peskine049c7532019-05-15 20:22:09 +02004874 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004875 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004876 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4877 key_bits = psa_get_key_bits( &attributes );
4878
4879 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4880 alg );
4881 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4882 expected_result != PSA_ERROR_NOT_SUPPORTED )
4883 {
4884 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4885 * should be exact. */
4886 TEST_EQUAL( output_size,
4887 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004888 TEST_LE_U( output_size,
4889 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004890 }
4891 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004892
Steven Cooremand588ea12021-01-11 19:36:04 +01004893 status = psa_aead_decrypt( key, alg,
4894 nonce->x, nonce->len,
4895 additional_data->x,
4896 additional_data->len,
4897 input_data->x, input_data->len,
4898 output_data, output_size,
4899 &output_length );
4900
Ronald Cron28a45ed2021-02-09 20:35:42 +01004901 /* If the operation is not supported, just skip and not fail in case the
4902 * decryption involves a common limitation of cryptography hardwares and
4903 * an alternative implementation. */
4904 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004905 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004906 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4907 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004908 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004909
4910 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004911
Gilles Peskine2d277862018-06-18 15:41:12 +02004912 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004913 ASSERT_COMPARE( expected_data->x, expected_data->len,
4914 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004915
Gilles Peskinea1cac842018-06-11 19:33:02 +02004916exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004917 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004918 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004919 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004920}
4921/* END_CASE */
4922
4923/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004924void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4925 int alg_arg,
4926 data_t *nonce,
4927 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004928 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004929 int do_set_lengths,
4930 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004931{
Paul Elliottd3f82412021-06-16 16:52:21 +01004932 size_t ad_part_len = 0;
4933 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004934 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004935
Paul Elliott32f46ba2021-09-23 18:24:36 +01004936 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004937 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004938 mbedtls_test_set_step( ad_part_len );
4939
4940 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004941 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004942 if( ad_part_len & 0x01 )
4943 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4944 else
4945 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004946 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004947
4948 /* Split ad into length(ad_part_len) parts. */
4949 if( !aead_multipart_internal_func( key_type_arg, key_data,
4950 alg_arg, nonce,
4951 additional_data,
4952 ad_part_len,
4953 input_data, -1,
4954 set_lengths_method,
4955 expected_output,
4956 1, 0 ) )
4957 break;
4958
4959 /* length(0) part, length(ad_part_len) part, length(0) part... */
4960 mbedtls_test_set_step( 1000 + ad_part_len );
4961
4962 if( !aead_multipart_internal_func( key_type_arg, key_data,
4963 alg_arg, nonce,
4964 additional_data,
4965 ad_part_len,
4966 input_data, -1,
4967 set_lengths_method,
4968 expected_output,
4969 1, 1 ) )
4970 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004972
Paul Elliott32f46ba2021-09-23 18:24:36 +01004973 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004974 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004975 /* Split data into length(data_part_len) parts. */
4976 mbedtls_test_set_step( 2000 + data_part_len );
4977
4978 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004979 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004980 if( data_part_len & 0x01 )
4981 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4982 else
4983 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004984 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004985
Paul Elliott32f46ba2021-09-23 18:24:36 +01004986 if( !aead_multipart_internal_func( key_type_arg, key_data,
4987 alg_arg, nonce,
4988 additional_data, -1,
4989 input_data, data_part_len,
4990 set_lengths_method,
4991 expected_output,
4992 1, 0 ) )
4993 break;
4994
4995 /* length(0) part, length(data_part_len) part, length(0) part... */
4996 mbedtls_test_set_step( 3000 + data_part_len );
4997
4998 if( !aead_multipart_internal_func( key_type_arg, key_data,
4999 alg_arg, nonce,
5000 additional_data, -1,
5001 input_data, data_part_len,
5002 set_lengths_method,
5003 expected_output,
5004 1, 1 ) )
5005 break;
5006 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005007
Paul Elliott8fc45162021-06-23 16:06:01 +01005008 /* Goto is required to silence warnings about unused labels, as we
5009 * don't actually do any test assertions in this function. */
5010 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005011}
5012/* END_CASE */
5013
5014/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005015void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5016 int alg_arg,
5017 data_t *nonce,
5018 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005019 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005020 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005021 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005022{
Paul Elliottd3f82412021-06-16 16:52:21 +01005023 size_t ad_part_len = 0;
5024 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005025 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005026
Paul Elliott32f46ba2021-09-23 18:24:36 +01005027 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005028 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005029 /* Split ad into length(ad_part_len) parts. */
5030 mbedtls_test_set_step( ad_part_len );
5031
5032 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005033 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005034 if( ad_part_len & 0x01 )
5035 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5036 else
5037 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005038 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005039
5040 if( !aead_multipart_internal_func( key_type_arg, key_data,
5041 alg_arg, nonce,
5042 additional_data,
5043 ad_part_len,
5044 input_data, -1,
5045 set_lengths_method,
5046 expected_output,
5047 0, 0 ) )
5048 break;
5049
5050 /* length(0) part, length(ad_part_len) part, length(0) part... */
5051 mbedtls_test_set_step( 1000 + ad_part_len );
5052
5053 if( !aead_multipart_internal_func( key_type_arg, key_data,
5054 alg_arg, nonce,
5055 additional_data,
5056 ad_part_len,
5057 input_data, -1,
5058 set_lengths_method,
5059 expected_output,
5060 0, 1 ) )
5061 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005062 }
5063
Paul Elliott32f46ba2021-09-23 18:24:36 +01005064 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005065 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005066 /* Split data into length(data_part_len) parts. */
5067 mbedtls_test_set_step( 2000 + data_part_len );
5068
5069 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005070 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005071 if( data_part_len & 0x01 )
5072 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5073 else
5074 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005075 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005076
5077 if( !aead_multipart_internal_func( key_type_arg, key_data,
5078 alg_arg, nonce,
5079 additional_data, -1,
5080 input_data, data_part_len,
5081 set_lengths_method,
5082 expected_output,
5083 0, 0 ) )
5084 break;
5085
5086 /* length(0) part, length(data_part_len) part, length(0) part... */
5087 mbedtls_test_set_step( 3000 + data_part_len );
5088
5089 if( !aead_multipart_internal_func( key_type_arg, key_data,
5090 alg_arg, nonce,
5091 additional_data, -1,
5092 input_data, data_part_len,
5093 set_lengths_method,
5094 expected_output,
5095 0, 1 ) )
5096 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005097 }
5098
Paul Elliott8fc45162021-06-23 16:06:01 +01005099 /* Goto is required to silence warnings about unused labels, as we
5100 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005101 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005102}
5103/* END_CASE */
5104
5105/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005106void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5107 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005108 int nonce_length,
5109 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005110 data_t *additional_data,
5111 data_t *input_data,
5112 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005113{
5114
5115 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5116 psa_key_type_t key_type = key_type_arg;
5117 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005118 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005119 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5121 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005122 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005123 size_t actual_nonce_length = 0;
5124 size_t expected_nonce_length = expected_nonce_length_arg;
5125 unsigned char *output = NULL;
5126 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005127 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005128 size_t ciphertext_size = 0;
5129 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005130 size_t tag_length = 0;
5131 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005132
5133 PSA_ASSERT( psa_crypto_init( ) );
5134
5135 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5136 psa_set_key_algorithm( & attributes, alg );
5137 psa_set_key_type( & attributes, key_type );
5138
5139 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5140 &key ) );
5141
5142 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5143
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005144 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5145
Paul Elliottf1277632021-08-24 18:11:37 +01005146 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005147
Paul Elliottf1277632021-08-24 18:11:37 +01005148 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005149
Gilles Peskine7be11a72022-04-14 00:12:57 +02005150 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005151
Paul Elliottf1277632021-08-24 18:11:37 +01005152 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005153
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005154 status = psa_aead_encrypt_setup( &operation, key, alg );
5155
5156 /* If the operation is not supported, just skip and not fail in case the
5157 * encryption involves a common limitation of cryptography hardwares and
5158 * an alternative implementation. */
5159 if( status == PSA_ERROR_NOT_SUPPORTED )
5160 {
5161 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005162 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005163 }
5164
5165 PSA_ASSERT( status );
5166
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005167 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005168 nonce_length,
5169 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005170
Paul Elliott693bf312021-07-23 17:40:41 +01005171 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005172
Paul Elliottf1277632021-08-24 18:11:37 +01005173 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005174
Paul Elliott88ecbe12021-09-22 17:23:03 +01005175 if( expected_status == PSA_SUCCESS )
5176 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5177 alg ) );
5178
Gilles Peskine7be11a72022-04-14 00:12:57 +02005179 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005180
Paul Elliott693bf312021-07-23 17:40:41 +01005181 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005182 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005183 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005184 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5185 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005186
5187 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5188 additional_data->len ) );
5189
5190 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005191 output, output_size,
5192 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005193
Paul Elliottf1277632021-08-24 18:11:37 +01005194 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5195 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005196 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5197 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005198
5199exit:
5200 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005201 mbedtls_free( output );
5202 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005203 psa_aead_abort( &operation );
5204 PSA_DONE( );
5205}
5206/* END_CASE */
5207
5208/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005209void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5210 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005211 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005212 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005213 data_t *additional_data,
5214 data_t *input_data,
5215 int expected_status_arg )
5216{
5217
5218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5219 psa_key_type_t key_type = key_type_arg;
5220 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005221 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005222 uint8_t *nonce_buffer = NULL;
5223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5224 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5225 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005226 unsigned char *output = NULL;
5227 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005228 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005229 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005230 size_t ciphertext_size = 0;
5231 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005232 size_t tag_length = 0;
5233 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005234 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005235 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005236
5237 PSA_ASSERT( psa_crypto_init( ) );
5238
5239 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5240 psa_set_key_algorithm( &attributes, alg );
5241 psa_set_key_type( &attributes, key_type );
5242
5243 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5244 &key ) );
5245
5246 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5247
5248 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5249
Paul Elliott6f0e7202021-08-25 12:57:18 +01005250 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Paul Elliott6f0e7202021-08-25 12:57:18 +01005252 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Gilles Peskine7be11a72022-04-14 00:12:57 +02005254 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005255
Paul Elliott6f0e7202021-08-25 12:57:18 +01005256 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005257
Paul Elliott863864a2021-07-23 17:28:31 +01005258 status = psa_aead_encrypt_setup( &operation, key, alg );
5259
5260 /* If the operation is not supported, just skip and not fail in case the
5261 * encryption involves a common limitation of cryptography hardwares and
5262 * an alternative implementation. */
5263 if( status == PSA_ERROR_NOT_SUPPORTED )
5264 {
5265 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005266 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005267 }
5268
5269 PSA_ASSERT( status );
5270
Paul Elliott4023ffd2021-09-10 16:21:22 +01005271 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5272 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005273 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005274 /* Arbitrary size buffer, to test zero length valid buffer. */
5275 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005276 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005277 }
5278 else
5279 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005280 /* If length is zero, then this will return NULL. */
5281 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005282 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005283
Paul Elliott4023ffd2021-09-10 16:21:22 +01005284 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005285 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005286 for( index = 0; index < nonce_length - 1; ++index )
5287 {
5288 nonce_buffer[index] = 'a' + index;
5289 }
Paul Elliott66696b52021-08-16 18:42:41 +01005290 }
Paul Elliott863864a2021-07-23 17:28:31 +01005291 }
5292
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005293 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5294 {
5295 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5296 input_data->len ) );
5297 }
5298
Paul Elliott6f0e7202021-08-25 12:57:18 +01005299 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005300
Paul Elliott693bf312021-07-23 17:40:41 +01005301 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005302
5303 if( expected_status == PSA_SUCCESS )
5304 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005305 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5306 {
5307 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5308 input_data->len ) );
5309 }
5310 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5311 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005312
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005313 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5314 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5315 additional_data->len ),
5316 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005317
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005318 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005319 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005320 &ciphertext_length ),
5321 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005322
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005323 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005324 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005325 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5326 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005327 }
5328
5329exit:
5330 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005331 mbedtls_free( output );
5332 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005333 mbedtls_free( nonce_buffer );
5334 psa_aead_abort( &operation );
5335 PSA_DONE( );
5336}
5337/* END_CASE */
5338
5339/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005340void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5341 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005342 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005343 data_t *nonce,
5344 data_t *additional_data,
5345 data_t *input_data,
5346 int expected_status_arg )
5347{
5348
5349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5350 psa_key_type_t key_type = key_type_arg;
5351 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005352 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5354 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5355 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005356 unsigned char *output = NULL;
5357 unsigned char *ciphertext = NULL;
5358 size_t output_size = output_size_arg;
5359 size_t ciphertext_size = 0;
5360 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005361 size_t tag_length = 0;
5362 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5363
5364 PSA_ASSERT( psa_crypto_init( ) );
5365
5366 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5367 psa_set_key_algorithm( &attributes, alg );
5368 psa_set_key_type( &attributes, key_type );
5369
5370 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5371 &key ) );
5372
5373 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5374
Paul Elliottc6d11d02021-09-01 12:04:23 +01005375 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005376
Paul Elliottc6d11d02021-09-01 12:04:23 +01005377 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005378
Paul Elliottc6d11d02021-09-01 12:04:23 +01005379 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005380
Paul Elliott43fbda62021-07-23 18:30:59 +01005381 status = psa_aead_encrypt_setup( &operation, key, alg );
5382
5383 /* If the operation is not supported, just skip and not fail in case the
5384 * encryption involves a common limitation of cryptography hardwares and
5385 * an alternative implementation. */
5386 if( status == PSA_ERROR_NOT_SUPPORTED )
5387 {
5388 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5389 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5390 }
5391
5392 PSA_ASSERT( status );
5393
Paul Elliott47b9a142021-10-07 15:04:57 +01005394 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5395 input_data->len ) );
5396
Paul Elliott43fbda62021-07-23 18:30:59 +01005397 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5398
5399 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5400 additional_data->len ) );
5401
5402 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005403 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005404
5405 TEST_EQUAL( status, expected_status );
5406
5407 if( expected_status == PSA_SUCCESS )
5408 {
5409 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005410 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5411 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005412 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5413 }
5414
5415exit:
5416 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005417 mbedtls_free( output );
5418 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005419 psa_aead_abort( &operation );
5420 PSA_DONE( );
5421}
5422/* END_CASE */
5423
Paul Elliott91b021e2021-07-23 18:52:31 +01005424/* BEGIN_CASE */
5425void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5426 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005427 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005428 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005429 data_t *nonce,
5430 data_t *additional_data,
5431 data_t *input_data,
5432 int expected_status_arg )
5433{
5434
5435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5436 psa_key_type_t key_type = key_type_arg;
5437 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005438 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5440 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5441 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005442 unsigned char *ciphertext = NULL;
5443 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005444 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005445 size_t ciphertext_size = 0;
5446 size_t ciphertext_length = 0;
5447 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005448 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005449 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005450
5451 PSA_ASSERT( psa_crypto_init( ) );
5452
5453 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5454 psa_set_key_algorithm( &attributes, alg );
5455 psa_set_key_type( &attributes, key_type );
5456
5457 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5458 &key ) );
5459
5460 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5461
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005462 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005463
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005464 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005465
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005466 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005467
Paul Elliott719c1322021-09-13 18:27:22 +01005468 ASSERT_ALLOC( tag_buffer, tag_size );
5469
Paul Elliott91b021e2021-07-23 18:52:31 +01005470 status = psa_aead_encrypt_setup( &operation, key, alg );
5471
5472 /* If the operation is not supported, just skip and not fail in case the
5473 * encryption involves a common limitation of cryptography hardwares and
5474 * an alternative implementation. */
5475 if( status == PSA_ERROR_NOT_SUPPORTED )
5476 {
5477 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5478 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5479 }
5480
5481 PSA_ASSERT( status );
5482
5483 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5484
Paul Elliott76bda482021-10-07 17:07:23 +01005485 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5486 input_data->len ) );
5487
Paul Elliott91b021e2021-07-23 18:52:31 +01005488 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5489 additional_data->len ) );
5490
5491 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005492 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005493
5494 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005495 status = psa_aead_finish( &operation, finish_ciphertext,
5496 finish_ciphertext_size,
5497 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005498 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005499
5500 TEST_EQUAL( status, expected_status );
5501
5502exit:
5503 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005504 mbedtls_free( ciphertext );
5505 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005506 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005507 psa_aead_abort( &operation );
5508 PSA_DONE( );
5509}
5510/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005511
5512/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005513void aead_multipart_verify( int key_type_arg, data_t *key_data,
5514 int alg_arg,
5515 data_t *nonce,
5516 data_t *additional_data,
5517 data_t *input_data,
5518 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005519 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005520 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005521 int expected_status_arg )
5522{
5523 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5524 psa_key_type_t key_type = key_type_arg;
5525 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005526 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5528 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5529 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005530 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005531 unsigned char *plaintext = NULL;
5532 unsigned char *finish_plaintext = NULL;
5533 size_t plaintext_size = 0;
5534 size_t plaintext_length = 0;
5535 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005536 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005537 unsigned char *tag_buffer = NULL;
5538 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005539
5540 PSA_ASSERT( psa_crypto_init( ) );
5541
5542 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5543 psa_set_key_algorithm( &attributes, alg );
5544 psa_set_key_type( &attributes, key_type );
5545
5546 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5547 &key ) );
5548
5549 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5550
5551 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5552 input_data->len );
5553
5554 ASSERT_ALLOC( plaintext, plaintext_size );
5555
5556 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5557
5558 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5559
Paul Elliott9961a662021-09-17 19:19:02 +01005560 status = psa_aead_decrypt_setup( &operation, key, alg );
5561
5562 /* If the operation is not supported, just skip and not fail in case the
5563 * encryption involves a common limitation of cryptography hardwares and
5564 * an alternative implementation. */
5565 if( status == PSA_ERROR_NOT_SUPPORTED )
5566 {
5567 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5568 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5569 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005570 TEST_EQUAL( status, expected_setup_status );
5571
5572 if( status != PSA_SUCCESS )
5573 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005574
5575 PSA_ASSERT( status );
5576
5577 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5578
Paul Elliottfec6f372021-10-06 17:15:02 +01005579 status = psa_aead_set_lengths( &operation, additional_data->len,
5580 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005581 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005582
Paul Elliott9961a662021-09-17 19:19:02 +01005583 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5584 additional_data->len ) );
5585
5586 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5587 input_data->len,
5588 plaintext, plaintext_size,
5589 &plaintext_length ) );
5590
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005591 if( tag_usage == USE_GIVEN_TAG )
5592 {
5593 tag_buffer = tag->x;
5594 tag_size = tag->len;
5595 }
5596
Paul Elliott9961a662021-09-17 19:19:02 +01005597 status = psa_aead_verify( &operation, finish_plaintext,
5598 verify_plaintext_size,
5599 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005600 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005601
5602 TEST_EQUAL( status, expected_status );
5603
5604exit:
5605 psa_destroy_key( key );
5606 mbedtls_free( plaintext );
5607 mbedtls_free( finish_plaintext );
5608 psa_aead_abort( &operation );
5609 PSA_DONE( );
5610}
5611/* END_CASE */
5612
Paul Elliott9961a662021-09-17 19:19:02 +01005613/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005614void aead_multipart_setup( int key_type_arg, data_t *key_data,
5615 int alg_arg, int expected_status_arg )
5616{
5617 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5618 psa_key_type_t key_type = key_type_arg;
5619 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005620 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5622 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5623 psa_status_t expected_status = expected_status_arg;
5624
5625 PSA_ASSERT( psa_crypto_init( ) );
5626
5627 psa_set_key_usage_flags( &attributes,
5628 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5629 psa_set_key_algorithm( &attributes, alg );
5630 psa_set_key_type( &attributes, key_type );
5631
5632 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5633 &key ) );
5634
Paul Elliott5221ef62021-09-19 17:33:03 +01005635 status = psa_aead_encrypt_setup( &operation, key, alg );
5636
5637 TEST_EQUAL( status, expected_status );
5638
5639 psa_aead_abort( &operation );
5640
Paul Elliott5221ef62021-09-19 17:33:03 +01005641 status = psa_aead_decrypt_setup( &operation, key, alg );
5642
5643 TEST_EQUAL(status, expected_status );
5644
5645exit:
5646 psa_destroy_key( key );
5647 psa_aead_abort( &operation );
5648 PSA_DONE( );
5649}
5650/* END_CASE */
5651
5652/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005653void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5654 int alg_arg,
5655 data_t *nonce,
5656 data_t *additional_data,
5657 data_t *input_data )
5658{
5659 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5660 psa_key_type_t key_type = key_type_arg;
5661 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005662 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005663 unsigned char *output_data = NULL;
5664 unsigned char *final_data = NULL;
5665 size_t output_size = 0;
5666 size_t finish_output_size = 0;
5667 size_t output_length = 0;
5668 size_t key_bits = 0;
5669 size_t tag_length = 0;
5670 size_t tag_size = 0;
5671 size_t nonce_length = 0;
5672 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5673 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5674 size_t output_part_length = 0;
5675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5676
5677 PSA_ASSERT( psa_crypto_init( ) );
5678
5679 psa_set_key_usage_flags( & attributes,
5680 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5681 psa_set_key_algorithm( & attributes, alg );
5682 psa_set_key_type( & attributes, key_type );
5683
5684 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5685 &key ) );
5686
5687 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5688 key_bits = psa_get_key_bits( &attributes );
5689
5690 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5691
Gilles Peskine7be11a72022-04-14 00:12:57 +02005692 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
5694 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5695
5696 ASSERT_ALLOC( output_data, output_size );
5697
5698 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5699
Gilles Peskine7be11a72022-04-14 00:12:57 +02005700 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005701
5702 ASSERT_ALLOC( final_data, finish_output_size );
5703
5704 /* Test all operations error without calling setup first. */
5705
Paul Elliottc23a9a02021-06-21 18:32:46 +01005706 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5707 PSA_ERROR_BAD_STATE );
5708
5709 psa_aead_abort( &operation );
5710
Paul Elliottc23a9a02021-06-21 18:32:46 +01005711 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5712 PSA_AEAD_NONCE_MAX_SIZE,
5713 &nonce_length ),
5714 PSA_ERROR_BAD_STATE );
5715
5716 psa_aead_abort( &operation );
5717
Paul Elliott481be342021-07-16 17:38:47 +01005718 /* ------------------------------------------------------- */
5719
Paul Elliottc23a9a02021-06-21 18:32:46 +01005720 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5721 input_data->len ),
5722 PSA_ERROR_BAD_STATE );
5723
5724 psa_aead_abort( &operation );
5725
Paul Elliott481be342021-07-16 17:38:47 +01005726 /* ------------------------------------------------------- */
5727
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5729 additional_data->len ),
5730 PSA_ERROR_BAD_STATE );
5731
5732 psa_aead_abort( &operation );
5733
Paul Elliott481be342021-07-16 17:38:47 +01005734 /* ------------------------------------------------------- */
5735
Paul Elliottc23a9a02021-06-21 18:32:46 +01005736 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5737 input_data->len, output_data,
5738 output_size, &output_length ),
5739 PSA_ERROR_BAD_STATE );
5740
5741 psa_aead_abort( &operation );
5742
Paul Elliott481be342021-07-16 17:38:47 +01005743 /* ------------------------------------------------------- */
5744
Paul Elliottc23a9a02021-06-21 18:32:46 +01005745 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5746 finish_output_size,
5747 &output_part_length,
5748 tag_buffer, tag_length,
5749 &tag_size ),
5750 PSA_ERROR_BAD_STATE );
5751
5752 psa_aead_abort( &operation );
5753
Paul Elliott481be342021-07-16 17:38:47 +01005754 /* ------------------------------------------------------- */
5755
Paul Elliottc23a9a02021-06-21 18:32:46 +01005756 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5757 finish_output_size,
5758 &output_part_length,
5759 tag_buffer,
5760 tag_length ),
5761 PSA_ERROR_BAD_STATE );
5762
5763 psa_aead_abort( &operation );
5764
5765 /* Test for double setups. */
5766
Paul Elliottc23a9a02021-06-21 18:32:46 +01005767 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5768
5769 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5770 PSA_ERROR_BAD_STATE );
5771
5772 psa_aead_abort( &operation );
5773
Paul Elliott481be342021-07-16 17:38:47 +01005774 /* ------------------------------------------------------- */
5775
Paul Elliottc23a9a02021-06-21 18:32:46 +01005776 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5777
5778 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5779 PSA_ERROR_BAD_STATE );
5780
5781 psa_aead_abort( &operation );
5782
Paul Elliott374a2be2021-07-16 17:53:40 +01005783 /* ------------------------------------------------------- */
5784
Paul Elliott374a2be2021-07-16 17:53:40 +01005785 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5786
5787 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5788 PSA_ERROR_BAD_STATE );
5789
5790 psa_aead_abort( &operation );
5791
5792 /* ------------------------------------------------------- */
5793
Paul Elliott374a2be2021-07-16 17:53:40 +01005794 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5795
5796 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5797 PSA_ERROR_BAD_STATE );
5798
5799 psa_aead_abort( &operation );
5800
Paul Elliottc23a9a02021-06-21 18:32:46 +01005801 /* Test for not setting a nonce. */
5802
Paul Elliottc23a9a02021-06-21 18:32:46 +01005803 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5804
5805 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5806 additional_data->len ),
5807 PSA_ERROR_BAD_STATE );
5808
5809 psa_aead_abort( &operation );
5810
Paul Elliott7f628422021-09-01 12:08:29 +01005811 /* ------------------------------------------------------- */
5812
Paul Elliott7f628422021-09-01 12:08:29 +01005813 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5814
5815 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5816 input_data->len, output_data,
5817 output_size, &output_length ),
5818 PSA_ERROR_BAD_STATE );
5819
5820 psa_aead_abort( &operation );
5821
Paul Elliottbdc2c682021-09-21 18:37:10 +01005822 /* ------------------------------------------------------- */
5823
Paul Elliottbdc2c682021-09-21 18:37:10 +01005824 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5825
5826 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5827 finish_output_size,
5828 &output_part_length,
5829 tag_buffer, tag_length,
5830 &tag_size ),
5831 PSA_ERROR_BAD_STATE );
5832
5833 psa_aead_abort( &operation );
5834
5835 /* ------------------------------------------------------- */
5836
Paul Elliottbdc2c682021-09-21 18:37:10 +01005837 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5838
5839 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5840 finish_output_size,
5841 &output_part_length,
5842 tag_buffer,
5843 tag_length ),
5844 PSA_ERROR_BAD_STATE );
5845
5846 psa_aead_abort( &operation );
5847
Paul Elliottc23a9a02021-06-21 18:32:46 +01005848 /* Test for double setting nonce. */
5849
Paul Elliottc23a9a02021-06-21 18:32:46 +01005850 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5851
5852 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5853
5854 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5855 PSA_ERROR_BAD_STATE );
5856
5857 psa_aead_abort( &operation );
5858
Paul Elliott374a2be2021-07-16 17:53:40 +01005859 /* Test for double generating nonce. */
5860
Paul Elliott374a2be2021-07-16 17:53:40 +01005861 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5862
5863 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5864 PSA_AEAD_NONCE_MAX_SIZE,
5865 &nonce_length ) );
5866
5867 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5868 PSA_AEAD_NONCE_MAX_SIZE,
5869 &nonce_length ),
5870 PSA_ERROR_BAD_STATE );
5871
5872
5873 psa_aead_abort( &operation );
5874
5875 /* Test for generate nonce then set and vice versa */
5876
Paul Elliott374a2be2021-07-16 17:53:40 +01005877 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5878
5879 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5880 PSA_AEAD_NONCE_MAX_SIZE,
5881 &nonce_length ) );
5882
5883 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5884 PSA_ERROR_BAD_STATE );
5885
5886 psa_aead_abort( &operation );
5887
Andrzej Kurekad837522021-12-15 15:28:49 +01005888 /* Test for generating nonce after calling set lengths */
5889
5890 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5891
5892 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5893 input_data->len ) );
5894
5895 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5896 PSA_AEAD_NONCE_MAX_SIZE,
5897 &nonce_length ) );
5898
5899 psa_aead_abort( &operation );
5900
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005901 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005902
5903 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5904
5905 if( operation.alg == PSA_ALG_CCM )
5906 {
5907 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5908 input_data->len ),
5909 PSA_ERROR_INVALID_ARGUMENT );
5910 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5911 PSA_AEAD_NONCE_MAX_SIZE,
5912 &nonce_length ),
5913 PSA_ERROR_BAD_STATE );
5914 }
5915 else
5916 {
5917 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5918 input_data->len ) );
5919 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5920 PSA_AEAD_NONCE_MAX_SIZE,
5921 &nonce_length ) );
5922 }
5923
5924 psa_aead_abort( &operation );
5925
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005926 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005927#if SIZE_MAX > UINT32_MAX
5928 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5929
5930 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5931 {
5932 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5933 input_data->len ),
5934 PSA_ERROR_INVALID_ARGUMENT );
5935 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5936 PSA_AEAD_NONCE_MAX_SIZE,
5937 &nonce_length ),
5938 PSA_ERROR_BAD_STATE );
5939 }
5940 else
5941 {
5942 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5943 input_data->len ) );
5944 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5945 PSA_AEAD_NONCE_MAX_SIZE,
5946 &nonce_length ) );
5947 }
5948
5949 psa_aead_abort( &operation );
5950#endif
5951
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005952 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005953
5954 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5955
5956 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5957 PSA_AEAD_NONCE_MAX_SIZE,
5958 &nonce_length ) );
5959
5960 if( operation.alg == PSA_ALG_CCM )
5961 {
5962 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5963 input_data->len ),
5964 PSA_ERROR_INVALID_ARGUMENT );
5965 }
5966 else
5967 {
5968 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5969 input_data->len ) );
5970 }
5971
5972 psa_aead_abort( &operation );
5973
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005974 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005975 /* Test for setting nonce after calling set lengths */
5976
5977 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5978
5979 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5980 input_data->len ) );
5981
5982 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5983
5984 psa_aead_abort( &operation );
5985
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005986 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005987
5988 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5989
5990 if( operation.alg == PSA_ALG_CCM )
5991 {
5992 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5993 input_data->len ),
5994 PSA_ERROR_INVALID_ARGUMENT );
5995 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5996 PSA_ERROR_BAD_STATE );
5997 }
5998 else
5999 {
6000 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6001 input_data->len ) );
6002 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6003 }
6004
6005 psa_aead_abort( &operation );
6006
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006007 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006008#if SIZE_MAX > UINT32_MAX
6009 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6010
6011 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6012 {
6013 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6014 input_data->len ),
6015 PSA_ERROR_INVALID_ARGUMENT );
6016 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6017 PSA_ERROR_BAD_STATE );
6018 }
6019 else
6020 {
6021 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6022 input_data->len ) );
6023 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6024 }
6025
6026 psa_aead_abort( &operation );
6027#endif
6028
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006029 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006030
6031 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6032
6033 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6034
6035 if( operation.alg == PSA_ALG_CCM )
6036 {
6037 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6038 input_data->len ),
6039 PSA_ERROR_INVALID_ARGUMENT );
6040 }
6041 else
6042 {
6043 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6044 input_data->len ) );
6045 }
6046
6047 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006048
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006049 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006050#if SIZE_MAX > UINT32_MAX
6051 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6052
6053 if( operation.alg == PSA_ALG_GCM )
6054 {
6055 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6056 SIZE_MAX ),
6057 PSA_ERROR_INVALID_ARGUMENT );
6058 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6059 PSA_ERROR_BAD_STATE );
6060 }
6061 else if ( operation.alg != PSA_ALG_CCM )
6062 {
6063 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6064 SIZE_MAX ) );
6065 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6066 }
6067
6068 psa_aead_abort( &operation );
6069
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006070 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006071 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6072
6073 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6074
6075 if( operation.alg == PSA_ALG_GCM )
6076 {
6077 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6078 SIZE_MAX ),
6079 PSA_ERROR_INVALID_ARGUMENT );
6080 }
6081 else if ( operation.alg != PSA_ALG_CCM )
6082 {
6083 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6084 SIZE_MAX ) );
6085 }
6086
6087 psa_aead_abort( &operation );
6088#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006089
6090 /* ------------------------------------------------------- */
6091
Paul Elliott374a2be2021-07-16 17:53:40 +01006092 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6093
6094 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6095
6096 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6097 PSA_AEAD_NONCE_MAX_SIZE,
6098 &nonce_length ),
6099 PSA_ERROR_BAD_STATE );
6100
6101 psa_aead_abort( &operation );
6102
Paul Elliott7220cae2021-06-22 17:25:57 +01006103 /* Test for generating nonce in decrypt setup. */
6104
Paul Elliott7220cae2021-06-22 17:25:57 +01006105 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6106
6107 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6108 PSA_AEAD_NONCE_MAX_SIZE,
6109 &nonce_length ),
6110 PSA_ERROR_BAD_STATE );
6111
6112 psa_aead_abort( &operation );
6113
Paul Elliottc23a9a02021-06-21 18:32:46 +01006114 /* Test for setting lengths twice. */
6115
Paul Elliottc23a9a02021-06-21 18:32:46 +01006116 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6117
6118 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6119
6120 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6121 input_data->len ) );
6122
6123 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6124 input_data->len ),
6125 PSA_ERROR_BAD_STATE );
6126
6127 psa_aead_abort( &operation );
6128
Andrzej Kurekad837522021-12-15 15:28:49 +01006129 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006130
Paul Elliottc23a9a02021-06-21 18:32:46 +01006131 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6132
6133 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6134
Andrzej Kurekad837522021-12-15 15:28:49 +01006135 if( operation.alg == PSA_ALG_CCM )
6136 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006137
Andrzej Kurekad837522021-12-15 15:28:49 +01006138 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6139 additional_data->len ),
6140 PSA_ERROR_BAD_STATE );
6141 }
6142 else
6143 {
6144 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6145 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006146
Andrzej Kurekad837522021-12-15 15:28:49 +01006147 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6148 input_data->len ),
6149 PSA_ERROR_BAD_STATE );
6150 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006151 psa_aead_abort( &operation );
6152
6153 /* ------------------------------------------------------- */
6154
Paul Elliottf94bd992021-09-19 18:15:59 +01006155 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6156
6157 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6158
Andrzej Kurekad837522021-12-15 15:28:49 +01006159 if( operation.alg == PSA_ALG_CCM )
6160 {
6161 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6162 input_data->len, output_data,
6163 output_size, &output_length ),
6164 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006165
Andrzej Kurekad837522021-12-15 15:28:49 +01006166 }
6167 else
6168 {
6169 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6170 input_data->len, output_data,
6171 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006172
Andrzej Kurekad837522021-12-15 15:28:49 +01006173 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6174 input_data->len ),
6175 PSA_ERROR_BAD_STATE );
6176 }
6177 psa_aead_abort( &operation );
6178
6179 /* ------------------------------------------------------- */
6180
6181 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6182
6183 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6184
6185 if( operation.alg == PSA_ALG_CCM )
6186 {
6187 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6188 finish_output_size,
6189 &output_part_length,
6190 tag_buffer, tag_length,
6191 &tag_size ) );
6192 }
6193 else
6194 {
6195 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6196 finish_output_size,
6197 &output_part_length,
6198 tag_buffer, tag_length,
6199 &tag_size ) );
6200
6201 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6202 input_data->len ),
6203 PSA_ERROR_BAD_STATE );
6204 }
6205 psa_aead_abort( &operation );
6206
6207 /* Test for setting lengths after generating nonce + already starting data. */
6208
6209 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6210
6211 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6212 PSA_AEAD_NONCE_MAX_SIZE,
6213 &nonce_length ) );
6214 if( operation.alg == PSA_ALG_CCM )
6215 {
6216
6217 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6218 additional_data->len ),
6219 PSA_ERROR_BAD_STATE );
6220 }
6221 else
6222 {
6223 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6224 additional_data->len ) );
6225
6226 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6227 input_data->len ),
6228 PSA_ERROR_BAD_STATE );
6229 }
6230 psa_aead_abort( &operation );
6231
6232 /* ------------------------------------------------------- */
6233
6234 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6235
6236 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6237 PSA_AEAD_NONCE_MAX_SIZE,
6238 &nonce_length ) );
6239 if( operation.alg == PSA_ALG_CCM )
6240 {
6241 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6242 input_data->len, output_data,
6243 output_size, &output_length ),
6244 PSA_ERROR_BAD_STATE );
6245
6246 }
6247 else
6248 {
6249 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6250 input_data->len, output_data,
6251 output_size, &output_length ) );
6252
6253 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6254 input_data->len ),
6255 PSA_ERROR_BAD_STATE );
6256 }
6257 psa_aead_abort( &operation );
6258
6259 /* ------------------------------------------------------- */
6260
6261 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6262
6263 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6264 PSA_AEAD_NONCE_MAX_SIZE,
6265 &nonce_length ) );
6266 if( operation.alg == PSA_ALG_CCM )
6267 {
6268 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6269 finish_output_size,
6270 &output_part_length,
6271 tag_buffer, tag_length,
6272 &tag_size ) );
6273 }
6274 else
6275 {
6276 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6277 finish_output_size,
6278 &output_part_length,
6279 tag_buffer, tag_length,
6280 &tag_size ) );
6281
6282 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6283 input_data->len ),
6284 PSA_ERROR_BAD_STATE );
6285 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006286 psa_aead_abort( &operation );
6287
Paul Elliott243080c2021-07-21 19:01:17 +01006288 /* Test for not sending any additional data or data after setting non zero
6289 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006290
Paul Elliottc23a9a02021-06-21 18:32:46 +01006291 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6292
6293 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6294
6295 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6296 input_data->len ) );
6297
6298 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6299 finish_output_size,
6300 &output_part_length,
6301 tag_buffer, tag_length,
6302 &tag_size ),
6303 PSA_ERROR_INVALID_ARGUMENT );
6304
6305 psa_aead_abort( &operation );
6306
Paul Elliott243080c2021-07-21 19:01:17 +01006307 /* Test for not sending any additional data or data after setting non-zero
6308 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006309
Paul Elliottc23a9a02021-06-21 18:32:46 +01006310 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6311
6312 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6313
6314 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6315 input_data->len ) );
6316
6317 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6318 finish_output_size,
6319 &output_part_length,
6320 tag_buffer,
6321 tag_length ),
6322 PSA_ERROR_INVALID_ARGUMENT );
6323
6324 psa_aead_abort( &operation );
6325
Paul Elliott243080c2021-07-21 19:01:17 +01006326 /* Test for not sending any additional data after setting a non-zero length
6327 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006328
Paul Elliottc23a9a02021-06-21 18:32:46 +01006329 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6330
6331 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6332
6333 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6334 input_data->len ) );
6335
6336 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6337 input_data->len, output_data,
6338 output_size, &output_length ),
6339 PSA_ERROR_INVALID_ARGUMENT );
6340
6341 psa_aead_abort( &operation );
6342
Paul Elliottf94bd992021-09-19 18:15:59 +01006343 /* Test for not sending any data after setting a non-zero length for it.*/
6344
Paul Elliottf94bd992021-09-19 18:15:59 +01006345 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6346
6347 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6348
6349 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6350 input_data->len ) );
6351
6352 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6353 additional_data->len ) );
6354
6355 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6356 finish_output_size,
6357 &output_part_length,
6358 tag_buffer, tag_length,
6359 &tag_size ),
6360 PSA_ERROR_INVALID_ARGUMENT );
6361
6362 psa_aead_abort( &operation );
6363
Paul Elliottb0450fe2021-09-01 15:06:26 +01006364 /* Test for sending too much additional data after setting lengths. */
6365
Paul Elliottb0450fe2021-09-01 15:06:26 +01006366 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6367
6368 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6369
6370 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6371
6372
6373 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6374 additional_data->len ),
6375 PSA_ERROR_INVALID_ARGUMENT );
6376
6377 psa_aead_abort( &operation );
6378
Paul Elliotta2a09b02021-09-22 14:56:40 +01006379 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006380
6381 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6382
6383 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6384
6385 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6386 input_data->len ) );
6387
6388 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6389 additional_data->len ) );
6390
6391 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6392 1 ),
6393 PSA_ERROR_INVALID_ARGUMENT );
6394
6395 psa_aead_abort( &operation );
6396
Paul Elliottb0450fe2021-09-01 15:06:26 +01006397 /* Test for sending too much data after setting lengths. */
6398
Paul Elliottb0450fe2021-09-01 15:06:26 +01006399 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6400
6401 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6402
6403 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6404
6405 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6406 input_data->len, output_data,
6407 output_size, &output_length ),
6408 PSA_ERROR_INVALID_ARGUMENT );
6409
6410 psa_aead_abort( &operation );
6411
Paul Elliotta2a09b02021-09-22 14:56:40 +01006412 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006413
6414 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6415
6416 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6417
6418 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6419 input_data->len ) );
6420
6421 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6422 additional_data->len ) );
6423
6424 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6425 input_data->len, output_data,
6426 output_size, &output_length ) );
6427
6428 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6429 1, output_data,
6430 output_size, &output_length ),
6431 PSA_ERROR_INVALID_ARGUMENT );
6432
6433 psa_aead_abort( &operation );
6434
Paul Elliottc23a9a02021-06-21 18:32:46 +01006435 /* Test sending additional data after data. */
6436
Paul Elliottc23a9a02021-06-21 18:32:46 +01006437 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6438
6439 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6440
Andrzej Kurekad837522021-12-15 15:28:49 +01006441 if( operation.alg != PSA_ALG_CCM )
6442 {
6443 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6444 input_data->len, output_data,
6445 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006446
Andrzej Kurekad837522021-12-15 15:28:49 +01006447 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6448 additional_data->len ),
6449 PSA_ERROR_BAD_STATE );
6450 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006451 psa_aead_abort( &operation );
6452
Paul Elliott534d0b42021-06-22 19:15:20 +01006453 /* Test calling finish on decryption. */
6454
Paul Elliott534d0b42021-06-22 19:15:20 +01006455 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6456
6457 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6458
6459 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6460 finish_output_size,
6461 &output_part_length,
6462 tag_buffer, tag_length,
6463 &tag_size ),
6464 PSA_ERROR_BAD_STATE );
6465
6466 psa_aead_abort( &operation );
6467
6468 /* Test calling verify on encryption. */
6469
Paul Elliott534d0b42021-06-22 19:15:20 +01006470 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6471
6472 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6473
6474 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6475 finish_output_size,
6476 &output_part_length,
6477 tag_buffer,
6478 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006479 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006480
6481 psa_aead_abort( &operation );
6482
6483
Paul Elliottc23a9a02021-06-21 18:32:46 +01006484exit:
6485 psa_destroy_key( key );
6486 psa_aead_abort( &operation );
6487 mbedtls_free( output_data );
6488 mbedtls_free( final_data );
6489 PSA_DONE( );
6490}
6491/* END_CASE */
6492
6493/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006494void signature_size( int type_arg,
6495 int bits,
6496 int alg_arg,
6497 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006498{
6499 psa_key_type_t type = type_arg;
6500 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006501 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006502
Gilles Peskinefe11b722018-12-18 00:24:04 +01006503 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006504
Gilles Peskinee59236f2018-01-27 23:32:46 +01006505exit:
6506 ;
6507}
6508/* END_CASE */
6509
6510/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006511void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6512 int alg_arg, data_t *input_data,
6513 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006514{
Ronald Cron5425a212020-08-04 14:58:35 +02006515 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006516 psa_key_type_t key_type = key_type_arg;
6517 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006518 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006519 unsigned char *signature = NULL;
6520 size_t signature_size;
6521 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006522 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006523
Gilles Peskine8817f612018-12-18 00:18:46 +01006524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006525
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006526 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006527 psa_set_key_algorithm( &attributes, alg );
6528 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006529
Gilles Peskine049c7532019-05-15 20:22:09 +02006530 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006531 &key ) );
6532 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006533 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006534
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006535 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006536 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006537 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006538 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006539 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006540 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006541 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006542
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006543 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006544 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006545 input_data->x, input_data->len,
6546 signature, signature_size,
6547 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006548 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006549 ASSERT_COMPARE( output_data->x, output_data->len,
6550 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006551
6552exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006553 /*
6554 * Key attributes may have been returned by psa_get_key_attributes()
6555 * thus reset them as required.
6556 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006557 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006558
Ronald Cron5425a212020-08-04 14:58:35 +02006559 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006560 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006561 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006562}
6563/* END_CASE */
6564
6565/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006566void sign_hash_fail( int key_type_arg, data_t *key_data,
6567 int alg_arg, data_t *input_data,
6568 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006569{
Ronald Cron5425a212020-08-04 14:58:35 +02006570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006571 psa_key_type_t key_type = key_type_arg;
6572 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006573 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006574 psa_status_t actual_status;
6575 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006576 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006577 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006579
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006580 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006581
Gilles Peskine8817f612018-12-18 00:18:46 +01006582 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006583
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006584 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006585 psa_set_key_algorithm( &attributes, alg );
6586 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006587
Gilles Peskine049c7532019-05-15 20:22:09 +02006588 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006589 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006590
Ronald Cron5425a212020-08-04 14:58:35 +02006591 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006592 input_data->x, input_data->len,
6593 signature, signature_size,
6594 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006595 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006596 /* The value of *signature_length is unspecified on error, but
6597 * whatever it is, it should be less than signature_size, so that
6598 * if the caller tries to read *signature_length bytes without
6599 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006600 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006601
6602exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006603 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006604 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006605 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006606 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006607}
6608/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006609
6610/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006611void sign_verify_hash( int key_type_arg, data_t *key_data,
6612 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006613{
Ronald Cron5425a212020-08-04 14:58:35 +02006614 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006615 psa_key_type_t key_type = key_type_arg;
6616 psa_algorithm_t alg = alg_arg;
6617 size_t key_bits;
6618 unsigned char *signature = NULL;
6619 size_t signature_size;
6620 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006622
Gilles Peskine8817f612018-12-18 00:18:46 +01006623 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006624
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006625 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006626 psa_set_key_algorithm( &attributes, alg );
6627 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006628
Gilles Peskine049c7532019-05-15 20:22:09 +02006629 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006630 &key ) );
6631 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006632 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006633
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006634 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006635 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006636 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006637 key_bits, alg );
6638 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006639 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006640 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006641
6642 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006643 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006644 input_data->x, input_data->len,
6645 signature, signature_size,
6646 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006647 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006648 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006649 TEST_ASSERT( signature_length > 0 );
6650
6651 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006652 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006653 input_data->x, input_data->len,
6654 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006655
6656 if( input_data->len != 0 )
6657 {
6658 /* Flip a bit in the input and verify that the signature is now
6659 * detected as invalid. Flip a bit at the beginning, not at the end,
6660 * because ECDSA may ignore the last few bits of the input. */
6661 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006662 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006663 input_data->x, input_data->len,
6664 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006665 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006666 }
6667
6668exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006669 /*
6670 * Key attributes may have been returned by psa_get_key_attributes()
6671 * thus reset them as required.
6672 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006673 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006674
Ronald Cron5425a212020-08-04 14:58:35 +02006675 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006676 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006677 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006678}
6679/* END_CASE */
6680
6681/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006682void verify_hash( int key_type_arg, data_t *key_data,
6683 int alg_arg, data_t *hash_data,
6684 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006685{
Ronald Cron5425a212020-08-04 14:58:35 +02006686 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006687 psa_key_type_t key_type = key_type_arg;
6688 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006689 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006690
Gilles Peskine7be11a72022-04-14 00:12:57 +02006691 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006692
Gilles Peskine8817f612018-12-18 00:18:46 +01006693 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006694
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006695 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006696 psa_set_key_algorithm( &attributes, alg );
6697 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006698
Gilles Peskine049c7532019-05-15 20:22:09 +02006699 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006700 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006701
Ronald Cron5425a212020-08-04 14:58:35 +02006702 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006703 hash_data->x, hash_data->len,
6704 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006705
itayzafrir5c753392018-05-08 11:18:38 +03006706exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006707 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006708 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006709 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006710}
6711/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006712
6713/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006714void verify_hash_fail( int key_type_arg, data_t *key_data,
6715 int alg_arg, data_t *hash_data,
6716 data_t *signature_data,
6717 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006718{
Ronald Cron5425a212020-08-04 14:58:35 +02006719 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006720 psa_key_type_t key_type = key_type_arg;
6721 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006722 psa_status_t actual_status;
6723 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006725
Gilles Peskine8817f612018-12-18 00:18:46 +01006726 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006727
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006729 psa_set_key_algorithm( &attributes, alg );
6730 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006731
Gilles Peskine049c7532019-05-15 20:22:09 +02006732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006733 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006734
Ronald Cron5425a212020-08-04 14:58:35 +02006735 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006736 hash_data->x, hash_data->len,
6737 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006738 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006739
6740exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006741 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006742 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006743 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006744}
6745/* END_CASE */
6746
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006747/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006748void sign_message_deterministic( int key_type_arg,
6749 data_t *key_data,
6750 int alg_arg,
6751 data_t *input_data,
6752 data_t *output_data )
6753{
6754 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6755 psa_key_type_t key_type = key_type_arg;
6756 psa_algorithm_t alg = alg_arg;
6757 size_t key_bits;
6758 unsigned char *signature = NULL;
6759 size_t signature_size;
6760 size_t signature_length = 0xdeadbeef;
6761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6762
6763 PSA_ASSERT( psa_crypto_init( ) );
6764
6765 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6766 psa_set_key_algorithm( &attributes, alg );
6767 psa_set_key_type( &attributes, key_type );
6768
6769 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6770 &key ) );
6771 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6772 key_bits = psa_get_key_bits( &attributes );
6773
6774 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6775 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006776 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006777 ASSERT_ALLOC( signature, signature_size );
6778
6779 PSA_ASSERT( psa_sign_message( key, alg,
6780 input_data->x, input_data->len,
6781 signature, signature_size,
6782 &signature_length ) );
6783
6784 ASSERT_COMPARE( output_data->x, output_data->len,
6785 signature, signature_length );
6786
6787exit:
6788 psa_reset_key_attributes( &attributes );
6789
6790 psa_destroy_key( key );
6791 mbedtls_free( signature );
6792 PSA_DONE( );
6793
6794}
6795/* END_CASE */
6796
6797/* BEGIN_CASE */
6798void sign_message_fail( int key_type_arg,
6799 data_t *key_data,
6800 int alg_arg,
6801 data_t *input_data,
6802 int signature_size_arg,
6803 int expected_status_arg )
6804{
6805 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6806 psa_key_type_t key_type = key_type_arg;
6807 psa_algorithm_t alg = alg_arg;
6808 size_t signature_size = signature_size_arg;
6809 psa_status_t actual_status;
6810 psa_status_t expected_status = expected_status_arg;
6811 unsigned char *signature = NULL;
6812 size_t signature_length = 0xdeadbeef;
6813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6814
6815 ASSERT_ALLOC( signature, signature_size );
6816
6817 PSA_ASSERT( psa_crypto_init( ) );
6818
6819 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6820 psa_set_key_algorithm( &attributes, alg );
6821 psa_set_key_type( &attributes, key_type );
6822
6823 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6824 &key ) );
6825
6826 actual_status = psa_sign_message( key, alg,
6827 input_data->x, input_data->len,
6828 signature, signature_size,
6829 &signature_length );
6830 TEST_EQUAL( actual_status, expected_status );
6831 /* The value of *signature_length is unspecified on error, but
6832 * whatever it is, it should be less than signature_size, so that
6833 * if the caller tries to read *signature_length bytes without
6834 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006835 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006836
6837exit:
6838 psa_reset_key_attributes( &attributes );
6839 psa_destroy_key( key );
6840 mbedtls_free( signature );
6841 PSA_DONE( );
6842}
6843/* END_CASE */
6844
6845/* BEGIN_CASE */
6846void sign_verify_message( int key_type_arg,
6847 data_t *key_data,
6848 int alg_arg,
6849 data_t *input_data )
6850{
6851 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6852 psa_key_type_t key_type = key_type_arg;
6853 psa_algorithm_t alg = alg_arg;
6854 size_t key_bits;
6855 unsigned char *signature = NULL;
6856 size_t signature_size;
6857 size_t signature_length = 0xdeadbeef;
6858 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6859
6860 PSA_ASSERT( psa_crypto_init( ) );
6861
6862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6863 PSA_KEY_USAGE_VERIFY_MESSAGE );
6864 psa_set_key_algorithm( &attributes, alg );
6865 psa_set_key_type( &attributes, key_type );
6866
6867 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6868 &key ) );
6869 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6870 key_bits = psa_get_key_bits( &attributes );
6871
6872 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6873 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006874 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006875 ASSERT_ALLOC( signature, signature_size );
6876
6877 PSA_ASSERT( psa_sign_message( key, alg,
6878 input_data->x, input_data->len,
6879 signature, signature_size,
6880 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006881 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006882 TEST_ASSERT( signature_length > 0 );
6883
6884 PSA_ASSERT( psa_verify_message( key, alg,
6885 input_data->x, input_data->len,
6886 signature, signature_length ) );
6887
6888 if( input_data->len != 0 )
6889 {
6890 /* Flip a bit in the input and verify that the signature is now
6891 * detected as invalid. Flip a bit at the beginning, not at the end,
6892 * because ECDSA may ignore the last few bits of the input. */
6893 input_data->x[0] ^= 1;
6894 TEST_EQUAL( psa_verify_message( key, alg,
6895 input_data->x, input_data->len,
6896 signature, signature_length ),
6897 PSA_ERROR_INVALID_SIGNATURE );
6898 }
6899
6900exit:
6901 psa_reset_key_attributes( &attributes );
6902
6903 psa_destroy_key( key );
6904 mbedtls_free( signature );
6905 PSA_DONE( );
6906}
6907/* END_CASE */
6908
6909/* BEGIN_CASE */
6910void verify_message( int key_type_arg,
6911 data_t *key_data,
6912 int alg_arg,
6913 data_t *input_data,
6914 data_t *signature_data )
6915{
6916 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6917 psa_key_type_t key_type = key_type_arg;
6918 psa_algorithm_t alg = alg_arg;
6919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6920
Gilles Peskine7be11a72022-04-14 00:12:57 +02006921 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006922
6923 PSA_ASSERT( psa_crypto_init( ) );
6924
6925 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6926 psa_set_key_algorithm( &attributes, alg );
6927 psa_set_key_type( &attributes, key_type );
6928
6929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6930 &key ) );
6931
6932 PSA_ASSERT( psa_verify_message( key, alg,
6933 input_data->x, input_data->len,
6934 signature_data->x, signature_data->len ) );
6935
6936exit:
6937 psa_reset_key_attributes( &attributes );
6938 psa_destroy_key( key );
6939 PSA_DONE( );
6940}
6941/* END_CASE */
6942
6943/* BEGIN_CASE */
6944void verify_message_fail( int key_type_arg,
6945 data_t *key_data,
6946 int alg_arg,
6947 data_t *hash_data,
6948 data_t *signature_data,
6949 int expected_status_arg )
6950{
6951 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6952 psa_key_type_t key_type = key_type_arg;
6953 psa_algorithm_t alg = alg_arg;
6954 psa_status_t actual_status;
6955 psa_status_t expected_status = expected_status_arg;
6956 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6957
6958 PSA_ASSERT( psa_crypto_init( ) );
6959
6960 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6961 psa_set_key_algorithm( &attributes, alg );
6962 psa_set_key_type( &attributes, key_type );
6963
6964 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6965 &key ) );
6966
6967 actual_status = psa_verify_message( key, alg,
6968 hash_data->x, hash_data->len,
6969 signature_data->x,
6970 signature_data->len );
6971 TEST_EQUAL( actual_status, expected_status );
6972
6973exit:
6974 psa_reset_key_attributes( &attributes );
6975 psa_destroy_key( key );
6976 PSA_DONE( );
6977}
6978/* END_CASE */
6979
6980/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006981void asymmetric_encrypt( int key_type_arg,
6982 data_t *key_data,
6983 int alg_arg,
6984 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006985 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006986 int expected_output_length_arg,
6987 int expected_status_arg )
6988{
Ronald Cron5425a212020-08-04 14:58:35 +02006989 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006990 psa_key_type_t key_type = key_type_arg;
6991 psa_algorithm_t alg = alg_arg;
6992 size_t expected_output_length = expected_output_length_arg;
6993 size_t key_bits;
6994 unsigned char *output = NULL;
6995 size_t output_size;
6996 size_t output_length = ~0;
6997 psa_status_t actual_status;
6998 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02007000
Gilles Peskine8817f612018-12-18 00:18:46 +01007001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01007002
Gilles Peskine656896e2018-06-29 19:12:28 +02007003 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007004 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7005 psa_set_key_algorithm( &attributes, alg );
7006 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007008 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007009
7010 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007011 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007012 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007013
Gilles Peskine656896e2018-06-29 19:12:28 +02007014 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007015 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007016 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007017
7018 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007019 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007020 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007021 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007022 output, output_size,
7023 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007024 TEST_EQUAL( actual_status, expected_status );
7025 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007026
Gilles Peskine68428122018-06-30 18:42:41 +02007027 /* If the label is empty, the test framework puts a non-null pointer
7028 * in label->x. Test that a null pointer works as well. */
7029 if( label->len == 0 )
7030 {
7031 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007032 if( output_size != 0 )
7033 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007034 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007035 input_data->x, input_data->len,
7036 NULL, label->len,
7037 output, output_size,
7038 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007039 TEST_EQUAL( actual_status, expected_status );
7040 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007041 }
7042
Gilles Peskine656896e2018-06-29 19:12:28 +02007043exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007044 /*
7045 * Key attributes may have been returned by psa_get_key_attributes()
7046 * thus reset them as required.
7047 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007048 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007049
Ronald Cron5425a212020-08-04 14:58:35 +02007050 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007051 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007052 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007053}
7054/* END_CASE */
7055
7056/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007057void asymmetric_encrypt_decrypt( int key_type_arg,
7058 data_t *key_data,
7059 int alg_arg,
7060 data_t *input_data,
7061 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007062{
Ronald Cron5425a212020-08-04 14:58:35 +02007063 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007064 psa_key_type_t key_type = key_type_arg;
7065 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007066 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007067 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007068 size_t output_size;
7069 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007070 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007071 size_t output2_size;
7072 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007074
Gilles Peskine8817f612018-12-18 00:18:46 +01007075 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007076
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007077 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7078 psa_set_key_algorithm( &attributes, alg );
7079 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007080
Gilles Peskine049c7532019-05-15 20:22:09 +02007081 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007082 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007083
7084 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007085 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007086 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007087
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007088 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007089 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007090 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007091
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007092 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007093 TEST_LE_U( output2_size,
7094 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7095 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007096 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007097
Gilles Peskineeebd7382018-06-08 18:11:54 +02007098 /* We test encryption by checking that encrypt-then-decrypt gives back
7099 * the original plaintext because of the non-optional random
7100 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007101 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007102 input_data->x, input_data->len,
7103 label->x, label->len,
7104 output, output_size,
7105 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007106 /* We don't know what ciphertext length to expect, but check that
7107 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007108 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007109
Ronald Cron5425a212020-08-04 14:58:35 +02007110 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007111 output, output_length,
7112 label->x, label->len,
7113 output2, output2_size,
7114 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007115 ASSERT_COMPARE( input_data->x, input_data->len,
7116 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007117
7118exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007119 /*
7120 * Key attributes may have been returned by psa_get_key_attributes()
7121 * thus reset them as required.
7122 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007123 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007124
Ronald Cron5425a212020-08-04 14:58:35 +02007125 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007126 mbedtls_free( output );
7127 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007128 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007129}
7130/* END_CASE */
7131
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007132/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007133void asymmetric_decrypt( int key_type_arg,
7134 data_t *key_data,
7135 int alg_arg,
7136 data_t *input_data,
7137 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007138 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007139{
Ronald Cron5425a212020-08-04 14:58:35 +02007140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007141 psa_key_type_t key_type = key_type_arg;
7142 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007143 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007144 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007145 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007146 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007148
Gilles Peskine8817f612018-12-18 00:18:46 +01007149 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007150
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007151 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7152 psa_set_key_algorithm( &attributes, alg );
7153 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007154
Gilles Peskine049c7532019-05-15 20:22:09 +02007155 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007156 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007157
gabor-mezei-armceface22021-01-21 12:26:17 +01007158 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7159 key_bits = psa_get_key_bits( &attributes );
7160
7161 /* Determine the maximum ciphertext length */
7162 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007163 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007164 ASSERT_ALLOC( output, output_size );
7165
Ronald Cron5425a212020-08-04 14:58:35 +02007166 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007167 input_data->x, input_data->len,
7168 label->x, label->len,
7169 output,
7170 output_size,
7171 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007172 ASSERT_COMPARE( expected_data->x, expected_data->len,
7173 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007174
Gilles Peskine68428122018-06-30 18:42:41 +02007175 /* If the label is empty, the test framework puts a non-null pointer
7176 * in label->x. Test that a null pointer works as well. */
7177 if( label->len == 0 )
7178 {
7179 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007180 if( output_size != 0 )
7181 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007182 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007183 input_data->x, input_data->len,
7184 NULL, label->len,
7185 output,
7186 output_size,
7187 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007188 ASSERT_COMPARE( expected_data->x, expected_data->len,
7189 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007190 }
7191
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007192exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007193 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007194 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007195 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007196 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007197}
7198/* END_CASE */
7199
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007200/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007201void asymmetric_decrypt_fail( int key_type_arg,
7202 data_t *key_data,
7203 int alg_arg,
7204 data_t *input_data,
7205 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007206 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007207 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007208{
Ronald Cron5425a212020-08-04 14:58:35 +02007209 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007210 psa_key_type_t key_type = key_type_arg;
7211 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007212 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007213 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007214 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007215 psa_status_t actual_status;
7216 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007218
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007219 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007220
Gilles Peskine8817f612018-12-18 00:18:46 +01007221 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007222
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007223 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7224 psa_set_key_algorithm( &attributes, alg );
7225 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007226
Gilles Peskine049c7532019-05-15 20:22:09 +02007227 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007228 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007229
Ronald Cron5425a212020-08-04 14:58:35 +02007230 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007231 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007232 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007233 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007234 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007235 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007236 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007237
Gilles Peskine68428122018-06-30 18:42:41 +02007238 /* If the label is empty, the test framework puts a non-null pointer
7239 * in label->x. Test that a null pointer works as well. */
7240 if( label->len == 0 )
7241 {
7242 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007243 if( output_size != 0 )
7244 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007245 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007246 input_data->x, input_data->len,
7247 NULL, label->len,
7248 output, output_size,
7249 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007250 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007251 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007252 }
7253
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007254exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007255 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007256 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007257 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007258 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007259}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007260/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007261
7262/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007263void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007264{
7265 /* Test each valid way of initializing the object, except for `= {0}`, as
7266 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7267 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007268 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007269 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007270 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7271 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7272 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007273
7274 memset( &zero, 0, sizeof( zero ) );
7275
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007276 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007277 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007278 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007279 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007280 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007281 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007282 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007283
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007284 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007285 PSA_ASSERT( psa_key_derivation_abort(&func) );
7286 PSA_ASSERT( psa_key_derivation_abort(&init) );
7287 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007288}
7289/* END_CASE */
7290
Janos Follath16de4a42019-06-13 16:32:24 +01007291/* BEGIN_CASE */
7292void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007293{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007294 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007295 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007296 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007297
Gilles Peskine8817f612018-12-18 00:18:46 +01007298 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007299
Janos Follath16de4a42019-06-13 16:32:24 +01007300 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007301 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007302
7303exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007304 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007305 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007306}
7307/* END_CASE */
7308
Janos Follathaf3c2a02019-06-12 12:34:34 +01007309/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007310void derive_set_capacity( int alg_arg, int capacity_arg,
7311 int expected_status_arg )
7312{
7313 psa_algorithm_t alg = alg_arg;
7314 size_t capacity = capacity_arg;
7315 psa_status_t expected_status = expected_status_arg;
7316 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7317
7318 PSA_ASSERT( psa_crypto_init( ) );
7319
7320 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7321
7322 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7323 expected_status );
7324
7325exit:
7326 psa_key_derivation_abort( &operation );
7327 PSA_DONE( );
7328}
7329/* END_CASE */
7330
7331/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007332void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007333 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007334 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007335 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007336 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007337 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007338 int expected_status_arg3,
7339 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007340{
7341 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007342 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7343 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007344 psa_status_t expected_statuses[] = {expected_status_arg1,
7345 expected_status_arg2,
7346 expected_status_arg3};
7347 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007348 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7349 MBEDTLS_SVC_KEY_ID_INIT,
7350 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007351 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7353 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007354 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007355 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007356 psa_status_t expected_output_status = expected_output_status_arg;
7357 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007358
7359 PSA_ASSERT( psa_crypto_init( ) );
7360
7361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7362 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007363
7364 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7365
7366 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7367 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007368 mbedtls_test_set_step( i );
7369 if( steps[i] == 0 )
7370 {
7371 /* Skip this step */
7372 }
7373 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007374 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007375 psa_set_key_type( &attributes, key_types[i] );
7376 PSA_ASSERT( psa_import_key( &attributes,
7377 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007378 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007379 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7380 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7381 {
7382 // When taking a private key as secret input, use key agreement
7383 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007384 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7385 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007386 expected_statuses[i] );
7387 }
7388 else
7389 {
7390 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007391 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007392 expected_statuses[i] );
7393 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007394 }
7395 else
7396 {
7397 TEST_EQUAL( psa_key_derivation_input_bytes(
7398 &operation, steps[i],
7399 inputs[i]->x, inputs[i]->len ),
7400 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007401 }
7402 }
7403
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007404 if( output_key_type != PSA_KEY_TYPE_NONE )
7405 {
7406 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007407 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007408 psa_set_key_bits( &attributes, 8 );
7409 actual_output_status =
7410 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007411 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007412 }
7413 else
7414 {
7415 uint8_t buffer[1];
7416 actual_output_status =
7417 psa_key_derivation_output_bytes( &operation,
7418 buffer, sizeof( buffer ) );
7419 }
7420 TEST_EQUAL( actual_output_status, expected_output_status );
7421
Janos Follathaf3c2a02019-06-12 12:34:34 +01007422exit:
7423 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007424 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7425 psa_destroy_key( keys[i] );
7426 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007427 PSA_DONE( );
7428}
7429/* END_CASE */
7430
Janos Follathd958bb72019-07-03 15:02:16 +01007431/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007432void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007433{
Janos Follathd958bb72019-07-03 15:02:16 +01007434 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007435 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007436 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007437 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007438 unsigned char input1[] = "Input 1";
7439 size_t input1_length = sizeof( input1 );
7440 unsigned char input2[] = "Input 2";
7441 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007442 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007443 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007444 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7445 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7446 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007448
Gilles Peskine8817f612018-12-18 00:18:46 +01007449 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007450
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7452 psa_set_key_algorithm( &attributes, alg );
7453 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007454
Gilles Peskine73676cb2019-05-15 20:15:10 +02007455 PSA_ASSERT( psa_import_key( &attributes,
7456 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007457 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007458
7459 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007460 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7461 input1, input1_length,
7462 input2, input2_length,
7463 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007464 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007465
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007466 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007467 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007468 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007469
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007470 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007471
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007472 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007473 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007474
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007475exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007476 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007477 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007478 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007479}
7480/* END_CASE */
7481
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007482/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007483void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007484{
7485 uint8_t output_buffer[16];
7486 size_t buffer_size = 16;
7487 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007488 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007489
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007490 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7491 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007492 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007493
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007494 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007495 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007496
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007497 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007498
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007499 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7500 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007501 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007502
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007503 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007504 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007505
7506exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007507 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007508}
7509/* END_CASE */
7510
7511/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007512void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007513 int step1_arg, data_t *input1, int expected_status_arg1,
7514 int step2_arg, data_t *input2, int expected_status_arg2,
7515 int step3_arg, data_t *input3, int expected_status_arg3,
7516 int step4_arg, data_t *input4, int expected_status_arg4,
7517 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007518 int requested_capacity_arg,
7519 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007520 data_t *expected_output2,
7521 int other_key_input_type,
7522 int key_input_type,
7523 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007524{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007525 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007526 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7527 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007528 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7529 MBEDTLS_SVC_KEY_ID_INIT,
7530 MBEDTLS_SVC_KEY_ID_INIT,
7531 MBEDTLS_SVC_KEY_ID_INIT};
7532 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7533 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007534 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007535 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007536 uint8_t *expected_outputs[2] =
7537 {expected_output1->x, expected_output2->x};
7538 size_t output_sizes[2] =
7539 {expected_output1->len, expected_output2->len};
7540 size_t output_buffer_size = 0;
7541 uint8_t *output_buffer = NULL;
7542 size_t expected_capacity;
7543 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007544 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7545 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7546 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7547 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007548 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007549 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007550 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007551
7552 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7553 {
7554 if( output_sizes[i] > output_buffer_size )
7555 output_buffer_size = output_sizes[i];
7556 if( output_sizes[i] == 0 )
7557 expected_outputs[i] = NULL;
7558 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007559 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007560 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007561
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007562 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007563 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7564 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7565 requested_capacity ) );
7566 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007567 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007568 switch( steps[i] )
7569 {
7570 case 0:
7571 break;
7572 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007573 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007574 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007575 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007576 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007577 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007578 inputs[i]->x, inputs[i]->len ),
7579 statuses[i] );
7580
7581 if( statuses[i] != PSA_SUCCESS )
7582 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007583 break;
7584 case 1: // input key
7585 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7586 psa_set_key_algorithm( &attributes1, alg );
7587 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7588
7589 PSA_ASSERT( psa_import_key( &attributes1,
7590 inputs[i]->x, inputs[i]->len,
7591 &keys[i] ) );
7592
Przemek Stekiel38647de2022-04-19 13:27:47 +02007593 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007594 {
7595 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007596 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7597 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007598 }
7599
Przemek Stekiel38647de2022-04-19 13:27:47 +02007600 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7601 steps[i],
7602 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007603 break;
7604 default:
7605 TEST_ASSERT( ! "default case not supported" );
7606 break;
7607 }
7608 break;
7609 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007610 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007611 {
7612 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007613 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7614 steps[i],
7615 inputs[i]->x,
7616 inputs[i]->len ),
7617 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007618 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007619 case 1: // input key, type DERIVE
7620 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007621 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7622 psa_set_key_algorithm( &attributes2, alg );
7623 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7624
7625 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007626 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007627 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7628
7629 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007630 inputs[i]->x, inputs[i]->len,
7631 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007632
Przemek Stekiel38647de2022-04-19 13:27:47 +02007633 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7634 steps[i],
7635 keys[i] ),
7636 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007637 break;
7638 case 2: // key agreement
7639 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7640 psa_set_key_algorithm( &attributes3, alg );
7641 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7642
7643 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007644 inputs[i]->x, inputs[i]->len,
7645 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007646
7647 TEST_EQUAL( psa_key_derivation_key_agreement(
7648 &operation,
7649 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7650 keys[i], key_agreement_peer_key->x,
7651 key_agreement_peer_key->len ), statuses[i] );
7652 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007653 default:
7654 TEST_ASSERT( ! "default case not supported" );
7655 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007656 }
7657
Przemek Stekiel38647de2022-04-19 13:27:47 +02007658 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007659 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007660 break;
7661 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007662 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007663 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007664 inputs[i]->x, inputs[i]->len ), statuses[i] );
7665
7666 if( statuses[i] != PSA_SUCCESS )
7667 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007668 break;
7669 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007670 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007671
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007672 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007673 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007674 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007675 expected_capacity = requested_capacity;
7676
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007677 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007678 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007679 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7680
7681 /* For output key derivation secret must be provided using
7682 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007683 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007684 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007685
7686 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7687 psa_set_key_algorithm( &attributes4, alg );
7688 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007689 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007690
7691 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007692 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007693 }
7694 else // output bytes
7695 {
7696 /* Expansion phase. */
7697 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007698 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007699 /* Read some bytes. */
7700 status = psa_key_derivation_output_bytes( &operation,
7701 output_buffer, output_sizes[i] );
7702 if( expected_capacity == 0 && output_sizes[i] == 0 )
7703 {
7704 /* Reading 0 bytes when 0 bytes are available can go either way. */
7705 TEST_ASSERT( status == PSA_SUCCESS ||
7706 status == PSA_ERROR_INSUFFICIENT_DATA );
7707 continue;
7708 }
7709 else if( expected_capacity == 0 ||
7710 output_sizes[i] > expected_capacity )
7711 {
7712 /* Capacity exceeded. */
7713 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7714 expected_capacity = 0;
7715 continue;
7716 }
7717 /* Success. Check the read data. */
7718 PSA_ASSERT( status );
7719 if( output_sizes[i] != 0 )
7720 ASSERT_COMPARE( output_buffer, output_sizes[i],
7721 expected_outputs[i], output_sizes[i] );
7722 /* Check the operation status. */
7723 expected_capacity -= output_sizes[i];
7724 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7725 &current_capacity ) );
7726 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007727 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007728 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007729 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007730
7731exit:
7732 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007733 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007734 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7735 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007736 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007737 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007738}
7739/* END_CASE */
7740
7741/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007742void derive_full( int alg_arg,
7743 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007744 data_t *input1,
7745 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007746 int requested_capacity_arg )
7747{
Ronald Cron5425a212020-08-04 14:58:35 +02007748 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007749 psa_algorithm_t alg = alg_arg;
7750 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007751 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007752 unsigned char output_buffer[16];
7753 size_t expected_capacity = requested_capacity;
7754 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007755 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007756
Gilles Peskine8817f612018-12-18 00:18:46 +01007757 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007758
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007759 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7760 psa_set_key_algorithm( &attributes, alg );
7761 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007762
Gilles Peskine049c7532019-05-15 20:22:09 +02007763 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007764 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007765
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007766 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7767 input1->x, input1->len,
7768 input2->x, input2->len,
7769 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007770 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007771
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007772 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007773 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007774 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007775
7776 /* Expansion phase. */
7777 while( current_capacity > 0 )
7778 {
7779 size_t read_size = sizeof( output_buffer );
7780 if( read_size > current_capacity )
7781 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007782 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007783 output_buffer,
7784 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007785 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007786 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007787 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007788 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007789 }
7790
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007791 /* Check that the operation refuses to go over capacity. */
7792 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007793 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007794
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007795 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007796
7797exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007798 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007799 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007800 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007801}
7802/* END_CASE */
7803
Janos Follathe60c9052019-07-03 13:51:30 +01007804/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007805void derive_key_exercise( int alg_arg,
7806 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007807 data_t *input1,
7808 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007809 int derived_type_arg,
7810 int derived_bits_arg,
7811 int derived_usage_arg,
7812 int derived_alg_arg )
7813{
Ronald Cron5425a212020-08-04 14:58:35 +02007814 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7815 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007816 psa_algorithm_t alg = alg_arg;
7817 psa_key_type_t derived_type = derived_type_arg;
7818 size_t derived_bits = derived_bits_arg;
7819 psa_key_usage_t derived_usage = derived_usage_arg;
7820 psa_algorithm_t derived_alg = derived_alg_arg;
7821 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007822 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007823 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007824 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007825
Gilles Peskine8817f612018-12-18 00:18:46 +01007826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007827
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007828 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7829 psa_set_key_algorithm( &attributes, alg );
7830 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007831 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007832 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007833
7834 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007835 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7836 input1->x, input1->len,
7837 input2->x, input2->len,
7838 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007839 goto exit;
7840
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007841 psa_set_key_usage_flags( &attributes, derived_usage );
7842 psa_set_key_algorithm( &attributes, derived_alg );
7843 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007844 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007845 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007846 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007847
7848 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007849 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007850 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7851 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007852
7853 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007854 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007855 goto exit;
7856
7857exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007858 /*
7859 * Key attributes may have been returned by psa_get_key_attributes()
7860 * thus reset them as required.
7861 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007862 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007863
7864 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007865 psa_destroy_key( base_key );
7866 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007867 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007868}
7869/* END_CASE */
7870
Janos Follath42fd8882019-07-03 14:17:09 +01007871/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007872void derive_key_export( int alg_arg,
7873 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007874 data_t *input1,
7875 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007876 int bytes1_arg,
7877 int bytes2_arg )
7878{
Ronald Cron5425a212020-08-04 14:58:35 +02007879 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7880 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007881 psa_algorithm_t alg = alg_arg;
7882 size_t bytes1 = bytes1_arg;
7883 size_t bytes2 = bytes2_arg;
7884 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007885 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007886 uint8_t *output_buffer = NULL;
7887 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007888 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7889 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007890 size_t length;
7891
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007892 ASSERT_ALLOC( output_buffer, capacity );
7893 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007895
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007896 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7897 psa_set_key_algorithm( &base_attributes, alg );
7898 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007899 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007900 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007901
7902 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007903 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7904 input1->x, input1->len,
7905 input2->x, input2->len,
7906 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007907 goto exit;
7908
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007909 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007910 output_buffer,
7911 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007912 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007913
7914 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007915 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7916 input1->x, input1->len,
7917 input2->x, input2->len,
7918 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007919 goto exit;
7920
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007921 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7922 psa_set_key_algorithm( &derived_attributes, 0 );
7923 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007924 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007925 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007926 &derived_key ) );
7927 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007928 export_buffer, bytes1,
7929 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007930 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007931 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007932 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007933 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007934 &derived_key ) );
7935 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007936 export_buffer + bytes1, bytes2,
7937 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007938 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007939
7940 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007941 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7942 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007943
7944exit:
7945 mbedtls_free( output_buffer );
7946 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007947 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007948 psa_destroy_key( base_key );
7949 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007950 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007951}
7952/* END_CASE */
7953
7954/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007955void derive_key_type( int alg_arg,
7956 data_t *key_data,
7957 data_t *input1,
7958 data_t *input2,
7959 int key_type_arg, int bits_arg,
7960 data_t *expected_export )
7961{
7962 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7963 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7964 const psa_algorithm_t alg = alg_arg;
7965 const psa_key_type_t key_type = key_type_arg;
7966 const size_t bits = bits_arg;
7967 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7968 const size_t export_buffer_size =
7969 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
7970 uint8_t *export_buffer = NULL;
7971 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7972 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7973 size_t export_length;
7974
7975 ASSERT_ALLOC( export_buffer, export_buffer_size );
7976 PSA_ASSERT( psa_crypto_init( ) );
7977
7978 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7979 psa_set_key_algorithm( &base_attributes, alg );
7980 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7981 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
7982 &base_key ) );
7983
Przemek Stekielc85f0912022-03-08 11:37:54 +01007984 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007985 &operation, base_key, alg,
7986 input1->x, input1->len,
7987 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01007988 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007989 goto exit;
7990
7991 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7992 psa_set_key_algorithm( &derived_attributes, 0 );
7993 psa_set_key_type( &derived_attributes, key_type );
7994 psa_set_key_bits( &derived_attributes, bits );
7995 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
7996 &derived_key ) );
7997
7998 PSA_ASSERT( psa_export_key( derived_key,
7999 export_buffer, export_buffer_size,
8000 &export_length ) );
8001 ASSERT_COMPARE( export_buffer, export_length,
8002 expected_export->x, expected_export->len );
8003
8004exit:
8005 mbedtls_free( export_buffer );
8006 psa_key_derivation_abort( &operation );
8007 psa_destroy_key( base_key );
8008 psa_destroy_key( derived_key );
8009 PSA_DONE( );
8010}
8011/* END_CASE */
8012
8013/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008014void derive_key( int alg_arg,
8015 data_t *key_data, data_t *input1, data_t *input2,
8016 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008017 int expected_status_arg,
8018 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008019{
Ronald Cron5425a212020-08-04 14:58:35 +02008020 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8021 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008022 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008023 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008024 size_t bits = bits_arg;
8025 psa_status_t expected_status = expected_status_arg;
8026 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8027 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8028 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8029
8030 PSA_ASSERT( psa_crypto_init( ) );
8031
8032 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8033 psa_set_key_algorithm( &base_attributes, alg );
8034 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8035 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008036 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008037
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008038 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8039 input1->x, input1->len,
8040 input2->x, input2->len,
8041 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008042 goto exit;
8043
8044 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8045 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008046 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008047 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008048
8049 psa_status_t status =
8050 psa_key_derivation_output_key( &derived_attributes,
8051 &operation,
8052 &derived_key );
8053 if( is_large_output > 0 )
8054 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8055 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008056
8057exit:
8058 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008059 psa_destroy_key( base_key );
8060 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008061 PSA_DONE( );
8062}
8063/* END_CASE */
8064
8065/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008066void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008067 int our_key_type_arg, int our_key_alg_arg,
8068 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008069 int expected_status_arg )
8070{
Ronald Cron5425a212020-08-04 14:58:35 +02008071 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008072 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008073 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008074 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008075 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008077 psa_status_t expected_status = expected_status_arg;
8078 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008079
Gilles Peskine8817f612018-12-18 00:18:46 +01008080 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008081
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008082 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008083 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008084 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008085 PSA_ASSERT( psa_import_key( &attributes,
8086 our_key_data->x, our_key_data->len,
8087 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008088
Gilles Peskine77f40d82019-04-11 21:27:06 +02008089 /* The tests currently include inputs that should fail at either step.
8090 * Test cases that fail at the setup step should be changed to call
8091 * key_derivation_setup instead, and this function should be renamed
8092 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008093 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008094 if( status == PSA_SUCCESS )
8095 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008096 TEST_EQUAL( psa_key_derivation_key_agreement(
8097 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8098 our_key,
8099 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008100 expected_status );
8101 }
8102 else
8103 {
8104 TEST_ASSERT( status == expected_status );
8105 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008106
8107exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008108 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008109 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008110 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008111}
8112/* END_CASE */
8113
8114/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008115void raw_key_agreement( int alg_arg,
8116 int our_key_type_arg, data_t *our_key_data,
8117 data_t *peer_key_data,
8118 data_t *expected_output )
8119{
Ronald Cron5425a212020-08-04 14:58:35 +02008120 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008121 psa_algorithm_t alg = alg_arg;
8122 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008123 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008124 unsigned char *output = NULL;
8125 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008126 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008127
Gilles Peskinef0cba732019-04-11 22:12:38 +02008128 PSA_ASSERT( psa_crypto_init( ) );
8129
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8131 psa_set_key_algorithm( &attributes, alg );
8132 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008133 PSA_ASSERT( psa_import_key( &attributes,
8134 our_key_data->x, our_key_data->len,
8135 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008136
gabor-mezei-armceface22021-01-21 12:26:17 +01008137 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8138 key_bits = psa_get_key_bits( &attributes );
8139
Gilles Peskine992bee82022-04-13 23:25:52 +02008140 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008141 TEST_LE_U( expected_output->len,
8142 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8143 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8144 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008145
8146 /* Good case with exact output size */
8147 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008148 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8149 peer_key_data->x, peer_key_data->len,
8150 output, expected_output->len,
8151 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008152 ASSERT_COMPARE( output, output_length,
8153 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008154 mbedtls_free( output );
8155 output = NULL;
8156 output_length = ~0;
8157
8158 /* Larger buffer */
8159 ASSERT_ALLOC( output, expected_output->len + 1 );
8160 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8161 peer_key_data->x, peer_key_data->len,
8162 output, expected_output->len + 1,
8163 &output_length ) );
8164 ASSERT_COMPARE( output, output_length,
8165 expected_output->x, expected_output->len );
8166 mbedtls_free( output );
8167 output = NULL;
8168 output_length = ~0;
8169
8170 /* Buffer too small */
8171 ASSERT_ALLOC( output, expected_output->len - 1 );
8172 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8173 peer_key_data->x, peer_key_data->len,
8174 output, expected_output->len - 1,
8175 &output_length ),
8176 PSA_ERROR_BUFFER_TOO_SMALL );
8177 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008178 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008179 mbedtls_free( output );
8180 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008181
8182exit:
8183 mbedtls_free( output );
8184 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008185 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008186}
8187/* END_CASE */
8188
8189/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008190void key_agreement_capacity( int alg_arg,
8191 int our_key_type_arg, data_t *our_key_data,
8192 data_t *peer_key_data,
8193 int expected_capacity_arg )
8194{
Ronald Cron5425a212020-08-04 14:58:35 +02008195 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008196 psa_algorithm_t alg = alg_arg;
8197 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008198 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008199 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008200 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008201 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008202
Gilles Peskine8817f612018-12-18 00:18:46 +01008203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008204
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008205 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8206 psa_set_key_algorithm( &attributes, alg );
8207 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008208 PSA_ASSERT( psa_import_key( &attributes,
8209 our_key_data->x, our_key_data->len,
8210 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008211
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008212 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008213 PSA_ASSERT( psa_key_derivation_key_agreement(
8214 &operation,
8215 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8216 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008217 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8218 {
8219 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008220 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008221 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008222 NULL, 0 ) );
8223 }
Gilles Peskine59685592018-09-18 12:11:34 +02008224
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008225 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008226 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008227 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008228 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008229
Gilles Peskinebf491972018-10-25 22:36:12 +02008230 /* Test the actual capacity by reading the output. */
8231 while( actual_capacity > sizeof( output ) )
8232 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008233 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008234 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008235 actual_capacity -= sizeof( output );
8236 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008237 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008238 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008239 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008240 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008241
Gilles Peskine59685592018-09-18 12:11:34 +02008242exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008243 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008244 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008245 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008246}
8247/* END_CASE */
8248
8249/* BEGIN_CASE */
8250void key_agreement_output( int alg_arg,
8251 int our_key_type_arg, data_t *our_key_data,
8252 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008253 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008254{
Ronald Cron5425a212020-08-04 14:58:35 +02008255 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008256 psa_algorithm_t alg = alg_arg;
8257 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008258 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008259 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008260 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008261
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008262 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8263 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008264
Gilles Peskine8817f612018-12-18 00:18:46 +01008265 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008266
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008267 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8268 psa_set_key_algorithm( &attributes, alg );
8269 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008270 PSA_ASSERT( psa_import_key( &attributes,
8271 our_key_data->x, our_key_data->len,
8272 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008273
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008274 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008275 PSA_ASSERT( psa_key_derivation_key_agreement(
8276 &operation,
8277 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8278 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008279 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8280 {
8281 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008282 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008283 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008284 NULL, 0 ) );
8285 }
Gilles Peskine59685592018-09-18 12:11:34 +02008286
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008287 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008288 actual_output,
8289 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008290 ASSERT_COMPARE( actual_output, expected_output1->len,
8291 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008292 if( expected_output2->len != 0 )
8293 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008294 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008295 actual_output,
8296 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008297 ASSERT_COMPARE( actual_output, expected_output2->len,
8298 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008299 }
Gilles Peskine59685592018-09-18 12:11:34 +02008300
8301exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008302 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008303 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008304 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008305 mbedtls_free( actual_output );
8306}
8307/* END_CASE */
8308
8309/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008310void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008311{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008312 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008313 unsigned char *output = NULL;
8314 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008315 size_t i;
8316 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008317
Simon Butcher49f8e312020-03-03 15:51:50 +00008318 TEST_ASSERT( bytes_arg >= 0 );
8319
Gilles Peskine91892022021-02-08 19:50:26 +01008320 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008321 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008322
Gilles Peskine8817f612018-12-18 00:18:46 +01008323 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008324
Gilles Peskinea50d7392018-06-21 10:22:13 +02008325 /* Run several times, to ensure that every output byte will be
8326 * nonzero at least once with overwhelming probability
8327 * (2^(-8*number_of_runs)). */
8328 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008329 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008330 if( bytes != 0 )
8331 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008332 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008333
Gilles Peskinea50d7392018-06-21 10:22:13 +02008334 for( i = 0; i < bytes; i++ )
8335 {
8336 if( output[i] != 0 )
8337 ++changed[i];
8338 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008339 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008340
8341 /* Check that every byte was changed to nonzero at least once. This
8342 * validates that psa_generate_random is overwriting every byte of
8343 * the output buffer. */
8344 for( i = 0; i < bytes; i++ )
8345 {
8346 TEST_ASSERT( changed[i] != 0 );
8347 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008348
8349exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008350 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008351 mbedtls_free( output );
8352 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008353}
8354/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008355
8356/* BEGIN_CASE */
8357void generate_key( int type_arg,
8358 int bits_arg,
8359 int usage_arg,
8360 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008361 int expected_status_arg,
8362 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008363{
Ronald Cron5425a212020-08-04 14:58:35 +02008364 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008365 psa_key_type_t type = type_arg;
8366 psa_key_usage_t usage = usage_arg;
8367 size_t bits = bits_arg;
8368 psa_algorithm_t alg = alg_arg;
8369 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008371 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008372
Gilles Peskine8817f612018-12-18 00:18:46 +01008373 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008374
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008375 psa_set_key_usage_flags( &attributes, usage );
8376 psa_set_key_algorithm( &attributes, alg );
8377 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008378 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008379
8380 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008381 psa_status_t status = psa_generate_key( &attributes, &key );
8382
8383 if( is_large_key > 0 )
8384 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8385 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008386 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008387 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008388
8389 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008390 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008391 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8392 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008393
Gilles Peskine818ca122018-06-20 18:16:48 +02008394 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008395 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008396 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008397
8398exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008399 /*
8400 * Key attributes may have been returned by psa_get_key_attributes()
8401 * thus reset them as required.
8402 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008403 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008404
Ronald Cron5425a212020-08-04 14:58:35 +02008405 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008406 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008407}
8408/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008409
Ronald Cronee414c72021-03-18 18:50:08 +01008410/* 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 +02008411void generate_key_rsa( int bits_arg,
8412 data_t *e_arg,
8413 int expected_status_arg )
8414{
Ronald Cron5425a212020-08-04 14:58:35 +02008415 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008416 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008417 size_t bits = bits_arg;
8418 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8419 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8420 psa_status_t expected_status = expected_status_arg;
8421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8422 uint8_t *exported = NULL;
8423 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008424 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008425 size_t exported_length = SIZE_MAX;
8426 uint8_t *e_read_buffer = NULL;
8427 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008428 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008429 size_t e_read_length = SIZE_MAX;
8430
8431 if( e_arg->len == 0 ||
8432 ( e_arg->len == 3 &&
8433 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8434 {
8435 is_default_public_exponent = 1;
8436 e_read_size = 0;
8437 }
8438 ASSERT_ALLOC( e_read_buffer, e_read_size );
8439 ASSERT_ALLOC( exported, exported_size );
8440
8441 PSA_ASSERT( psa_crypto_init( ) );
8442
8443 psa_set_key_usage_flags( &attributes, usage );
8444 psa_set_key_algorithm( &attributes, alg );
8445 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8446 e_arg->x, e_arg->len ) );
8447 psa_set_key_bits( &attributes, bits );
8448
8449 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008450 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008451 if( expected_status != PSA_SUCCESS )
8452 goto exit;
8453
8454 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008455 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008456 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8457 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8458 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8459 e_read_buffer, e_read_size,
8460 &e_read_length ) );
8461 if( is_default_public_exponent )
8462 TEST_EQUAL( e_read_length, 0 );
8463 else
8464 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8465
8466 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008467 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008468 goto exit;
8469
8470 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008471 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008472 exported, exported_size,
8473 &exported_length ) );
8474 {
8475 uint8_t *p = exported;
8476 uint8_t *end = exported + exported_length;
8477 size_t len;
8478 /* RSAPublicKey ::= SEQUENCE {
8479 * modulus INTEGER, -- n
8480 * publicExponent INTEGER } -- e
8481 */
8482 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008483 MBEDTLS_ASN1_SEQUENCE |
8484 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008485 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008486 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8487 MBEDTLS_ASN1_INTEGER ) );
8488 if( len >= 1 && p[0] == 0 )
8489 {
8490 ++p;
8491 --len;
8492 }
8493 if( e_arg->len == 0 )
8494 {
8495 TEST_EQUAL( len, 3 );
8496 TEST_EQUAL( p[0], 1 );
8497 TEST_EQUAL( p[1], 0 );
8498 TEST_EQUAL( p[2], 1 );
8499 }
8500 else
8501 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8502 }
8503
8504exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008505 /*
8506 * Key attributes may have been returned by psa_get_key_attributes() or
8507 * set by psa_set_key_domain_parameters() thus reset them as required.
8508 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008509 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008510
Ronald Cron5425a212020-08-04 14:58:35 +02008511 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008512 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008513 mbedtls_free( e_read_buffer );
8514 mbedtls_free( exported );
8515}
8516/* END_CASE */
8517
Darryl Greend49a4992018-06-18 17:27:26 +01008518/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008519void persistent_key_load_key_from_storage( data_t *data,
8520 int type_arg, int bits_arg,
8521 int usage_flags_arg, int alg_arg,
8522 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008523{
Ronald Cron71016a92020-08-28 19:01:50 +02008524 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008525 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008526 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8527 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008528 psa_key_type_t type = type_arg;
8529 size_t bits = bits_arg;
8530 psa_key_usage_t usage_flags = usage_flags_arg;
8531 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008532 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008533 unsigned char *first_export = NULL;
8534 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008535 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008536 size_t first_exported_length;
8537 size_t second_exported_length;
8538
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008539 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8540 {
8541 ASSERT_ALLOC( first_export, export_size );
8542 ASSERT_ALLOC( second_export, export_size );
8543 }
Darryl Greend49a4992018-06-18 17:27:26 +01008544
Gilles Peskine8817f612018-12-18 00:18:46 +01008545 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008546
Gilles Peskinec87af662019-05-15 16:12:22 +02008547 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008548 psa_set_key_usage_flags( &attributes, usage_flags );
8549 psa_set_key_algorithm( &attributes, alg );
8550 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008551 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008552
Darryl Green0c6575a2018-11-07 16:05:30 +00008553 switch( generation_method )
8554 {
8555 case IMPORT_KEY:
8556 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008557 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008558 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008559 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008560
Darryl Green0c6575a2018-11-07 16:05:30 +00008561 case GENERATE_KEY:
8562 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008563 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008564 break;
8565
8566 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008567#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008568 {
8569 /* Create base key */
8570 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8571 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8572 psa_set_key_usage_flags( &base_attributes,
8573 PSA_KEY_USAGE_DERIVE );
8574 psa_set_key_algorithm( &base_attributes, derive_alg );
8575 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008576 PSA_ASSERT( psa_import_key( &base_attributes,
8577 data->x, data->len,
8578 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008579 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008580 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008581 PSA_ASSERT( psa_key_derivation_input_key(
8582 &operation,
8583 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008584 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008585 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008586 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008587 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8588 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008589 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008590 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008591 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008592 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008593 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008594#else
8595 TEST_ASSUME( ! "KDF not supported in this configuration" );
8596#endif
8597 break;
8598
8599 default:
8600 TEST_ASSERT( ! "generation_method not implemented in test" );
8601 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008602 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008603 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008604
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008605 /* Export the key if permitted by the key policy. */
8606 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8607 {
Ronald Cron5425a212020-08-04 14:58:35 +02008608 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008609 first_export, export_size,
8610 &first_exported_length ) );
8611 if( generation_method == IMPORT_KEY )
8612 ASSERT_COMPARE( data->x, data->len,
8613 first_export, first_exported_length );
8614 }
Darryl Greend49a4992018-06-18 17:27:26 +01008615
8616 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008617 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008618 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008619 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008620
Darryl Greend49a4992018-06-18 17:27:26 +01008621 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008622 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008623 TEST_ASSERT( mbedtls_svc_key_id_equal(
8624 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008625 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8626 PSA_KEY_LIFETIME_PERSISTENT );
8627 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8628 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008629 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008630 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008631 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008632
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008633 /* Export the key again if permitted by the key policy. */
8634 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008635 {
Ronald Cron5425a212020-08-04 14:58:35 +02008636 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008637 second_export, export_size,
8638 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008639 ASSERT_COMPARE( first_export, first_exported_length,
8640 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008641 }
8642
8643 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008644 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008645 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008646
8647exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008648 /*
8649 * Key attributes may have been returned by psa_get_key_attributes()
8650 * thus reset them as required.
8651 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008652 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008653
Darryl Greend49a4992018-06-18 17:27:26 +01008654 mbedtls_free( first_export );
8655 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008656 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008657 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008658 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008659 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008660}
8661/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008662
Neil Armstronga557cb82022-06-10 08:58:32 +02008663/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008664void ecjpake_setup( int alg_arg, int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008665 int input_first, data_t *pw_data,
Neil Armstrongd597bc72022-05-25 11:28:39 +02008666 int expected_status_arg )
8667{
8668 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8669 psa_pake_operation_t operation = psa_pake_operation_init();
8670 psa_algorithm_t alg = alg_arg;
8671 psa_algorithm_t hash_alg = hash_arg;
8672 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008673 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8674 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8675 psa_status_t expected_status = expected_status_arg;
8676 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8677 unsigned char *output_buffer = NULL;
8678 size_t output_len = 0;
8679
8680 PSA_INIT( );
8681
8682 ASSERT_ALLOC( output_buffer,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008683 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8684 PSA_PAKE_STEP_KEY_SHARE) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008685
8686 if( pw_data->len > 0 )
8687 {
8688 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8689 psa_set_key_algorithm( &attributes, alg );
8690 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8691 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8692 &key ) );
8693 }
8694
8695 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8696 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8697 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8698
Neil Armstrong645cccd2022-06-08 17:36:23 +02008699 PSA_ASSERT( psa_pake_abort( &operation ) );
8700
8701 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8702 PSA_ERROR_BAD_STATE );
8703 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8704 PSA_ERROR_BAD_STATE );
8705 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8706 PSA_ERROR_BAD_STATE );
8707 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8708 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008709 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8710 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008711 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008712 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008713 PSA_ERROR_BAD_STATE );
8714
8715 PSA_ASSERT( psa_pake_abort( &operation ) );
8716
Neil Armstrongd597bc72022-05-25 11:28:39 +02008717 status = psa_pake_setup( &operation, &cipher_suite );
8718 if( status != PSA_SUCCESS )
8719 {
8720 TEST_EQUAL( status, expected_status );
8721 goto exit;
8722 }
8723 else
8724 PSA_ASSERT( status );
8725
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008726 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8727 PSA_ERROR_BAD_STATE );
8728
Neil Armstrongd597bc72022-05-25 11:28:39 +02008729 status = psa_pake_set_role( &operation, role );
8730 if( status != PSA_SUCCESS )
8731 {
8732 TEST_EQUAL( status, expected_status );
8733 goto exit;
8734 }
8735 else
8736 PSA_ASSERT( status );
8737
8738 if( pw_data->len > 0 )
8739 {
8740 status = psa_pake_set_password_key( &operation, key );
8741 if( status != PSA_SUCCESS )
8742 {
8743 TEST_EQUAL( status, expected_status );
8744 goto exit;
8745 }
8746 else
8747 PSA_ASSERT( status );
8748 }
8749
Neil Armstrong707d9572022-06-08 17:31:49 +02008750 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8751 PSA_ERROR_INVALID_ARGUMENT );
8752 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8753 PSA_ERROR_INVALID_ARGUMENT );
8754
8755 const uint8_t unsupported_id[] = "abcd";
8756
8757 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8758 PSA_ERROR_NOT_SUPPORTED );
8759 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8760 PSA_ERROR_NOT_SUPPORTED );
8761
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008762 /* First round */
8763 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008764 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008765 /* Invalid parameters */
8766 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8767 NULL, 0 ),
8768 PSA_ERROR_INVALID_ARGUMENT );
8769 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8770 output_buffer, 66 ),
8771 PSA_ERROR_INVALID_ARGUMENT );
8772 /* Invalid first step */
8773 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8774 output_buffer, 66 ),
8775 PSA_ERROR_BAD_STATE );
8776
8777 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8778 output_buffer, 66 ),
8779 expected_status);
8780
8781 if( expected_status == PSA_SUCCESS )
8782 {
8783 /* Buffer too large */
8784 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8785 output_buffer, 512 ),
8786 PSA_ERROR_INSUFFICIENT_MEMORY );
8787
8788 /* The operation should be aborted at this point */
8789 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8790 output_buffer, 66 ),
8791 PSA_ERROR_BAD_STATE );
8792 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008793 }
8794 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008795 {
8796 /* Invalid parameters */
8797 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8798 NULL, 0, NULL ),
8799 PSA_ERROR_INVALID_ARGUMENT );
8800 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8801 output_buffer, 512, &output_len ),
8802 PSA_ERROR_INVALID_ARGUMENT );
8803 /* Invalid first step */
8804 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8805 output_buffer, 512, &output_len ),
8806 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008807
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008808 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8809 output_buffer, 512, &output_len ),
8810 expected_status );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008811
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008812 if( expected_status == PSA_SUCCESS )
8813 {
8814 TEST_ASSERT( output_len > 0 );
8815
8816 /* Buffer too small */
8817 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8818 output_buffer, 5, &output_len ),
8819 PSA_ERROR_BUFFER_TOO_SMALL );
8820
8821 /* The operation should be aborted at this point */
8822 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8823 output_buffer, 512, &output_len ),
8824 PSA_ERROR_BAD_STATE );
8825 }
8826 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008827
8828exit:
8829 PSA_ASSERT( psa_destroy_key( key ) );
8830 PSA_ASSERT( psa_pake_abort( &operation ) );
8831 mbedtls_free( output_buffer );
8832 PSA_DONE( );
8833}
8834/* END_CASE */
8835
Neil Armstronga557cb82022-06-10 08:58:32 +02008836/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008837void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8838 int client_input_first, int inject_error,
8839 data_t *pw_data )
8840{
8841 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8842 psa_pake_operation_t server = psa_pake_operation_init();
8843 psa_pake_operation_t client = psa_pake_operation_init();
8844 psa_algorithm_t alg = alg_arg;
8845 psa_algorithm_t hash_alg = hash_arg;
8846 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8848
8849 PSA_INIT( );
8850
8851 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8852 psa_set_key_algorithm( &attributes, alg );
8853 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8854 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8855 &key ) );
8856
8857 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8858 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8859 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8860
8861
8862 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8863 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8864
8865 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8866 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8867
8868 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8869 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8870
8871 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8872 client_input_first, 1,
8873 inject_error ), 1 );
8874
8875 if( inject_error == 1 || inject_error == 2 )
8876 goto exit;
8877
8878 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8879 client_input_first, 2,
8880 inject_error ), 1 );
8881
8882exit:
8883 psa_destroy_key( key );
8884 psa_pake_abort( &server );
8885 psa_pake_abort( &client );
8886 PSA_DONE( );
8887}
8888/* END_CASE */
8889
8890/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008891void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008892 int derive_alg_arg, data_t *pw_data,
8893 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008894{
8895 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8896 psa_pake_operation_t server = psa_pake_operation_init();
8897 psa_pake_operation_t client = psa_pake_operation_init();
8898 psa_algorithm_t alg = alg_arg;
8899 psa_algorithm_t hash_alg = hash_arg;
8900 psa_algorithm_t derive_alg = derive_alg_arg;
8901 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8902 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8903 psa_key_derivation_operation_t server_derive =
8904 PSA_KEY_DERIVATION_OPERATION_INIT;
8905 psa_key_derivation_operation_t client_derive =
8906 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008907
8908 PSA_INIT( );
8909
Neil Armstrongd597bc72022-05-25 11:28:39 +02008910 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8911 psa_set_key_algorithm( &attributes, alg );
8912 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8913 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8914 &key ) );
8915
8916 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8917 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8918 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8919
Neil Armstrong1e855602022-06-15 11:32:11 +02008920 /* Get shared key */
8921 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
8922 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
8923
8924 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
8925 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
8926 {
8927 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
8928 PSA_KEY_DERIVATION_INPUT_SEED,
8929 (const uint8_t*) "", 0) );
8930 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
8931 PSA_KEY_DERIVATION_INPUT_SEED,
8932 (const uint8_t*) "", 0) );
8933 }
8934
Neil Armstrongd597bc72022-05-25 11:28:39 +02008935 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8936 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8937
8938 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8939 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8940
8941 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8942 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8943
Neil Armstrong1e855602022-06-15 11:32:11 +02008944 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8945 PSA_ERROR_BAD_STATE );
8946 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8947 PSA_ERROR_BAD_STATE );
8948
Neil Armstrongf983caf2022-06-15 15:27:48 +02008949 /* First round */
8950 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8951 client_input_first, 1, 0 ), 1 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008952
Neil Armstrong1e855602022-06-15 11:32:11 +02008953 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8954 PSA_ERROR_BAD_STATE );
8955 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8956 PSA_ERROR_BAD_STATE );
8957
Neil Armstrongf983caf2022-06-15 15:27:48 +02008958 /* Second round */
8959 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8960 client_input_first, 2, 0 ), 1 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008961
Neil Armstrongd597bc72022-05-25 11:28:39 +02008962 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
8963 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
8964
8965exit:
8966 psa_key_derivation_abort( &server_derive );
8967 psa_key_derivation_abort( &client_derive );
8968 psa_destroy_key( key );
8969 psa_pake_abort( &server );
8970 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008971 PSA_DONE( );
8972}
8973/* END_CASE */