blob: 29a007792ff74be1fdd513fad174834582f8ff8c [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;
735 int ret;
736
737 ASSERT_ALLOC( buffer0, buffer_length );
738 ASSERT_ALLOC( buffer1, buffer_length );
739
740 switch( round )
741 {
742 case 1:
743 /* Server first round Output */
744 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
745 buffer0 + buffer0_off,
746 512 - buffer0_off, &s_g1_len ) );
747 s_g1_off = buffer0_off;
748 buffer0_off += s_g1_len;
749 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
750 buffer0 + buffer0_off,
751 512 - buffer0_off, &s_x1_pk_len ) );
752 s_x1_pk_off = buffer0_off;
753 buffer0_off += s_x1_pk_len;
754 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
755 buffer0 + buffer0_off,
756 512 - buffer0_off, &s_x1_pr_len ) );
757 s_x1_pr_off = buffer0_off;
758 buffer0_off += s_x1_pr_len;
759 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
760 buffer0 + buffer0_off,
761 512 - buffer0_off, &s_g2_len ) );
762 s_g2_off = buffer0_off;
763 buffer0_off += s_g2_len;
764 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
765 buffer0 + buffer0_off,
766 512 - buffer0_off, &s_x2_pk_len ) );
767 s_x2_pk_off = buffer0_off;
768 buffer0_off += s_x2_pk_len;
769 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
770 buffer0 + buffer0_off,
771 512 - buffer0_off, &s_x2_pr_len ) );
772 s_x2_pr_off = buffer0_off;
773 buffer0_off += s_x2_pr_len;
774
775 if( inject_error == 1 )
776 {
777 buffer0[s_x1_pk_off + 12] >>= 4;
778 buffer0[s_x2_pk_off + 7] <<= 4;
779 expected_status = PSA_ERROR_DATA_INVALID;
780 }
781
782 if( client_input_first == 1 )
783 {
784 /* Client first round Input */
785 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
786 buffer0 + s_g1_off, s_g1_len ) );
787 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
788 buffer0 + s_x1_pk_off,
789 s_x1_pk_len ) );
790 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
791 buffer0 + s_x1_pr_off,
792 s_x1_pr_len ) );
793 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
794 buffer0 + s_g2_off,
795 s_g2_len ) );
796 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
797 buffer0 + s_x2_pk_off,
798 s_x2_pk_len ) );
799 TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
800 buffer0 + s_x2_pr_off,
801 s_x2_pr_len ),
802 expected_status );
803
804 if( inject_error == 1 )
805 {
806 ret = 1;
807 goto exit;
808 }
809 }
810
811 /* Client first round Output */
812 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
813 buffer1 + buffer1_off,
814 512 - buffer1_off, &c_g1_len ) );
815 c_g1_off = buffer1_off;
816 buffer1_off += c_g1_len;
817 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
818 buffer1 + buffer1_off,
819 512 - buffer1_off, &c_x1_pk_len ) );
820 c_x1_pk_off = buffer1_off;
821 buffer1_off += c_x1_pk_len;
822 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
823 buffer1 + buffer1_off,
824 512 - buffer1_off, &c_x1_pr_len ) );
825 c_x1_pr_off = buffer1_off;
826 buffer1_off += c_x1_pr_len;
827 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
828 buffer1 + buffer1_off,
829 512 - buffer1_off, &c_g2_len ) );
830 c_g2_off = buffer1_off;
831 buffer1_off += c_g2_len;
832 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
833 buffer1 + buffer1_off,
834 512 - buffer1_off, &c_x2_pk_len ) );
835 c_x2_pk_off = buffer1_off;
836 buffer1_off += c_x2_pk_len;
837 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
838 buffer1 + buffer1_off,
839 512 - buffer1_off, &c_x2_pr_len ) );
840 c_x2_pr_off = buffer1_off;
841 buffer1_off += c_x2_pr_len;
842
843 if( client_input_first == 0 )
844 {
845 /* Client first round Input */
846 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
847 buffer0 + s_g1_off, s_g1_len ) );
848 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
849 buffer0 + s_x1_pk_off,
850 s_x1_pk_len ) );
851 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
852 buffer0 + s_x1_pr_off,
853 s_x1_pr_len ) );
854 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
855 buffer0 + s_g2_off,
856 s_g2_len ) );
857 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
858 buffer0 + s_x2_pk_off,
859 s_x2_pk_len ) );
860 TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
861 buffer0 + s_x2_pr_off,
862 s_x2_pr_len ),
863 expected_status );
864
865 if( inject_error == 1 )
866 break;
867 }
868
869 if( inject_error == 2 )
870 {
871 buffer1[c_x1_pk_off + 12] >>= 4;
872 buffer1[c_x2_pk_off + 7] <<= 4;
873 expected_status = PSA_ERROR_DATA_INVALID;
874 }
875
876 /* Server first round Input */
877 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
878 buffer1 + c_g1_off, c_g1_len ) );
879 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
880 buffer1 + c_x1_pk_off, c_x1_pk_len ) );
881 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
882 buffer1 + c_x1_pr_off, c_x1_pr_len ) );
883 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
884 buffer1 + c_g2_off, c_g2_len ) );
885 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
886 buffer1 + c_x2_pk_off, c_x2_pk_len ) );
887 TEST_EQUAL( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
888 buffer1 + c_x2_pr_off, c_x2_pr_len ),
889 expected_status );
890
891 break;
892
893 case 2:
894 /* Server second round Output */
895 buffer0_off = 0;
896
897 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
898 buffer0 + buffer0_off,
899 512 - buffer0_off, &s_a_len ) );
900 s_a_off = buffer0_off;
901 buffer0_off += s_a_len;
902 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
903 buffer0 + buffer0_off,
904 512 - buffer0_off, &s_x2s_pk_len ) );
905 s_x2s_pk_off = buffer0_off;
906 buffer0_off += s_x2s_pk_len;
907 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
908 buffer0 + buffer0_off,
909 512 - buffer0_off, &s_x2s_pr_len ) );
910 s_x2s_pr_off = buffer0_off;
911 buffer0_off += s_x2s_pr_len;
912
913 if( inject_error == 3 )
914 {
915 buffer0[s_x2s_pk_off + 12] >>= 4;
916 expected_status = PSA_ERROR_DATA_INVALID;
917 }
918
919 if( client_input_first == 1 )
920 {
921 /* Client second round Input */
922 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
923 buffer0 + s_a_off, s_a_len ) );
924 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
925 buffer0 + s_x2s_pk_off,
926 s_x2s_pk_len ) );
927 TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
928 buffer0 + s_x2s_pr_off,
929 s_x2s_pr_len ),
930 expected_status );
931
932 if( inject_error == 3 )
933 break;
934 }
935
936 /* Client second round Output */
937 buffer1_off = 0;
938
939 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
940 buffer1 + buffer1_off,
941 512 - buffer1_off, &c_a_len ) );
942 c_a_off = buffer1_off;
943 buffer1_off += c_a_len;
944 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
945 buffer1 + buffer1_off,
946 512 - buffer1_off, &c_x2s_pk_len ) );
947 c_x2s_pk_off = buffer1_off;
948 buffer1_off += c_x2s_pk_len;
949 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
950 buffer1 + buffer1_off,
951 512 - buffer1_off, &c_x2s_pr_len ) );
952 c_x2s_pr_off = buffer1_off;
953 buffer1_off += c_x2s_pr_len;
954
955 if( client_input_first == 0 )
956 {
957 /* Client second round Input */
958 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
959 buffer0 + s_a_off, s_a_len ) );
960 PSA_ASSERT( psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
961 buffer0 + s_x2s_pk_off,
962 s_x2s_pk_len ) );
963 TEST_EQUAL( psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
964 buffer0 + s_x2s_pr_off,
965 s_x2s_pr_len ),
966 expected_status );
967
968 if( inject_error == 3 )
969 break;
970 }
971
972 if( inject_error == 4 )
973 {
974 buffer1[c_x2s_pk_off + 12] >>= 4;
975 expected_status = PSA_ERROR_DATA_INVALID;
976 }
977
978 /* Server second round Input */
979 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
980 buffer1 + c_a_off, c_a_len ) );
981 PSA_ASSERT( psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
982 buffer1 + c_x2s_pk_off, c_x2s_pk_len ) );
983 TEST_EQUAL( psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
984 buffer1 + c_x2s_pr_off, c_x2s_pr_len ),
985 expected_status );
986
987 break;
988
989 }
990
991 ret = 1;
992
993exit:
994 mbedtls_free( buffer0 );
995 mbedtls_free( buffer1 );
996 return( ret );
997}
Neil Armstrong75673ab2022-06-15 17:39:01 +0200998#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +0200999
Gilles Peskinee59236f2018-01-27 23:32:46 +01001000/* END_HEADER */
1001
1002/* BEGIN_DEPENDENCIES
1003 * depends_on:MBEDTLS_PSA_CRYPTO_C
1004 * END_DEPENDENCIES
1005 */
1006
1007/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001008void static_checks( )
1009{
1010 size_t max_truncated_mac_size =
1011 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1012
1013 /* Check that the length for a truncated MAC always fits in the algorithm
1014 * encoding. The shifted mask is the maximum truncated value. The
1015 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001016 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001017}
1018/* END_CASE */
1019
1020/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001021void import_with_policy( int type_arg,
1022 int usage_arg, int alg_arg,
1023 int expected_status_arg )
1024{
1025 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1026 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001027 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001028 psa_key_type_t type = type_arg;
1029 psa_key_usage_t usage = usage_arg;
1030 psa_algorithm_t alg = alg_arg;
1031 psa_status_t expected_status = expected_status_arg;
1032 const uint8_t key_material[16] = {0};
1033 psa_status_t status;
1034
1035 PSA_ASSERT( psa_crypto_init( ) );
1036
1037 psa_set_key_type( &attributes, type );
1038 psa_set_key_usage_flags( &attributes, usage );
1039 psa_set_key_algorithm( &attributes, alg );
1040
1041 status = psa_import_key( &attributes,
1042 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001043 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001044 TEST_EQUAL( status, expected_status );
1045 if( status != PSA_SUCCESS )
1046 goto exit;
1047
Ronald Cron5425a212020-08-04 14:58:35 +02001048 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001049 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001050 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001051 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001052 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001053 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001054
Ronald Cron5425a212020-08-04 14:58:35 +02001055 PSA_ASSERT( psa_destroy_key( key ) );
1056 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001057
1058exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001059 /*
1060 * Key attributes may have been returned by psa_get_key_attributes()
1061 * thus reset them as required.
1062 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001063 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001064
1065 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001066 PSA_DONE( );
1067}
1068/* END_CASE */
1069
1070/* BEGIN_CASE */
1071void import_with_data( data_t *data, int type_arg,
1072 int attr_bits_arg,
1073 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001074{
1075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1076 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001077 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001078 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001079 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001080 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001082
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001084
Gilles Peskine4747d192019-04-17 15:05:45 +02001085 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001086 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001087
Ronald Cron5425a212020-08-04 14:58:35 +02001088 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001089 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001090 if( status != PSA_SUCCESS )
1091 goto exit;
1092
Ronald Cron5425a212020-08-04 14:58:35 +02001093 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001094 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001095 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001096 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001097 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001098
Ronald Cron5425a212020-08-04 14:58:35 +02001099 PSA_ASSERT( psa_destroy_key( key ) );
1100 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001101
1102exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001103 /*
1104 * Key attributes may have been returned by psa_get_key_attributes()
1105 * thus reset them as required.
1106 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001107 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001108
1109 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001110 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001115void import_large_key( int type_arg, int byte_size_arg,
1116 int expected_status_arg )
1117{
1118 psa_key_type_t type = type_arg;
1119 size_t byte_size = byte_size_arg;
1120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1121 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001122 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001123 psa_status_t status;
1124 uint8_t *buffer = NULL;
1125 size_t buffer_size = byte_size + 1;
1126 size_t n;
1127
Steven Cooreman69967ce2021-01-18 18:01:08 +01001128 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001129 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001130 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001131 memset( buffer, 'K', byte_size );
1132
1133 PSA_ASSERT( psa_crypto_init( ) );
1134
1135 /* Try importing the key */
1136 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1137 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001138 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001139 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001140 TEST_EQUAL( status, expected_status );
1141
1142 if( status == PSA_SUCCESS )
1143 {
Ronald Cron5425a212020-08-04 14:58:35 +02001144 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001145 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1146 TEST_EQUAL( psa_get_key_bits( &attributes ),
1147 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001148 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001149 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001150 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001151 for( n = 0; n < byte_size; n++ )
1152 TEST_EQUAL( buffer[n], 'K' );
1153 for( n = byte_size; n < buffer_size; n++ )
1154 TEST_EQUAL( buffer[n], 0 );
1155 }
1156
1157exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001158 /*
1159 * Key attributes may have been returned by psa_get_key_attributes()
1160 * thus reset them as required.
1161 */
1162 psa_reset_key_attributes( &attributes );
1163
Ronald Cron5425a212020-08-04 14:58:35 +02001164 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001165 PSA_DONE( );
1166 mbedtls_free( buffer );
1167}
1168/* END_CASE */
1169
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001170/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001171void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1172{
Ronald Cron5425a212020-08-04 14:58:35 +02001173 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001174 size_t bits = bits_arg;
1175 psa_status_t expected_status = expected_status_arg;
1176 psa_status_t status;
1177 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001178 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001179 size_t buffer_size = /* Slight overapproximations */
1180 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001181 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001182 unsigned char *p;
1183 int ret;
1184 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001185 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001186
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001188 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001189
1190 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1191 bits, keypair ) ) >= 0 );
1192 length = ret;
1193
1194 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001195 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001196 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001197 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001198
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001199 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001200 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001201
1202exit:
1203 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001204 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001209void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001210 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001211 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301212 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001213 int expected_bits,
1214 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001215 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001216 int canonical_input )
1217{
Ronald Cron5425a212020-08-04 14:58:35 +02001218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001219 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001220 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001221 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001222 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301223 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224 unsigned char *exported = NULL;
1225 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001226 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001227 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001230 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001231
Moran Pekercb088e72018-07-17 17:36:59 +03001232 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001233 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001235 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001236 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001237
Archana4d7ae1d2021-07-07 02:50:22 +05301238 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001239 psa_set_key_usage_flags( &attributes, usage_arg );
1240 psa_set_key_algorithm( &attributes, alg );
1241 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001242
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001243 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001244 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245
1246 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001247 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001248 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1249 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001250 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001251
1252 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001253 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001254 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001255
1256 /* The exported length must be set by psa_export_key() to a value between 0
1257 * and export_size. On errors, the exported length must be 0. */
1258 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1259 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001260 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001261
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001262 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001263 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001265 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001266 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001267 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001268 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269
Gilles Peskineea38a922021-02-13 00:05:16 +01001270 /* Run sanity checks on the exported key. For non-canonical inputs,
1271 * this validates the canonical representations. For canonical inputs,
1272 * this doesn't directly validate the implementation, but it still helps
1273 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301274 if( !psa_key_lifetime_is_external( lifetime ) )
1275 {
1276 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1277 goto exit;
1278 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001279
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001281 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282 else
1283 {
Ronald Cron5425a212020-08-04 14:58:35 +02001284 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001285 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001286 &key2 ) );
1287 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001288 reexported,
1289 export_size,
1290 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001291 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301292 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001293 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001295 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301296 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301297 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001298 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299
1300destroy:
1301 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001302 PSA_ASSERT( psa_destroy_key( key ) );
1303 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304
1305exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001306 /*
1307 * Key attributes may have been returned by psa_get_key_attributes()
1308 * thus reset them as required.
1309 */
1310 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301311 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001312 mbedtls_free( exported );
1313 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001314 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315}
1316/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001317
Moran Pekerf709f4a2018-06-06 17:26:04 +03001318/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001319void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001320 int type_arg,
1321 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301322 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001323 int export_size_delta,
1324 int expected_export_status_arg,
1325 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001326{
Ronald Cron5425a212020-08-04 14:58:35 +02001327 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001328 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001329 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001330 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001331 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301332 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001333 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001334 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001335 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001337
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001339
Archana4d7ae1d2021-07-07 02:50:22 +05301340 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001341 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1342 psa_set_key_algorithm( &attributes, alg );
1343 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001344
1345 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001346 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001347
Gilles Peskine49c25912018-10-29 15:15:31 +01001348 /* Export the public key */
1349 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001350 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001351 exported, export_size,
1352 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001353 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001354 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001355 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001356 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001357 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001358 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001360 TEST_LE_U( expected_public_key->len,
1361 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1362 TEST_LE_U( expected_public_key->len,
1363 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1364 TEST_LE_U( expected_public_key->len,
1365 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001366 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1367 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001368 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001369exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001370 /*
1371 * Key attributes may have been returned by psa_get_key_attributes()
1372 * thus reset them as required.
1373 */
1374 psa_reset_key_attributes( &attributes );
1375
itayzafrir3e02b3b2018-06-12 17:06:52 +03001376 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001377 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001378 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001379}
1380/* END_CASE */
1381
Gilles Peskine20035e32018-02-03 22:44:14 +01001382/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001383void import_and_exercise_key( data_t *data,
1384 int type_arg,
1385 int bits_arg,
1386 int alg_arg )
1387{
Ronald Cron5425a212020-08-04 14:58:35 +02001388 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001389 psa_key_type_t type = type_arg;
1390 size_t bits = bits_arg;
1391 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001392 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001393 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001394 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001395
Gilles Peskine8817f612018-12-18 00:18:46 +01001396 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001397
Gilles Peskine4747d192019-04-17 15:05:45 +02001398 psa_set_key_usage_flags( &attributes, usage );
1399 psa_set_key_algorithm( &attributes, alg );
1400 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001401
1402 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001403 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001404
1405 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001406 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001407 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1408 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001409
1410 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001411 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001412 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001413
Ronald Cron5425a212020-08-04 14:58:35 +02001414 PSA_ASSERT( psa_destroy_key( key ) );
1415 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001416
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001417exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001418 /*
1419 * Key attributes may have been returned by psa_get_key_attributes()
1420 * thus reset them as required.
1421 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001422 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001423
1424 psa_reset_key_attributes( &attributes );
1425 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001426 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001427}
1428/* END_CASE */
1429
1430/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001431void effective_key_attributes( int type_arg, int expected_type_arg,
1432 int bits_arg, int expected_bits_arg,
1433 int usage_arg, int expected_usage_arg,
1434 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001435{
Ronald Cron5425a212020-08-04 14:58:35 +02001436 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001437 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001438 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001439 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001440 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001441 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001442 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001443 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001444 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001446
Gilles Peskine8817f612018-12-18 00:18:46 +01001447 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001448
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001449 psa_set_key_usage_flags( &attributes, usage );
1450 psa_set_key_algorithm( &attributes, alg );
1451 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001452 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001453
Ronald Cron5425a212020-08-04 14:58:35 +02001454 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001455 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001456
Ronald Cron5425a212020-08-04 14:58:35 +02001457 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001458 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1459 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1460 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1461 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
1463exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001464 /*
1465 * Key attributes may have been returned by psa_get_key_attributes()
1466 * thus reset them as required.
1467 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001468 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001469
1470 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001471 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001472}
1473/* END_CASE */
1474
1475/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001476void check_key_policy( int type_arg, int bits_arg,
1477 int usage_arg, int alg_arg )
1478{
1479 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001480 usage_arg,
1481 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001482 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001483 goto exit;
1484}
1485/* END_CASE */
1486
1487/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001488void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001489{
1490 /* Test each valid way of initializing the object, except for `= {0}`, as
1491 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1492 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001493 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001494 psa_key_attributes_t func = psa_key_attributes_init( );
1495 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1496 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001497
1498 memset( &zero, 0, sizeof( zero ) );
1499
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001500 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1501 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1502 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001503
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001504 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1505 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1506 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1507
1508 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1509 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1510 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1511
1512 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1513 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1514 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1515
1516 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1517 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1518 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001519}
1520/* END_CASE */
1521
1522/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001523void mac_key_policy( int policy_usage_arg,
1524 int policy_alg_arg,
1525 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001527 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001528 int expected_status_sign_arg,
1529 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001530{
Ronald Cron5425a212020-08-04 14:58:35 +02001531 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001533 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001534 psa_key_type_t key_type = key_type_arg;
1535 psa_algorithm_t policy_alg = policy_alg_arg;
1536 psa_algorithm_t exercise_alg = exercise_alg_arg;
1537 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001538 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001539 psa_status_t expected_status_sign = expected_status_sign_arg;
1540 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001541 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001542
Gilles Peskine8817f612018-12-18 00:18:46 +01001543 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001544
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001545 psa_set_key_usage_flags( &attributes, policy_usage );
1546 psa_set_key_algorithm( &attributes, policy_alg );
1547 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001548
Gilles Peskine049c7532019-05-15 20:22:09 +02001549 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001550 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001551
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001552 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1553 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001554
Ronald Cron5425a212020-08-04 14:58:35 +02001555 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001556 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001557
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001558 /* Calculate the MAC, one-shot case. */
1559 uint8_t input[128] = {0};
1560 size_t mac_len;
1561 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1562 input, 128,
1563 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1564 expected_status_sign );
1565
Neil Armstrong3af9b972022-02-07 12:20:21 +01001566 /* Calculate the MAC, multi-part case. */
1567 PSA_ASSERT( psa_mac_abort( &operation ) );
1568 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1569 if( status == PSA_SUCCESS )
1570 {
1571 status = psa_mac_update( &operation, input, 128 );
1572 if( status == PSA_SUCCESS )
1573 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1574 &mac_len ),
1575 expected_status_sign );
1576 else
1577 TEST_EQUAL( status, expected_status_sign );
1578 }
1579 else
1580 {
1581 TEST_EQUAL( status, expected_status_sign );
1582 }
1583 PSA_ASSERT( psa_mac_abort( &operation ) );
1584
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001585 /* Verify correct MAC, one-shot case. */
1586 status = psa_mac_verify( key, exercise_alg, input, 128,
1587 mac, mac_len );
1588
1589 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1590 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001591 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001592 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001593
Neil Armstrong3af9b972022-02-07 12:20:21 +01001594 /* Verify correct MAC, multi-part case. */
1595 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1596 if( status == PSA_SUCCESS )
1597 {
1598 status = psa_mac_update( &operation, input, 128 );
1599 if( status == PSA_SUCCESS )
1600 {
1601 status = psa_mac_verify_finish( &operation, mac, mac_len );
1602 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1603 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1604 else
1605 TEST_EQUAL( status, expected_status_verify );
1606 }
1607 else
1608 {
1609 TEST_EQUAL( status, expected_status_verify );
1610 }
1611 }
1612 else
1613 {
1614 TEST_EQUAL( status, expected_status_verify );
1615 }
1616
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001617 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001618
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001620 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001621 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622
1623exit:
1624 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001625 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001626 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627}
1628/* END_CASE */
1629
1630/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001631void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 int policy_alg,
1633 int key_type,
1634 data_t *key_data,
1635 int exercise_alg )
1636{
Ronald Cron5425a212020-08-04 14:58:35 +02001637 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001638 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001639 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001640 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001641 size_t output_buffer_size = 0;
1642 size_t input_buffer_size = 0;
1643 size_t output_length = 0;
1644 uint8_t *output = NULL;
1645 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001646 psa_status_t status;
1647
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001648 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1649 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1650 input_buffer_size );
1651
1652 ASSERT_ALLOC( input, input_buffer_size );
1653 ASSERT_ALLOC( output, output_buffer_size );
1654
Gilles Peskine8817f612018-12-18 00:18:46 +01001655 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001656
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001657 psa_set_key_usage_flags( &attributes, policy_usage );
1658 psa_set_key_algorithm( &attributes, policy_alg );
1659 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660
Gilles Peskine049c7532019-05-15 20:22:09 +02001661 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001662 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001663
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001664 /* Check if no key usage flag implication is done */
1665 TEST_EQUAL( policy_usage,
1666 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001667
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001668 /* Encrypt check, one-shot */
1669 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1670 output, output_buffer_size,
1671 &output_length);
1672 if( policy_alg == exercise_alg &&
1673 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1674 PSA_ASSERT( status );
1675 else
1676 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1677
1678 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001679 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680 if( policy_alg == exercise_alg &&
1681 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001682 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001684 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685 psa_cipher_abort( &operation );
1686
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001687 /* Decrypt check, one-shot */
1688 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1689 input, input_buffer_size,
1690 &output_length);
1691 if( policy_alg == exercise_alg &&
1692 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1693 PSA_ASSERT( status );
1694 else
1695 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1696
1697 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001698 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001699 if( policy_alg == exercise_alg &&
1700 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001701 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001702 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001703 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001704
1705exit:
1706 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001707 mbedtls_free( input );
1708 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001709 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001710 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001711}
1712/* END_CASE */
1713
1714/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001715void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716 int policy_alg,
1717 int key_type,
1718 data_t *key_data,
1719 int nonce_length_arg,
1720 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001721 int exercise_alg,
1722 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001723{
Ronald Cron5425a212020-08-04 14:58:35 +02001724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001726 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001727 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001728 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001729 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001730 unsigned char nonce[16] = {0};
1731 size_t nonce_length = nonce_length_arg;
1732 unsigned char tag[16];
1733 size_t tag_length = tag_length_arg;
1734 size_t output_length;
1735
Gilles Peskine7be11a72022-04-14 00:12:57 +02001736 TEST_LE_U( nonce_length, sizeof( nonce ) );
1737 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001738
Gilles Peskine8817f612018-12-18 00:18:46 +01001739 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001740
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001741 psa_set_key_usage_flags( &attributes, policy_usage );
1742 psa_set_key_algorithm( &attributes, policy_alg );
1743 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001744
Gilles Peskine049c7532019-05-15 20:22:09 +02001745 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001746 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001747
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001748 /* Check if no key usage implication is done */
1749 TEST_EQUAL( policy_usage,
1750 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001751
Neil Armstrong752d8112022-02-07 14:51:11 +01001752 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02001753 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001754 nonce, nonce_length,
1755 NULL, 0,
1756 NULL, 0,
1757 tag, tag_length,
1758 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001759 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1760 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001761 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001762 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001763
Neil Armstrong752d8112022-02-07 14:51:11 +01001764 /* Encrypt check, multi-part */
1765 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
1766 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1767 TEST_EQUAL( status, expected_status );
1768 else
1769 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1770
1771 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001772 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001773 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001774 nonce, nonce_length,
1775 NULL, 0,
1776 tag, tag_length,
1777 NULL, 0,
1778 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001779 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1780 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1781 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001782 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001783 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001784 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001785
Neil Armstrong752d8112022-02-07 14:51:11 +01001786 /* Decrypt check, multi-part */
1787 PSA_ASSERT( psa_aead_abort( &operation ) );
1788 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
1789 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1790 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1791 else
1792 TEST_EQUAL( status, expected_status );
1793
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001794exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01001795 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001796 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001797 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001802void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001803 int policy_alg,
1804 int key_type,
1805 data_t *key_data,
1806 int exercise_alg )
1807{
Ronald Cron5425a212020-08-04 14:58:35 +02001808 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001810 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001811 psa_status_t status;
1812 size_t key_bits;
1813 size_t buffer_length;
1814 unsigned char *buffer = NULL;
1815 size_t output_length;
1816
Gilles Peskine8817f612018-12-18 00:18:46 +01001817 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001818
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001819 psa_set_key_usage_flags( &attributes, policy_usage );
1820 psa_set_key_algorithm( &attributes, policy_alg );
1821 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001822
Gilles Peskine049c7532019-05-15 20:22:09 +02001823 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001824 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001825
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001826 /* Check if no key usage implication is done */
1827 TEST_EQUAL( policy_usage,
1828 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001829
Ronald Cron5425a212020-08-04 14:58:35 +02001830 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001831 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001832 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1833 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001834 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001835
Ronald Cron5425a212020-08-04 14:58:35 +02001836 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837 NULL, 0,
1838 NULL, 0,
1839 buffer, buffer_length,
1840 &output_length );
1841 if( policy_alg == exercise_alg &&
1842 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001843 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001844 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001845 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001846
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001847 if( buffer_length != 0 )
1848 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001849 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001850 buffer, buffer_length,
1851 NULL, 0,
1852 buffer, buffer_length,
1853 &output_length );
1854 if( policy_alg == exercise_alg &&
1855 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001856 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001857 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001858 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001859
1860exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001861 /*
1862 * Key attributes may have been returned by psa_get_key_attributes()
1863 * thus reset them as required.
1864 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001865 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001866
1867 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001868 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001869 mbedtls_free( buffer );
1870}
1871/* END_CASE */
1872
1873/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001874void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001875 int policy_alg,
1876 int key_type,
1877 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001878 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001879 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001880 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001881{
Ronald Cron5425a212020-08-04 14:58:35 +02001882 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001884 psa_key_usage_t policy_usage = policy_usage_arg;
1885 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001887 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1888 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1889 * compatible with the policy and `payload_length_arg` is supposed to be
1890 * a valid input length to sign. If `payload_length_arg <= 0`,
1891 * `exercise_alg` is supposed to be forbidden by the policy. */
1892 int compatible_alg = payload_length_arg > 0;
1893 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001894 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895 size_t signature_length;
1896
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001897 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001898 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001899 TEST_EQUAL( expected_usage,
1900 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001901
Gilles Peskine8817f612018-12-18 00:18:46 +01001902 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001903
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001904 psa_set_key_usage_flags( &attributes, policy_usage );
1905 psa_set_key_algorithm( &attributes, policy_alg );
1906 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001907
Gilles Peskine049c7532019-05-15 20:22:09 +02001908 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001909 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001910
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001911 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1912
Ronald Cron5425a212020-08-04 14:58:35 +02001913 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001914 payload, payload_length,
1915 signature, sizeof( signature ),
1916 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001917 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001918 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001919 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001920 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921
1922 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001923 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001924 payload, payload_length,
1925 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001926 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001927 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001929 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001930
Gilles Peskinef7b41372021-09-22 16:15:05 +02001931 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001932 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001933 {
1934 status = psa_sign_message( key, exercise_alg,
1935 payload, payload_length,
1936 signature, sizeof( signature ),
1937 &signature_length );
1938 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1939 PSA_ASSERT( status );
1940 else
1941 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1942
1943 memset( signature, 0, sizeof( signature ) );
1944 status = psa_verify_message( key, exercise_alg,
1945 payload, payload_length,
1946 signature, sizeof( signature ) );
1947 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1948 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1949 else
1950 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1951 }
1952
Gilles Peskined5b33222018-06-18 22:20:03 +02001953exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001954 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001955 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001956}
1957/* END_CASE */
1958
Janos Follathba3fab92019-06-11 14:50:16 +01001959/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001960void derive_key_policy( int policy_usage,
1961 int policy_alg,
1962 int key_type,
1963 data_t *key_data,
1964 int exercise_alg )
1965{
Ronald Cron5425a212020-08-04 14:58:35 +02001966 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001967 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001968 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001969 psa_status_t status;
1970
Gilles Peskine8817f612018-12-18 00:18:46 +01001971 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001972
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001973 psa_set_key_usage_flags( &attributes, policy_usage );
1974 psa_set_key_algorithm( &attributes, policy_alg );
1975 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001976
Gilles Peskine049c7532019-05-15 20:22:09 +02001977 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001978 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001979
Janos Follathba3fab92019-06-11 14:50:16 +01001980 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1981
1982 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1983 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001984 {
Janos Follathba3fab92019-06-11 14:50:16 +01001985 PSA_ASSERT( psa_key_derivation_input_bytes(
1986 &operation,
1987 PSA_KEY_DERIVATION_INPUT_SEED,
1988 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001989 }
Janos Follathba3fab92019-06-11 14:50:16 +01001990
1991 status = psa_key_derivation_input_key( &operation,
1992 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001993 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001994
Gilles Peskineea0fb492018-07-12 17:17:20 +02001995 if( policy_alg == exercise_alg &&
1996 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001997 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001998 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001999 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002000
2001exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002002 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002003 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002004 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002005}
2006/* END_CASE */
2007
2008/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002009void agreement_key_policy( int policy_usage,
2010 int policy_alg,
2011 int key_type_arg,
2012 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002013 int exercise_alg,
2014 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002015{
Ronald Cron5425a212020-08-04 14:58:35 +02002016 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002017 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002018 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002019 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002020 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002021 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002022
Gilles Peskine8817f612018-12-18 00:18:46 +01002023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002024
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002025 psa_set_key_usage_flags( &attributes, policy_usage );
2026 psa_set_key_algorithm( &attributes, policy_alg );
2027 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002028
Gilles Peskine049c7532019-05-15 20:22:09 +02002029 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002030 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002032 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002033 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002034
Steven Cooremance48e852020-10-05 16:02:45 +02002035 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002036
2037exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002038 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002039 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002040 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002041}
2042/* END_CASE */
2043
2044/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002045void key_policy_alg2( int key_type_arg, data_t *key_data,
2046 int usage_arg, int alg_arg, int alg2_arg )
2047{
Ronald Cron5425a212020-08-04 14:58:35 +02002048 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002049 psa_key_type_t key_type = key_type_arg;
2050 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2051 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2052 psa_key_usage_t usage = usage_arg;
2053 psa_algorithm_t alg = alg_arg;
2054 psa_algorithm_t alg2 = alg2_arg;
2055
2056 PSA_ASSERT( psa_crypto_init( ) );
2057
2058 psa_set_key_usage_flags( &attributes, usage );
2059 psa_set_key_algorithm( &attributes, alg );
2060 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2061 psa_set_key_type( &attributes, key_type );
2062 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002063 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002064
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002065 /* Update the usage flags to obtain implicit usage flags */
2066 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002067 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002068 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2069 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2070 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2071
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002072 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002073 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002074 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002075 goto exit;
2076
2077exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002078 /*
2079 * Key attributes may have been returned by psa_get_key_attributes()
2080 * thus reset them as required.
2081 */
2082 psa_reset_key_attributes( &got_attributes );
2083
Ronald Cron5425a212020-08-04 14:58:35 +02002084 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002085 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002086}
2087/* END_CASE */
2088
2089/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002090void raw_agreement_key_policy( int policy_usage,
2091 int policy_alg,
2092 int key_type_arg,
2093 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002094 int exercise_alg,
2095 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002096{
Ronald Cron5425a212020-08-04 14:58:35 +02002097 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002099 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002100 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002101 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002102 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002103
2104 PSA_ASSERT( psa_crypto_init( ) );
2105
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002106 psa_set_key_usage_flags( &attributes, policy_usage );
2107 psa_set_key_algorithm( &attributes, policy_alg );
2108 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002109
Gilles Peskine049c7532019-05-15 20:22:09 +02002110 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002111 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002112
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002113 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002114
Steven Cooremance48e852020-10-05 16:02:45 +02002115 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002116
2117exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002118 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002119 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002120 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002121}
2122/* END_CASE */
2123
2124/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002125void copy_success( int source_usage_arg,
2126 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302127 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002128 int type_arg, data_t *material,
2129 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002130 int target_usage_arg,
2131 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302132 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002133 int expected_usage_arg,
2134 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002135{
Gilles Peskineca25db92019-04-19 11:43:08 +02002136 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2137 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002138 psa_key_usage_t expected_usage = expected_usage_arg;
2139 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002140 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302141 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2142 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002143 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2144 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002145 uint8_t *export_buffer = NULL;
2146
Gilles Peskine57ab7212019-01-28 13:03:09 +01002147 PSA_ASSERT( psa_crypto_init( ) );
2148
Gilles Peskineca25db92019-04-19 11:43:08 +02002149 /* Prepare the source key. */
2150 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2151 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002152 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002153 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302154 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002155 PSA_ASSERT( psa_import_key( &source_attributes,
2156 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002157 &source_key ) );
2158 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002159
Gilles Peskineca25db92019-04-19 11:43:08 +02002160 /* Prepare the target attributes. */
2161 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002162 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002163 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002164 }
Archana8a180362021-07-05 02:18:48 +05302165 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002166
Gilles Peskineca25db92019-04-19 11:43:08 +02002167 if( target_usage_arg != -1 )
2168 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2169 if( target_alg_arg != -1 )
2170 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002171 if( target_alg2_arg != -1 )
2172 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002173
Archana8a180362021-07-05 02:18:48 +05302174
Gilles Peskine57ab7212019-01-28 13:03:09 +01002175 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002176 PSA_ASSERT( psa_copy_key( source_key,
2177 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002178
2179 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002180 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002181
2182 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002183 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002184 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2185 psa_get_key_type( &target_attributes ) );
2186 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2187 psa_get_key_bits( &target_attributes ) );
2188 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2189 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002190 TEST_EQUAL( expected_alg2,
2191 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002192 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2193 {
2194 size_t length;
2195 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002196 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002197 material->len, &length ) );
2198 ASSERT_COMPARE( material->x, material->len,
2199 export_buffer, length );
2200 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002201
Archana8a180362021-07-05 02:18:48 +05302202 if( !psa_key_lifetime_is_external( target_lifetime ) )
2203 {
2204 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2205 goto exit;
2206 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2207 goto exit;
2208 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002209
Ronald Cron5425a212020-08-04 14:58:35 +02002210 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002211
2212exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002213 /*
2214 * Source and target key attributes may have been returned by
2215 * psa_get_key_attributes() thus reset them as required.
2216 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002217 psa_reset_key_attributes( &source_attributes );
2218 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002219
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002220 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002221 mbedtls_free( export_buffer );
2222}
2223/* END_CASE */
2224
2225/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002226void copy_fail( int source_usage_arg,
2227 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302228 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002229 int type_arg, data_t *material,
2230 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002231 int target_usage_arg,
2232 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002233 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002234 int expected_status_arg )
2235{
2236 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2237 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002238 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2239 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002240 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002241
2242 PSA_ASSERT( psa_crypto_init( ) );
2243
2244 /* Prepare the source key. */
2245 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2246 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002247 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002248 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302249 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002250 PSA_ASSERT( psa_import_key( &source_attributes,
2251 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002252 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002253
2254 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002255 psa_set_key_id( &target_attributes, key_id );
2256 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002257 psa_set_key_type( &target_attributes, target_type_arg );
2258 psa_set_key_bits( &target_attributes, target_bits_arg );
2259 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2260 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002261 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002262
2263 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002264 TEST_EQUAL( psa_copy_key( source_key,
2265 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002266 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002267
Ronald Cron5425a212020-08-04 14:58:35 +02002268 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002269
Gilles Peskine4a644642019-05-03 17:14:08 +02002270exit:
2271 psa_reset_key_attributes( &source_attributes );
2272 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002273 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002274}
2275/* END_CASE */
2276
2277/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002278void hash_operation_init( )
2279{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002280 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002281 /* Test each valid way of initializing the object, except for `= {0}`, as
2282 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2283 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002284 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002285 psa_hash_operation_t func = psa_hash_operation_init( );
2286 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2287 psa_hash_operation_t zero;
2288
2289 memset( &zero, 0, sizeof( zero ) );
2290
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002291 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002292 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2293 PSA_ERROR_BAD_STATE );
2294 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2295 PSA_ERROR_BAD_STATE );
2296 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2297 PSA_ERROR_BAD_STATE );
2298
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002299 /* A default hash operation should be abortable without error. */
2300 PSA_ASSERT( psa_hash_abort( &func ) );
2301 PSA_ASSERT( psa_hash_abort( &init ) );
2302 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002303}
2304/* END_CASE */
2305
2306/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002307void hash_setup( int alg_arg,
2308 int expected_status_arg )
2309{
2310 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002311 uint8_t *output = NULL;
2312 size_t output_size = 0;
2313 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002314 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002315 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002316 psa_status_t status;
2317
Gilles Peskine8817f612018-12-18 00:18:46 +01002318 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002319
Neil Armstrongedb20862022-02-07 15:47:44 +01002320 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002321 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002322 ASSERT_ALLOC( output, output_size );
2323
2324 status = psa_hash_compute( alg, NULL, 0,
2325 output, output_size, &output_length );
2326 TEST_EQUAL( status, expected_status );
2327
2328 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002329 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002330 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002331
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002332 /* Whether setup succeeded or failed, abort must succeed. */
2333 PSA_ASSERT( psa_hash_abort( &operation ) );
2334
2335 /* If setup failed, reproduce the failure, so as to
2336 * test the resulting state of the operation object. */
2337 if( status != PSA_SUCCESS )
2338 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2339
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002340 /* Now the operation object should be reusable. */
2341#if defined(KNOWN_SUPPORTED_HASH_ALG)
2342 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2343 PSA_ASSERT( psa_hash_abort( &operation ) );
2344#endif
2345
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002346exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002347 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002348 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002349}
2350/* END_CASE */
2351
2352/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002353void hash_compute_fail( int alg_arg, data_t *input,
2354 int output_size_arg, int expected_status_arg )
2355{
2356 psa_algorithm_t alg = alg_arg;
2357 uint8_t *output = NULL;
2358 size_t output_size = output_size_arg;
2359 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002360 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002361 psa_status_t expected_status = expected_status_arg;
2362 psa_status_t status;
2363
2364 ASSERT_ALLOC( output, output_size );
2365
2366 PSA_ASSERT( psa_crypto_init( ) );
2367
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002368 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002369 status = psa_hash_compute( alg, input->x, input->len,
2370 output, output_size, &output_length );
2371 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002372 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002373
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002374 /* Hash Compute, multi-part */
2375 status = psa_hash_setup( &operation, alg );
2376 if( status == PSA_SUCCESS )
2377 {
2378 status = psa_hash_update( &operation, input->x, input->len );
2379 if( status == PSA_SUCCESS )
2380 {
2381 status = psa_hash_finish( &operation, output, output_size,
2382 &output_length );
2383 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002384 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002385 else
2386 TEST_EQUAL( status, expected_status );
2387 }
2388 else
2389 {
2390 TEST_EQUAL( status, expected_status );
2391 }
2392 }
2393 else
2394 {
2395 TEST_EQUAL( status, expected_status );
2396 }
2397
Gilles Peskine0a749c82019-11-28 19:33:58 +01002398exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002399 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002400 mbedtls_free( output );
2401 PSA_DONE( );
2402}
2403/* END_CASE */
2404
2405/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002406void hash_compare_fail( int alg_arg, data_t *input,
2407 data_t *reference_hash,
2408 int expected_status_arg )
2409{
2410 psa_algorithm_t alg = alg_arg;
2411 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002412 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002413 psa_status_t status;
2414
2415 PSA_ASSERT( psa_crypto_init( ) );
2416
Neil Armstrong55a1be12022-02-07 11:23:20 +01002417 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002418 status = psa_hash_compare( alg, input->x, input->len,
2419 reference_hash->x, reference_hash->len );
2420 TEST_EQUAL( status, expected_status );
2421
Neil Armstrong55a1be12022-02-07 11:23:20 +01002422 /* Hash Compare, multi-part */
2423 status = psa_hash_setup( &operation, alg );
2424 if( status == PSA_SUCCESS )
2425 {
2426 status = psa_hash_update( &operation, input->x, input->len );
2427 if( status == PSA_SUCCESS )
2428 {
2429 status = psa_hash_verify( &operation, reference_hash->x,
2430 reference_hash->len );
2431 TEST_EQUAL( status, expected_status );
2432 }
2433 else
2434 {
2435 TEST_EQUAL( status, expected_status );
2436 }
2437 }
2438 else
2439 {
2440 TEST_EQUAL( status, expected_status );
2441 }
2442
Gilles Peskine88e08462020-01-28 20:43:00 +01002443exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002444 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002445 PSA_DONE( );
2446}
2447/* END_CASE */
2448
2449/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002450void hash_compute_compare( int alg_arg, data_t *input,
2451 data_t *expected_output )
2452{
2453 psa_algorithm_t alg = alg_arg;
2454 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2455 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002456 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002457 size_t i;
2458
2459 PSA_ASSERT( psa_crypto_init( ) );
2460
Neil Armstrongca30a002022-02-07 11:40:23 +01002461 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002462 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002463 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002464 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002465 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002466 ASSERT_COMPARE( output, output_length,
2467 expected_output->x, expected_output->len );
2468
Neil Armstrongca30a002022-02-07 11:40:23 +01002469 /* Compute with tight buffer, multi-part */
2470 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2471 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2472 PSA_ASSERT( psa_hash_finish( &operation, output,
2473 PSA_HASH_LENGTH( alg ),
2474 &output_length ) );
2475 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2476 ASSERT_COMPARE( output, output_length,
2477 expected_output->x, expected_output->len );
2478
2479 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002480 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2481 output, sizeof( output ),
2482 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002483 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002484 ASSERT_COMPARE( output, output_length,
2485 expected_output->x, expected_output->len );
2486
Neil Armstrongca30a002022-02-07 11:40:23 +01002487 /* Compute with larger buffer, multi-part */
2488 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2489 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2490 PSA_ASSERT( psa_hash_finish( &operation, output,
2491 sizeof( output ), &output_length ) );
2492 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2493 ASSERT_COMPARE( output, output_length,
2494 expected_output->x, expected_output->len );
2495
2496 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002497 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2498 output, output_length ) );
2499
Neil Armstrongca30a002022-02-07 11:40:23 +01002500 /* Compare with correct hash, multi-part */
2501 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2502 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2503 PSA_ASSERT( psa_hash_verify( &operation, output,
2504 output_length ) );
2505
2506 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002507 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2508 output, output_length + 1 ),
2509 PSA_ERROR_INVALID_SIGNATURE );
2510
Neil Armstrongca30a002022-02-07 11:40:23 +01002511 /* Compare with trailing garbage, multi-part */
2512 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2513 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2514 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2515 PSA_ERROR_INVALID_SIGNATURE );
2516
2517 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002518 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2519 output, output_length - 1 ),
2520 PSA_ERROR_INVALID_SIGNATURE );
2521
Neil Armstrongca30a002022-02-07 11:40:23 +01002522 /* Compare with truncated hash, multi-part */
2523 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2524 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2525 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2526 PSA_ERROR_INVALID_SIGNATURE );
2527
Gilles Peskine0a749c82019-11-28 19:33:58 +01002528 /* Compare with corrupted value */
2529 for( i = 0; i < output_length; i++ )
2530 {
Chris Jones9634bb12021-01-20 15:56:42 +00002531 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002532 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002533
2534 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002535 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2536 output, output_length ),
2537 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002538
2539 /* Multi-Part */
2540 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2541 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2542 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2543 PSA_ERROR_INVALID_SIGNATURE );
2544
Gilles Peskine0a749c82019-11-28 19:33:58 +01002545 output[i] ^= 1;
2546 }
2547
2548exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002549 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002550 PSA_DONE( );
2551}
2552/* END_CASE */
2553
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002554/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002555void hash_bad_order( )
2556{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002557 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002558 unsigned char input[] = "";
2559 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002560 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002561 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2562 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2563 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002564 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002565 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002566 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002567
Gilles Peskine8817f612018-12-18 00:18:46 +01002568 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002569
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002570 /* Call setup twice in a row. */
2571 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002572 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002573 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2574 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002575 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002576 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002577 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002578
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002579 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002580 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002581 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002582 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002583
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002584 /* Check that update calls abort on error. */
2585 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002586 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002587 ASSERT_OPERATION_IS_ACTIVE( operation );
2588 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2589 PSA_ERROR_BAD_STATE );
2590 ASSERT_OPERATION_IS_INACTIVE( operation );
2591 PSA_ASSERT( psa_hash_abort( &operation ) );
2592 ASSERT_OPERATION_IS_INACTIVE( operation );
2593
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002594 /* Call update after finish. */
2595 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2596 PSA_ASSERT( psa_hash_finish( &operation,
2597 hash, sizeof( hash ), &hash_len ) );
2598 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002599 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002600 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002601
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002602 /* Call verify without calling setup beforehand. */
2603 TEST_EQUAL( psa_hash_verify( &operation,
2604 valid_hash, sizeof( valid_hash ) ),
2605 PSA_ERROR_BAD_STATE );
2606 PSA_ASSERT( psa_hash_abort( &operation ) );
2607
2608 /* Call verify after finish. */
2609 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2610 PSA_ASSERT( psa_hash_finish( &operation,
2611 hash, sizeof( hash ), &hash_len ) );
2612 TEST_EQUAL( psa_hash_verify( &operation,
2613 valid_hash, sizeof( valid_hash ) ),
2614 PSA_ERROR_BAD_STATE );
2615 PSA_ASSERT( psa_hash_abort( &operation ) );
2616
2617 /* Call verify twice in a row. */
2618 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002619 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002620 PSA_ASSERT( psa_hash_verify( &operation,
2621 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002622 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002623 TEST_EQUAL( psa_hash_verify( &operation,
2624 valid_hash, sizeof( valid_hash ) ),
2625 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002626 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002627 PSA_ASSERT( psa_hash_abort( &operation ) );
2628
2629 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002630 TEST_EQUAL( psa_hash_finish( &operation,
2631 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002632 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002633 PSA_ASSERT( psa_hash_abort( &operation ) );
2634
2635 /* Call finish twice in a row. */
2636 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2637 PSA_ASSERT( psa_hash_finish( &operation,
2638 hash, sizeof( hash ), &hash_len ) );
2639 TEST_EQUAL( psa_hash_finish( &operation,
2640 hash, sizeof( hash ), &hash_len ),
2641 PSA_ERROR_BAD_STATE );
2642 PSA_ASSERT( psa_hash_abort( &operation ) );
2643
2644 /* Call finish after calling verify. */
2645 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2646 PSA_ASSERT( psa_hash_verify( &operation,
2647 valid_hash, sizeof( valid_hash ) ) );
2648 TEST_EQUAL( psa_hash_finish( &operation,
2649 hash, sizeof( hash ), &hash_len ),
2650 PSA_ERROR_BAD_STATE );
2651 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002652
2653exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002654 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002655}
2656/* END_CASE */
2657
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002658/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002659void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002660{
2661 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002662 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2663 * appended to it */
2664 unsigned char hash[] = {
2665 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2666 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2667 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002668 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002669 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002670
Gilles Peskine8817f612018-12-18 00:18:46 +01002671 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002672
itayzafrir27e69452018-11-01 14:26:34 +02002673 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002675 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002676 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002677 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002678 ASSERT_OPERATION_IS_INACTIVE( operation );
2679 PSA_ASSERT( psa_hash_abort( &operation ) );
2680 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002681
itayzafrir27e69452018-11-01 14:26:34 +02002682 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002683 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002684 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002685 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002686
itayzafrir27e69452018-11-01 14:26:34 +02002687 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002688 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002689 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002690 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002691
itayzafrirec93d302018-10-18 18:01:10 +03002692exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002693 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002694}
2695/* END_CASE */
2696
Ronald Cronee414c72021-03-18 18:50:08 +01002697/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002698void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002699{
2700 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002701 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002702 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002703 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002704 size_t hash_len;
2705
Gilles Peskine8817f612018-12-18 00:18:46 +01002706 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002707
itayzafrir58028322018-10-25 10:22:01 +03002708 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002709 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002710 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002711 hash, expected_size - 1, &hash_len ),
2712 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002713
2714exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002715 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002716}
2717/* END_CASE */
2718
Ronald Cronee414c72021-03-18 18:50:08 +01002719/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002720void hash_clone_source_state( )
2721{
2722 psa_algorithm_t alg = PSA_ALG_SHA_256;
2723 unsigned char hash[PSA_HASH_MAX_SIZE];
2724 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2725 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2726 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2727 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2728 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2729 size_t hash_len;
2730
2731 PSA_ASSERT( psa_crypto_init( ) );
2732 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2733
2734 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2735 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2736 PSA_ASSERT( psa_hash_finish( &op_finished,
2737 hash, sizeof( hash ), &hash_len ) );
2738 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2739 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2740
2741 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2742 PSA_ERROR_BAD_STATE );
2743
2744 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2745 PSA_ASSERT( psa_hash_finish( &op_init,
2746 hash, sizeof( hash ), &hash_len ) );
2747 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2748 PSA_ASSERT( psa_hash_finish( &op_finished,
2749 hash, sizeof( hash ), &hash_len ) );
2750 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2751 PSA_ASSERT( psa_hash_finish( &op_aborted,
2752 hash, sizeof( hash ), &hash_len ) );
2753
2754exit:
2755 psa_hash_abort( &op_source );
2756 psa_hash_abort( &op_init );
2757 psa_hash_abort( &op_setup );
2758 psa_hash_abort( &op_finished );
2759 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002760 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002761}
2762/* END_CASE */
2763
Ronald Cronee414c72021-03-18 18:50:08 +01002764/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002765void hash_clone_target_state( )
2766{
2767 psa_algorithm_t alg = PSA_ALG_SHA_256;
2768 unsigned char hash[PSA_HASH_MAX_SIZE];
2769 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2770 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2771 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2772 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2773 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2774 size_t hash_len;
2775
2776 PSA_ASSERT( psa_crypto_init( ) );
2777
2778 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2779 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2780 PSA_ASSERT( psa_hash_finish( &op_finished,
2781 hash, sizeof( hash ), &hash_len ) );
2782 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2783 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2784
2785 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2786 PSA_ASSERT( psa_hash_finish( &op_target,
2787 hash, sizeof( hash ), &hash_len ) );
2788
2789 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2790 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2791 PSA_ERROR_BAD_STATE );
2792 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2793 PSA_ERROR_BAD_STATE );
2794
2795exit:
2796 psa_hash_abort( &op_target );
2797 psa_hash_abort( &op_init );
2798 psa_hash_abort( &op_setup );
2799 psa_hash_abort( &op_finished );
2800 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002801 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002802}
2803/* END_CASE */
2804
itayzafrir58028322018-10-25 10:22:01 +03002805/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002806void mac_operation_init( )
2807{
Jaeden Amero252ef282019-02-15 14:05:35 +00002808 const uint8_t input[1] = { 0 };
2809
Jaeden Amero769ce272019-01-04 11:48:03 +00002810 /* Test each valid way of initializing the object, except for `= {0}`, as
2811 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2812 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002813 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00002814 psa_mac_operation_t func = psa_mac_operation_init( );
2815 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2816 psa_mac_operation_t zero;
2817
2818 memset( &zero, 0, sizeof( zero ) );
2819
Jaeden Amero252ef282019-02-15 14:05:35 +00002820 /* A freshly-initialized MAC operation should not be usable. */
2821 TEST_EQUAL( psa_mac_update( &func,
2822 input, sizeof( input ) ),
2823 PSA_ERROR_BAD_STATE );
2824 TEST_EQUAL( psa_mac_update( &init,
2825 input, sizeof( input ) ),
2826 PSA_ERROR_BAD_STATE );
2827 TEST_EQUAL( psa_mac_update( &zero,
2828 input, sizeof( input ) ),
2829 PSA_ERROR_BAD_STATE );
2830
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002831 /* A default MAC operation should be abortable without error. */
2832 PSA_ASSERT( psa_mac_abort( &func ) );
2833 PSA_ASSERT( psa_mac_abort( &init ) );
2834 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002835}
2836/* END_CASE */
2837
2838/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002839void mac_setup( int key_type_arg,
2840 data_t *key,
2841 int alg_arg,
2842 int expected_status_arg )
2843{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002844 psa_key_type_t key_type = key_type_arg;
2845 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002846 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002847 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002848 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2849#if defined(KNOWN_SUPPORTED_MAC_ALG)
2850 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2851#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002852
Gilles Peskine8817f612018-12-18 00:18:46 +01002853 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002854
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002855 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2856 &operation, &status ) )
2857 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002858 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002859
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002860 /* The operation object should be reusable. */
2861#if defined(KNOWN_SUPPORTED_MAC_ALG)
2862 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2863 smoke_test_key_data,
2864 sizeof( smoke_test_key_data ),
2865 KNOWN_SUPPORTED_MAC_ALG,
2866 &operation, &status ) )
2867 goto exit;
2868 TEST_EQUAL( status, PSA_SUCCESS );
2869#endif
2870
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002871exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002872 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002873}
2874/* END_CASE */
2875
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002876/* 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 +00002877void mac_bad_order( )
2878{
Ronald Cron5425a212020-08-04 14:58:35 +02002879 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002880 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2881 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002882 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002883 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2884 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2885 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002887 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2888 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2889 size_t sign_mac_length = 0;
2890 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2891 const uint8_t verify_mac[] = {
2892 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2893 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2894 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2895
2896 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002897 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002898 psa_set_key_algorithm( &attributes, alg );
2899 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002900
Ronald Cron5425a212020-08-04 14:58:35 +02002901 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2902 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002903
Jaeden Amero252ef282019-02-15 14:05:35 +00002904 /* Call update without calling setup beforehand. */
2905 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2906 PSA_ERROR_BAD_STATE );
2907 PSA_ASSERT( psa_mac_abort( &operation ) );
2908
2909 /* Call sign finish without calling setup beforehand. */
2910 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2911 &sign_mac_length),
2912 PSA_ERROR_BAD_STATE );
2913 PSA_ASSERT( psa_mac_abort( &operation ) );
2914
2915 /* Call verify finish without calling setup beforehand. */
2916 TEST_EQUAL( psa_mac_verify_finish( &operation,
2917 verify_mac, sizeof( verify_mac ) ),
2918 PSA_ERROR_BAD_STATE );
2919 PSA_ASSERT( psa_mac_abort( &operation ) );
2920
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002921 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002922 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002923 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002924 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002925 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002926 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002927 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002928 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002929
Jaeden Amero252ef282019-02-15 14:05:35 +00002930 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002931 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002932 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2933 PSA_ASSERT( psa_mac_sign_finish( &operation,
2934 sign_mac, sizeof( sign_mac ),
2935 &sign_mac_length ) );
2936 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2937 PSA_ERROR_BAD_STATE );
2938 PSA_ASSERT( psa_mac_abort( &operation ) );
2939
2940 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002941 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002942 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2943 PSA_ASSERT( psa_mac_verify_finish( &operation,
2944 verify_mac, sizeof( verify_mac ) ) );
2945 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2946 PSA_ERROR_BAD_STATE );
2947 PSA_ASSERT( psa_mac_abort( &operation ) );
2948
2949 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002950 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002951 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2952 PSA_ASSERT( psa_mac_sign_finish( &operation,
2953 sign_mac, sizeof( sign_mac ),
2954 &sign_mac_length ) );
2955 TEST_EQUAL( psa_mac_sign_finish( &operation,
2956 sign_mac, sizeof( sign_mac ),
2957 &sign_mac_length ),
2958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_mac_abort( &operation ) );
2960
2961 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002962 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002963 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2964 PSA_ASSERT( psa_mac_verify_finish( &operation,
2965 verify_mac, sizeof( verify_mac ) ) );
2966 TEST_EQUAL( psa_mac_verify_finish( &operation,
2967 verify_mac, sizeof( verify_mac ) ),
2968 PSA_ERROR_BAD_STATE );
2969 PSA_ASSERT( psa_mac_abort( &operation ) );
2970
2971 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002972 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002973 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002974 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002975 TEST_EQUAL( psa_mac_verify_finish( &operation,
2976 verify_mac, sizeof( verify_mac ) ),
2977 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002978 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002979 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002980 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002981
2982 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002983 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002984 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002985 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002986 TEST_EQUAL( psa_mac_sign_finish( &operation,
2987 sign_mac, sizeof( sign_mac ),
2988 &sign_mac_length ),
2989 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002990 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002991 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002992 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002993
Ronald Cron5425a212020-08-04 14:58:35 +02002994 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002995
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002996exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002997 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002998}
2999/* END_CASE */
3000
3001/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003002void mac_sign_verify_multi( int key_type_arg,
3003 data_t *key_data,
3004 int alg_arg,
3005 data_t *input,
3006 int is_verify,
3007 data_t *expected_mac )
3008{
3009 size_t data_part_len = 0;
3010
3011 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3012 {
3013 /* Split data into length(data_part_len) parts. */
3014 mbedtls_test_set_step( 2000 + data_part_len );
3015
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003016 if( mac_multipart_internal_func( key_type_arg, key_data,
3017 alg_arg,
3018 input, data_part_len,
3019 expected_mac,
3020 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003021 break;
3022
3023 /* length(0) part, length(data_part_len) part, length(0) part... */
3024 mbedtls_test_set_step( 3000 + data_part_len );
3025
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003026 if( mac_multipart_internal_func( key_type_arg, key_data,
3027 alg_arg,
3028 input, data_part_len,
3029 expected_mac,
3030 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003031 break;
3032 }
3033
3034 /* Goto is required to silence warnings about unused labels, as we
3035 * don't actually do any test assertions in this function. */
3036 goto exit;
3037}
3038/* END_CASE */
3039
3040/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003041void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003042 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003043 int alg_arg,
3044 data_t *input,
3045 data_t *expected_mac )
3046{
Ronald Cron5425a212020-08-04 14:58:35 +02003047 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003048 psa_key_type_t key_type = key_type_arg;
3049 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003050 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003051 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003052 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003053 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003054 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003055 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003056 const size_t output_sizes_to_test[] = {
3057 0,
3058 1,
3059 expected_mac->len - 1,
3060 expected_mac->len,
3061 expected_mac->len + 1,
3062 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003063
Gilles Peskine7be11a72022-04-14 00:12:57 +02003064 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003065 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003066 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003067
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003069
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003070 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003071 psa_set_key_algorithm( &attributes, alg );
3072 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003073
Ronald Cron5425a212020-08-04 14:58:35 +02003074 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3075 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003076
Gilles Peskine8b356b52020-08-25 23:44:59 +02003077 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3078 {
3079 const size_t output_size = output_sizes_to_test[i];
3080 psa_status_t expected_status =
3081 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3082 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003083
Chris Jones9634bb12021-01-20 15:56:42 +00003084 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003085 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003086
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003087 /* Calculate the MAC, one-shot case. */
3088 TEST_EQUAL( psa_mac_compute( key, alg,
3089 input->x, input->len,
3090 actual_mac, output_size, &mac_length ),
3091 expected_status );
3092 if( expected_status == PSA_SUCCESS )
3093 {
3094 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3095 actual_mac, mac_length );
3096 }
3097
3098 if( output_size > 0 )
3099 memset( actual_mac, 0, output_size );
3100
3101 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003102 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003103 PSA_ASSERT( psa_mac_update( &operation,
3104 input->x, input->len ) );
3105 TEST_EQUAL( psa_mac_sign_finish( &operation,
3106 actual_mac, output_size,
3107 &mac_length ),
3108 expected_status );
3109 PSA_ASSERT( psa_mac_abort( &operation ) );
3110
3111 if( expected_status == PSA_SUCCESS )
3112 {
3113 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3114 actual_mac, mac_length );
3115 }
3116 mbedtls_free( actual_mac );
3117 actual_mac = NULL;
3118 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003119
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003120exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003121 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003122 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003123 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003124 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003125}
3126/* END_CASE */
3127
3128/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003129void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003130 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003131 int alg_arg,
3132 data_t *input,
3133 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003134{
Ronald Cron5425a212020-08-04 14:58:35 +02003135 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003136 psa_key_type_t key_type = key_type_arg;
3137 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003138 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003140 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003141
Gilles Peskine7be11a72022-04-14 00:12:57 +02003142 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003143
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003147 psa_set_key_algorithm( &attributes, alg );
3148 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003149
Ronald Cron5425a212020-08-04 14:58:35 +02003150 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3151 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003152
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003153 /* Verify correct MAC, one-shot case. */
3154 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3155 expected_mac->x, expected_mac->len ) );
3156
3157 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003158 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003159 PSA_ASSERT( psa_mac_update( &operation,
3160 input->x, input->len ) );
3161 PSA_ASSERT( psa_mac_verify_finish( &operation,
3162 expected_mac->x,
3163 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003164
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003165 /* Test a MAC that's too short, one-shot case. */
3166 TEST_EQUAL( psa_mac_verify( key, alg,
3167 input->x, input->len,
3168 expected_mac->x,
3169 expected_mac->len - 1 ),
3170 PSA_ERROR_INVALID_SIGNATURE );
3171
3172 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003173 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003174 PSA_ASSERT( psa_mac_update( &operation,
3175 input->x, input->len ) );
3176 TEST_EQUAL( psa_mac_verify_finish( &operation,
3177 expected_mac->x,
3178 expected_mac->len - 1 ),
3179 PSA_ERROR_INVALID_SIGNATURE );
3180
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003181 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003182 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3183 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003184 TEST_EQUAL( psa_mac_verify( key, alg,
3185 input->x, input->len,
3186 perturbed_mac, expected_mac->len + 1 ),
3187 PSA_ERROR_INVALID_SIGNATURE );
3188
3189 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003190 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003191 PSA_ASSERT( psa_mac_update( &operation,
3192 input->x, input->len ) );
3193 TEST_EQUAL( psa_mac_verify_finish( &operation,
3194 perturbed_mac,
3195 expected_mac->len + 1 ),
3196 PSA_ERROR_INVALID_SIGNATURE );
3197
3198 /* Test changing one byte. */
3199 for( size_t i = 0; i < expected_mac->len; i++ )
3200 {
Chris Jones9634bb12021-01-20 15:56:42 +00003201 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003202 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003203
3204 TEST_EQUAL( psa_mac_verify( key, alg,
3205 input->x, input->len,
3206 perturbed_mac, expected_mac->len ),
3207 PSA_ERROR_INVALID_SIGNATURE );
3208
Ronald Cron5425a212020-08-04 14:58:35 +02003209 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003210 PSA_ASSERT( psa_mac_update( &operation,
3211 input->x, input->len ) );
3212 TEST_EQUAL( psa_mac_verify_finish( &operation,
3213 perturbed_mac,
3214 expected_mac->len ),
3215 PSA_ERROR_INVALID_SIGNATURE );
3216 perturbed_mac[i] ^= 1;
3217 }
3218
Gilles Peskine8c9def32018-02-08 10:02:12 +01003219exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003220 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003221 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003222 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003223 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003224}
3225/* END_CASE */
3226
3227/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003228void cipher_operation_init( )
3229{
Jaeden Ameroab439972019-02-15 14:12:05 +00003230 const uint8_t input[1] = { 0 };
3231 unsigned char output[1] = { 0 };
3232 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003233 /* Test each valid way of initializing the object, except for `= {0}`, as
3234 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3235 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003236 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003237 psa_cipher_operation_t func = psa_cipher_operation_init( );
3238 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3239 psa_cipher_operation_t zero;
3240
3241 memset( &zero, 0, sizeof( zero ) );
3242
Jaeden Ameroab439972019-02-15 14:12:05 +00003243 /* A freshly-initialized cipher operation should not be usable. */
3244 TEST_EQUAL( psa_cipher_update( &func,
3245 input, sizeof( input ),
3246 output, sizeof( output ),
3247 &output_length ),
3248 PSA_ERROR_BAD_STATE );
3249 TEST_EQUAL( psa_cipher_update( &init,
3250 input, sizeof( input ),
3251 output, sizeof( output ),
3252 &output_length ),
3253 PSA_ERROR_BAD_STATE );
3254 TEST_EQUAL( psa_cipher_update( &zero,
3255 input, sizeof( input ),
3256 output, sizeof( output ),
3257 &output_length ),
3258 PSA_ERROR_BAD_STATE );
3259
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003260 /* A default cipher operation should be abortable without error. */
3261 PSA_ASSERT( psa_cipher_abort( &func ) );
3262 PSA_ASSERT( psa_cipher_abort( &init ) );
3263 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003264}
3265/* END_CASE */
3266
3267/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003268void cipher_setup( int key_type_arg,
3269 data_t *key,
3270 int alg_arg,
3271 int expected_status_arg )
3272{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003273 psa_key_type_t key_type = key_type_arg;
3274 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003275 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003276 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003277 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003278#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003279 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3280#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003283
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003284 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3285 &operation, &status ) )
3286 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003287 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003288
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003289 /* The operation object should be reusable. */
3290#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3291 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3292 smoke_test_key_data,
3293 sizeof( smoke_test_key_data ),
3294 KNOWN_SUPPORTED_CIPHER_ALG,
3295 &operation, &status ) )
3296 goto exit;
3297 TEST_EQUAL( status, PSA_SUCCESS );
3298#endif
3299
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003300exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003301 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003302 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003303}
3304/* END_CASE */
3305
Ronald Cronee414c72021-03-18 18:50:08 +01003306/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003307void cipher_bad_order( )
3308{
Ronald Cron5425a212020-08-04 14:58:35 +02003309 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003310 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3311 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003313 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003314 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003315 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003316 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3317 0xaa, 0xaa, 0xaa, 0xaa };
3318 const uint8_t text[] = {
3319 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3320 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003321 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003322 size_t length = 0;
3323
3324 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003325 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3326 psa_set_key_algorithm( &attributes, alg );
3327 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003328 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3329 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003330
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003331 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003332 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003333 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003334 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003335 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003336 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003337 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003338 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003339
3340 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003341 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003342 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003343 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003344 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003345 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003346 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003347 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003348
Jaeden Ameroab439972019-02-15 14:12:05 +00003349 /* Generate an IV without calling setup beforehand. */
3350 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3351 buffer, sizeof( buffer ),
3352 &length ),
3353 PSA_ERROR_BAD_STATE );
3354 PSA_ASSERT( psa_cipher_abort( &operation ) );
3355
3356 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003357 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003358 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3359 buffer, sizeof( buffer ),
3360 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003361 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003362 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3363 buffer, sizeof( buffer ),
3364 &length ),
3365 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003366 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003367 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003368 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003369
3370 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003371 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003372 PSA_ASSERT( psa_cipher_set_iv( &operation,
3373 iv, sizeof( iv ) ) );
3374 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3375 buffer, sizeof( buffer ),
3376 &length ),
3377 PSA_ERROR_BAD_STATE );
3378 PSA_ASSERT( psa_cipher_abort( &operation ) );
3379
3380 /* Set an IV without calling setup beforehand. */
3381 TEST_EQUAL( psa_cipher_set_iv( &operation,
3382 iv, sizeof( iv ) ),
3383 PSA_ERROR_BAD_STATE );
3384 PSA_ASSERT( psa_cipher_abort( &operation ) );
3385
3386 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003387 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003388 PSA_ASSERT( psa_cipher_set_iv( &operation,
3389 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003390 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003391 TEST_EQUAL( psa_cipher_set_iv( &operation,
3392 iv, sizeof( iv ) ),
3393 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003394 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003395 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003396 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003397
3398 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003399 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003400 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3401 buffer, sizeof( buffer ),
3402 &length ) );
3403 TEST_EQUAL( psa_cipher_set_iv( &operation,
3404 iv, sizeof( iv ) ),
3405 PSA_ERROR_BAD_STATE );
3406 PSA_ASSERT( psa_cipher_abort( &operation ) );
3407
3408 /* Call update without calling setup beforehand. */
3409 TEST_EQUAL( psa_cipher_update( &operation,
3410 text, sizeof( text ),
3411 buffer, sizeof( buffer ),
3412 &length ),
3413 PSA_ERROR_BAD_STATE );
3414 PSA_ASSERT( psa_cipher_abort( &operation ) );
3415
3416 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003417 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003418 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003419 TEST_EQUAL( psa_cipher_update( &operation,
3420 text, sizeof( text ),
3421 buffer, sizeof( buffer ),
3422 &length ),
3423 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003424 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003425 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003426 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003427
3428 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003429 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003430 PSA_ASSERT( psa_cipher_set_iv( &operation,
3431 iv, sizeof( iv ) ) );
3432 PSA_ASSERT( psa_cipher_finish( &operation,
3433 buffer, sizeof( buffer ), &length ) );
3434 TEST_EQUAL( psa_cipher_update( &operation,
3435 text, sizeof( text ),
3436 buffer, sizeof( buffer ),
3437 &length ),
3438 PSA_ERROR_BAD_STATE );
3439 PSA_ASSERT( psa_cipher_abort( &operation ) );
3440
3441 /* Call finish without calling setup beforehand. */
3442 TEST_EQUAL( psa_cipher_finish( &operation,
3443 buffer, sizeof( buffer ), &length ),
3444 PSA_ERROR_BAD_STATE );
3445 PSA_ASSERT( psa_cipher_abort( &operation ) );
3446
3447 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003448 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003449 /* Not calling update means we are encrypting an empty buffer, which is OK
3450 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003451 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003452 TEST_EQUAL( psa_cipher_finish( &operation,
3453 buffer, sizeof( buffer ), &length ),
3454 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003455 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003456 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003457 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003458
3459 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003460 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003461 PSA_ASSERT( psa_cipher_set_iv( &operation,
3462 iv, sizeof( iv ) ) );
3463 PSA_ASSERT( psa_cipher_finish( &operation,
3464 buffer, sizeof( buffer ), &length ) );
3465 TEST_EQUAL( psa_cipher_finish( &operation,
3466 buffer, sizeof( buffer ), &length ),
3467 PSA_ERROR_BAD_STATE );
3468 PSA_ASSERT( psa_cipher_abort( &operation ) );
3469
Ronald Cron5425a212020-08-04 14:58:35 +02003470 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003471
Jaeden Ameroab439972019-02-15 14:12:05 +00003472exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003473 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003474 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003475}
3476/* END_CASE */
3477
3478/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003479void cipher_encrypt_fail( int alg_arg,
3480 int key_type_arg,
3481 data_t *key_data,
3482 data_t *input,
3483 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003484{
Ronald Cron5425a212020-08-04 14:58:35 +02003485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003486 psa_status_t status;
3487 psa_key_type_t key_type = key_type_arg;
3488 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003489 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003490 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3491 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3492 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003493 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003494 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003495 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003496 size_t function_output_length;
3497 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003498 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3499
3500 if ( PSA_ERROR_BAD_STATE != expected_status )
3501 {
3502 PSA_ASSERT( psa_crypto_init( ) );
3503
3504 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3505 psa_set_key_algorithm( &attributes, alg );
3506 psa_set_key_type( &attributes, key_type );
3507
3508 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3509 input->len );
3510 ASSERT_ALLOC( output, output_buffer_size );
3511
3512 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3513 &key ) );
3514 }
3515
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003516 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003517 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3518 output_buffer_size, &output_length );
3519
3520 TEST_EQUAL( status, expected_status );
3521
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003522 /* Encrypt, multi-part */
3523 status = psa_cipher_encrypt_setup( &operation, key, alg );
3524 if( status == PSA_SUCCESS )
3525 {
3526 if( alg != PSA_ALG_ECB_NO_PADDING )
3527 {
3528 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3529 iv, iv_size,
3530 &iv_length ) );
3531 }
3532
3533 status = psa_cipher_update( &operation, input->x, input->len,
3534 output, output_buffer_size,
3535 &function_output_length );
3536 if( status == PSA_SUCCESS )
3537 {
3538 output_length += function_output_length;
3539
3540 status = psa_cipher_finish( &operation, output + output_length,
3541 output_buffer_size - output_length,
3542 &function_output_length );
3543
3544 TEST_EQUAL( status, expected_status );
3545 }
3546 else
3547 {
3548 TEST_EQUAL( status, expected_status );
3549 }
3550 }
3551 else
3552 {
3553 TEST_EQUAL( status, expected_status );
3554 }
3555
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003556exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003557 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003558 mbedtls_free( output );
3559 psa_destroy_key( key );
3560 PSA_DONE( );
3561}
3562/* END_CASE */
3563
3564/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003565void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3566 data_t *input, int iv_length,
3567 int expected_result )
3568{
3569 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3570 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3572 size_t output_buffer_size = 0;
3573 unsigned char *output = NULL;
3574
3575 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3576 ASSERT_ALLOC( output, output_buffer_size );
3577
3578 PSA_ASSERT( psa_crypto_init( ) );
3579
3580 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3581 psa_set_key_algorithm( &attributes, alg );
3582 psa_set_key_type( &attributes, key_type );
3583
3584 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3585 &key ) );
3586 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3587 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3588 iv_length ) );
3589
3590exit:
3591 psa_cipher_abort( &operation );
3592 mbedtls_free( output );
3593 psa_destroy_key( key );
3594 PSA_DONE( );
3595}
3596/* END_CASE */
3597
3598/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003599void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3600 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003601{
3602 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3603 psa_key_type_t key_type = key_type_arg;
3604 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003605 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3606 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003607 unsigned char *output = NULL;
3608 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003609 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003610 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3611
3612 PSA_ASSERT( psa_crypto_init( ) );
3613
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003614 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003615 TEST_LE_U( ciphertext->len,
3616 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3617 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003618 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003619 TEST_LE_U( plaintext->len,
3620 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3621 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3622 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003623
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003624
3625 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003626 psa_set_key_usage_flags( &attributes,
3627 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003628 psa_set_key_algorithm( &attributes, alg );
3629 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003630 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3631 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003632 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3633 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003634 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003635
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003636 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003637 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3638 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3639 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003640 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3641 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003642 PSA_ERROR_BAD_STATE );
3643
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003644 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003645 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3646 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003647 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003648 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003649 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3650 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003651 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003652 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003653
Gilles Peskine286c3142022-04-20 17:09:38 +02003654 /* Multipart encryption */
3655 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3656 output_length = 0;
3657 length = ~0;
3658 PSA_ASSERT( psa_cipher_update( &operation,
3659 plaintext->x, plaintext->len,
3660 output, output_buffer_size,
3661 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003662 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003663 output_length += length;
3664 PSA_ASSERT( psa_cipher_finish( &operation,
3665 output + output_length,
3666 output_buffer_size - output_length,
3667 &length ) );
3668 output_length += length;
3669 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003670 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003671
Gilles Peskine286c3142022-04-20 17:09:38 +02003672 /* Multipart encryption */
3673 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3674 output_length = 0;
3675 length = ~0;
3676 PSA_ASSERT( psa_cipher_update( &operation,
3677 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003678 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003679 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003680 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003681 output_length += length;
3682 PSA_ASSERT( psa_cipher_finish( &operation,
3683 output + output_length,
3684 output_buffer_size - output_length,
3685 &length ) );
3686 output_length += length;
3687 ASSERT_COMPARE( plaintext->x, plaintext->len,
3688 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003689
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003690 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003691 output_length = ~0;
3692 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3693 output, output_buffer_size,
3694 &output_length ) );
3695 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3696 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003697
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003698 /* One-shot decryption */
3699 output_length = ~0;
3700 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
3701 output, output_buffer_size,
3702 &output_length ) );
3703 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003704 output, output_length );
3705
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003706exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003707 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003708 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003709 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003710 psa_destroy_key( key );
3711 PSA_DONE( );
3712}
3713/* END_CASE */
3714
3715/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003716void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3717{
3718 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3719 psa_algorithm_t alg = alg_arg;
3720 psa_key_type_t key_type = key_type_arg;
3721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3722 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3723 psa_status_t status;
3724
3725 PSA_ASSERT( psa_crypto_init( ) );
3726
3727 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3728 psa_set_key_algorithm( &attributes, alg );
3729 psa_set_key_type( &attributes, key_type );
3730
3731 /* Usage of either of these two size macros would cause divide by zero
3732 * with incorrect key types previously. Input length should be irrelevant
3733 * here. */
3734 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3735 0 );
3736 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3737
3738
3739 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3740 &key ) );
3741
3742 /* Should fail due to invalid alg type (to support invalid key type).
3743 * Encrypt or decrypt will end up in the same place. */
3744 status = psa_cipher_encrypt_setup( &operation, key, alg );
3745
3746 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3747
3748exit:
3749 psa_cipher_abort( &operation );
3750 psa_destroy_key( key );
3751 PSA_DONE( );
3752}
3753/* END_CASE */
3754
3755/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003756void cipher_encrypt_validation( int alg_arg,
3757 int key_type_arg,
3758 data_t *key_data,
3759 data_t *input )
3760{
3761 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3762 psa_key_type_t key_type = key_type_arg;
3763 psa_algorithm_t alg = alg_arg;
3764 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3765 unsigned char *output1 = NULL;
3766 size_t output1_buffer_size = 0;
3767 size_t output1_length = 0;
3768 unsigned char *output2 = NULL;
3769 size_t output2_buffer_size = 0;
3770 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003771 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003772 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003773 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003774
Gilles Peskine8817f612018-12-18 00:18:46 +01003775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003776
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003777 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3778 psa_set_key_algorithm( &attributes, alg );
3779 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003780
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003781 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3782 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3783 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3784 ASSERT_ALLOC( output1, output1_buffer_size );
3785 ASSERT_ALLOC( output2, output2_buffer_size );
3786
Ronald Cron5425a212020-08-04 14:58:35 +02003787 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3788 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003789
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003790 /* The one-shot cipher encryption uses generated iv so validating
3791 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003792 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3793 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003794 TEST_LE_U( output1_length,
3795 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3796 TEST_LE_U( output1_length,
3797 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003798
3799 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3800 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003801
Gilles Peskine8817f612018-12-18 00:18:46 +01003802 PSA_ASSERT( psa_cipher_update( &operation,
3803 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003804 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003805 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003806 TEST_LE_U( function_output_length,
3807 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3808 TEST_LE_U( function_output_length,
3809 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003810 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003811
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003812 PSA_ASSERT( psa_cipher_finish( &operation,
3813 output2 + output2_length,
3814 output2_buffer_size - output2_length,
3815 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003816 TEST_LE_U( function_output_length,
3817 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3818 TEST_LE_U( function_output_length,
3819 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003820 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003821
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003822 PSA_ASSERT( psa_cipher_abort( &operation ) );
3823 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3824 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003825
Gilles Peskine50e586b2018-06-08 14:28:46 +02003826exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003827 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003828 mbedtls_free( output1 );
3829 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003830 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003831 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003832}
3833/* END_CASE */
3834
3835/* BEGIN_CASE */
3836void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003837 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003838 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003839 int first_part_size_arg,
3840 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003841 data_t *expected_output,
3842 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003843{
Ronald Cron5425a212020-08-04 14:58:35 +02003844 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003845 psa_key_type_t key_type = key_type_arg;
3846 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003847 psa_status_t status;
3848 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003849 size_t first_part_size = first_part_size_arg;
3850 size_t output1_length = output1_length_arg;
3851 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003852 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003853 size_t output_buffer_size = 0;
3854 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003855 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003856 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003857 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003858
Gilles Peskine8817f612018-12-18 00:18:46 +01003859 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003860
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003861 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3862 psa_set_key_algorithm( &attributes, alg );
3863 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003864
Ronald Cron5425a212020-08-04 14:58:35 +02003865 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3866 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003867
Ronald Cron5425a212020-08-04 14:58:35 +02003868 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003869
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003870 if( iv->len > 0 )
3871 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003872 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003873 }
3874
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003875 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3876 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003877 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003878
Gilles Peskine7be11a72022-04-14 00:12:57 +02003879 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003880 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3881 output, output_buffer_size,
3882 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003883 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003884 TEST_LE_U( function_output_length,
3885 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3886 TEST_LE_U( function_output_length,
3887 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003888 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003889
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003890 if( first_part_size < input->len )
3891 {
3892 PSA_ASSERT( psa_cipher_update( &operation,
3893 input->x + first_part_size,
3894 input->len - first_part_size,
3895 ( output_buffer_size == 0 ? NULL :
3896 output + total_output_length ),
3897 output_buffer_size - total_output_length,
3898 &function_output_length ) );
3899 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003900 TEST_LE_U( function_output_length,
3901 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3902 alg,
3903 input->len - first_part_size ) );
3904 TEST_LE_U( function_output_length,
3905 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003906 total_output_length += function_output_length;
3907 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003908
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003909 status = psa_cipher_finish( &operation,
3910 ( output_buffer_size == 0 ? NULL :
3911 output + total_output_length ),
3912 output_buffer_size - total_output_length,
3913 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003914 TEST_LE_U( function_output_length,
3915 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3916 TEST_LE_U( function_output_length,
3917 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003918 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003919 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003920
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003921 if( expected_status == PSA_SUCCESS )
3922 {
3923 PSA_ASSERT( psa_cipher_abort( &operation ) );
3924
3925 ASSERT_COMPARE( expected_output->x, expected_output->len,
3926 output, total_output_length );
3927 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003928
3929exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003930 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003931 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003932 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003933 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003934}
3935/* END_CASE */
3936
3937/* BEGIN_CASE */
3938void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003939 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003940 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003941 int first_part_size_arg,
3942 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003943 data_t *expected_output,
3944 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003945{
Ronald Cron5425a212020-08-04 14:58:35 +02003946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003947 psa_key_type_t key_type = key_type_arg;
3948 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003949 psa_status_t status;
3950 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003951 size_t first_part_size = first_part_size_arg;
3952 size_t output1_length = output1_length_arg;
3953 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003954 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003955 size_t output_buffer_size = 0;
3956 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003957 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003958 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003960
Gilles Peskine8817f612018-12-18 00:18:46 +01003961 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003962
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3964 psa_set_key_algorithm( &attributes, alg );
3965 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003966
Ronald Cron5425a212020-08-04 14:58:35 +02003967 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3968 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003969
Ronald Cron5425a212020-08-04 14:58:35 +02003970 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003971
Steven Cooreman177deba2020-09-07 17:14:14 +02003972 if( iv->len > 0 )
3973 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003974 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003975 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003976
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003977 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3978 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003979 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003980
Gilles Peskine7be11a72022-04-14 00:12:57 +02003981 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003982 PSA_ASSERT( psa_cipher_update( &operation,
3983 input->x, first_part_size,
3984 output, output_buffer_size,
3985 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003986 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003987 TEST_LE_U( function_output_length,
3988 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3989 TEST_LE_U( function_output_length,
3990 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003991 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003992
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003993 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003994 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003995 PSA_ASSERT( psa_cipher_update( &operation,
3996 input->x + first_part_size,
3997 input->len - first_part_size,
3998 ( output_buffer_size == 0 ? NULL :
3999 output + total_output_length ),
4000 output_buffer_size - total_output_length,
4001 &function_output_length ) );
4002 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004003 TEST_LE_U( function_output_length,
4004 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4005 alg,
4006 input->len - first_part_size ) );
4007 TEST_LE_U( function_output_length,
4008 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004009 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004010 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004011
Gilles Peskine50e586b2018-06-08 14:28:46 +02004012 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004013 ( output_buffer_size == 0 ? NULL :
4014 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004015 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004016 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004017 TEST_LE_U( function_output_length,
4018 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4019 TEST_LE_U( function_output_length,
4020 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004021 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004022 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004023
4024 if( expected_status == PSA_SUCCESS )
4025 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004026 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004027
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004028 ASSERT_COMPARE( expected_output->x, expected_output->len,
4029 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004030 }
4031
Gilles Peskine50e586b2018-06-08 14:28:46 +02004032exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004033 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004034 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004035 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004036 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004037}
4038/* END_CASE */
4039
Gilles Peskine50e586b2018-06-08 14:28:46 +02004040/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004041void cipher_decrypt_fail( int alg_arg,
4042 int key_type_arg,
4043 data_t *key_data,
4044 data_t *iv,
4045 data_t *input_arg,
4046 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004047{
4048 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4049 psa_status_t status;
4050 psa_key_type_t key_type = key_type_arg;
4051 psa_algorithm_t alg = alg_arg;
4052 psa_status_t expected_status = expected_status_arg;
4053 unsigned char *input = NULL;
4054 size_t input_buffer_size = 0;
4055 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004056 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004057 size_t output_buffer_size = 0;
4058 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004059 size_t function_output_length;
4060 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004061 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4062
4063 if ( PSA_ERROR_BAD_STATE != expected_status )
4064 {
4065 PSA_ASSERT( psa_crypto_init( ) );
4066
4067 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4068 psa_set_key_algorithm( &attributes, alg );
4069 psa_set_key_type( &attributes, key_type );
4070
4071 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4072 &key ) );
4073 }
4074
4075 /* Allocate input buffer and copy the iv and the plaintext */
4076 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4077 if ( input_buffer_size > 0 )
4078 {
4079 ASSERT_ALLOC( input, input_buffer_size );
4080 memcpy( input, iv->x, iv->len );
4081 memcpy( input + iv->len, input_arg->x, input_arg->len );
4082 }
4083
4084 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4085 ASSERT_ALLOC( output, output_buffer_size );
4086
Neil Armstrong66a479f2022-02-07 15:41:19 +01004087 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004088 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4089 output_buffer_size, &output_length );
4090 TEST_EQUAL( status, expected_status );
4091
Neil Armstrong66a479f2022-02-07 15:41:19 +01004092 /* Decrypt, multi-part */
4093 status = psa_cipher_decrypt_setup( &operation, key, alg );
4094 if( status == PSA_SUCCESS )
4095 {
4096 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4097 input_arg->len ) +
4098 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4099 ASSERT_ALLOC( output_multi, output_buffer_size );
4100
4101 if( iv->len > 0 )
4102 {
4103 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4104
4105 if( status != PSA_SUCCESS )
4106 TEST_EQUAL( status, expected_status );
4107 }
4108
4109 if( status == PSA_SUCCESS )
4110 {
4111 status = psa_cipher_update( &operation,
4112 input_arg->x, input_arg->len,
4113 output_multi, output_buffer_size,
4114 &function_output_length );
4115 if( status == PSA_SUCCESS )
4116 {
4117 output_length = function_output_length;
4118
4119 status = psa_cipher_finish( &operation,
4120 output_multi + output_length,
4121 output_buffer_size - output_length,
4122 &function_output_length );
4123
4124 TEST_EQUAL( status, expected_status );
4125 }
4126 else
4127 {
4128 TEST_EQUAL( status, expected_status );
4129 }
4130 }
4131 else
4132 {
4133 TEST_EQUAL( status, expected_status );
4134 }
4135 }
4136 else
4137 {
4138 TEST_EQUAL( status, expected_status );
4139 }
4140
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004141exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004142 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004143 mbedtls_free( input );
4144 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004145 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004146 psa_destroy_key( key );
4147 PSA_DONE( );
4148}
4149/* END_CASE */
4150
4151/* BEGIN_CASE */
4152void cipher_decrypt( int alg_arg,
4153 int key_type_arg,
4154 data_t *key_data,
4155 data_t *iv,
4156 data_t *input_arg,
4157 data_t *expected_output )
4158{
4159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4160 psa_key_type_t key_type = key_type_arg;
4161 psa_algorithm_t alg = alg_arg;
4162 unsigned char *input = NULL;
4163 size_t input_buffer_size = 0;
4164 unsigned char *output = NULL;
4165 size_t output_buffer_size = 0;
4166 size_t output_length = 0;
4167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4168
4169 PSA_ASSERT( psa_crypto_init( ) );
4170
4171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4172 psa_set_key_algorithm( &attributes, alg );
4173 psa_set_key_type( &attributes, key_type );
4174
4175 /* Allocate input buffer and copy the iv and the plaintext */
4176 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4177 if ( input_buffer_size > 0 )
4178 {
4179 ASSERT_ALLOC( input, input_buffer_size );
4180 memcpy( input, iv->x, iv->len );
4181 memcpy( input + iv->len, input_arg->x, input_arg->len );
4182 }
4183
4184 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4185 ASSERT_ALLOC( output, output_buffer_size );
4186
4187 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4188 &key ) );
4189
4190 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4191 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004192 TEST_LE_U( output_length,
4193 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4194 TEST_LE_U( output_length,
4195 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004196
4197 ASSERT_COMPARE( expected_output->x, expected_output->len,
4198 output, output_length );
4199exit:
4200 mbedtls_free( input );
4201 mbedtls_free( output );
4202 psa_destroy_key( key );
4203 PSA_DONE( );
4204}
4205/* END_CASE */
4206
4207/* BEGIN_CASE */
4208void cipher_verify_output( int alg_arg,
4209 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004210 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004211 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004212{
Ronald Cron5425a212020-08-04 14:58:35 +02004213 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004214 psa_key_type_t key_type = key_type_arg;
4215 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004216 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004217 size_t output1_size = 0;
4218 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004219 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004220 size_t output2_size = 0;
4221 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004222 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004223
Gilles Peskine8817f612018-12-18 00:18:46 +01004224 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004225
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004226 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4227 psa_set_key_algorithm( &attributes, alg );
4228 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004229
Ronald Cron5425a212020-08-04 14:58:35 +02004230 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4231 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004232 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004233 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004234
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004235 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4236 output1, output1_size,
4237 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004238 TEST_LE_U( output1_length,
4239 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4240 TEST_LE_U( output1_length,
4241 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004242
4243 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004244 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004245
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004246 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4247 output2, output2_size,
4248 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004249 TEST_LE_U( output2_length,
4250 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4251 TEST_LE_U( output2_length,
4252 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004253
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004254 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004255
4256exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004257 mbedtls_free( output1 );
4258 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004259 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004260 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004261}
4262/* END_CASE */
4263
4264/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004265void cipher_verify_output_multipart( int alg_arg,
4266 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004267 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004268 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004269 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004270{
Ronald Cron5425a212020-08-04 14:58:35 +02004271 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004272 psa_key_type_t key_type = key_type_arg;
4273 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004274 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004275 unsigned char iv[16] = {0};
4276 size_t iv_size = 16;
4277 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004278 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004279 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004280 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004281 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004282 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004283 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004284 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004285 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4286 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004287 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004288
Gilles Peskine8817f612018-12-18 00:18:46 +01004289 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004290
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004291 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4292 psa_set_key_algorithm( &attributes, alg );
4293 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004294
Ronald Cron5425a212020-08-04 14:58:35 +02004295 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4296 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004297
Ronald Cron5425a212020-08-04 14:58:35 +02004298 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4299 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004300
Steven Cooreman177deba2020-09-07 17:14:14 +02004301 if( alg != PSA_ALG_ECB_NO_PADDING )
4302 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004303 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4304 iv, iv_size,
4305 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004306 }
4307
gabor-mezei-armceface22021-01-21 12:26:17 +01004308 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004309 TEST_LE_U( output1_buffer_size,
4310 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004311 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004312
Gilles Peskine7be11a72022-04-14 00:12:57 +02004313 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004314
Gilles Peskine8817f612018-12-18 00:18:46 +01004315 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4316 output1, output1_buffer_size,
4317 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004318 TEST_LE_U( function_output_length,
4319 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4320 TEST_LE_U( function_output_length,
4321 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004322 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004323
Gilles Peskine8817f612018-12-18 00:18:46 +01004324 PSA_ASSERT( psa_cipher_update( &operation1,
4325 input->x + first_part_size,
4326 input->len - first_part_size,
4327 output1, output1_buffer_size,
4328 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004329 TEST_LE_U( function_output_length,
4330 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4331 alg,
4332 input->len - first_part_size ) );
4333 TEST_LE_U( function_output_length,
4334 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004335 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004336
Gilles Peskine8817f612018-12-18 00:18:46 +01004337 PSA_ASSERT( psa_cipher_finish( &operation1,
4338 output1 + output1_length,
4339 output1_buffer_size - output1_length,
4340 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004341 TEST_LE_U( function_output_length,
4342 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4343 TEST_LE_U( function_output_length,
4344 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004345 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004346
Gilles Peskine8817f612018-12-18 00:18:46 +01004347 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004348
Gilles Peskine048b7f02018-06-08 14:20:49 +02004349 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004350 TEST_LE_U( output2_buffer_size,
4351 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4352 TEST_LE_U( output2_buffer_size,
4353 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004354 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004355
Steven Cooreman177deba2020-09-07 17:14:14 +02004356 if( iv_length > 0 )
4357 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004358 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4359 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004360 }
Moran Pekerded84402018-06-06 16:36:50 +03004361
Gilles Peskine8817f612018-12-18 00:18:46 +01004362 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4363 output2, output2_buffer_size,
4364 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004365 TEST_LE_U( function_output_length,
4366 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4367 TEST_LE_U( function_output_length,
4368 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004369 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004370
Gilles Peskine8817f612018-12-18 00:18:46 +01004371 PSA_ASSERT( psa_cipher_update( &operation2,
4372 output1 + first_part_size,
4373 output1_length - first_part_size,
4374 output2, output2_buffer_size,
4375 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004376 TEST_LE_U( function_output_length,
4377 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4378 alg,
4379 output1_length - first_part_size ) );
4380 TEST_LE_U( function_output_length,
4381 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004382 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004383
Gilles Peskine8817f612018-12-18 00:18:46 +01004384 PSA_ASSERT( psa_cipher_finish( &operation2,
4385 output2 + output2_length,
4386 output2_buffer_size - output2_length,
4387 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004388 TEST_LE_U( function_output_length,
4389 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4390 TEST_LE_U( function_output_length,
4391 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004392 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004393
Gilles Peskine8817f612018-12-18 00:18:46 +01004394 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004395
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004396 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004397
4398exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004399 psa_cipher_abort( &operation1 );
4400 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004401 mbedtls_free( output1 );
4402 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004403 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004404 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004405}
4406/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004407
Gilles Peskine20035e32018-02-03 22:44:14 +01004408/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004409void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004410 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004411 data_t *nonce,
4412 data_t *additional_data,
4413 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004414 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004415{
Ronald Cron5425a212020-08-04 14:58:35 +02004416 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004417 psa_key_type_t key_type = key_type_arg;
4418 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004419 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004420 unsigned char *output_data = NULL;
4421 size_t output_size = 0;
4422 size_t output_length = 0;
4423 unsigned char *output_data2 = NULL;
4424 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004425 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004426 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004428
Gilles Peskine8817f612018-12-18 00:18:46 +01004429 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004430
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004431 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4432 psa_set_key_algorithm( &attributes, alg );
4433 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004434
Gilles Peskine049c7532019-05-15 20:22:09 +02004435 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004436 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004437 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4438 key_bits = psa_get_key_bits( &attributes );
4439
4440 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4441 alg );
4442 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4443 * should be exact. */
4444 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4445 expected_result != PSA_ERROR_NOT_SUPPORTED )
4446 {
4447 TEST_EQUAL( output_size,
4448 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004449 TEST_LE_U( output_size,
4450 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004451 }
4452 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004453
Steven Cooremanf49478b2021-02-15 15:19:25 +01004454 status = psa_aead_encrypt( key, alg,
4455 nonce->x, nonce->len,
4456 additional_data->x,
4457 additional_data->len,
4458 input_data->x, input_data->len,
4459 output_data, output_size,
4460 &output_length );
4461
4462 /* If the operation is not supported, just skip and not fail in case the
4463 * encryption involves a common limitation of cryptography hardwares and
4464 * an alternative implementation. */
4465 if( status == PSA_ERROR_NOT_SUPPORTED )
4466 {
4467 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4468 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4469 }
4470
4471 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004472
4473 if( PSA_SUCCESS == expected_result )
4474 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004475 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004476
Gilles Peskine003a4a92019-05-14 16:09:40 +02004477 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4478 * should be exact. */
4479 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004480 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004481
Gilles Peskine7be11a72022-04-14 00:12:57 +02004482 TEST_LE_U( input_data->len,
4483 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004484
Ronald Cron5425a212020-08-04 14:58:35 +02004485 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004486 nonce->x, nonce->len,
4487 additional_data->x,
4488 additional_data->len,
4489 output_data, output_length,
4490 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004491 &output_length2 ),
4492 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004493
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004494 ASSERT_COMPARE( input_data->x, input_data->len,
4495 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004496 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004497
Gilles Peskinea1cac842018-06-11 19:33:02 +02004498exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004499 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004500 mbedtls_free( output_data );
4501 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004502 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004503}
4504/* END_CASE */
4505
4506/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004507void aead_encrypt( int key_type_arg, data_t *key_data,
4508 int alg_arg,
4509 data_t *nonce,
4510 data_t *additional_data,
4511 data_t *input_data,
4512 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004513{
Ronald Cron5425a212020-08-04 14:58:35 +02004514 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004515 psa_key_type_t key_type = key_type_arg;
4516 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004517 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004518 unsigned char *output_data = NULL;
4519 size_t output_size = 0;
4520 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004522 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004523
Gilles Peskine8817f612018-12-18 00:18:46 +01004524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004525
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004526 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4527 psa_set_key_algorithm( &attributes, alg );
4528 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004529
Gilles Peskine049c7532019-05-15 20:22:09 +02004530 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004531 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004532 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4533 key_bits = psa_get_key_bits( &attributes );
4534
4535 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4536 alg );
4537 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4538 * should be exact. */
4539 TEST_EQUAL( output_size,
4540 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004541 TEST_LE_U( output_size,
4542 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004543 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004544
Steven Cooremand588ea12021-01-11 19:36:04 +01004545 status = psa_aead_encrypt( key, alg,
4546 nonce->x, nonce->len,
4547 additional_data->x, additional_data->len,
4548 input_data->x, input_data->len,
4549 output_data, output_size,
4550 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004551
Ronald Cron28a45ed2021-02-09 20:35:42 +01004552 /* If the operation is not supported, just skip and not fail in case the
4553 * encryption involves a common limitation of cryptography hardwares and
4554 * an alternative implementation. */
4555 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004556 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004557 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4558 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004559 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004560
4561 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004562 ASSERT_COMPARE( expected_result->x, expected_result->len,
4563 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004564
Gilles Peskinea1cac842018-06-11 19:33:02 +02004565exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004566 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004567 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004568 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004569}
4570/* END_CASE */
4571
4572/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004573void aead_decrypt( int key_type_arg, data_t *key_data,
4574 int alg_arg,
4575 data_t *nonce,
4576 data_t *additional_data,
4577 data_t *input_data,
4578 data_t *expected_data,
4579 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004580{
Ronald Cron5425a212020-08-04 14:58:35 +02004581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004582 psa_key_type_t key_type = key_type_arg;
4583 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004584 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004585 unsigned char *output_data = NULL;
4586 size_t output_size = 0;
4587 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004589 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004590 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004591
Gilles Peskine8817f612018-12-18 00:18:46 +01004592 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004593
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004594 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4595 psa_set_key_algorithm( &attributes, alg );
4596 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004597
Gilles Peskine049c7532019-05-15 20:22:09 +02004598 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004599 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004600 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4601 key_bits = psa_get_key_bits( &attributes );
4602
4603 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4604 alg );
4605 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4606 expected_result != PSA_ERROR_NOT_SUPPORTED )
4607 {
4608 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4609 * should be exact. */
4610 TEST_EQUAL( output_size,
4611 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004612 TEST_LE_U( output_size,
4613 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004614 }
4615 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004616
Steven Cooremand588ea12021-01-11 19:36:04 +01004617 status = psa_aead_decrypt( key, alg,
4618 nonce->x, nonce->len,
4619 additional_data->x,
4620 additional_data->len,
4621 input_data->x, input_data->len,
4622 output_data, output_size,
4623 &output_length );
4624
Ronald Cron28a45ed2021-02-09 20:35:42 +01004625 /* If the operation is not supported, just skip and not fail in case the
4626 * decryption involves a common limitation of cryptography hardwares and
4627 * an alternative implementation. */
4628 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004629 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004630 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4631 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004632 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004633
4634 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004635
Gilles Peskine2d277862018-06-18 15:41:12 +02004636 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004637 ASSERT_COMPARE( expected_data->x, expected_data->len,
4638 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004639
Gilles Peskinea1cac842018-06-11 19:33:02 +02004640exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004641 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004642 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004643 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004644}
4645/* END_CASE */
4646
4647/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004648void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4649 int alg_arg,
4650 data_t *nonce,
4651 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004652 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004653 int do_set_lengths,
4654 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004655{
Paul Elliottd3f82412021-06-16 16:52:21 +01004656 size_t ad_part_len = 0;
4657 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004658 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004659
Paul Elliott32f46ba2021-09-23 18:24:36 +01004660 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004661 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004662 mbedtls_test_set_step( ad_part_len );
4663
4664 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004665 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004666 if( ad_part_len & 0x01 )
4667 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4668 else
4669 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004670 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004671
4672 /* Split ad into length(ad_part_len) parts. */
4673 if( !aead_multipart_internal_func( key_type_arg, key_data,
4674 alg_arg, nonce,
4675 additional_data,
4676 ad_part_len,
4677 input_data, -1,
4678 set_lengths_method,
4679 expected_output,
4680 1, 0 ) )
4681 break;
4682
4683 /* length(0) part, length(ad_part_len) part, length(0) part... */
4684 mbedtls_test_set_step( 1000 + ad_part_len );
4685
4686 if( !aead_multipart_internal_func( key_type_arg, key_data,
4687 alg_arg, nonce,
4688 additional_data,
4689 ad_part_len,
4690 input_data, -1,
4691 set_lengths_method,
4692 expected_output,
4693 1, 1 ) )
4694 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004695 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004696
Paul Elliott32f46ba2021-09-23 18:24:36 +01004697 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004698 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004699 /* Split data into length(data_part_len) parts. */
4700 mbedtls_test_set_step( 2000 + data_part_len );
4701
4702 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004703 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004704 if( data_part_len & 0x01 )
4705 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4706 else
4707 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004708 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004709
Paul Elliott32f46ba2021-09-23 18:24:36 +01004710 if( !aead_multipart_internal_func( key_type_arg, key_data,
4711 alg_arg, nonce,
4712 additional_data, -1,
4713 input_data, data_part_len,
4714 set_lengths_method,
4715 expected_output,
4716 1, 0 ) )
4717 break;
4718
4719 /* length(0) part, length(data_part_len) part, length(0) part... */
4720 mbedtls_test_set_step( 3000 + data_part_len );
4721
4722 if( !aead_multipart_internal_func( key_type_arg, key_data,
4723 alg_arg, nonce,
4724 additional_data, -1,
4725 input_data, data_part_len,
4726 set_lengths_method,
4727 expected_output,
4728 1, 1 ) )
4729 break;
4730 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004731
Paul Elliott8fc45162021-06-23 16:06:01 +01004732 /* Goto is required to silence warnings about unused labels, as we
4733 * don't actually do any test assertions in this function. */
4734 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004735}
4736/* END_CASE */
4737
4738/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004739void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4740 int alg_arg,
4741 data_t *nonce,
4742 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004743 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004744 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004745 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004746{
Paul Elliottd3f82412021-06-16 16:52:21 +01004747 size_t ad_part_len = 0;
4748 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004749 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004750
Paul Elliott32f46ba2021-09-23 18:24:36 +01004751 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004752 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004753 /* Split ad into length(ad_part_len) parts. */
4754 mbedtls_test_set_step( ad_part_len );
4755
4756 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004757 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004758 if( ad_part_len & 0x01 )
4759 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4760 else
4761 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004762 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004763
4764 if( !aead_multipart_internal_func( key_type_arg, key_data,
4765 alg_arg, nonce,
4766 additional_data,
4767 ad_part_len,
4768 input_data, -1,
4769 set_lengths_method,
4770 expected_output,
4771 0, 0 ) )
4772 break;
4773
4774 /* length(0) part, length(ad_part_len) part, length(0) part... */
4775 mbedtls_test_set_step( 1000 + ad_part_len );
4776
4777 if( !aead_multipart_internal_func( key_type_arg, key_data,
4778 alg_arg, nonce,
4779 additional_data,
4780 ad_part_len,
4781 input_data, -1,
4782 set_lengths_method,
4783 expected_output,
4784 0, 1 ) )
4785 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004786 }
4787
Paul Elliott32f46ba2021-09-23 18:24:36 +01004788 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004789 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004790 /* Split data into length(data_part_len) parts. */
4791 mbedtls_test_set_step( 2000 + data_part_len );
4792
4793 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004794 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004795 if( data_part_len & 0x01 )
4796 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4797 else
4798 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004799 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004800
4801 if( !aead_multipart_internal_func( key_type_arg, key_data,
4802 alg_arg, nonce,
4803 additional_data, -1,
4804 input_data, data_part_len,
4805 set_lengths_method,
4806 expected_output,
4807 0, 0 ) )
4808 break;
4809
4810 /* length(0) part, length(data_part_len) part, length(0) part... */
4811 mbedtls_test_set_step( 3000 + data_part_len );
4812
4813 if( !aead_multipart_internal_func( key_type_arg, key_data,
4814 alg_arg, nonce,
4815 additional_data, -1,
4816 input_data, data_part_len,
4817 set_lengths_method,
4818 expected_output,
4819 0, 1 ) )
4820 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004821 }
4822
Paul Elliott8fc45162021-06-23 16:06:01 +01004823 /* Goto is required to silence warnings about unused labels, as we
4824 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004825 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004826}
4827/* END_CASE */
4828
4829/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004830void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4831 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004832 int nonce_length,
4833 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004834 data_t *additional_data,
4835 data_t *input_data,
4836 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004837{
4838
4839 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4840 psa_key_type_t key_type = key_type_arg;
4841 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004842 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004843 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4845 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004846 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004847 size_t actual_nonce_length = 0;
4848 size_t expected_nonce_length = expected_nonce_length_arg;
4849 unsigned char *output = NULL;
4850 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004851 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004852 size_t ciphertext_size = 0;
4853 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004854 size_t tag_length = 0;
4855 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004856
4857 PSA_ASSERT( psa_crypto_init( ) );
4858
4859 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4860 psa_set_key_algorithm( & attributes, alg );
4861 psa_set_key_type( & attributes, key_type );
4862
4863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4864 &key ) );
4865
4866 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4867
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004868 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4869
Paul Elliottf1277632021-08-24 18:11:37 +01004870 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004871
Paul Elliottf1277632021-08-24 18:11:37 +01004872 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004873
Gilles Peskine7be11a72022-04-14 00:12:57 +02004874 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004875
Paul Elliottf1277632021-08-24 18:11:37 +01004876 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004877
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004878 status = psa_aead_encrypt_setup( &operation, key, alg );
4879
4880 /* If the operation is not supported, just skip and not fail in case the
4881 * encryption involves a common limitation of cryptography hardwares and
4882 * an alternative implementation. */
4883 if( status == PSA_ERROR_NOT_SUPPORTED )
4884 {
4885 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004886 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004887 }
4888
4889 PSA_ASSERT( status );
4890
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004891 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004892 nonce_length,
4893 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004894
Paul Elliott693bf312021-07-23 17:40:41 +01004895 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004896
Paul Elliottf1277632021-08-24 18:11:37 +01004897 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004898
Paul Elliott88ecbe12021-09-22 17:23:03 +01004899 if( expected_status == PSA_SUCCESS )
4900 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4901 alg ) );
4902
Gilles Peskine7be11a72022-04-14 00:12:57 +02004903 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004904
Paul Elliott693bf312021-07-23 17:40:41 +01004905 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004906 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004907 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004908 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4909 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004910
4911 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4912 additional_data->len ) );
4913
4914 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004915 output, output_size,
4916 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004917
Paul Elliottf1277632021-08-24 18:11:37 +01004918 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4919 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004920 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4921 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004922
4923exit:
4924 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004925 mbedtls_free( output );
4926 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004927 psa_aead_abort( &operation );
4928 PSA_DONE( );
4929}
4930/* END_CASE */
4931
4932/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004933void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4934 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004935 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004936 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004937 data_t *additional_data,
4938 data_t *input_data,
4939 int expected_status_arg )
4940{
4941
4942 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4943 psa_key_type_t key_type = key_type_arg;
4944 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004945 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004946 uint8_t *nonce_buffer = NULL;
4947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4948 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4949 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004950 unsigned char *output = NULL;
4951 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004952 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004953 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004954 size_t ciphertext_size = 0;
4955 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004956 size_t tag_length = 0;
4957 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004958 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004959 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004960
4961 PSA_ASSERT( psa_crypto_init( ) );
4962
4963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4964 psa_set_key_algorithm( &attributes, alg );
4965 psa_set_key_type( &attributes, key_type );
4966
4967 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4968 &key ) );
4969
4970 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4971
4972 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4973
Paul Elliott6f0e7202021-08-25 12:57:18 +01004974 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004975
Paul Elliott6f0e7202021-08-25 12:57:18 +01004976 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004977
Gilles Peskine7be11a72022-04-14 00:12:57 +02004978 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004979
Paul Elliott6f0e7202021-08-25 12:57:18 +01004980 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004981
Paul Elliott863864a2021-07-23 17:28:31 +01004982 status = psa_aead_encrypt_setup( &operation, key, alg );
4983
4984 /* If the operation is not supported, just skip and not fail in case the
4985 * encryption involves a common limitation of cryptography hardwares and
4986 * an alternative implementation. */
4987 if( status == PSA_ERROR_NOT_SUPPORTED )
4988 {
4989 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004990 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004991 }
4992
4993 PSA_ASSERT( status );
4994
Paul Elliott4023ffd2021-09-10 16:21:22 +01004995 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4996 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004997 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004998 /* Arbitrary size buffer, to test zero length valid buffer. */
4999 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005000 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005001 }
5002 else
5003 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005004 /* If length is zero, then this will return NULL. */
5005 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005006 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005007
Paul Elliott4023ffd2021-09-10 16:21:22 +01005008 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005009 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005010 for( index = 0; index < nonce_length - 1; ++index )
5011 {
5012 nonce_buffer[index] = 'a' + index;
5013 }
Paul Elliott66696b52021-08-16 18:42:41 +01005014 }
Paul Elliott863864a2021-07-23 17:28:31 +01005015 }
5016
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005017 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5018 {
5019 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5020 input_data->len ) );
5021 }
5022
Paul Elliott6f0e7202021-08-25 12:57:18 +01005023 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005024
Paul Elliott693bf312021-07-23 17:40:41 +01005025 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005026
5027 if( expected_status == PSA_SUCCESS )
5028 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005029 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5030 {
5031 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5032 input_data->len ) );
5033 }
5034 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5035 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005036
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005037 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5038 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5039 additional_data->len ),
5040 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005041
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005042 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005043 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005044 &ciphertext_length ),
5045 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005046
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005047 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005048 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005049 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5050 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005051 }
5052
5053exit:
5054 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005055 mbedtls_free( output );
5056 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005057 mbedtls_free( nonce_buffer );
5058 psa_aead_abort( &operation );
5059 PSA_DONE( );
5060}
5061/* END_CASE */
5062
5063/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005064void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5065 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005066 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005067 data_t *nonce,
5068 data_t *additional_data,
5069 data_t *input_data,
5070 int expected_status_arg )
5071{
5072
5073 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5074 psa_key_type_t key_type = key_type_arg;
5075 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005076 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005077 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5078 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5079 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005080 unsigned char *output = NULL;
5081 unsigned char *ciphertext = NULL;
5082 size_t output_size = output_size_arg;
5083 size_t ciphertext_size = 0;
5084 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005085 size_t tag_length = 0;
5086 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5087
5088 PSA_ASSERT( psa_crypto_init( ) );
5089
5090 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5091 psa_set_key_algorithm( &attributes, alg );
5092 psa_set_key_type( &attributes, key_type );
5093
5094 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5095 &key ) );
5096
5097 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5098
Paul Elliottc6d11d02021-09-01 12:04:23 +01005099 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005100
Paul Elliottc6d11d02021-09-01 12:04:23 +01005101 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005102
Paul Elliottc6d11d02021-09-01 12:04:23 +01005103 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005104
Paul Elliott43fbda62021-07-23 18:30:59 +01005105 status = psa_aead_encrypt_setup( &operation, key, alg );
5106
5107 /* If the operation is not supported, just skip and not fail in case the
5108 * encryption involves a common limitation of cryptography hardwares and
5109 * an alternative implementation. */
5110 if( status == PSA_ERROR_NOT_SUPPORTED )
5111 {
5112 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5113 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5114 }
5115
5116 PSA_ASSERT( status );
5117
Paul Elliott47b9a142021-10-07 15:04:57 +01005118 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5119 input_data->len ) );
5120
Paul Elliott43fbda62021-07-23 18:30:59 +01005121 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5122
5123 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5124 additional_data->len ) );
5125
5126 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005127 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005128
5129 TEST_EQUAL( status, expected_status );
5130
5131 if( expected_status == PSA_SUCCESS )
5132 {
5133 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005134 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5135 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005136 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5137 }
5138
5139exit:
5140 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005141 mbedtls_free( output );
5142 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005143 psa_aead_abort( &operation );
5144 PSA_DONE( );
5145}
5146/* END_CASE */
5147
Paul Elliott91b021e2021-07-23 18:52:31 +01005148/* BEGIN_CASE */
5149void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5150 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005151 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005152 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005153 data_t *nonce,
5154 data_t *additional_data,
5155 data_t *input_data,
5156 int expected_status_arg )
5157{
5158
5159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5160 psa_key_type_t key_type = key_type_arg;
5161 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005162 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5164 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5165 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005166 unsigned char *ciphertext = NULL;
5167 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005168 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005169 size_t ciphertext_size = 0;
5170 size_t ciphertext_length = 0;
5171 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005172 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005173 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005174
5175 PSA_ASSERT( psa_crypto_init( ) );
5176
5177 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5178 psa_set_key_algorithm( &attributes, alg );
5179 psa_set_key_type( &attributes, key_type );
5180
5181 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5182 &key ) );
5183
5184 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5185
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005186 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005187
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005188 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005189
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005190 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005191
Paul Elliott719c1322021-09-13 18:27:22 +01005192 ASSERT_ALLOC( tag_buffer, tag_size );
5193
Paul Elliott91b021e2021-07-23 18:52:31 +01005194 status = psa_aead_encrypt_setup( &operation, key, alg );
5195
5196 /* If the operation is not supported, just skip and not fail in case the
5197 * encryption involves a common limitation of cryptography hardwares and
5198 * an alternative implementation. */
5199 if( status == PSA_ERROR_NOT_SUPPORTED )
5200 {
5201 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5202 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5203 }
5204
5205 PSA_ASSERT( status );
5206
5207 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5208
Paul Elliott76bda482021-10-07 17:07:23 +01005209 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5210 input_data->len ) );
5211
Paul Elliott91b021e2021-07-23 18:52:31 +01005212 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5213 additional_data->len ) );
5214
5215 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005216 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005217
5218 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005219 status = psa_aead_finish( &operation, finish_ciphertext,
5220 finish_ciphertext_size,
5221 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005222 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005223
5224 TEST_EQUAL( status, expected_status );
5225
5226exit:
5227 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005228 mbedtls_free( ciphertext );
5229 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005230 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005231 psa_aead_abort( &operation );
5232 PSA_DONE( );
5233}
5234/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005235
5236/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005237void aead_multipart_verify( int key_type_arg, data_t *key_data,
5238 int alg_arg,
5239 data_t *nonce,
5240 data_t *additional_data,
5241 data_t *input_data,
5242 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005243 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005244 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005245 int expected_status_arg )
5246{
5247 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5248 psa_key_type_t key_type = key_type_arg;
5249 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005250 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5252 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5253 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005254 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005255 unsigned char *plaintext = NULL;
5256 unsigned char *finish_plaintext = NULL;
5257 size_t plaintext_size = 0;
5258 size_t plaintext_length = 0;
5259 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005260 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005261 unsigned char *tag_buffer = NULL;
5262 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005263
5264 PSA_ASSERT( psa_crypto_init( ) );
5265
5266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5267 psa_set_key_algorithm( &attributes, alg );
5268 psa_set_key_type( &attributes, key_type );
5269
5270 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5271 &key ) );
5272
5273 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5274
5275 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5276 input_data->len );
5277
5278 ASSERT_ALLOC( plaintext, plaintext_size );
5279
5280 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5281
5282 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5283
Paul Elliott9961a662021-09-17 19:19:02 +01005284 status = psa_aead_decrypt_setup( &operation, key, alg );
5285
5286 /* If the operation is not supported, just skip and not fail in case the
5287 * encryption involves a common limitation of cryptography hardwares and
5288 * an alternative implementation. */
5289 if( status == PSA_ERROR_NOT_SUPPORTED )
5290 {
5291 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5292 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5293 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005294 TEST_EQUAL( status, expected_setup_status );
5295
5296 if( status != PSA_SUCCESS )
5297 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005298
5299 PSA_ASSERT( status );
5300
5301 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5302
Paul Elliottfec6f372021-10-06 17:15:02 +01005303 status = psa_aead_set_lengths( &operation, additional_data->len,
5304 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005305 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005306
Paul Elliott9961a662021-09-17 19:19:02 +01005307 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5308 additional_data->len ) );
5309
5310 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5311 input_data->len,
5312 plaintext, plaintext_size,
5313 &plaintext_length ) );
5314
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005315 if( tag_usage == USE_GIVEN_TAG )
5316 {
5317 tag_buffer = tag->x;
5318 tag_size = tag->len;
5319 }
5320
Paul Elliott9961a662021-09-17 19:19:02 +01005321 status = psa_aead_verify( &operation, finish_plaintext,
5322 verify_plaintext_size,
5323 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005324 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005325
5326 TEST_EQUAL( status, expected_status );
5327
5328exit:
5329 psa_destroy_key( key );
5330 mbedtls_free( plaintext );
5331 mbedtls_free( finish_plaintext );
5332 psa_aead_abort( &operation );
5333 PSA_DONE( );
5334}
5335/* END_CASE */
5336
Paul Elliott9961a662021-09-17 19:19:02 +01005337/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005338void aead_multipart_setup( int key_type_arg, data_t *key_data,
5339 int alg_arg, int expected_status_arg )
5340{
5341 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5342 psa_key_type_t key_type = key_type_arg;
5343 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005344 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5346 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5347 psa_status_t expected_status = expected_status_arg;
5348
5349 PSA_ASSERT( psa_crypto_init( ) );
5350
5351 psa_set_key_usage_flags( &attributes,
5352 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5353 psa_set_key_algorithm( &attributes, alg );
5354 psa_set_key_type( &attributes, key_type );
5355
5356 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5357 &key ) );
5358
Paul Elliott5221ef62021-09-19 17:33:03 +01005359 status = psa_aead_encrypt_setup( &operation, key, alg );
5360
5361 TEST_EQUAL( status, expected_status );
5362
5363 psa_aead_abort( &operation );
5364
Paul Elliott5221ef62021-09-19 17:33:03 +01005365 status = psa_aead_decrypt_setup( &operation, key, alg );
5366
5367 TEST_EQUAL(status, expected_status );
5368
5369exit:
5370 psa_destroy_key( key );
5371 psa_aead_abort( &operation );
5372 PSA_DONE( );
5373}
5374/* END_CASE */
5375
5376/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005377void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5378 int alg_arg,
5379 data_t *nonce,
5380 data_t *additional_data,
5381 data_t *input_data )
5382{
5383 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5384 psa_key_type_t key_type = key_type_arg;
5385 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005386 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005387 unsigned char *output_data = NULL;
5388 unsigned char *final_data = NULL;
5389 size_t output_size = 0;
5390 size_t finish_output_size = 0;
5391 size_t output_length = 0;
5392 size_t key_bits = 0;
5393 size_t tag_length = 0;
5394 size_t tag_size = 0;
5395 size_t nonce_length = 0;
5396 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5397 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5398 size_t output_part_length = 0;
5399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5400
5401 PSA_ASSERT( psa_crypto_init( ) );
5402
5403 psa_set_key_usage_flags( & attributes,
5404 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5405 psa_set_key_algorithm( & attributes, alg );
5406 psa_set_key_type( & attributes, key_type );
5407
5408 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5409 &key ) );
5410
5411 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5412 key_bits = psa_get_key_bits( &attributes );
5413
5414 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5415
Gilles Peskine7be11a72022-04-14 00:12:57 +02005416 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005417
5418 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5419
5420 ASSERT_ALLOC( output_data, output_size );
5421
5422 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5423
Gilles Peskine7be11a72022-04-14 00:12:57 +02005424 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005425
5426 ASSERT_ALLOC( final_data, finish_output_size );
5427
5428 /* Test all operations error without calling setup first. */
5429
Paul Elliottc23a9a02021-06-21 18:32:46 +01005430 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5431 PSA_ERROR_BAD_STATE );
5432
5433 psa_aead_abort( &operation );
5434
Paul Elliottc23a9a02021-06-21 18:32:46 +01005435 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5436 PSA_AEAD_NONCE_MAX_SIZE,
5437 &nonce_length ),
5438 PSA_ERROR_BAD_STATE );
5439
5440 psa_aead_abort( &operation );
5441
Paul Elliott481be342021-07-16 17:38:47 +01005442 /* ------------------------------------------------------- */
5443
Paul Elliottc23a9a02021-06-21 18:32:46 +01005444 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5445 input_data->len ),
5446 PSA_ERROR_BAD_STATE );
5447
5448 psa_aead_abort( &operation );
5449
Paul Elliott481be342021-07-16 17:38:47 +01005450 /* ------------------------------------------------------- */
5451
Paul Elliottc23a9a02021-06-21 18:32:46 +01005452 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5453 additional_data->len ),
5454 PSA_ERROR_BAD_STATE );
5455
5456 psa_aead_abort( &operation );
5457
Paul Elliott481be342021-07-16 17:38:47 +01005458 /* ------------------------------------------------------- */
5459
Paul Elliottc23a9a02021-06-21 18:32:46 +01005460 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5461 input_data->len, output_data,
5462 output_size, &output_length ),
5463 PSA_ERROR_BAD_STATE );
5464
5465 psa_aead_abort( &operation );
5466
Paul Elliott481be342021-07-16 17:38:47 +01005467 /* ------------------------------------------------------- */
5468
Paul Elliottc23a9a02021-06-21 18:32:46 +01005469 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5470 finish_output_size,
5471 &output_part_length,
5472 tag_buffer, tag_length,
5473 &tag_size ),
5474 PSA_ERROR_BAD_STATE );
5475
5476 psa_aead_abort( &operation );
5477
Paul Elliott481be342021-07-16 17:38:47 +01005478 /* ------------------------------------------------------- */
5479
Paul Elliottc23a9a02021-06-21 18:32:46 +01005480 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5481 finish_output_size,
5482 &output_part_length,
5483 tag_buffer,
5484 tag_length ),
5485 PSA_ERROR_BAD_STATE );
5486
5487 psa_aead_abort( &operation );
5488
5489 /* Test for double setups. */
5490
Paul Elliottc23a9a02021-06-21 18:32:46 +01005491 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5492
5493 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5494 PSA_ERROR_BAD_STATE );
5495
5496 psa_aead_abort( &operation );
5497
Paul Elliott481be342021-07-16 17:38:47 +01005498 /* ------------------------------------------------------- */
5499
Paul Elliottc23a9a02021-06-21 18:32:46 +01005500 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5501
5502 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5503 PSA_ERROR_BAD_STATE );
5504
5505 psa_aead_abort( &operation );
5506
Paul Elliott374a2be2021-07-16 17:53:40 +01005507 /* ------------------------------------------------------- */
5508
Paul Elliott374a2be2021-07-16 17:53:40 +01005509 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5510
5511 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5512 PSA_ERROR_BAD_STATE );
5513
5514 psa_aead_abort( &operation );
5515
5516 /* ------------------------------------------------------- */
5517
Paul Elliott374a2be2021-07-16 17:53:40 +01005518 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5519
5520 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5521 PSA_ERROR_BAD_STATE );
5522
5523 psa_aead_abort( &operation );
5524
Paul Elliottc23a9a02021-06-21 18:32:46 +01005525 /* Test for not setting a nonce. */
5526
Paul Elliottc23a9a02021-06-21 18:32:46 +01005527 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5528
5529 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5530 additional_data->len ),
5531 PSA_ERROR_BAD_STATE );
5532
5533 psa_aead_abort( &operation );
5534
Paul Elliott7f628422021-09-01 12:08:29 +01005535 /* ------------------------------------------------------- */
5536
Paul Elliott7f628422021-09-01 12:08:29 +01005537 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5538
5539 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5540 input_data->len, output_data,
5541 output_size, &output_length ),
5542 PSA_ERROR_BAD_STATE );
5543
5544 psa_aead_abort( &operation );
5545
Paul Elliottbdc2c682021-09-21 18:37:10 +01005546 /* ------------------------------------------------------- */
5547
Paul Elliottbdc2c682021-09-21 18:37:10 +01005548 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5549
5550 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5551 finish_output_size,
5552 &output_part_length,
5553 tag_buffer, tag_length,
5554 &tag_size ),
5555 PSA_ERROR_BAD_STATE );
5556
5557 psa_aead_abort( &operation );
5558
5559 /* ------------------------------------------------------- */
5560
Paul Elliottbdc2c682021-09-21 18:37:10 +01005561 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5562
5563 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5564 finish_output_size,
5565 &output_part_length,
5566 tag_buffer,
5567 tag_length ),
5568 PSA_ERROR_BAD_STATE );
5569
5570 psa_aead_abort( &operation );
5571
Paul Elliottc23a9a02021-06-21 18:32:46 +01005572 /* Test for double setting nonce. */
5573
Paul Elliottc23a9a02021-06-21 18:32:46 +01005574 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5575
5576 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5577
5578 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5579 PSA_ERROR_BAD_STATE );
5580
5581 psa_aead_abort( &operation );
5582
Paul Elliott374a2be2021-07-16 17:53:40 +01005583 /* Test for double generating nonce. */
5584
Paul Elliott374a2be2021-07-16 17:53:40 +01005585 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5586
5587 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5588 PSA_AEAD_NONCE_MAX_SIZE,
5589 &nonce_length ) );
5590
5591 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5592 PSA_AEAD_NONCE_MAX_SIZE,
5593 &nonce_length ),
5594 PSA_ERROR_BAD_STATE );
5595
5596
5597 psa_aead_abort( &operation );
5598
5599 /* Test for generate nonce then set and vice versa */
5600
Paul Elliott374a2be2021-07-16 17:53:40 +01005601 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5602
5603 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5604 PSA_AEAD_NONCE_MAX_SIZE,
5605 &nonce_length ) );
5606
5607 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5608 PSA_ERROR_BAD_STATE );
5609
5610 psa_aead_abort( &operation );
5611
Andrzej Kurekad837522021-12-15 15:28:49 +01005612 /* Test for generating nonce after calling set lengths */
5613
5614 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5615
5616 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5617 input_data->len ) );
5618
5619 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5620 PSA_AEAD_NONCE_MAX_SIZE,
5621 &nonce_length ) );
5622
5623 psa_aead_abort( &operation );
5624
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005625 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005626
5627 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5628
5629 if( operation.alg == PSA_ALG_CCM )
5630 {
5631 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5632 input_data->len ),
5633 PSA_ERROR_INVALID_ARGUMENT );
5634 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5635 PSA_AEAD_NONCE_MAX_SIZE,
5636 &nonce_length ),
5637 PSA_ERROR_BAD_STATE );
5638 }
5639 else
5640 {
5641 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5642 input_data->len ) );
5643 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5644 PSA_AEAD_NONCE_MAX_SIZE,
5645 &nonce_length ) );
5646 }
5647
5648 psa_aead_abort( &operation );
5649
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005650 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005651#if SIZE_MAX > UINT32_MAX
5652 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5653
5654 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5655 {
5656 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5657 input_data->len ),
5658 PSA_ERROR_INVALID_ARGUMENT );
5659 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5660 PSA_AEAD_NONCE_MAX_SIZE,
5661 &nonce_length ),
5662 PSA_ERROR_BAD_STATE );
5663 }
5664 else
5665 {
5666 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5667 input_data->len ) );
5668 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5669 PSA_AEAD_NONCE_MAX_SIZE,
5670 &nonce_length ) );
5671 }
5672
5673 psa_aead_abort( &operation );
5674#endif
5675
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005676 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005677
5678 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5679
5680 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5681 PSA_AEAD_NONCE_MAX_SIZE,
5682 &nonce_length ) );
5683
5684 if( operation.alg == PSA_ALG_CCM )
5685 {
5686 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5687 input_data->len ),
5688 PSA_ERROR_INVALID_ARGUMENT );
5689 }
5690 else
5691 {
5692 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5693 input_data->len ) );
5694 }
5695
5696 psa_aead_abort( &operation );
5697
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005698 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005699 /* Test for setting nonce after calling set lengths */
5700
5701 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5702
5703 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5704 input_data->len ) );
5705
5706 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5707
5708 psa_aead_abort( &operation );
5709
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005710 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005711
5712 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5713
5714 if( operation.alg == PSA_ALG_CCM )
5715 {
5716 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5717 input_data->len ),
5718 PSA_ERROR_INVALID_ARGUMENT );
5719 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5720 PSA_ERROR_BAD_STATE );
5721 }
5722 else
5723 {
5724 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5725 input_data->len ) );
5726 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5727 }
5728
5729 psa_aead_abort( &operation );
5730
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005731 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005732#if SIZE_MAX > UINT32_MAX
5733 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5734
5735 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5736 {
5737 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5738 input_data->len ),
5739 PSA_ERROR_INVALID_ARGUMENT );
5740 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5741 PSA_ERROR_BAD_STATE );
5742 }
5743 else
5744 {
5745 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5746 input_data->len ) );
5747 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5748 }
5749
5750 psa_aead_abort( &operation );
5751#endif
5752
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005753 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005754
5755 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5756
5757 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5758
5759 if( operation.alg == PSA_ALG_CCM )
5760 {
5761 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5762 input_data->len ),
5763 PSA_ERROR_INVALID_ARGUMENT );
5764 }
5765 else
5766 {
5767 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5768 input_data->len ) );
5769 }
5770
5771 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005772
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005773 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005774#if SIZE_MAX > UINT32_MAX
5775 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5776
5777 if( operation.alg == PSA_ALG_GCM )
5778 {
5779 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5780 SIZE_MAX ),
5781 PSA_ERROR_INVALID_ARGUMENT );
5782 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5783 PSA_ERROR_BAD_STATE );
5784 }
5785 else if ( operation.alg != PSA_ALG_CCM )
5786 {
5787 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5788 SIZE_MAX ) );
5789 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5790 }
5791
5792 psa_aead_abort( &operation );
5793
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005794 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005795 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5796
5797 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5798
5799 if( operation.alg == PSA_ALG_GCM )
5800 {
5801 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5802 SIZE_MAX ),
5803 PSA_ERROR_INVALID_ARGUMENT );
5804 }
5805 else if ( operation.alg != PSA_ALG_CCM )
5806 {
5807 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5808 SIZE_MAX ) );
5809 }
5810
5811 psa_aead_abort( &operation );
5812#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005813
5814 /* ------------------------------------------------------- */
5815
Paul Elliott374a2be2021-07-16 17:53:40 +01005816 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5817
5818 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5819
5820 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5821 PSA_AEAD_NONCE_MAX_SIZE,
5822 &nonce_length ),
5823 PSA_ERROR_BAD_STATE );
5824
5825 psa_aead_abort( &operation );
5826
Paul Elliott7220cae2021-06-22 17:25:57 +01005827 /* Test for generating nonce in decrypt setup. */
5828
Paul Elliott7220cae2021-06-22 17:25:57 +01005829 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5830
5831 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5832 PSA_AEAD_NONCE_MAX_SIZE,
5833 &nonce_length ),
5834 PSA_ERROR_BAD_STATE );
5835
5836 psa_aead_abort( &operation );
5837
Paul Elliottc23a9a02021-06-21 18:32:46 +01005838 /* Test for setting lengths twice. */
5839
Paul Elliottc23a9a02021-06-21 18:32:46 +01005840 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5841
5842 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5843
5844 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5845 input_data->len ) );
5846
5847 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5848 input_data->len ),
5849 PSA_ERROR_BAD_STATE );
5850
5851 psa_aead_abort( &operation );
5852
Andrzej Kurekad837522021-12-15 15:28:49 +01005853 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005854
Paul Elliottc23a9a02021-06-21 18:32:46 +01005855 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5856
5857 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5858
Andrzej Kurekad837522021-12-15 15:28:49 +01005859 if( operation.alg == PSA_ALG_CCM )
5860 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005861
Andrzej Kurekad837522021-12-15 15:28:49 +01005862 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5863 additional_data->len ),
5864 PSA_ERROR_BAD_STATE );
5865 }
5866 else
5867 {
5868 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5869 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005870
Andrzej Kurekad837522021-12-15 15:28:49 +01005871 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5872 input_data->len ),
5873 PSA_ERROR_BAD_STATE );
5874 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005875 psa_aead_abort( &operation );
5876
5877 /* ------------------------------------------------------- */
5878
Paul Elliottf94bd992021-09-19 18:15:59 +01005879 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5880
5881 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5882
Andrzej Kurekad837522021-12-15 15:28:49 +01005883 if( operation.alg == PSA_ALG_CCM )
5884 {
5885 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5886 input_data->len, output_data,
5887 output_size, &output_length ),
5888 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005889
Andrzej Kurekad837522021-12-15 15:28:49 +01005890 }
5891 else
5892 {
5893 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5894 input_data->len, output_data,
5895 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005896
Andrzej Kurekad837522021-12-15 15:28:49 +01005897 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5898 input_data->len ),
5899 PSA_ERROR_BAD_STATE );
5900 }
5901 psa_aead_abort( &operation );
5902
5903 /* ------------------------------------------------------- */
5904
5905 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5906
5907 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5908
5909 if( operation.alg == PSA_ALG_CCM )
5910 {
5911 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5912 finish_output_size,
5913 &output_part_length,
5914 tag_buffer, tag_length,
5915 &tag_size ) );
5916 }
5917 else
5918 {
5919 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5920 finish_output_size,
5921 &output_part_length,
5922 tag_buffer, tag_length,
5923 &tag_size ) );
5924
5925 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5926 input_data->len ),
5927 PSA_ERROR_BAD_STATE );
5928 }
5929 psa_aead_abort( &operation );
5930
5931 /* Test for setting lengths after generating nonce + already starting data. */
5932
5933 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5934
5935 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5936 PSA_AEAD_NONCE_MAX_SIZE,
5937 &nonce_length ) );
5938 if( operation.alg == PSA_ALG_CCM )
5939 {
5940
5941 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5942 additional_data->len ),
5943 PSA_ERROR_BAD_STATE );
5944 }
5945 else
5946 {
5947 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5948 additional_data->len ) );
5949
5950 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5951 input_data->len ),
5952 PSA_ERROR_BAD_STATE );
5953 }
5954 psa_aead_abort( &operation );
5955
5956 /* ------------------------------------------------------- */
5957
5958 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5959
5960 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5961 PSA_AEAD_NONCE_MAX_SIZE,
5962 &nonce_length ) );
5963 if( operation.alg == PSA_ALG_CCM )
5964 {
5965 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5966 input_data->len, output_data,
5967 output_size, &output_length ),
5968 PSA_ERROR_BAD_STATE );
5969
5970 }
5971 else
5972 {
5973 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5974 input_data->len, output_data,
5975 output_size, &output_length ) );
5976
5977 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5978 input_data->len ),
5979 PSA_ERROR_BAD_STATE );
5980 }
5981 psa_aead_abort( &operation );
5982
5983 /* ------------------------------------------------------- */
5984
5985 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5986
5987 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5988 PSA_AEAD_NONCE_MAX_SIZE,
5989 &nonce_length ) );
5990 if( operation.alg == PSA_ALG_CCM )
5991 {
5992 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5993 finish_output_size,
5994 &output_part_length,
5995 tag_buffer, tag_length,
5996 &tag_size ) );
5997 }
5998 else
5999 {
6000 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6001 finish_output_size,
6002 &output_part_length,
6003 tag_buffer, tag_length,
6004 &tag_size ) );
6005
6006 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6007 input_data->len ),
6008 PSA_ERROR_BAD_STATE );
6009 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006010 psa_aead_abort( &operation );
6011
Paul Elliott243080c2021-07-21 19:01:17 +01006012 /* Test for not sending any additional data or data after setting non zero
6013 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006014
Paul Elliottc23a9a02021-06-21 18:32:46 +01006015 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6016
6017 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6018
6019 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6020 input_data->len ) );
6021
6022 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6023 finish_output_size,
6024 &output_part_length,
6025 tag_buffer, tag_length,
6026 &tag_size ),
6027 PSA_ERROR_INVALID_ARGUMENT );
6028
6029 psa_aead_abort( &operation );
6030
Paul Elliott243080c2021-07-21 19:01:17 +01006031 /* Test for not sending any additional data or data after setting non-zero
6032 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006033
Paul Elliottc23a9a02021-06-21 18:32:46 +01006034 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6035
6036 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6037
6038 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6039 input_data->len ) );
6040
6041 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6042 finish_output_size,
6043 &output_part_length,
6044 tag_buffer,
6045 tag_length ),
6046 PSA_ERROR_INVALID_ARGUMENT );
6047
6048 psa_aead_abort( &operation );
6049
Paul Elliott243080c2021-07-21 19:01:17 +01006050 /* Test for not sending any additional data after setting a non-zero length
6051 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006052
Paul Elliottc23a9a02021-06-21 18:32:46 +01006053 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6054
6055 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6056
6057 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6058 input_data->len ) );
6059
6060 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6061 input_data->len, output_data,
6062 output_size, &output_length ),
6063 PSA_ERROR_INVALID_ARGUMENT );
6064
6065 psa_aead_abort( &operation );
6066
Paul Elliottf94bd992021-09-19 18:15:59 +01006067 /* Test for not sending any data after setting a non-zero length for it.*/
6068
Paul Elliottf94bd992021-09-19 18:15:59 +01006069 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6070
6071 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6072
6073 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6074 input_data->len ) );
6075
6076 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6077 additional_data->len ) );
6078
6079 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6080 finish_output_size,
6081 &output_part_length,
6082 tag_buffer, tag_length,
6083 &tag_size ),
6084 PSA_ERROR_INVALID_ARGUMENT );
6085
6086 psa_aead_abort( &operation );
6087
Paul Elliottb0450fe2021-09-01 15:06:26 +01006088 /* Test for sending too much additional data after setting lengths. */
6089
Paul Elliottb0450fe2021-09-01 15:06:26 +01006090 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6091
6092 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6093
6094 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6095
6096
6097 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6098 additional_data->len ),
6099 PSA_ERROR_INVALID_ARGUMENT );
6100
6101 psa_aead_abort( &operation );
6102
Paul Elliotta2a09b02021-09-22 14:56:40 +01006103 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006104
6105 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6106
6107 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6108
6109 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6110 input_data->len ) );
6111
6112 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6113 additional_data->len ) );
6114
6115 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6116 1 ),
6117 PSA_ERROR_INVALID_ARGUMENT );
6118
6119 psa_aead_abort( &operation );
6120
Paul Elliottb0450fe2021-09-01 15:06:26 +01006121 /* Test for sending too much data after setting lengths. */
6122
Paul Elliottb0450fe2021-09-01 15:06:26 +01006123 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6124
6125 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6126
6127 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6128
6129 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6130 input_data->len, output_data,
6131 output_size, &output_length ),
6132 PSA_ERROR_INVALID_ARGUMENT );
6133
6134 psa_aead_abort( &operation );
6135
Paul Elliotta2a09b02021-09-22 14:56:40 +01006136 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006137
6138 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6139
6140 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6141
6142 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6143 input_data->len ) );
6144
6145 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6146 additional_data->len ) );
6147
6148 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6149 input_data->len, output_data,
6150 output_size, &output_length ) );
6151
6152 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6153 1, output_data,
6154 output_size, &output_length ),
6155 PSA_ERROR_INVALID_ARGUMENT );
6156
6157 psa_aead_abort( &operation );
6158
Paul Elliottc23a9a02021-06-21 18:32:46 +01006159 /* Test sending additional data after data. */
6160
Paul Elliottc23a9a02021-06-21 18:32:46 +01006161 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6162
6163 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6164
Andrzej Kurekad837522021-12-15 15:28:49 +01006165 if( operation.alg != PSA_ALG_CCM )
6166 {
6167 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6168 input_data->len, output_data,
6169 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006170
Andrzej Kurekad837522021-12-15 15:28:49 +01006171 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6172 additional_data->len ),
6173 PSA_ERROR_BAD_STATE );
6174 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006175 psa_aead_abort( &operation );
6176
Paul Elliott534d0b42021-06-22 19:15:20 +01006177 /* Test calling finish on decryption. */
6178
Paul Elliott534d0b42021-06-22 19:15:20 +01006179 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6180
6181 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6182
6183 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6184 finish_output_size,
6185 &output_part_length,
6186 tag_buffer, tag_length,
6187 &tag_size ),
6188 PSA_ERROR_BAD_STATE );
6189
6190 psa_aead_abort( &operation );
6191
6192 /* Test calling verify on encryption. */
6193
Paul Elliott534d0b42021-06-22 19:15:20 +01006194 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6195
6196 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6197
6198 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6199 finish_output_size,
6200 &output_part_length,
6201 tag_buffer,
6202 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006203 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006204
6205 psa_aead_abort( &operation );
6206
6207
Paul Elliottc23a9a02021-06-21 18:32:46 +01006208exit:
6209 psa_destroy_key( key );
6210 psa_aead_abort( &operation );
6211 mbedtls_free( output_data );
6212 mbedtls_free( final_data );
6213 PSA_DONE( );
6214}
6215/* END_CASE */
6216
6217/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006218void signature_size( int type_arg,
6219 int bits,
6220 int alg_arg,
6221 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006222{
6223 psa_key_type_t type = type_arg;
6224 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006225 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006226
Gilles Peskinefe11b722018-12-18 00:24:04 +01006227 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006228
Gilles Peskinee59236f2018-01-27 23:32:46 +01006229exit:
6230 ;
6231}
6232/* END_CASE */
6233
6234/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006235void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6236 int alg_arg, data_t *input_data,
6237 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006238{
Ronald Cron5425a212020-08-04 14:58:35 +02006239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006240 psa_key_type_t key_type = key_type_arg;
6241 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006242 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006243 unsigned char *signature = NULL;
6244 size_t signature_size;
6245 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006246 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006247
Gilles Peskine8817f612018-12-18 00:18:46 +01006248 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006249
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006251 psa_set_key_algorithm( &attributes, alg );
6252 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006253
Gilles Peskine049c7532019-05-15 20:22:09 +02006254 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006255 &key ) );
6256 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006257 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006258
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006259 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006260 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006261 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006262 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006263 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006264 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006265 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006266
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006267 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006268 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006269 input_data->x, input_data->len,
6270 signature, signature_size,
6271 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006272 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006273 ASSERT_COMPARE( output_data->x, output_data->len,
6274 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006275
6276exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006277 /*
6278 * Key attributes may have been returned by psa_get_key_attributes()
6279 * thus reset them as required.
6280 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006281 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006282
Ronald Cron5425a212020-08-04 14:58:35 +02006283 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006284 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006285 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006286}
6287/* END_CASE */
6288
6289/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006290void sign_hash_fail( int key_type_arg, data_t *key_data,
6291 int alg_arg, data_t *input_data,
6292 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006293{
Ronald Cron5425a212020-08-04 14:58:35 +02006294 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006295 psa_key_type_t key_type = key_type_arg;
6296 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006297 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006298 psa_status_t actual_status;
6299 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006300 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006301 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006303
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006304 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006305
Gilles Peskine8817f612018-12-18 00:18:46 +01006306 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006307
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006308 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006309 psa_set_key_algorithm( &attributes, alg );
6310 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006311
Gilles Peskine049c7532019-05-15 20:22:09 +02006312 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006313 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006314
Ronald Cron5425a212020-08-04 14:58:35 +02006315 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006316 input_data->x, input_data->len,
6317 signature, signature_size,
6318 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006319 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006320 /* The value of *signature_length is unspecified on error, but
6321 * whatever it is, it should be less than signature_size, so that
6322 * if the caller tries to read *signature_length bytes without
6323 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006324 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006325
6326exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006327 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006328 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006329 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006330 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006331}
6332/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006333
6334/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006335void sign_verify_hash( int key_type_arg, data_t *key_data,
6336 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006337{
Ronald Cron5425a212020-08-04 14:58:35 +02006338 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006339 psa_key_type_t key_type = key_type_arg;
6340 psa_algorithm_t alg = alg_arg;
6341 size_t key_bits;
6342 unsigned char *signature = NULL;
6343 size_t signature_size;
6344 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006346
Gilles Peskine8817f612018-12-18 00:18:46 +01006347 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006348
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006349 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006350 psa_set_key_algorithm( &attributes, alg );
6351 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006352
Gilles Peskine049c7532019-05-15 20:22:09 +02006353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006354 &key ) );
6355 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006356 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006357
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006358 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006359 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006360 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006361 key_bits, alg );
6362 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006363 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006364 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006365
6366 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006367 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006368 input_data->x, input_data->len,
6369 signature, signature_size,
6370 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006371 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006372 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006373 TEST_ASSERT( signature_length > 0 );
6374
6375 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006376 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006377 input_data->x, input_data->len,
6378 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006379
6380 if( input_data->len != 0 )
6381 {
6382 /* Flip a bit in the input and verify that the signature is now
6383 * detected as invalid. Flip a bit at the beginning, not at the end,
6384 * because ECDSA may ignore the last few bits of the input. */
6385 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006386 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006387 input_data->x, input_data->len,
6388 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006389 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006390 }
6391
6392exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006393 /*
6394 * Key attributes may have been returned by psa_get_key_attributes()
6395 * thus reset them as required.
6396 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006397 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006398
Ronald Cron5425a212020-08-04 14:58:35 +02006399 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006400 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006401 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006402}
6403/* END_CASE */
6404
6405/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006406void verify_hash( int key_type_arg, data_t *key_data,
6407 int alg_arg, data_t *hash_data,
6408 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006409{
Ronald Cron5425a212020-08-04 14:58:35 +02006410 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006411 psa_key_type_t key_type = key_type_arg;
6412 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006414
Gilles Peskine7be11a72022-04-14 00:12:57 +02006415 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006416
Gilles Peskine8817f612018-12-18 00:18:46 +01006417 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006418
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006419 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006420 psa_set_key_algorithm( &attributes, alg );
6421 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006422
Gilles Peskine049c7532019-05-15 20:22:09 +02006423 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006424 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006425
Ronald Cron5425a212020-08-04 14:58:35 +02006426 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006427 hash_data->x, hash_data->len,
6428 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006429
itayzafrir5c753392018-05-08 11:18:38 +03006430exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006431 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006432 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006433 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006434}
6435/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006436
6437/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006438void verify_hash_fail( int key_type_arg, data_t *key_data,
6439 int alg_arg, data_t *hash_data,
6440 data_t *signature_data,
6441 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006442{
Ronald Cron5425a212020-08-04 14:58:35 +02006443 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006444 psa_key_type_t key_type = key_type_arg;
6445 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006446 psa_status_t actual_status;
6447 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006449
Gilles Peskine8817f612018-12-18 00:18:46 +01006450 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006451
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006452 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006453 psa_set_key_algorithm( &attributes, alg );
6454 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006455
Gilles Peskine049c7532019-05-15 20:22:09 +02006456 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006457 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006458
Ronald Cron5425a212020-08-04 14:58:35 +02006459 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006460 hash_data->x, hash_data->len,
6461 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006462 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006463
6464exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006465 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006466 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006467 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006468}
6469/* END_CASE */
6470
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006471/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006472void sign_message_deterministic( int key_type_arg,
6473 data_t *key_data,
6474 int alg_arg,
6475 data_t *input_data,
6476 data_t *output_data )
6477{
6478 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6479 psa_key_type_t key_type = key_type_arg;
6480 psa_algorithm_t alg = alg_arg;
6481 size_t key_bits;
6482 unsigned char *signature = NULL;
6483 size_t signature_size;
6484 size_t signature_length = 0xdeadbeef;
6485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6486
6487 PSA_ASSERT( psa_crypto_init( ) );
6488
6489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6490 psa_set_key_algorithm( &attributes, alg );
6491 psa_set_key_type( &attributes, key_type );
6492
6493 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6494 &key ) );
6495 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6496 key_bits = psa_get_key_bits( &attributes );
6497
6498 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6499 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006500 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006501 ASSERT_ALLOC( signature, signature_size );
6502
6503 PSA_ASSERT( psa_sign_message( key, alg,
6504 input_data->x, input_data->len,
6505 signature, signature_size,
6506 &signature_length ) );
6507
6508 ASSERT_COMPARE( output_data->x, output_data->len,
6509 signature, signature_length );
6510
6511exit:
6512 psa_reset_key_attributes( &attributes );
6513
6514 psa_destroy_key( key );
6515 mbedtls_free( signature );
6516 PSA_DONE( );
6517
6518}
6519/* END_CASE */
6520
6521/* BEGIN_CASE */
6522void sign_message_fail( int key_type_arg,
6523 data_t *key_data,
6524 int alg_arg,
6525 data_t *input_data,
6526 int signature_size_arg,
6527 int expected_status_arg )
6528{
6529 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6530 psa_key_type_t key_type = key_type_arg;
6531 psa_algorithm_t alg = alg_arg;
6532 size_t signature_size = signature_size_arg;
6533 psa_status_t actual_status;
6534 psa_status_t expected_status = expected_status_arg;
6535 unsigned char *signature = NULL;
6536 size_t signature_length = 0xdeadbeef;
6537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6538
6539 ASSERT_ALLOC( signature, signature_size );
6540
6541 PSA_ASSERT( psa_crypto_init( ) );
6542
6543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6544 psa_set_key_algorithm( &attributes, alg );
6545 psa_set_key_type( &attributes, key_type );
6546
6547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6548 &key ) );
6549
6550 actual_status = psa_sign_message( key, alg,
6551 input_data->x, input_data->len,
6552 signature, signature_size,
6553 &signature_length );
6554 TEST_EQUAL( actual_status, expected_status );
6555 /* The value of *signature_length is unspecified on error, but
6556 * whatever it is, it should be less than signature_size, so that
6557 * if the caller tries to read *signature_length bytes without
6558 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006559 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006560
6561exit:
6562 psa_reset_key_attributes( &attributes );
6563 psa_destroy_key( key );
6564 mbedtls_free( signature );
6565 PSA_DONE( );
6566}
6567/* END_CASE */
6568
6569/* BEGIN_CASE */
6570void sign_verify_message( int key_type_arg,
6571 data_t *key_data,
6572 int alg_arg,
6573 data_t *input_data )
6574{
6575 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6576 psa_key_type_t key_type = key_type_arg;
6577 psa_algorithm_t alg = alg_arg;
6578 size_t key_bits;
6579 unsigned char *signature = NULL;
6580 size_t signature_size;
6581 size_t signature_length = 0xdeadbeef;
6582 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6583
6584 PSA_ASSERT( psa_crypto_init( ) );
6585
6586 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6587 PSA_KEY_USAGE_VERIFY_MESSAGE );
6588 psa_set_key_algorithm( &attributes, alg );
6589 psa_set_key_type( &attributes, key_type );
6590
6591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6592 &key ) );
6593 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6594 key_bits = psa_get_key_bits( &attributes );
6595
6596 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6597 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006598 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006599 ASSERT_ALLOC( signature, signature_size );
6600
6601 PSA_ASSERT( psa_sign_message( key, alg,
6602 input_data->x, input_data->len,
6603 signature, signature_size,
6604 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006605 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006606 TEST_ASSERT( signature_length > 0 );
6607
6608 PSA_ASSERT( psa_verify_message( key, alg,
6609 input_data->x, input_data->len,
6610 signature, signature_length ) );
6611
6612 if( input_data->len != 0 )
6613 {
6614 /* Flip a bit in the input and verify that the signature is now
6615 * detected as invalid. Flip a bit at the beginning, not at the end,
6616 * because ECDSA may ignore the last few bits of the input. */
6617 input_data->x[0] ^= 1;
6618 TEST_EQUAL( psa_verify_message( key, alg,
6619 input_data->x, input_data->len,
6620 signature, signature_length ),
6621 PSA_ERROR_INVALID_SIGNATURE );
6622 }
6623
6624exit:
6625 psa_reset_key_attributes( &attributes );
6626
6627 psa_destroy_key( key );
6628 mbedtls_free( signature );
6629 PSA_DONE( );
6630}
6631/* END_CASE */
6632
6633/* BEGIN_CASE */
6634void verify_message( int key_type_arg,
6635 data_t *key_data,
6636 int alg_arg,
6637 data_t *input_data,
6638 data_t *signature_data )
6639{
6640 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6641 psa_key_type_t key_type = key_type_arg;
6642 psa_algorithm_t alg = alg_arg;
6643 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6644
Gilles Peskine7be11a72022-04-14 00:12:57 +02006645 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006646
6647 PSA_ASSERT( psa_crypto_init( ) );
6648
6649 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6650 psa_set_key_algorithm( &attributes, alg );
6651 psa_set_key_type( &attributes, key_type );
6652
6653 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6654 &key ) );
6655
6656 PSA_ASSERT( psa_verify_message( key, alg,
6657 input_data->x, input_data->len,
6658 signature_data->x, signature_data->len ) );
6659
6660exit:
6661 psa_reset_key_attributes( &attributes );
6662 psa_destroy_key( key );
6663 PSA_DONE( );
6664}
6665/* END_CASE */
6666
6667/* BEGIN_CASE */
6668void verify_message_fail( int key_type_arg,
6669 data_t *key_data,
6670 int alg_arg,
6671 data_t *hash_data,
6672 data_t *signature_data,
6673 int expected_status_arg )
6674{
6675 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6676 psa_key_type_t key_type = key_type_arg;
6677 psa_algorithm_t alg = alg_arg;
6678 psa_status_t actual_status;
6679 psa_status_t expected_status = expected_status_arg;
6680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6681
6682 PSA_ASSERT( psa_crypto_init( ) );
6683
6684 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6685 psa_set_key_algorithm( &attributes, alg );
6686 psa_set_key_type( &attributes, key_type );
6687
6688 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6689 &key ) );
6690
6691 actual_status = psa_verify_message( key, alg,
6692 hash_data->x, hash_data->len,
6693 signature_data->x,
6694 signature_data->len );
6695 TEST_EQUAL( actual_status, expected_status );
6696
6697exit:
6698 psa_reset_key_attributes( &attributes );
6699 psa_destroy_key( key );
6700 PSA_DONE( );
6701}
6702/* END_CASE */
6703
6704/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006705void asymmetric_encrypt( int key_type_arg,
6706 data_t *key_data,
6707 int alg_arg,
6708 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006709 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006710 int expected_output_length_arg,
6711 int expected_status_arg )
6712{
Ronald Cron5425a212020-08-04 14:58:35 +02006713 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006714 psa_key_type_t key_type = key_type_arg;
6715 psa_algorithm_t alg = alg_arg;
6716 size_t expected_output_length = expected_output_length_arg;
6717 size_t key_bits;
6718 unsigned char *output = NULL;
6719 size_t output_size;
6720 size_t output_length = ~0;
6721 psa_status_t actual_status;
6722 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006724
Gilles Peskine8817f612018-12-18 00:18:46 +01006725 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006726
Gilles Peskine656896e2018-06-29 19:12:28 +02006727 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006728 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6729 psa_set_key_algorithm( &attributes, alg );
6730 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006731 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006732 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006733
6734 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006735 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006736 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006737
Gilles Peskine656896e2018-06-29 19:12:28 +02006738 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006739 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006740 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006741
6742 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006743 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006744 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006745 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006746 output, output_size,
6747 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006748 TEST_EQUAL( actual_status, expected_status );
6749 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006750
Gilles Peskine68428122018-06-30 18:42:41 +02006751 /* If the label is empty, the test framework puts a non-null pointer
6752 * in label->x. Test that a null pointer works as well. */
6753 if( label->len == 0 )
6754 {
6755 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006756 if( output_size != 0 )
6757 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006758 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006759 input_data->x, input_data->len,
6760 NULL, label->len,
6761 output, output_size,
6762 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006763 TEST_EQUAL( actual_status, expected_status );
6764 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006765 }
6766
Gilles Peskine656896e2018-06-29 19:12:28 +02006767exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006768 /*
6769 * Key attributes may have been returned by psa_get_key_attributes()
6770 * thus reset them as required.
6771 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006772 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006773
Ronald Cron5425a212020-08-04 14:58:35 +02006774 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006775 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006776 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006777}
6778/* END_CASE */
6779
6780/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006781void asymmetric_encrypt_decrypt( int key_type_arg,
6782 data_t *key_data,
6783 int alg_arg,
6784 data_t *input_data,
6785 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006786{
Ronald Cron5425a212020-08-04 14:58:35 +02006787 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006788 psa_key_type_t key_type = key_type_arg;
6789 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006790 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006791 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006792 size_t output_size;
6793 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006794 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006795 size_t output2_size;
6796 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006797 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006798
Gilles Peskine8817f612018-12-18 00:18:46 +01006799 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006800
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006801 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6802 psa_set_key_algorithm( &attributes, alg );
6803 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006804
Gilles Peskine049c7532019-05-15 20:22:09 +02006805 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006806 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006807
6808 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006809 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006810 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006811
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006812 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006813 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006814 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006815
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006816 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02006817 TEST_LE_U( output2_size,
6818 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6819 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006820 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006821
Gilles Peskineeebd7382018-06-08 18:11:54 +02006822 /* We test encryption by checking that encrypt-then-decrypt gives back
6823 * the original plaintext because of the non-optional random
6824 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006825 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006826 input_data->x, input_data->len,
6827 label->x, label->len,
6828 output, output_size,
6829 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006830 /* We don't know what ciphertext length to expect, but check that
6831 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006832 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006833
Ronald Cron5425a212020-08-04 14:58:35 +02006834 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006835 output, output_length,
6836 label->x, label->len,
6837 output2, output2_size,
6838 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006839 ASSERT_COMPARE( input_data->x, input_data->len,
6840 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006841
6842exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006843 /*
6844 * Key attributes may have been returned by psa_get_key_attributes()
6845 * thus reset them as required.
6846 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006847 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006848
Ronald Cron5425a212020-08-04 14:58:35 +02006849 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006850 mbedtls_free( output );
6851 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006852 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006853}
6854/* END_CASE */
6855
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006856/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006857void asymmetric_decrypt( int key_type_arg,
6858 data_t *key_data,
6859 int alg_arg,
6860 data_t *input_data,
6861 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006862 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006863{
Ronald Cron5425a212020-08-04 14:58:35 +02006864 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006865 psa_key_type_t key_type = key_type_arg;
6866 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006867 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006868 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006869 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006870 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006872
Gilles Peskine8817f612018-12-18 00:18:46 +01006873 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006874
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006875 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6876 psa_set_key_algorithm( &attributes, alg );
6877 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006878
Gilles Peskine049c7532019-05-15 20:22:09 +02006879 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006880 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006881
gabor-mezei-armceface22021-01-21 12:26:17 +01006882 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6883 key_bits = psa_get_key_bits( &attributes );
6884
6885 /* Determine the maximum ciphertext length */
6886 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006887 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01006888 ASSERT_ALLOC( output, output_size );
6889
Ronald Cron5425a212020-08-04 14:58:35 +02006890 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006891 input_data->x, input_data->len,
6892 label->x, label->len,
6893 output,
6894 output_size,
6895 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006896 ASSERT_COMPARE( expected_data->x, expected_data->len,
6897 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006898
Gilles Peskine68428122018-06-30 18:42:41 +02006899 /* If the label is empty, the test framework puts a non-null pointer
6900 * in label->x. Test that a null pointer works as well. */
6901 if( label->len == 0 )
6902 {
6903 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006904 if( output_size != 0 )
6905 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006906 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006907 input_data->x, input_data->len,
6908 NULL, label->len,
6909 output,
6910 output_size,
6911 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006912 ASSERT_COMPARE( expected_data->x, expected_data->len,
6913 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006914 }
6915
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006916exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006917 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006918 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006919 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006920 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006921}
6922/* END_CASE */
6923
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006924/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006925void asymmetric_decrypt_fail( int key_type_arg,
6926 data_t *key_data,
6927 int alg_arg,
6928 data_t *input_data,
6929 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006930 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006931 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006932{
Ronald Cron5425a212020-08-04 14:58:35 +02006933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006934 psa_key_type_t key_type = key_type_arg;
6935 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006936 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006937 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006938 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006939 psa_status_t actual_status;
6940 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006942
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006943 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006944
Gilles Peskine8817f612018-12-18 00:18:46 +01006945 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006946
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006947 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6948 psa_set_key_algorithm( &attributes, alg );
6949 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006950
Gilles Peskine049c7532019-05-15 20:22:09 +02006951 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006952 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006953
Ronald Cron5425a212020-08-04 14:58:35 +02006954 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006955 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006956 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006957 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006958 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006959 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006960 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006961
Gilles Peskine68428122018-06-30 18:42:41 +02006962 /* If the label is empty, the test framework puts a non-null pointer
6963 * in label->x. Test that a null pointer works as well. */
6964 if( label->len == 0 )
6965 {
6966 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006967 if( output_size != 0 )
6968 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006969 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006970 input_data->x, input_data->len,
6971 NULL, label->len,
6972 output, output_size,
6973 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006974 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006975 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006976 }
6977
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006978exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006979 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006980 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006981 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006982 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006983}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006984/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006985
6986/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006987void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006988{
6989 /* Test each valid way of initializing the object, except for `= {0}`, as
6990 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6991 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006992 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006993 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006994 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6995 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6996 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006997
6998 memset( &zero, 0, sizeof( zero ) );
6999
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007000 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007001 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007002 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007003 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007004 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007005 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007006 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007007
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007008 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007009 PSA_ASSERT( psa_key_derivation_abort(&func) );
7010 PSA_ASSERT( psa_key_derivation_abort(&init) );
7011 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007012}
7013/* END_CASE */
7014
Janos Follath16de4a42019-06-13 16:32:24 +01007015/* BEGIN_CASE */
7016void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007017{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007018 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007019 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007020 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007021
Gilles Peskine8817f612018-12-18 00:18:46 +01007022 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007023
Janos Follath16de4a42019-06-13 16:32:24 +01007024 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007025 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007026
7027exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007028 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007029 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007030}
7031/* END_CASE */
7032
Janos Follathaf3c2a02019-06-12 12:34:34 +01007033/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007034void derive_set_capacity( int alg_arg, int capacity_arg,
7035 int expected_status_arg )
7036{
7037 psa_algorithm_t alg = alg_arg;
7038 size_t capacity = capacity_arg;
7039 psa_status_t expected_status = expected_status_arg;
7040 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7041
7042 PSA_ASSERT( psa_crypto_init( ) );
7043
7044 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7045
7046 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7047 expected_status );
7048
7049exit:
7050 psa_key_derivation_abort( &operation );
7051 PSA_DONE( );
7052}
7053/* END_CASE */
7054
7055/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007056void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007057 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007058 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007059 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007060 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007061 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007062 int expected_status_arg3,
7063 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007064{
7065 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007066 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7067 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007068 psa_status_t expected_statuses[] = {expected_status_arg1,
7069 expected_status_arg2,
7070 expected_status_arg3};
7071 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007072 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7073 MBEDTLS_SVC_KEY_ID_INIT,
7074 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007075 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7077 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007078 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007079 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007080 psa_status_t expected_output_status = expected_output_status_arg;
7081 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007082
7083 PSA_ASSERT( psa_crypto_init( ) );
7084
7085 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7086 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007087
7088 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7089
7090 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7091 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007092 mbedtls_test_set_step( i );
7093 if( steps[i] == 0 )
7094 {
7095 /* Skip this step */
7096 }
7097 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007098 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007099 psa_set_key_type( &attributes, key_types[i] );
7100 PSA_ASSERT( psa_import_key( &attributes,
7101 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007102 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007103 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7104 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7105 {
7106 // When taking a private key as secret input, use key agreement
7107 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007108 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7109 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007110 expected_statuses[i] );
7111 }
7112 else
7113 {
7114 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007115 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007116 expected_statuses[i] );
7117 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007118 }
7119 else
7120 {
7121 TEST_EQUAL( psa_key_derivation_input_bytes(
7122 &operation, steps[i],
7123 inputs[i]->x, inputs[i]->len ),
7124 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007125 }
7126 }
7127
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007128 if( output_key_type != PSA_KEY_TYPE_NONE )
7129 {
7130 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007131 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007132 psa_set_key_bits( &attributes, 8 );
7133 actual_output_status =
7134 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007135 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007136 }
7137 else
7138 {
7139 uint8_t buffer[1];
7140 actual_output_status =
7141 psa_key_derivation_output_bytes( &operation,
7142 buffer, sizeof( buffer ) );
7143 }
7144 TEST_EQUAL( actual_output_status, expected_output_status );
7145
Janos Follathaf3c2a02019-06-12 12:34:34 +01007146exit:
7147 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007148 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7149 psa_destroy_key( keys[i] );
7150 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007151 PSA_DONE( );
7152}
7153/* END_CASE */
7154
Janos Follathd958bb72019-07-03 15:02:16 +01007155/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007156void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007157{
Janos Follathd958bb72019-07-03 15:02:16 +01007158 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007159 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007160 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007161 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007162 unsigned char input1[] = "Input 1";
7163 size_t input1_length = sizeof( input1 );
7164 unsigned char input2[] = "Input 2";
7165 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007166 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007167 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007168 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7169 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7170 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007172
Gilles Peskine8817f612018-12-18 00:18:46 +01007173 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007174
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007175 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7176 psa_set_key_algorithm( &attributes, alg );
7177 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007178
Gilles Peskine73676cb2019-05-15 20:15:10 +02007179 PSA_ASSERT( psa_import_key( &attributes,
7180 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007181 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007182
7183 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007184 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7185 input1, input1_length,
7186 input2, input2_length,
7187 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007188 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007189
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007190 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007191 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007192 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007193
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007194 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007195
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007196 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007197 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007198
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007199exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007200 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007201 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007202 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007203}
7204/* END_CASE */
7205
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007206/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007207void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007208{
7209 uint8_t output_buffer[16];
7210 size_t buffer_size = 16;
7211 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007212 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007213
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007214 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7215 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007216 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007217
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007218 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007219 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007220
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007221 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007222
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007223 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7224 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007225 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007226
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007227 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007228 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007229
7230exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007231 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007232}
7233/* END_CASE */
7234
7235/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007236void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007237 int step1_arg, data_t *input1, int expected_status_arg1,
7238 int step2_arg, data_t *input2, int expected_status_arg2,
7239 int step3_arg, data_t *input3, int expected_status_arg3,
7240 int step4_arg, data_t *input4, int expected_status_arg4,
7241 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007242 int requested_capacity_arg,
7243 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007244 data_t *expected_output2,
7245 int other_key_input_type,
7246 int key_input_type,
7247 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007248{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007249 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007250 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7251 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007252 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7253 MBEDTLS_SVC_KEY_ID_INIT,
7254 MBEDTLS_SVC_KEY_ID_INIT,
7255 MBEDTLS_SVC_KEY_ID_INIT};
7256 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7257 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007258 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007259 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007260 uint8_t *expected_outputs[2] =
7261 {expected_output1->x, expected_output2->x};
7262 size_t output_sizes[2] =
7263 {expected_output1->len, expected_output2->len};
7264 size_t output_buffer_size = 0;
7265 uint8_t *output_buffer = NULL;
7266 size_t expected_capacity;
7267 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007268 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7269 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7270 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7271 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007272 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007273 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007274 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007275
7276 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7277 {
7278 if( output_sizes[i] > output_buffer_size )
7279 output_buffer_size = output_sizes[i];
7280 if( output_sizes[i] == 0 )
7281 expected_outputs[i] = NULL;
7282 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007283 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007284 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007285
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007286 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007287 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7288 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7289 requested_capacity ) );
7290 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007291 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007292 switch( steps[i] )
7293 {
7294 case 0:
7295 break;
7296 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007297 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007298 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007299 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007300 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007301 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007302 inputs[i]->x, inputs[i]->len ),
7303 statuses[i] );
7304
7305 if( statuses[i] != PSA_SUCCESS )
7306 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007307 break;
7308 case 1: // input key
7309 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7310 psa_set_key_algorithm( &attributes1, alg );
7311 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7312
7313 PSA_ASSERT( psa_import_key( &attributes1,
7314 inputs[i]->x, inputs[i]->len,
7315 &keys[i] ) );
7316
Przemek Stekiel38647de2022-04-19 13:27:47 +02007317 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007318 {
7319 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007320 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7321 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007322 }
7323
Przemek Stekiel38647de2022-04-19 13:27:47 +02007324 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7325 steps[i],
7326 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007327 break;
7328 default:
7329 TEST_ASSERT( ! "default case not supported" );
7330 break;
7331 }
7332 break;
7333 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007334 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007335 {
7336 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007337 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7338 steps[i],
7339 inputs[i]->x,
7340 inputs[i]->len ),
7341 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007342 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007343 case 1: // input key, type DERIVE
7344 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007345 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7346 psa_set_key_algorithm( &attributes2, alg );
7347 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7348
7349 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007350 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007351 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7352
7353 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007354 inputs[i]->x, inputs[i]->len,
7355 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007356
Przemek Stekiel38647de2022-04-19 13:27:47 +02007357 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7358 steps[i],
7359 keys[i] ),
7360 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007361 break;
7362 case 2: // key agreement
7363 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7364 psa_set_key_algorithm( &attributes3, alg );
7365 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7366
7367 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007368 inputs[i]->x, inputs[i]->len,
7369 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007370
7371 TEST_EQUAL( psa_key_derivation_key_agreement(
7372 &operation,
7373 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7374 keys[i], key_agreement_peer_key->x,
7375 key_agreement_peer_key->len ), statuses[i] );
7376 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007377 default:
7378 TEST_ASSERT( ! "default case not supported" );
7379 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007380 }
7381
Przemek Stekiel38647de2022-04-19 13:27:47 +02007382 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007383 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007384 break;
7385 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007386 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007387 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007388 inputs[i]->x, inputs[i]->len ), statuses[i] );
7389
7390 if( statuses[i] != PSA_SUCCESS )
7391 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007392 break;
7393 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007394 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007395
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007396 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007397 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007398 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007399 expected_capacity = requested_capacity;
7400
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007401 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007402 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007403 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7404
7405 /* For output key derivation secret must be provided using
7406 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007407 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007408 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007409
7410 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7411 psa_set_key_algorithm( &attributes4, alg );
7412 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007413 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007414
7415 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007416 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007417 }
7418 else // output bytes
7419 {
7420 /* Expansion phase. */
7421 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007422 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007423 /* Read some bytes. */
7424 status = psa_key_derivation_output_bytes( &operation,
7425 output_buffer, output_sizes[i] );
7426 if( expected_capacity == 0 && output_sizes[i] == 0 )
7427 {
7428 /* Reading 0 bytes when 0 bytes are available can go either way. */
7429 TEST_ASSERT( status == PSA_SUCCESS ||
7430 status == PSA_ERROR_INSUFFICIENT_DATA );
7431 continue;
7432 }
7433 else if( expected_capacity == 0 ||
7434 output_sizes[i] > expected_capacity )
7435 {
7436 /* Capacity exceeded. */
7437 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7438 expected_capacity = 0;
7439 continue;
7440 }
7441 /* Success. Check the read data. */
7442 PSA_ASSERT( status );
7443 if( output_sizes[i] != 0 )
7444 ASSERT_COMPARE( output_buffer, output_sizes[i],
7445 expected_outputs[i], output_sizes[i] );
7446 /* Check the operation status. */
7447 expected_capacity -= output_sizes[i];
7448 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7449 &current_capacity ) );
7450 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007451 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007452 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007453 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007454
7455exit:
7456 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007457 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007458 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7459 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007460 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007461 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007462}
7463/* END_CASE */
7464
7465/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007466void derive_full( int alg_arg,
7467 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007468 data_t *input1,
7469 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007470 int requested_capacity_arg )
7471{
Ronald Cron5425a212020-08-04 14:58:35 +02007472 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007473 psa_algorithm_t alg = alg_arg;
7474 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007475 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007476 unsigned char output_buffer[16];
7477 size_t expected_capacity = requested_capacity;
7478 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007479 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007480
Gilles Peskine8817f612018-12-18 00:18:46 +01007481 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007482
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007483 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7484 psa_set_key_algorithm( &attributes, alg );
7485 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007486
Gilles Peskine049c7532019-05-15 20:22:09 +02007487 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007488 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007489
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007490 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7491 input1->x, input1->len,
7492 input2->x, input2->len,
7493 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007494 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007495
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007496 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007497 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007498 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007499
7500 /* Expansion phase. */
7501 while( current_capacity > 0 )
7502 {
7503 size_t read_size = sizeof( output_buffer );
7504 if( read_size > current_capacity )
7505 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007506 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007507 output_buffer,
7508 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007509 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007510 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007511 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007512 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007513 }
7514
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007515 /* Check that the operation refuses to go over capacity. */
7516 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007517 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007518
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007519 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007520
7521exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007522 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007523 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007524 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007525}
7526/* END_CASE */
7527
Janos Follathe60c9052019-07-03 13:51:30 +01007528/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007529void derive_key_exercise( int alg_arg,
7530 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007531 data_t *input1,
7532 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007533 int derived_type_arg,
7534 int derived_bits_arg,
7535 int derived_usage_arg,
7536 int derived_alg_arg )
7537{
Ronald Cron5425a212020-08-04 14:58:35 +02007538 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7539 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007540 psa_algorithm_t alg = alg_arg;
7541 psa_key_type_t derived_type = derived_type_arg;
7542 size_t derived_bits = derived_bits_arg;
7543 psa_key_usage_t derived_usage = derived_usage_arg;
7544 psa_algorithm_t derived_alg = derived_alg_arg;
7545 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007546 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007547 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007548 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007549
Gilles Peskine8817f612018-12-18 00:18:46 +01007550 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007551
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007552 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7553 psa_set_key_algorithm( &attributes, alg );
7554 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007555 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007556 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007557
7558 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007559 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7560 input1->x, input1->len,
7561 input2->x, input2->len,
7562 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007563 goto exit;
7564
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007565 psa_set_key_usage_flags( &attributes, derived_usage );
7566 psa_set_key_algorithm( &attributes, derived_alg );
7567 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007568 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007569 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007570 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007571
7572 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007573 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007574 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7575 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007576
7577 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007578 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007579 goto exit;
7580
7581exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007582 /*
7583 * Key attributes may have been returned by psa_get_key_attributes()
7584 * thus reset them as required.
7585 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007586 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007587
7588 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007589 psa_destroy_key( base_key );
7590 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007591 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007592}
7593/* END_CASE */
7594
Janos Follath42fd8882019-07-03 14:17:09 +01007595/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007596void derive_key_export( int alg_arg,
7597 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007598 data_t *input1,
7599 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007600 int bytes1_arg,
7601 int bytes2_arg )
7602{
Ronald Cron5425a212020-08-04 14:58:35 +02007603 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7604 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007605 psa_algorithm_t alg = alg_arg;
7606 size_t bytes1 = bytes1_arg;
7607 size_t bytes2 = bytes2_arg;
7608 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007609 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007610 uint8_t *output_buffer = NULL;
7611 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007612 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7613 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007614 size_t length;
7615
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007616 ASSERT_ALLOC( output_buffer, capacity );
7617 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007619
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007620 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7621 psa_set_key_algorithm( &base_attributes, alg );
7622 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007623 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007624 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007625
7626 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007627 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7628 input1->x, input1->len,
7629 input2->x, input2->len,
7630 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007631 goto exit;
7632
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007633 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007634 output_buffer,
7635 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007636 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007637
7638 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007639 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7640 input1->x, input1->len,
7641 input2->x, input2->len,
7642 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007643 goto exit;
7644
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007645 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7646 psa_set_key_algorithm( &derived_attributes, 0 );
7647 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007648 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007649 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007650 &derived_key ) );
7651 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007652 export_buffer, bytes1,
7653 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007654 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007655 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007656 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007657 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007658 &derived_key ) );
7659 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007660 export_buffer + bytes1, bytes2,
7661 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007662 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007663
7664 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007665 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7666 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007667
7668exit:
7669 mbedtls_free( output_buffer );
7670 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007671 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007672 psa_destroy_key( base_key );
7673 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007674 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007675}
7676/* END_CASE */
7677
7678/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007679void derive_key_type( int alg_arg,
7680 data_t *key_data,
7681 data_t *input1,
7682 data_t *input2,
7683 int key_type_arg, int bits_arg,
7684 data_t *expected_export )
7685{
7686 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7687 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7688 const psa_algorithm_t alg = alg_arg;
7689 const psa_key_type_t key_type = key_type_arg;
7690 const size_t bits = bits_arg;
7691 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7692 const size_t export_buffer_size =
7693 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
7694 uint8_t *export_buffer = NULL;
7695 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7696 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7697 size_t export_length;
7698
7699 ASSERT_ALLOC( export_buffer, export_buffer_size );
7700 PSA_ASSERT( psa_crypto_init( ) );
7701
7702 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7703 psa_set_key_algorithm( &base_attributes, alg );
7704 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7705 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
7706 &base_key ) );
7707
Przemek Stekielc85f0912022-03-08 11:37:54 +01007708 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007709 &operation, base_key, alg,
7710 input1->x, input1->len,
7711 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01007712 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007713 goto exit;
7714
7715 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7716 psa_set_key_algorithm( &derived_attributes, 0 );
7717 psa_set_key_type( &derived_attributes, key_type );
7718 psa_set_key_bits( &derived_attributes, bits );
7719 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
7720 &derived_key ) );
7721
7722 PSA_ASSERT( psa_export_key( derived_key,
7723 export_buffer, export_buffer_size,
7724 &export_length ) );
7725 ASSERT_COMPARE( export_buffer, export_length,
7726 expected_export->x, expected_export->len );
7727
7728exit:
7729 mbedtls_free( export_buffer );
7730 psa_key_derivation_abort( &operation );
7731 psa_destroy_key( base_key );
7732 psa_destroy_key( derived_key );
7733 PSA_DONE( );
7734}
7735/* END_CASE */
7736
7737/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007738void derive_key( int alg_arg,
7739 data_t *key_data, data_t *input1, data_t *input2,
7740 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007741 int expected_status_arg,
7742 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02007743{
Ronald Cron5425a212020-08-04 14:58:35 +02007744 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7745 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007746 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007747 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007748 size_t bits = bits_arg;
7749 psa_status_t expected_status = expected_status_arg;
7750 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7751 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7752 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7753
7754 PSA_ASSERT( psa_crypto_init( ) );
7755
7756 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7757 psa_set_key_algorithm( &base_attributes, alg );
7758 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7759 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007760 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007761
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007762 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7763 input1->x, input1->len,
7764 input2->x, input2->len,
7765 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007766 goto exit;
7767
7768 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7769 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007770 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007771 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007772
7773 psa_status_t status =
7774 psa_key_derivation_output_key( &derived_attributes,
7775 &operation,
7776 &derived_key );
7777 if( is_large_output > 0 )
7778 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7779 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007780
7781exit:
7782 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007783 psa_destroy_key( base_key );
7784 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007785 PSA_DONE( );
7786}
7787/* END_CASE */
7788
7789/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007790void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007791 int our_key_type_arg, int our_key_alg_arg,
7792 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007793 int expected_status_arg )
7794{
Ronald Cron5425a212020-08-04 14:58:35 +02007795 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007796 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007797 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007798 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007799 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007801 psa_status_t expected_status = expected_status_arg;
7802 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007803
Gilles Peskine8817f612018-12-18 00:18:46 +01007804 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007805
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007806 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007807 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007808 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007809 PSA_ASSERT( psa_import_key( &attributes,
7810 our_key_data->x, our_key_data->len,
7811 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007812
Gilles Peskine77f40d82019-04-11 21:27:06 +02007813 /* The tests currently include inputs that should fail at either step.
7814 * Test cases that fail at the setup step should be changed to call
7815 * key_derivation_setup instead, and this function should be renamed
7816 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007817 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007818 if( status == PSA_SUCCESS )
7819 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007820 TEST_EQUAL( psa_key_derivation_key_agreement(
7821 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7822 our_key,
7823 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007824 expected_status );
7825 }
7826 else
7827 {
7828 TEST_ASSERT( status == expected_status );
7829 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007830
7831exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007832 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007833 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007834 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007835}
7836/* END_CASE */
7837
7838/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007839void raw_key_agreement( int alg_arg,
7840 int our_key_type_arg, data_t *our_key_data,
7841 data_t *peer_key_data,
7842 data_t *expected_output )
7843{
Ronald Cron5425a212020-08-04 14:58:35 +02007844 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007845 psa_algorithm_t alg = alg_arg;
7846 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007848 unsigned char *output = NULL;
7849 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007850 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007851
Gilles Peskinef0cba732019-04-11 22:12:38 +02007852 PSA_ASSERT( psa_crypto_init( ) );
7853
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007854 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7855 psa_set_key_algorithm( &attributes, alg );
7856 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007857 PSA_ASSERT( psa_import_key( &attributes,
7858 our_key_data->x, our_key_data->len,
7859 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007860
gabor-mezei-armceface22021-01-21 12:26:17 +01007861 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7862 key_bits = psa_get_key_bits( &attributes );
7863
Gilles Peskine992bee82022-04-13 23:25:52 +02007864 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007865 TEST_LE_U( expected_output->len,
7866 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7867 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
7868 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02007869
7870 /* Good case with exact output size */
7871 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02007872 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7873 peer_key_data->x, peer_key_data->len,
7874 output, expected_output->len,
7875 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007876 ASSERT_COMPARE( output, output_length,
7877 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02007878 mbedtls_free( output );
7879 output = NULL;
7880 output_length = ~0;
7881
7882 /* Larger buffer */
7883 ASSERT_ALLOC( output, expected_output->len + 1 );
7884 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7885 peer_key_data->x, peer_key_data->len,
7886 output, expected_output->len + 1,
7887 &output_length ) );
7888 ASSERT_COMPARE( output, output_length,
7889 expected_output->x, expected_output->len );
7890 mbedtls_free( output );
7891 output = NULL;
7892 output_length = ~0;
7893
7894 /* Buffer too small */
7895 ASSERT_ALLOC( output, expected_output->len - 1 );
7896 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
7897 peer_key_data->x, peer_key_data->len,
7898 output, expected_output->len - 1,
7899 &output_length ),
7900 PSA_ERROR_BUFFER_TOO_SMALL );
7901 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007902 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02007903 mbedtls_free( output );
7904 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007905
7906exit:
7907 mbedtls_free( output );
7908 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007909 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007910}
7911/* END_CASE */
7912
7913/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007914void key_agreement_capacity( int alg_arg,
7915 int our_key_type_arg, data_t *our_key_data,
7916 data_t *peer_key_data,
7917 int expected_capacity_arg )
7918{
Ronald Cron5425a212020-08-04 14:58:35 +02007919 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007920 psa_algorithm_t alg = alg_arg;
7921 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007922 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007924 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007925 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007926
Gilles Peskine8817f612018-12-18 00:18:46 +01007927 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007928
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007929 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7930 psa_set_key_algorithm( &attributes, alg );
7931 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007932 PSA_ASSERT( psa_import_key( &attributes,
7933 our_key_data->x, our_key_data->len,
7934 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007936 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007937 PSA_ASSERT( psa_key_derivation_key_agreement(
7938 &operation,
7939 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7940 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007941 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7942 {
7943 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007944 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007945 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007946 NULL, 0 ) );
7947 }
Gilles Peskine59685592018-09-18 12:11:34 +02007948
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007949 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007950 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007951 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007952 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007953
Gilles Peskinebf491972018-10-25 22:36:12 +02007954 /* Test the actual capacity by reading the output. */
7955 while( actual_capacity > sizeof( output ) )
7956 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007957 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007958 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007959 actual_capacity -= sizeof( output );
7960 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007961 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007962 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007963 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007964 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007965
Gilles Peskine59685592018-09-18 12:11:34 +02007966exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007967 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007968 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007969 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007970}
7971/* END_CASE */
7972
7973/* BEGIN_CASE */
7974void key_agreement_output( int alg_arg,
7975 int our_key_type_arg, data_t *our_key_data,
7976 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007977 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007978{
Ronald Cron5425a212020-08-04 14:58:35 +02007979 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007980 psa_algorithm_t alg = alg_arg;
7981 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007982 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007983 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007984 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007985
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007986 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7987 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007988
Gilles Peskine8817f612018-12-18 00:18:46 +01007989 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007990
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007991 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7992 psa_set_key_algorithm( &attributes, alg );
7993 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007994 PSA_ASSERT( psa_import_key( &attributes,
7995 our_key_data->x, our_key_data->len,
7996 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007997
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007998 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007999 PSA_ASSERT( psa_key_derivation_key_agreement(
8000 &operation,
8001 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8002 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008003 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8004 {
8005 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008006 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008007 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008008 NULL, 0 ) );
8009 }
Gilles Peskine59685592018-09-18 12:11:34 +02008010
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008011 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008012 actual_output,
8013 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008014 ASSERT_COMPARE( actual_output, expected_output1->len,
8015 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008016 if( expected_output2->len != 0 )
8017 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008018 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008019 actual_output,
8020 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008021 ASSERT_COMPARE( actual_output, expected_output2->len,
8022 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008023 }
Gilles Peskine59685592018-09-18 12:11:34 +02008024
8025exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008026 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008027 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008028 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008029 mbedtls_free( actual_output );
8030}
8031/* END_CASE */
8032
8033/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008034void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008035{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008036 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008037 unsigned char *output = NULL;
8038 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008039 size_t i;
8040 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008041
Simon Butcher49f8e312020-03-03 15:51:50 +00008042 TEST_ASSERT( bytes_arg >= 0 );
8043
Gilles Peskine91892022021-02-08 19:50:26 +01008044 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008045 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008046
Gilles Peskine8817f612018-12-18 00:18:46 +01008047 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008048
Gilles Peskinea50d7392018-06-21 10:22:13 +02008049 /* Run several times, to ensure that every output byte will be
8050 * nonzero at least once with overwhelming probability
8051 * (2^(-8*number_of_runs)). */
8052 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008053 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008054 if( bytes != 0 )
8055 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008056 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008057
Gilles Peskinea50d7392018-06-21 10:22:13 +02008058 for( i = 0; i < bytes; i++ )
8059 {
8060 if( output[i] != 0 )
8061 ++changed[i];
8062 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008063 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008064
8065 /* Check that every byte was changed to nonzero at least once. This
8066 * validates that psa_generate_random is overwriting every byte of
8067 * the output buffer. */
8068 for( i = 0; i < bytes; i++ )
8069 {
8070 TEST_ASSERT( changed[i] != 0 );
8071 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008072
8073exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008074 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008075 mbedtls_free( output );
8076 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008077}
8078/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008079
8080/* BEGIN_CASE */
8081void generate_key( int type_arg,
8082 int bits_arg,
8083 int usage_arg,
8084 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008085 int expected_status_arg,
8086 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008087{
Ronald Cron5425a212020-08-04 14:58:35 +02008088 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008089 psa_key_type_t type = type_arg;
8090 psa_key_usage_t usage = usage_arg;
8091 size_t bits = bits_arg;
8092 psa_algorithm_t alg = alg_arg;
8093 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008095 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008096
Gilles Peskine8817f612018-12-18 00:18:46 +01008097 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008098
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008099 psa_set_key_usage_flags( &attributes, usage );
8100 psa_set_key_algorithm( &attributes, alg );
8101 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008102 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008103
8104 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008105 psa_status_t status = psa_generate_key( &attributes, &key );
8106
8107 if( is_large_key > 0 )
8108 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8109 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008110 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008111 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008112
8113 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008114 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008115 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8116 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008117
Gilles Peskine818ca122018-06-20 18:16:48 +02008118 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008119 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008120 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008121
8122exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008123 /*
8124 * Key attributes may have been returned by psa_get_key_attributes()
8125 * thus reset them as required.
8126 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008127 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008128
Ronald Cron5425a212020-08-04 14:58:35 +02008129 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008130 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008131}
8132/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008133
Ronald Cronee414c72021-03-18 18:50:08 +01008134/* 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 +02008135void generate_key_rsa( int bits_arg,
8136 data_t *e_arg,
8137 int expected_status_arg )
8138{
Ronald Cron5425a212020-08-04 14:58:35 +02008139 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008140 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008141 size_t bits = bits_arg;
8142 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8143 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8144 psa_status_t expected_status = expected_status_arg;
8145 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8146 uint8_t *exported = NULL;
8147 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008148 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008149 size_t exported_length = SIZE_MAX;
8150 uint8_t *e_read_buffer = NULL;
8151 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008152 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008153 size_t e_read_length = SIZE_MAX;
8154
8155 if( e_arg->len == 0 ||
8156 ( e_arg->len == 3 &&
8157 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8158 {
8159 is_default_public_exponent = 1;
8160 e_read_size = 0;
8161 }
8162 ASSERT_ALLOC( e_read_buffer, e_read_size );
8163 ASSERT_ALLOC( exported, exported_size );
8164
8165 PSA_ASSERT( psa_crypto_init( ) );
8166
8167 psa_set_key_usage_flags( &attributes, usage );
8168 psa_set_key_algorithm( &attributes, alg );
8169 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8170 e_arg->x, e_arg->len ) );
8171 psa_set_key_bits( &attributes, bits );
8172
8173 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008174 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008175 if( expected_status != PSA_SUCCESS )
8176 goto exit;
8177
8178 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008179 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008180 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8181 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8182 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8183 e_read_buffer, e_read_size,
8184 &e_read_length ) );
8185 if( is_default_public_exponent )
8186 TEST_EQUAL( e_read_length, 0 );
8187 else
8188 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8189
8190 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008191 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008192 goto exit;
8193
8194 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008195 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008196 exported, exported_size,
8197 &exported_length ) );
8198 {
8199 uint8_t *p = exported;
8200 uint8_t *end = exported + exported_length;
8201 size_t len;
8202 /* RSAPublicKey ::= SEQUENCE {
8203 * modulus INTEGER, -- n
8204 * publicExponent INTEGER } -- e
8205 */
8206 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008207 MBEDTLS_ASN1_SEQUENCE |
8208 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008209 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008210 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8211 MBEDTLS_ASN1_INTEGER ) );
8212 if( len >= 1 && p[0] == 0 )
8213 {
8214 ++p;
8215 --len;
8216 }
8217 if( e_arg->len == 0 )
8218 {
8219 TEST_EQUAL( len, 3 );
8220 TEST_EQUAL( p[0], 1 );
8221 TEST_EQUAL( p[1], 0 );
8222 TEST_EQUAL( p[2], 1 );
8223 }
8224 else
8225 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8226 }
8227
8228exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008229 /*
8230 * Key attributes may have been returned by psa_get_key_attributes() or
8231 * set by psa_set_key_domain_parameters() thus reset them as required.
8232 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008233 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008234
Ronald Cron5425a212020-08-04 14:58:35 +02008235 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008236 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008237 mbedtls_free( e_read_buffer );
8238 mbedtls_free( exported );
8239}
8240/* END_CASE */
8241
Darryl Greend49a4992018-06-18 17:27:26 +01008242/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008243void persistent_key_load_key_from_storage( data_t *data,
8244 int type_arg, int bits_arg,
8245 int usage_flags_arg, int alg_arg,
8246 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008247{
Ronald Cron71016a92020-08-28 19:01:50 +02008248 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008250 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8251 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008252 psa_key_type_t type = type_arg;
8253 size_t bits = bits_arg;
8254 psa_key_usage_t usage_flags = usage_flags_arg;
8255 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008256 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008257 unsigned char *first_export = NULL;
8258 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008259 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008260 size_t first_exported_length;
8261 size_t second_exported_length;
8262
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008263 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8264 {
8265 ASSERT_ALLOC( first_export, export_size );
8266 ASSERT_ALLOC( second_export, export_size );
8267 }
Darryl Greend49a4992018-06-18 17:27:26 +01008268
Gilles Peskine8817f612018-12-18 00:18:46 +01008269 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008270
Gilles Peskinec87af662019-05-15 16:12:22 +02008271 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008272 psa_set_key_usage_flags( &attributes, usage_flags );
8273 psa_set_key_algorithm( &attributes, alg );
8274 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008275 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008276
Darryl Green0c6575a2018-11-07 16:05:30 +00008277 switch( generation_method )
8278 {
8279 case IMPORT_KEY:
8280 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008281 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008282 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008283 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008284
Darryl Green0c6575a2018-11-07 16:05:30 +00008285 case GENERATE_KEY:
8286 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008287 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008288 break;
8289
8290 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008291#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008292 {
8293 /* Create base key */
8294 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8295 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8296 psa_set_key_usage_flags( &base_attributes,
8297 PSA_KEY_USAGE_DERIVE );
8298 psa_set_key_algorithm( &base_attributes, derive_alg );
8299 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008300 PSA_ASSERT( psa_import_key( &base_attributes,
8301 data->x, data->len,
8302 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008303 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008304 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008305 PSA_ASSERT( psa_key_derivation_input_key(
8306 &operation,
8307 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008308 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008309 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008310 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008311 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8312 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008313 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008314 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008315 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008316 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008317 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008318#else
8319 TEST_ASSUME( ! "KDF not supported in this configuration" );
8320#endif
8321 break;
8322
8323 default:
8324 TEST_ASSERT( ! "generation_method not implemented in test" );
8325 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008326 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008327 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008328
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008329 /* Export the key if permitted by the key policy. */
8330 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8331 {
Ronald Cron5425a212020-08-04 14:58:35 +02008332 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008333 first_export, export_size,
8334 &first_exported_length ) );
8335 if( generation_method == IMPORT_KEY )
8336 ASSERT_COMPARE( data->x, data->len,
8337 first_export, first_exported_length );
8338 }
Darryl Greend49a4992018-06-18 17:27:26 +01008339
8340 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008341 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008342 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008343 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008344
Darryl Greend49a4992018-06-18 17:27:26 +01008345 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008346 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008347 TEST_ASSERT( mbedtls_svc_key_id_equal(
8348 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008349 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8350 PSA_KEY_LIFETIME_PERSISTENT );
8351 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8352 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008353 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008354 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008355 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008356
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008357 /* Export the key again if permitted by the key policy. */
8358 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008359 {
Ronald Cron5425a212020-08-04 14:58:35 +02008360 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008361 second_export, export_size,
8362 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008363 ASSERT_COMPARE( first_export, first_exported_length,
8364 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008365 }
8366
8367 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008368 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008369 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008370
8371exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008372 /*
8373 * Key attributes may have been returned by psa_get_key_attributes()
8374 * thus reset them as required.
8375 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008376 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008377
Darryl Greend49a4992018-06-18 17:27:26 +01008378 mbedtls_free( first_export );
8379 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008380 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008381 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008382 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008383 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008384}
8385/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008386
Neil Armstronga557cb82022-06-10 08:58:32 +02008387/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008388void ecjpake_setup( int alg_arg, int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008389 int input_first, data_t *pw_data,
Neil Armstrongd597bc72022-05-25 11:28:39 +02008390 int expected_status_arg )
8391{
8392 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8393 psa_pake_operation_t operation = psa_pake_operation_init();
8394 psa_algorithm_t alg = alg_arg;
8395 psa_algorithm_t hash_alg = hash_arg;
8396 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8398 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8399 psa_status_t expected_status = expected_status_arg;
8400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8401 unsigned char *output_buffer = NULL;
8402 size_t output_len = 0;
8403
8404 PSA_INIT( );
8405
8406 ASSERT_ALLOC( output_buffer,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008407 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8408 PSA_PAKE_STEP_KEY_SHARE) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008409
8410 if( pw_data->len > 0 )
8411 {
8412 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8413 psa_set_key_algorithm( &attributes, alg );
8414 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8415 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8416 &key ) );
8417 }
8418
8419 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8420 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8421 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8422
Neil Armstrong645cccd2022-06-08 17:36:23 +02008423 PSA_ASSERT( psa_pake_abort( &operation ) );
8424
8425 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8426 PSA_ERROR_BAD_STATE );
8427 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8428 PSA_ERROR_BAD_STATE );
8429 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8430 PSA_ERROR_BAD_STATE );
8431 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8432 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008433 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8434 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008435 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008436 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008437 PSA_ERROR_BAD_STATE );
8438
8439 PSA_ASSERT( psa_pake_abort( &operation ) );
8440
Neil Armstrongd597bc72022-05-25 11:28:39 +02008441 status = psa_pake_setup( &operation, &cipher_suite );
8442 if( status != PSA_SUCCESS )
8443 {
8444 TEST_EQUAL( status, expected_status );
8445 goto exit;
8446 }
8447 else
8448 PSA_ASSERT( status );
8449
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008450 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8451 PSA_ERROR_BAD_STATE );
8452
Neil Armstrongd597bc72022-05-25 11:28:39 +02008453 status = psa_pake_set_role( &operation, role );
8454 if( status != PSA_SUCCESS )
8455 {
8456 TEST_EQUAL( status, expected_status );
8457 goto exit;
8458 }
8459 else
8460 PSA_ASSERT( status );
8461
8462 if( pw_data->len > 0 )
8463 {
8464 status = psa_pake_set_password_key( &operation, key );
8465 if( status != PSA_SUCCESS )
8466 {
8467 TEST_EQUAL( status, expected_status );
8468 goto exit;
8469 }
8470 else
8471 PSA_ASSERT( status );
8472 }
8473
Neil Armstrong707d9572022-06-08 17:31:49 +02008474 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8475 PSA_ERROR_INVALID_ARGUMENT );
8476 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8477 PSA_ERROR_INVALID_ARGUMENT );
8478
8479 const uint8_t unsupported_id[] = "abcd";
8480
8481 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8482 PSA_ERROR_NOT_SUPPORTED );
8483 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8484 PSA_ERROR_NOT_SUPPORTED );
8485
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008486 /* First round */
8487 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008488 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008489 /* Invalid parameters */
8490 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8491 NULL, 0 ),
8492 PSA_ERROR_INVALID_ARGUMENT );
8493 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8494 output_buffer, 66 ),
8495 PSA_ERROR_INVALID_ARGUMENT );
8496 /* Invalid first step */
8497 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8498 output_buffer, 66 ),
8499 PSA_ERROR_BAD_STATE );
8500
8501 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8502 output_buffer, 66 ),
8503 expected_status);
8504
8505 if( expected_status == PSA_SUCCESS )
8506 {
8507 /* Buffer too large */
8508 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8509 output_buffer, 512 ),
8510 PSA_ERROR_INSUFFICIENT_MEMORY );
8511
8512 /* The operation should be aborted at this point */
8513 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8514 output_buffer, 66 ),
8515 PSA_ERROR_BAD_STATE );
8516 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008517 }
8518 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008519 {
8520 /* Invalid parameters */
8521 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8522 NULL, 0, NULL ),
8523 PSA_ERROR_INVALID_ARGUMENT );
8524 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8525 output_buffer, 512, &output_len ),
8526 PSA_ERROR_INVALID_ARGUMENT );
8527 /* Invalid first step */
8528 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8529 output_buffer, 512, &output_len ),
8530 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008531
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008532 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8533 output_buffer, 512, &output_len ),
8534 expected_status );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008535
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008536 if( expected_status == PSA_SUCCESS )
8537 {
8538 TEST_ASSERT( output_len > 0 );
8539
8540 /* Buffer too small */
8541 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8542 output_buffer, 5, &output_len ),
8543 PSA_ERROR_BUFFER_TOO_SMALL );
8544
8545 /* The operation should be aborted at this point */
8546 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8547 output_buffer, 512, &output_len ),
8548 PSA_ERROR_BAD_STATE );
8549 }
8550 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008551
8552exit:
8553 PSA_ASSERT( psa_destroy_key( key ) );
8554 PSA_ASSERT( psa_pake_abort( &operation ) );
8555 mbedtls_free( output_buffer );
8556 PSA_DONE( );
8557}
8558/* END_CASE */
8559
Neil Armstronga557cb82022-06-10 08:58:32 +02008560/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008561void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8562 int client_input_first, int inject_error,
8563 data_t *pw_data )
8564{
8565 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8566 psa_pake_operation_t server = psa_pake_operation_init();
8567 psa_pake_operation_t client = psa_pake_operation_init();
8568 psa_algorithm_t alg = alg_arg;
8569 psa_algorithm_t hash_alg = hash_arg;
8570 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8572
8573 PSA_INIT( );
8574
8575 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8576 psa_set_key_algorithm( &attributes, alg );
8577 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8578 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8579 &key ) );
8580
8581 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8582 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8583 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8584
8585
8586 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8587 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8588
8589 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8590 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8591
8592 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8593 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8594
8595 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8596 client_input_first, 1,
8597 inject_error ), 1 );
8598
8599 if( inject_error == 1 || inject_error == 2 )
8600 goto exit;
8601
8602 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8603 client_input_first, 2,
8604 inject_error ), 1 );
8605
8606exit:
8607 psa_destroy_key( key );
8608 psa_pake_abort( &server );
8609 psa_pake_abort( &client );
8610 PSA_DONE( );
8611}
8612/* END_CASE */
8613
8614/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008615void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008616 int derive_alg_arg, data_t *pw_data,
8617 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008618{
8619 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8620 psa_pake_operation_t server = psa_pake_operation_init();
8621 psa_pake_operation_t client = psa_pake_operation_init();
8622 psa_algorithm_t alg = alg_arg;
8623 psa_algorithm_t hash_alg = hash_arg;
8624 psa_algorithm_t derive_alg = derive_alg_arg;
8625 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8627 psa_key_derivation_operation_t server_derive =
8628 PSA_KEY_DERIVATION_OPERATION_INIT;
8629 psa_key_derivation_operation_t client_derive =
8630 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008631
8632 PSA_INIT( );
8633
Neil Armstrongd597bc72022-05-25 11:28:39 +02008634 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8635 psa_set_key_algorithm( &attributes, alg );
8636 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8637 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8638 &key ) );
8639
8640 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8641 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8642 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8643
Neil Armstrong1e855602022-06-15 11:32:11 +02008644 /* Get shared key */
8645 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
8646 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
8647
8648 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
8649 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
8650 {
8651 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
8652 PSA_KEY_DERIVATION_INPUT_SEED,
8653 (const uint8_t*) "", 0) );
8654 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
8655 PSA_KEY_DERIVATION_INPUT_SEED,
8656 (const uint8_t*) "", 0) );
8657 }
8658
Neil Armstrongd597bc72022-05-25 11:28:39 +02008659 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8660 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8661
8662 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8663 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8664
8665 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8666 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8667
Neil Armstrong1e855602022-06-15 11:32:11 +02008668 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8669 PSA_ERROR_BAD_STATE );
8670 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8671 PSA_ERROR_BAD_STATE );
8672
Neil Armstrongf983caf2022-06-15 15:27:48 +02008673 /* First round */
8674 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8675 client_input_first, 1, 0 ), 1 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008676
Neil Armstrong1e855602022-06-15 11:32:11 +02008677 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8678 PSA_ERROR_BAD_STATE );
8679 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8680 PSA_ERROR_BAD_STATE );
8681
Neil Armstrongf983caf2022-06-15 15:27:48 +02008682 /* Second round */
8683 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8684 client_input_first, 2, 0 ), 1 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008685
Neil Armstrongd597bc72022-05-25 11:28:39 +02008686 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
8687 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
8688
8689exit:
8690 psa_key_derivation_abort( &server_derive );
8691 psa_key_derivation_abort( &client_derive );
8692 psa_destroy_key( key );
8693 psa_pake_abort( &server );
8694 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008695 PSA_DONE( );
8696}
8697/* END_CASE */