blob: 5261dedb44467402fed959945e2294b5b76e571a [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200399 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200404 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100510 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_LE_U( output_length,
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200570 TEST_LE_U( output_length,
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Neil Armstrong4766f992022-02-28 16:23:59 +0100592/*!
593 * \brief Internal Function for MAC multipart tests.
594 * \param key_type_arg Type of key passed in
595 * \param key_data The encryption / decryption key data
596 * \param alg_arg The type of algorithm used
597 * \param input_data Data to encrypt / decrypt
598 * \param data_part_len_arg If not -1, the length of chunks to feed
599 * the data in to be encrypted / decrypted. If
600 * -1, no chunking
601 * \param expected_output Expected output
602 * \param is_verify If non-zero this is an verify operation.
603 * \param do_zero_parts If non-zero, interleave zero length chunks
604 * with normal length chunks.
605 * \return int Zero on failure, non-zero on success.
606 */
607static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
608 int alg_arg,
609 data_t *input_data,
610 int data_part_len_arg,
611 data_t *expected_output,
612 int is_verify,
613 int do_zero_parts )
614{
615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
616 psa_key_type_t key_type = key_type_arg;
617 psa_algorithm_t alg = alg_arg;
618 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
619 unsigned char mac[PSA_MAC_MAX_SIZE];
620 size_t part_offset = 0;
621 size_t part_length = 0;
622 size_t data_part_len = 0;
623 size_t mac_len = 0;
624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
625 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
626
627 int test_ok = 0;
628 size_t part_count = 0;
629
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100630 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100631
632 if( is_verify )
633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
634 else
635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
636
637 psa_set_key_algorithm( &attributes, alg );
638 psa_set_key_type( &attributes, key_type );
639
640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
641 &key ) );
642
643 if( is_verify )
644 status = psa_mac_verify_setup( &operation, key, alg );
645 else
646 status = psa_mac_sign_setup( &operation, key, alg );
647
648 PSA_ASSERT( status );
649
650 if( data_part_len_arg != -1 )
651 {
652 /* Pass data in parts */
653 data_part_len = ( size_t ) data_part_len_arg;
654
655 for( part_offset = 0, part_count = 0;
656 part_offset < input_data->len;
657 part_offset += part_length, part_count++ )
658 {
659 if( do_zero_parts && ( part_count & 0x01 ) )
660 {
661 part_length = 0;
662 }
663 else if( ( input_data->len - part_offset ) < data_part_len )
664 {
665 part_length = ( input_data->len - part_offset );
666 }
667 else
668 {
669 part_length = data_part_len;
670 }
671
672 PSA_ASSERT( psa_mac_update( &operation,
673 ( input_data->x + part_offset ),
674 part_length ) );
675 }
676 }
677 else
678 {
679 /* Pass all data in one go. */
680 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
681 input_data->len ) );
682 }
683
684 if( is_verify )
685 {
686 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
687 expected_output->len ) );
688 }
689 else
690 {
691 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
692 PSA_MAC_MAX_SIZE, &mac_len ) );
693
694 ASSERT_COMPARE( expected_output->x, expected_output->len,
695 mac, mac_len );
696 }
697
698 test_ok = 1;
699
700exit:
701 psa_destroy_key( key );
702 psa_mac_abort( &operation );
703 PSA_DONE( );
704
705 return( test_ok );
706}
707
Neil Armstrong75673ab2022-06-15 17:39:01 +0200708#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrongf983caf2022-06-15 15:27:48 +0200709static int ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
710 psa_pake_operation_t *server,
711 psa_pake_operation_t *client,
712 int client_input_first,
713 int round, int inject_error )
714{
715 unsigned char *buffer0 = NULL, *buffer1 = NULL;
716 size_t buffer_length = (
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
719 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
720 size_t buffer0_off = 0;
721 size_t buffer1_off = 0;
722 size_t s_g1_len, s_g2_len, s_a_len;
723 size_t s_g1_off, s_g2_off, s_a_off;
724 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
725 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
726 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
727 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
728 size_t c_g1_len, c_g2_len, c_a_len;
729 size_t c_g1_off, c_g2_off, c_a_off;
730 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
731 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
732 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
733 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
734 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200735 psa_status_t status;
736 int ret = 0;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200737
738 ASSERT_ALLOC( buffer0, buffer_length );
739 ASSERT_ALLOC( buffer1, buffer_length );
740
741 switch( round )
742 {
743 case 1:
744 /* Server first round Output */
745 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
746 buffer0 + buffer0_off,
747 512 - buffer0_off, &s_g1_len ) );
748 s_g1_off = buffer0_off;
749 buffer0_off += s_g1_len;
750 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
751 buffer0 + buffer0_off,
752 512 - buffer0_off, &s_x1_pk_len ) );
753 s_x1_pk_off = buffer0_off;
754 buffer0_off += s_x1_pk_len;
755 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
756 buffer0 + buffer0_off,
757 512 - buffer0_off, &s_x1_pr_len ) );
758 s_x1_pr_off = buffer0_off;
759 buffer0_off += s_x1_pr_len;
760 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
761 buffer0 + buffer0_off,
762 512 - buffer0_off, &s_g2_len ) );
763 s_g2_off = buffer0_off;
764 buffer0_off += s_g2_len;
765 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
766 buffer0 + buffer0_off,
767 512 - buffer0_off, &s_x2_pk_len ) );
768 s_x2_pk_off = buffer0_off;
769 buffer0_off += s_x2_pk_len;
770 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
771 buffer0 + buffer0_off,
772 512 - buffer0_off, &s_x2_pr_len ) );
773 s_x2_pr_off = buffer0_off;
774 buffer0_off += s_x2_pr_len;
775
776 if( inject_error == 1 )
777 {
778 buffer0[s_x1_pk_off + 12] >>= 4;
779 buffer0[s_x2_pk_off + 7] <<= 4;
780 expected_status = PSA_ERROR_DATA_INVALID;
781 }
782
783 if( client_input_first == 1 )
784 {
785 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200786 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
787 buffer0 + s_g1_off, s_g1_len );
788 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200789 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200790 TEST_EQUAL( status, expected_status );
791 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200792 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200793 else
794 {
795 TEST_EQUAL( status, PSA_SUCCESS );
796 }
797
798 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
799 buffer0 + s_x1_pk_off,
800 s_x1_pk_len );
801 if( inject_error == 1 && status != PSA_SUCCESS )
802 {
803 TEST_EQUAL( status, expected_status );
804 break;
805 }
806 else
807 {
808 TEST_EQUAL( status, PSA_SUCCESS );
809 }
810
811 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
812 buffer0 + s_x1_pr_off,
813 s_x1_pr_len );
814 if( inject_error == 1 && status != PSA_SUCCESS )
815 {
816 TEST_EQUAL( status, expected_status );
817 break;
818 }
819 else
820 {
821 TEST_EQUAL( status, PSA_SUCCESS );
822 }
823
824 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
825 buffer0 + s_g2_off,
826 s_g2_len );
827 if( inject_error == 1 && status != PSA_SUCCESS )
828 {
829 TEST_EQUAL( status, expected_status );
830 break;
831 }
832 else
833 {
834 TEST_EQUAL( status, PSA_SUCCESS );
835 }
836
837 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
838 buffer0 + s_x2_pk_off,
839 s_x2_pk_len );
840 if( inject_error == 1 && status != PSA_SUCCESS )
841 {
842 TEST_EQUAL( status, expected_status );
843 break;
844 }
845 else
846 {
847 TEST_EQUAL( status, PSA_SUCCESS );
848 }
849
850 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
851 buffer0 + s_x2_pr_off,
852 s_x2_pr_len );
853 if( inject_error == 1 && status != PSA_SUCCESS )
854 {
855 TEST_EQUAL( status, expected_status );
856 break;
857 }
858 else
859 {
860 TEST_EQUAL( status, PSA_SUCCESS );
861 }
862
863 /* Error didn't trigger, exit with error */
864 if( inject_error == 1 )
865 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200866 }
867
868 /* Client first round Output */
869 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
870 buffer1 + buffer1_off,
871 512 - buffer1_off, &c_g1_len ) );
872 c_g1_off = buffer1_off;
873 buffer1_off += c_g1_len;
874 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
875 buffer1 + buffer1_off,
876 512 - buffer1_off, &c_x1_pk_len ) );
877 c_x1_pk_off = buffer1_off;
878 buffer1_off += c_x1_pk_len;
879 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
880 buffer1 + buffer1_off,
881 512 - buffer1_off, &c_x1_pr_len ) );
882 c_x1_pr_off = buffer1_off;
883 buffer1_off += c_x1_pr_len;
884 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
885 buffer1 + buffer1_off,
886 512 - buffer1_off, &c_g2_len ) );
887 c_g2_off = buffer1_off;
888 buffer1_off += c_g2_len;
889 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
890 buffer1 + buffer1_off,
891 512 - buffer1_off, &c_x2_pk_len ) );
892 c_x2_pk_off = buffer1_off;
893 buffer1_off += c_x2_pk_len;
894 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
895 buffer1 + buffer1_off,
896 512 - buffer1_off, &c_x2_pr_len ) );
897 c_x2_pr_off = buffer1_off;
898 buffer1_off += c_x2_pr_len;
899
900 if( client_input_first == 0 )
901 {
902 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200903 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
904 buffer0 + s_g1_off, s_g1_len );
905 if( inject_error == 1 && status != PSA_SUCCESS )
906 {
907 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200908 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200909 }
910 else
911 {
912 TEST_EQUAL( status, PSA_SUCCESS );
913 }
914
915 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
916 buffer0 + s_x1_pk_off,
917 s_x1_pk_len );
918 if( inject_error == 1 && status != PSA_SUCCESS )
919 {
920 TEST_EQUAL( status, expected_status );
921 break;
922 }
923 else
924 {
925 TEST_EQUAL( status, PSA_SUCCESS );
926 }
927
928 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
929 buffer0 + s_x1_pr_off,
930 s_x1_pr_len );
931 if( inject_error == 1 && status != PSA_SUCCESS )
932 {
933 TEST_EQUAL( status, expected_status );
934 break;
935 }
936 else
937 {
938 TEST_EQUAL( status, PSA_SUCCESS );
939 }
940
941 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
942 buffer0 + s_g2_off,
943 s_g2_len );
944 if( inject_error == 1 && status != PSA_SUCCESS )
945 {
946 TEST_EQUAL( status, expected_status );
947 break;
948 }
949 else
950 {
951 TEST_EQUAL( status, PSA_SUCCESS );
952 }
953
954 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
955 buffer0 + s_x2_pk_off,
956 s_x2_pk_len );
957 if( inject_error == 1 && status != PSA_SUCCESS )
958 {
959 TEST_EQUAL( status, expected_status );
960 break;
961 }
962 else
963 {
964 TEST_EQUAL( status, PSA_SUCCESS );
965 }
966
967 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
968 buffer0 + s_x2_pr_off,
969 s_x2_pr_len );
970 if( inject_error == 1 && status != PSA_SUCCESS )
971 {
972 TEST_EQUAL( status, expected_status );
973 break;
974 }
975 else
976 {
977 TEST_EQUAL( status, PSA_SUCCESS );
978 }
979
980 /* Error didn't trigger, exit with error */
981 if( inject_error == 1 )
982 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200983 }
984
985 if( inject_error == 2 )
986 {
987 buffer1[c_x1_pk_off + 12] >>= 4;
988 buffer1[c_x2_pk_off + 7] <<= 4;
989 expected_status = PSA_ERROR_DATA_INVALID;
990 }
991
992 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200993 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
994 buffer1 + c_g1_off, c_g1_len );
995 if( inject_error == 2 && status != PSA_SUCCESS )
996 {
997 TEST_EQUAL( status, expected_status );
998 break;
999 }
1000 else
1001 {
1002 TEST_EQUAL( status, PSA_SUCCESS );
1003 }
1004
1005 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1006 buffer1 + c_x1_pk_off, c_x1_pk_len );
1007 if( inject_error == 2 && status != PSA_SUCCESS )
1008 {
1009 TEST_EQUAL( status, expected_status );
1010 break;
1011 }
1012 else
1013 {
1014 TEST_EQUAL( status, PSA_SUCCESS );
1015 }
1016
1017 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1018 buffer1 + c_x1_pr_off, c_x1_pr_len );
1019 if( inject_error == 2 && status != PSA_SUCCESS )
1020 {
1021 TEST_EQUAL( status, expected_status );
1022 break;
1023 }
1024 else
1025 {
1026 TEST_EQUAL( status, PSA_SUCCESS );
1027 }
1028
1029 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1030 buffer1 + c_g2_off, c_g2_len );
1031 if( inject_error == 2 && status != PSA_SUCCESS )
1032 {
1033 TEST_EQUAL( status, expected_status );
1034 break;
1035 }
1036 else
1037 {
1038 TEST_EQUAL( status, PSA_SUCCESS );
1039 }
1040
1041 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1042 buffer1 + c_x2_pk_off, c_x2_pk_len );
1043 if( inject_error == 2 && status != PSA_SUCCESS )
1044 {
1045 TEST_EQUAL( status, expected_status );
1046 break;
1047 }
1048 else
1049 {
1050 TEST_EQUAL( status, PSA_SUCCESS );
1051 }
1052
1053 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1054 buffer1 + c_x2_pr_off, c_x2_pr_len );
1055 if( inject_error == 2 && status != PSA_SUCCESS )
1056 {
1057 TEST_EQUAL( status, expected_status );
1058 break;
1059 }
1060 else
1061 {
1062 TEST_EQUAL( status, PSA_SUCCESS );
1063 }
1064
1065 /* Error didn't trigger, exit with error */
1066 if( inject_error == 2 )
1067 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001068
1069 break;
1070
1071 case 2:
1072 /* Server second round Output */
1073 buffer0_off = 0;
1074
1075 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1076 buffer0 + buffer0_off,
1077 512 - buffer0_off, &s_a_len ) );
1078 s_a_off = buffer0_off;
1079 buffer0_off += s_a_len;
1080 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1081 buffer0 + buffer0_off,
1082 512 - buffer0_off, &s_x2s_pk_len ) );
1083 s_x2s_pk_off = buffer0_off;
1084 buffer0_off += s_x2s_pk_len;
1085 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1086 buffer0 + buffer0_off,
1087 512 - buffer0_off, &s_x2s_pr_len ) );
1088 s_x2s_pr_off = buffer0_off;
1089 buffer0_off += s_x2s_pr_len;
1090
1091 if( inject_error == 3 )
1092 {
1093 buffer0[s_x2s_pk_off + 12] >>= 4;
1094 expected_status = PSA_ERROR_DATA_INVALID;
1095 }
1096
1097 if( client_input_first == 1 )
1098 {
1099 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001100 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1101 buffer0 + s_a_off, s_a_len );
1102 if( inject_error == 3 && status != PSA_SUCCESS )
1103 {
1104 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001105 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001106 }
1107 else
1108 {
1109 TEST_EQUAL( status, PSA_SUCCESS );
1110 }
1111
1112 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1113 buffer0 + s_x2s_pk_off,
1114 s_x2s_pk_len );
1115 if( inject_error == 3 && status != PSA_SUCCESS )
1116 {
1117 TEST_EQUAL( status, expected_status );
1118 break;
1119 }
1120 else
1121 {
1122 TEST_EQUAL( status, PSA_SUCCESS );
1123 }
1124
1125 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1126 buffer0 + s_x2s_pr_off,
1127 s_x2s_pr_len );
1128 if( inject_error == 3 && status != PSA_SUCCESS )
1129 {
1130 TEST_EQUAL( status, expected_status );
1131 break;
1132 }
1133 else
1134 {
1135 TEST_EQUAL( status, PSA_SUCCESS );
1136 }
1137
1138 /* Error didn't trigger, exit with error */
1139 if( inject_error == 3 )
1140 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001141 }
1142
1143 /* Client second round Output */
1144 buffer1_off = 0;
1145
1146 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1147 buffer1 + buffer1_off,
1148 512 - buffer1_off, &c_a_len ) );
1149 c_a_off = buffer1_off;
1150 buffer1_off += c_a_len;
1151 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1152 buffer1 + buffer1_off,
1153 512 - buffer1_off, &c_x2s_pk_len ) );
1154 c_x2s_pk_off = buffer1_off;
1155 buffer1_off += c_x2s_pk_len;
1156 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1157 buffer1 + buffer1_off,
1158 512 - buffer1_off, &c_x2s_pr_len ) );
1159 c_x2s_pr_off = buffer1_off;
1160 buffer1_off += c_x2s_pr_len;
1161
1162 if( client_input_first == 0 )
1163 {
1164 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001165 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1166 buffer0 + s_a_off, s_a_len );
1167 if( inject_error == 3 && status != PSA_SUCCESS )
1168 {
1169 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001170 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001171 }
1172 else
1173 {
1174 TEST_EQUAL( status, PSA_SUCCESS );
1175 }
1176
1177 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1178 buffer0 + s_x2s_pk_off,
1179 s_x2s_pk_len );
1180 if( inject_error == 3 && status != PSA_SUCCESS )
1181 {
1182 TEST_EQUAL( status, expected_status );
1183 break;
1184 }
1185 else
1186 {
1187 TEST_EQUAL( status, PSA_SUCCESS );
1188 }
1189
1190 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1191 buffer0 + s_x2s_pr_off,
1192 s_x2s_pr_len );
1193 if( inject_error == 3 && status != PSA_SUCCESS )
1194 {
1195 TEST_EQUAL( status, expected_status );
1196 break;
1197 }
1198 else
1199 {
1200 TEST_EQUAL( status, PSA_SUCCESS );
1201 }
1202
1203 /* Error didn't trigger, exit with error */
1204 if( inject_error == 3 )
1205 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001206 }
1207
1208 if( inject_error == 4 )
1209 {
1210 buffer1[c_x2s_pk_off + 12] >>= 4;
1211 expected_status = PSA_ERROR_DATA_INVALID;
1212 }
1213
1214 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001215 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1216 buffer1 + c_a_off, c_a_len );
1217 if( inject_error == 4 && status != PSA_SUCCESS )
1218 {
1219 TEST_EQUAL( status, expected_status );
1220 break;
1221 }
1222 else
1223 {
1224 TEST_EQUAL( status, PSA_SUCCESS );
1225 }
1226
1227 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1228 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1229 if( inject_error == 4 && status != PSA_SUCCESS )
1230 {
1231 TEST_EQUAL( status, expected_status );
1232 break;
1233 }
1234 else
1235 {
1236 TEST_EQUAL( status, PSA_SUCCESS );
1237 }
1238
1239 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1240 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1241 if( inject_error == 4 && status != PSA_SUCCESS )
1242 {
1243 TEST_EQUAL( status, expected_status );
1244 break;
1245 }
1246 else
1247 {
1248 TEST_EQUAL( status, PSA_SUCCESS );
1249 }
1250
1251 /* Error didn't trigger, exit with error */
1252 if( inject_error == 4 )
1253 goto exit;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001254
1255 break;
1256
1257 }
1258
1259 ret = 1;
1260
1261exit:
1262 mbedtls_free( buffer0 );
1263 mbedtls_free( buffer1 );
1264 return( ret );
1265}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001266#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001267
Gilles Peskinee59236f2018-01-27 23:32:46 +01001268/* END_HEADER */
1269
1270/* BEGIN_DEPENDENCIES
1271 * depends_on:MBEDTLS_PSA_CRYPTO_C
1272 * END_DEPENDENCIES
1273 */
1274
1275/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001276void static_checks( )
1277{
1278 size_t max_truncated_mac_size =
1279 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1280
1281 /* Check that the length for a truncated MAC always fits in the algorithm
1282 * encoding. The shifted mask is the maximum truncated value. The
1283 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001284 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001285}
1286/* END_CASE */
1287
1288/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001289void import_with_policy( int type_arg,
1290 int usage_arg, int alg_arg,
1291 int expected_status_arg )
1292{
1293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1294 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001295 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001296 psa_key_type_t type = type_arg;
1297 psa_key_usage_t usage = usage_arg;
1298 psa_algorithm_t alg = alg_arg;
1299 psa_status_t expected_status = expected_status_arg;
1300 const uint8_t key_material[16] = {0};
1301 psa_status_t status;
1302
1303 PSA_ASSERT( psa_crypto_init( ) );
1304
1305 psa_set_key_type( &attributes, type );
1306 psa_set_key_usage_flags( &attributes, usage );
1307 psa_set_key_algorithm( &attributes, alg );
1308
1309 status = psa_import_key( &attributes,
1310 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001311 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001312 TEST_EQUAL( status, expected_status );
1313 if( status != PSA_SUCCESS )
1314 goto exit;
1315
Ronald Cron5425a212020-08-04 14:58:35 +02001316 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001317 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001318 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001319 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001320 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001321 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001322
Ronald Cron5425a212020-08-04 14:58:35 +02001323 PSA_ASSERT( psa_destroy_key( key ) );
1324 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001325
1326exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001327 /*
1328 * Key attributes may have been returned by psa_get_key_attributes()
1329 * thus reset them as required.
1330 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001331 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001332
1333 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001334 PSA_DONE( );
1335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
1339void import_with_data( data_t *data, int type_arg,
1340 int attr_bits_arg,
1341 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342{
1343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1344 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001346 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001347 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001348 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001349 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352
Gilles Peskine4747d192019-04-17 15:05:45 +02001353 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001354 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001355
Ronald Cron5425a212020-08-04 14:58:35 +02001356 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001357 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001358 if( status != PSA_SUCCESS )
1359 goto exit;
1360
Ronald Cron5425a212020-08-04 14:58:35 +02001361 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001362 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001363 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001364 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001365 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001366
Ronald Cron5425a212020-08-04 14:58:35 +02001367 PSA_ASSERT( psa_destroy_key( key ) );
1368 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369
1370exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001371 /*
1372 * Key attributes may have been returned by psa_get_key_attributes()
1373 * thus reset them as required.
1374 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001375 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001376
1377 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001378 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379}
1380/* END_CASE */
1381
1382/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001383void import_large_key( int type_arg, int byte_size_arg,
1384 int expected_status_arg )
1385{
1386 psa_key_type_t type = type_arg;
1387 size_t byte_size = byte_size_arg;
1388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1389 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001390 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001391 psa_status_t status;
1392 uint8_t *buffer = NULL;
1393 size_t buffer_size = byte_size + 1;
1394 size_t n;
1395
Steven Cooreman69967ce2021-01-18 18:01:08 +01001396 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001397 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001398 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001399 memset( buffer, 'K', byte_size );
1400
1401 PSA_ASSERT( psa_crypto_init( ) );
1402
1403 /* Try importing the key */
1404 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1405 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001406 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001407 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001408 TEST_EQUAL( status, expected_status );
1409
1410 if( status == PSA_SUCCESS )
1411 {
Ronald Cron5425a212020-08-04 14:58:35 +02001412 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001413 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1414 TEST_EQUAL( psa_get_key_bits( &attributes ),
1415 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001416 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001417 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001418 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001419 for( n = 0; n < byte_size; n++ )
1420 TEST_EQUAL( buffer[n], 'K' );
1421 for( n = byte_size; n < buffer_size; n++ )
1422 TEST_EQUAL( buffer[n], 0 );
1423 }
1424
1425exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001426 /*
1427 * Key attributes may have been returned by psa_get_key_attributes()
1428 * thus reset them as required.
1429 */
1430 psa_reset_key_attributes( &attributes );
1431
Ronald Cron5425a212020-08-04 14:58:35 +02001432 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001433 PSA_DONE( );
1434 mbedtls_free( buffer );
1435}
1436/* END_CASE */
1437
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001438/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001439void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1440{
Ronald Cron5425a212020-08-04 14:58:35 +02001441 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001442 size_t bits = bits_arg;
1443 psa_status_t expected_status = expected_status_arg;
1444 psa_status_t status;
1445 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001446 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001447 size_t buffer_size = /* Slight overapproximations */
1448 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001449 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001450 unsigned char *p;
1451 int ret;
1452 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001454
Gilles Peskine8817f612018-12-18 00:18:46 +01001455 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001456 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001457
1458 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1459 bits, keypair ) ) >= 0 );
1460 length = ret;
1461
1462 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001463 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001464 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001465 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001466
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001467 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001468 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001469
1470exit:
1471 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001472 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001473}
1474/* END_CASE */
1475
1476/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001477void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001478 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001479 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301480 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001481 int expected_bits,
1482 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001483 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001484 int canonical_input )
1485{
Ronald Cron5425a212020-08-04 14:58:35 +02001486 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001487 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001488 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001489 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001490 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301491 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492 unsigned char *exported = NULL;
1493 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001494 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001495 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001496 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001498 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001499
Moran Pekercb088e72018-07-17 17:36:59 +03001500 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001501 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001502 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001503 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001504 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001505
Archana4d7ae1d2021-07-07 02:50:22 +05301506 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001507 psa_set_key_usage_flags( &attributes, usage_arg );
1508 psa_set_key_algorithm( &attributes, alg );
1509 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001510
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001512 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513
1514 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001515 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001516 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1517 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001518 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001519
1520 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001521 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001522 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001523
1524 /* The exported length must be set by psa_export_key() to a value between 0
1525 * and export_size. On errors, the exported length must be 0. */
1526 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1527 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001528 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001529
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001530 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001531 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001532 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001533 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001534 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001535 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001536 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001537
Gilles Peskineea38a922021-02-13 00:05:16 +01001538 /* Run sanity checks on the exported key. For non-canonical inputs,
1539 * this validates the canonical representations. For canonical inputs,
1540 * this doesn't directly validate the implementation, but it still helps
1541 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301542 if( !psa_key_lifetime_is_external( lifetime ) )
1543 {
1544 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1545 goto exit;
1546 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001547
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001548 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001549 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001550 else
1551 {
Ronald Cron5425a212020-08-04 14:58:35 +02001552 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001553 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001554 &key2 ) );
1555 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001556 reexported,
1557 export_size,
1558 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001559 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301560 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001561 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001562 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001563 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301564 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301565 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001566 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001567
1568destroy:
1569 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001570 PSA_ASSERT( psa_destroy_key( key ) );
1571 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001572
1573exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001574 /*
1575 * Key attributes may have been returned by psa_get_key_attributes()
1576 * thus reset them as required.
1577 */
1578 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301579 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001580 mbedtls_free( exported );
1581 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001582 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001583}
1584/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001585
Moran Pekerf709f4a2018-06-06 17:26:04 +03001586/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001587void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001588 int type_arg,
1589 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301590 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001591 int export_size_delta,
1592 int expected_export_status_arg,
1593 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001594{
Ronald Cron5425a212020-08-04 14:58:35 +02001595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001596 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001598 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001599 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301600 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001601 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001602 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001603 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001604 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001605
Gilles Peskine8817f612018-12-18 00:18:46 +01001606 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001607
Archana4d7ae1d2021-07-07 02:50:22 +05301608 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001609 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1610 psa_set_key_algorithm( &attributes, alg );
1611 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001612
1613 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001614 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001615
Gilles Peskine49c25912018-10-29 15:15:31 +01001616 /* Export the public key */
1617 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001618 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001619 exported, export_size,
1620 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001621 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001622 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001623 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001624 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001625 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001626 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001627 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001628 TEST_LE_U( expected_public_key->len,
1629 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1630 TEST_LE_U( expected_public_key->len,
1631 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1632 TEST_LE_U( expected_public_key->len,
1633 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001634 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1635 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001636 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001637exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001638 /*
1639 * Key attributes may have been returned by psa_get_key_attributes()
1640 * thus reset them as required.
1641 */
1642 psa_reset_key_attributes( &attributes );
1643
itayzafrir3e02b3b2018-06-12 17:06:52 +03001644 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001645 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001646 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001647}
1648/* END_CASE */
1649
Gilles Peskine20035e32018-02-03 22:44:14 +01001650/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001651void import_and_exercise_key( data_t *data,
1652 int type_arg,
1653 int bits_arg,
1654 int alg_arg )
1655{
Ronald Cron5425a212020-08-04 14:58:35 +02001656 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001657 psa_key_type_t type = type_arg;
1658 size_t bits = bits_arg;
1659 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001660 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001662 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001663
Gilles Peskine8817f612018-12-18 00:18:46 +01001664 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001665
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 psa_set_key_usage_flags( &attributes, usage );
1667 psa_set_key_algorithm( &attributes, alg );
1668 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001669
1670 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001671 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001672
1673 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001674 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001675 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1676 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001677
1678 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001679 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001680 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001681
Ronald Cron5425a212020-08-04 14:58:35 +02001682 PSA_ASSERT( psa_destroy_key( key ) );
1683 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001684
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001685exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001686 /*
1687 * Key attributes may have been returned by psa_get_key_attributes()
1688 * thus reset them as required.
1689 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001690 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001691
1692 psa_reset_key_attributes( &attributes );
1693 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001694 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001695}
1696/* END_CASE */
1697
1698/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001699void effective_key_attributes( int type_arg, int expected_type_arg,
1700 int bits_arg, int expected_bits_arg,
1701 int usage_arg, int expected_usage_arg,
1702 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001703{
Ronald Cron5425a212020-08-04 14:58:35 +02001704 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001705 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001706 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001707 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001708 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001709 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001710 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001711 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001712 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001713 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001714
Gilles Peskine8817f612018-12-18 00:18:46 +01001715 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001716
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001717 psa_set_key_usage_flags( &attributes, usage );
1718 psa_set_key_algorithm( &attributes, alg );
1719 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001720 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001721
Ronald Cron5425a212020-08-04 14:58:35 +02001722 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001723 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001724
Ronald Cron5425a212020-08-04 14:58:35 +02001725 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001726 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1727 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1728 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1729 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001730
1731exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001732 /*
1733 * Key attributes may have been returned by psa_get_key_attributes()
1734 * thus reset them as required.
1735 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001736 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001737
1738 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001739 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001740}
1741/* END_CASE */
1742
1743/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001744void check_key_policy( int type_arg, int bits_arg,
1745 int usage_arg, int alg_arg )
1746{
1747 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001748 usage_arg,
1749 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001750 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001751 goto exit;
1752}
1753/* END_CASE */
1754
1755/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001756void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001757{
1758 /* Test each valid way of initializing the object, except for `= {0}`, as
1759 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1760 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001761 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001762 psa_key_attributes_t func = psa_key_attributes_init( );
1763 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1764 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001765
1766 memset( &zero, 0, sizeof( zero ) );
1767
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001768 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1769 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1770 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001771
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001772 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1773 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1774 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1775
1776 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1777 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1778 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1779
1780 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1781 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1782 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1783
1784 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1785 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1786 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001791void mac_key_policy( int policy_usage_arg,
1792 int policy_alg_arg,
1793 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001794 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001795 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001796 int expected_status_sign_arg,
1797 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001798{
Ronald Cron5425a212020-08-04 14:58:35 +02001799 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001800 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001801 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001802 psa_key_type_t key_type = key_type_arg;
1803 psa_algorithm_t policy_alg = policy_alg_arg;
1804 psa_algorithm_t exercise_alg = exercise_alg_arg;
1805 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001806 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001807 psa_status_t expected_status_sign = expected_status_sign_arg;
1808 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001809 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001810
Gilles Peskine8817f612018-12-18 00:18:46 +01001811 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001812
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001813 psa_set_key_usage_flags( &attributes, policy_usage );
1814 psa_set_key_algorithm( &attributes, policy_alg );
1815 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001816
Gilles Peskine049c7532019-05-15 20:22:09 +02001817 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001818 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001819
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001820 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1821 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001822
Ronald Cron5425a212020-08-04 14:58:35 +02001823 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001824 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001825
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001826 /* Calculate the MAC, one-shot case. */
1827 uint8_t input[128] = {0};
1828 size_t mac_len;
1829 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1830 input, 128,
1831 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1832 expected_status_sign );
1833
Neil Armstrong3af9b972022-02-07 12:20:21 +01001834 /* Calculate the MAC, multi-part case. */
1835 PSA_ASSERT( psa_mac_abort( &operation ) );
1836 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1837 if( status == PSA_SUCCESS )
1838 {
1839 status = psa_mac_update( &operation, input, 128 );
1840 if( status == PSA_SUCCESS )
1841 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1842 &mac_len ),
1843 expected_status_sign );
1844 else
1845 TEST_EQUAL( status, expected_status_sign );
1846 }
1847 else
1848 {
1849 TEST_EQUAL( status, expected_status_sign );
1850 }
1851 PSA_ASSERT( psa_mac_abort( &operation ) );
1852
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001853 /* Verify correct MAC, one-shot case. */
1854 status = psa_mac_verify( key, exercise_alg, input, 128,
1855 mac, mac_len );
1856
1857 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1858 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001859 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001860 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001861
Neil Armstrong3af9b972022-02-07 12:20:21 +01001862 /* Verify correct MAC, multi-part case. */
1863 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1864 if( status == PSA_SUCCESS )
1865 {
1866 status = psa_mac_update( &operation, input, 128 );
1867 if( status == PSA_SUCCESS )
1868 {
1869 status = psa_mac_verify_finish( &operation, mac, mac_len );
1870 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1871 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1872 else
1873 TEST_EQUAL( status, expected_status_verify );
1874 }
1875 else
1876 {
1877 TEST_EQUAL( status, expected_status_verify );
1878 }
1879 }
1880 else
1881 {
1882 TEST_EQUAL( status, expected_status_verify );
1883 }
1884
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001885 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001886
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001887 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001888 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001889 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001890
1891exit:
1892 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001893 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001894 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001895}
1896/* END_CASE */
1897
1898/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001899void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001900 int policy_alg,
1901 int key_type,
1902 data_t *key_data,
1903 int exercise_alg )
1904{
Ronald Cron5425a212020-08-04 14:58:35 +02001905 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001907 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001908 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001909 size_t output_buffer_size = 0;
1910 size_t input_buffer_size = 0;
1911 size_t output_length = 0;
1912 uint8_t *output = NULL;
1913 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001914 psa_status_t status;
1915
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001916 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1917 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1918 input_buffer_size );
1919
1920 ASSERT_ALLOC( input, input_buffer_size );
1921 ASSERT_ALLOC( output, output_buffer_size );
1922
Gilles Peskine8817f612018-12-18 00:18:46 +01001923 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001925 psa_set_key_usage_flags( &attributes, policy_usage );
1926 psa_set_key_algorithm( &attributes, policy_alg );
1927 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928
Gilles Peskine049c7532019-05-15 20:22:09 +02001929 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001930 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001932 /* Check if no key usage flag implication is done */
1933 TEST_EQUAL( policy_usage,
1934 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001935
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001936 /* Encrypt check, one-shot */
1937 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1938 output, output_buffer_size,
1939 &output_length);
1940 if( policy_alg == exercise_alg &&
1941 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1942 PSA_ASSERT( status );
1943 else
1944 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1945
1946 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001947 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001948 if( policy_alg == exercise_alg &&
1949 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001952 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953 psa_cipher_abort( &operation );
1954
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001955 /* Decrypt check, one-shot */
1956 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1957 input, input_buffer_size,
1958 &output_length);
1959 if( policy_alg == exercise_alg &&
1960 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1961 PSA_ASSERT( status );
1962 else
1963 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1964
1965 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001966 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967 if( policy_alg == exercise_alg &&
1968 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001971 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001972
1973exit:
1974 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001975 mbedtls_free( input );
1976 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001977 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001978 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001979}
1980/* END_CASE */
1981
1982/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001983void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001984 int policy_alg,
1985 int key_type,
1986 data_t *key_data,
1987 int nonce_length_arg,
1988 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001989 int exercise_alg,
1990 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001991{
Ronald Cron5425a212020-08-04 14:58:35 +02001992 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001994 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001995 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001996 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001997 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001998 unsigned char nonce[16] = {0};
1999 size_t nonce_length = nonce_length_arg;
2000 unsigned char tag[16];
2001 size_t tag_length = tag_length_arg;
2002 size_t output_length;
2003
Gilles Peskine7be11a72022-04-14 00:12:57 +02002004 TEST_LE_U( nonce_length, sizeof( nonce ) );
2005 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
Gilles Peskine8817f612018-12-18 00:18:46 +01002007 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002008
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002009 psa_set_key_usage_flags( &attributes, policy_usage );
2010 psa_set_key_algorithm( &attributes, policy_alg );
2011 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012
Gilles Peskine049c7532019-05-15 20:22:09 +02002013 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002014 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002016 /* Check if no key usage implication is done */
2017 TEST_EQUAL( policy_usage,
2018 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002019
Neil Armstrong752d8112022-02-07 14:51:11 +01002020 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002021 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022 nonce, nonce_length,
2023 NULL, 0,
2024 NULL, 0,
2025 tag, tag_length,
2026 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002027 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2028 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002029 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002030 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002031
Neil Armstrong752d8112022-02-07 14:51:11 +01002032 /* Encrypt check, multi-part */
2033 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2034 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2035 TEST_EQUAL( status, expected_status );
2036 else
2037 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2038
2039 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002041 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002042 nonce, nonce_length,
2043 NULL, 0,
2044 tag, tag_length,
2045 NULL, 0,
2046 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002047 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2048 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2049 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002050 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002051 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002052 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053
Neil Armstrong752d8112022-02-07 14:51:11 +01002054 /* Decrypt check, multi-part */
2055 PSA_ASSERT( psa_aead_abort( &operation ) );
2056 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2057 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2058 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2059 else
2060 TEST_EQUAL( status, expected_status );
2061
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002062exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002063 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002064 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002065 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066}
2067/* END_CASE */
2068
2069/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002070void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002071 int policy_alg,
2072 int key_type,
2073 data_t *key_data,
2074 int exercise_alg )
2075{
Ronald Cron5425a212020-08-04 14:58:35 +02002076 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002077 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002078 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079 psa_status_t status;
2080 size_t key_bits;
2081 size_t buffer_length;
2082 unsigned char *buffer = NULL;
2083 size_t output_length;
2084
Gilles Peskine8817f612018-12-18 00:18:46 +01002085 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002086
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002087 psa_set_key_usage_flags( &attributes, policy_usage );
2088 psa_set_key_algorithm( &attributes, policy_alg );
2089 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090
Gilles Peskine049c7532019-05-15 20:22:09 +02002091 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002092 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002094 /* Check if no key usage implication is done */
2095 TEST_EQUAL( policy_usage,
2096 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002097
Ronald Cron5425a212020-08-04 14:58:35 +02002098 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002099 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002100 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2101 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002102 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002103
Ronald Cron5425a212020-08-04 14:58:35 +02002104 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105 NULL, 0,
2106 NULL, 0,
2107 buffer, buffer_length,
2108 &output_length );
2109 if( policy_alg == exercise_alg &&
2110 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002111 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002113 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002114
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002115 if( buffer_length != 0 )
2116 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002117 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118 buffer, buffer_length,
2119 NULL, 0,
2120 buffer, buffer_length,
2121 &output_length );
2122 if( policy_alg == exercise_alg &&
2123 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002124 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002126 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002127
2128exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002129 /*
2130 * Key attributes may have been returned by psa_get_key_attributes()
2131 * thus reset them as required.
2132 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002133 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002134
2135 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002136 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002137 mbedtls_free( buffer );
2138}
2139/* END_CASE */
2140
2141/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002142void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002143 int policy_alg,
2144 int key_type,
2145 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002146 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002147 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002148 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002149{
Ronald Cron5425a212020-08-04 14:58:35 +02002150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002152 psa_key_usage_t policy_usage = policy_usage_arg;
2153 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002154 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002155 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2156 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2157 * compatible with the policy and `payload_length_arg` is supposed to be
2158 * a valid input length to sign. If `payload_length_arg <= 0`,
2159 * `exercise_alg` is supposed to be forbidden by the policy. */
2160 int compatible_alg = payload_length_arg > 0;
2161 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002162 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002163 size_t signature_length;
2164
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002165 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002166 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002167 TEST_EQUAL( expected_usage,
2168 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002169
Gilles Peskine8817f612018-12-18 00:18:46 +01002170 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002171
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002172 psa_set_key_usage_flags( &attributes, policy_usage );
2173 psa_set_key_algorithm( &attributes, policy_alg );
2174 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175
Gilles Peskine049c7532019-05-15 20:22:09 +02002176 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002177 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002178
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002179 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2180
Ronald Cron5425a212020-08-04 14:58:35 +02002181 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002182 payload, payload_length,
2183 signature, sizeof( signature ),
2184 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002185 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002187 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002188 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002189
2190 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002191 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002192 payload, payload_length,
2193 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002194 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002195 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002196 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002197 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002198
Gilles Peskinef7b41372021-09-22 16:15:05 +02002199 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002200 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002201 {
2202 status = psa_sign_message( key, exercise_alg,
2203 payload, payload_length,
2204 signature, sizeof( signature ),
2205 &signature_length );
2206 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2207 PSA_ASSERT( status );
2208 else
2209 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2210
2211 memset( signature, 0, sizeof( signature ) );
2212 status = psa_verify_message( key, exercise_alg,
2213 payload, payload_length,
2214 signature, sizeof( signature ) );
2215 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2216 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2217 else
2218 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2219 }
2220
Gilles Peskined5b33222018-06-18 22:20:03 +02002221exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002222 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002223 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002224}
2225/* END_CASE */
2226
Janos Follathba3fab92019-06-11 14:50:16 +01002227/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002228void derive_key_policy( int policy_usage,
2229 int policy_alg,
2230 int key_type,
2231 data_t *key_data,
2232 int exercise_alg )
2233{
Ronald Cron5425a212020-08-04 14:58:35 +02002234 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002236 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002237 psa_status_t status;
2238
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002240
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002241 psa_set_key_usage_flags( &attributes, policy_usage );
2242 psa_set_key_algorithm( &attributes, policy_alg );
2243 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002244
Gilles Peskine049c7532019-05-15 20:22:09 +02002245 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002246 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002247
Janos Follathba3fab92019-06-11 14:50:16 +01002248 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2249
2250 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2251 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002252 {
Janos Follathba3fab92019-06-11 14:50:16 +01002253 PSA_ASSERT( psa_key_derivation_input_bytes(
2254 &operation,
2255 PSA_KEY_DERIVATION_INPUT_SEED,
2256 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002257 }
Janos Follathba3fab92019-06-11 14:50:16 +01002258
2259 status = psa_key_derivation_input_key( &operation,
2260 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002261 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002262
Gilles Peskineea0fb492018-07-12 17:17:20 +02002263 if( policy_alg == exercise_alg &&
2264 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002265 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002266 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002267 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002268
2269exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002270 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002271 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002272 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002273}
2274/* END_CASE */
2275
2276/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002277void agreement_key_policy( int policy_usage,
2278 int policy_alg,
2279 int key_type_arg,
2280 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002281 int exercise_alg,
2282 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002283{
Ronald Cron5425a212020-08-04 14:58:35 +02002284 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002285 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002286 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002287 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002288 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002289 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002290
Gilles Peskine8817f612018-12-18 00:18:46 +01002291 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002292
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002293 psa_set_key_usage_flags( &attributes, policy_usage );
2294 psa_set_key_algorithm( &attributes, policy_alg );
2295 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296
Gilles Peskine049c7532019-05-15 20:22:09 +02002297 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002298 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002300 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002301 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002302
Steven Cooremance48e852020-10-05 16:02:45 +02002303 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002304
2305exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002306 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002307 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002308 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002309}
2310/* END_CASE */
2311
2312/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002313void key_policy_alg2( int key_type_arg, data_t *key_data,
2314 int usage_arg, int alg_arg, int alg2_arg )
2315{
Ronald Cron5425a212020-08-04 14:58:35 +02002316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002317 psa_key_type_t key_type = key_type_arg;
2318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2319 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2320 psa_key_usage_t usage = usage_arg;
2321 psa_algorithm_t alg = alg_arg;
2322 psa_algorithm_t alg2 = alg2_arg;
2323
2324 PSA_ASSERT( psa_crypto_init( ) );
2325
2326 psa_set_key_usage_flags( &attributes, usage );
2327 psa_set_key_algorithm( &attributes, alg );
2328 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2329 psa_set_key_type( &attributes, key_type );
2330 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002331 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002332
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002333 /* Update the usage flags to obtain implicit usage flags */
2334 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002335 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002336 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2337 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2338 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2339
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002340 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002341 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002342 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002343 goto exit;
2344
2345exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002346 /*
2347 * Key attributes may have been returned by psa_get_key_attributes()
2348 * thus reset them as required.
2349 */
2350 psa_reset_key_attributes( &got_attributes );
2351
Ronald Cron5425a212020-08-04 14:58:35 +02002352 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002353 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002354}
2355/* END_CASE */
2356
2357/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002358void raw_agreement_key_policy( int policy_usage,
2359 int policy_alg,
2360 int key_type_arg,
2361 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002362 int exercise_alg,
2363 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002364{
Ronald Cron5425a212020-08-04 14:58:35 +02002365 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002366 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002367 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002368 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002369 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002370 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002371
2372 PSA_ASSERT( psa_crypto_init( ) );
2373
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002374 psa_set_key_usage_flags( &attributes, policy_usage );
2375 psa_set_key_algorithm( &attributes, policy_alg );
2376 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002377
Gilles Peskine049c7532019-05-15 20:22:09 +02002378 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002379 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002380
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002381 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002382
Steven Cooremance48e852020-10-05 16:02:45 +02002383 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002384
2385exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002386 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002387 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002388 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002389}
2390/* END_CASE */
2391
2392/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002393void copy_success( int source_usage_arg,
2394 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302395 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002396 int type_arg, data_t *material,
2397 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002398 int target_usage_arg,
2399 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302400 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401 int expected_usage_arg,
2402 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002403{
Gilles Peskineca25db92019-04-19 11:43:08 +02002404 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2405 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002406 psa_key_usage_t expected_usage = expected_usage_arg;
2407 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002408 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302409 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2410 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002411 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2412 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002413 uint8_t *export_buffer = NULL;
2414
Gilles Peskine57ab7212019-01-28 13:03:09 +01002415 PSA_ASSERT( psa_crypto_init( ) );
2416
Gilles Peskineca25db92019-04-19 11:43:08 +02002417 /* Prepare the source key. */
2418 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2419 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002420 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002421 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302422 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002423 PSA_ASSERT( psa_import_key( &source_attributes,
2424 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002425 &source_key ) );
2426 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002427
Gilles Peskineca25db92019-04-19 11:43:08 +02002428 /* Prepare the target attributes. */
2429 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002430 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002431 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002432 }
Archana8a180362021-07-05 02:18:48 +05302433 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002434
Gilles Peskineca25db92019-04-19 11:43:08 +02002435 if( target_usage_arg != -1 )
2436 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2437 if( target_alg_arg != -1 )
2438 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002439 if( target_alg2_arg != -1 )
2440 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002441
Archana8a180362021-07-05 02:18:48 +05302442
Gilles Peskine57ab7212019-01-28 13:03:09 +01002443 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002444 PSA_ASSERT( psa_copy_key( source_key,
2445 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002446
2447 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002448 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002449
2450 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002451 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002452 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2453 psa_get_key_type( &target_attributes ) );
2454 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2455 psa_get_key_bits( &target_attributes ) );
2456 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2457 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002458 TEST_EQUAL( expected_alg2,
2459 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002460 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2461 {
2462 size_t length;
2463 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002464 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002465 material->len, &length ) );
2466 ASSERT_COMPARE( material->x, material->len,
2467 export_buffer, length );
2468 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002469
Archana8a180362021-07-05 02:18:48 +05302470 if( !psa_key_lifetime_is_external( target_lifetime ) )
2471 {
2472 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2473 goto exit;
2474 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2475 goto exit;
2476 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002477
Ronald Cron5425a212020-08-04 14:58:35 +02002478 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002479
2480exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002481 /*
2482 * Source and target key attributes may have been returned by
2483 * psa_get_key_attributes() thus reset them as required.
2484 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002485 psa_reset_key_attributes( &source_attributes );
2486 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002487
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002488 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002489 mbedtls_free( export_buffer );
2490}
2491/* END_CASE */
2492
2493/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002494void copy_fail( int source_usage_arg,
2495 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302496 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002497 int type_arg, data_t *material,
2498 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002499 int target_usage_arg,
2500 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002501 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002502 int expected_status_arg )
2503{
2504 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2505 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002506 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2507 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002508 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002509
2510 PSA_ASSERT( psa_crypto_init( ) );
2511
2512 /* Prepare the source key. */
2513 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2514 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002515 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002516 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302517 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002518 PSA_ASSERT( psa_import_key( &source_attributes,
2519 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002520 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002521
2522 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002523 psa_set_key_id( &target_attributes, key_id );
2524 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002525 psa_set_key_type( &target_attributes, target_type_arg );
2526 psa_set_key_bits( &target_attributes, target_bits_arg );
2527 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2528 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002529 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002530
2531 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002532 TEST_EQUAL( psa_copy_key( source_key,
2533 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002534 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002535
Ronald Cron5425a212020-08-04 14:58:35 +02002536 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002537
Gilles Peskine4a644642019-05-03 17:14:08 +02002538exit:
2539 psa_reset_key_attributes( &source_attributes );
2540 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002541 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002542}
2543/* END_CASE */
2544
2545/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002546void hash_operation_init( )
2547{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002548 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002549 /* Test each valid way of initializing the object, except for `= {0}`, as
2550 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2551 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002552 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002553 psa_hash_operation_t func = psa_hash_operation_init( );
2554 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2555 psa_hash_operation_t zero;
2556
2557 memset( &zero, 0, sizeof( zero ) );
2558
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002559 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002560 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2561 PSA_ERROR_BAD_STATE );
2562 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2563 PSA_ERROR_BAD_STATE );
2564 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2565 PSA_ERROR_BAD_STATE );
2566
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002567 /* A default hash operation should be abortable without error. */
2568 PSA_ASSERT( psa_hash_abort( &func ) );
2569 PSA_ASSERT( psa_hash_abort( &init ) );
2570 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002571}
2572/* END_CASE */
2573
2574/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002575void hash_setup( int alg_arg,
2576 int expected_status_arg )
2577{
2578 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002579 uint8_t *output = NULL;
2580 size_t output_size = 0;
2581 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002582 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002583 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002584 psa_status_t status;
2585
Gilles Peskine8817f612018-12-18 00:18:46 +01002586 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002587
Neil Armstrongedb20862022-02-07 15:47:44 +01002588 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002589 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002590 ASSERT_ALLOC( output, output_size );
2591
2592 status = psa_hash_compute( alg, NULL, 0,
2593 output, output_size, &output_length );
2594 TEST_EQUAL( status, expected_status );
2595
2596 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002597 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002598 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002599
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002600 /* Whether setup succeeded or failed, abort must succeed. */
2601 PSA_ASSERT( psa_hash_abort( &operation ) );
2602
2603 /* If setup failed, reproduce the failure, so as to
2604 * test the resulting state of the operation object. */
2605 if( status != PSA_SUCCESS )
2606 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2607
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002608 /* Now the operation object should be reusable. */
2609#if defined(KNOWN_SUPPORTED_HASH_ALG)
2610 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2611 PSA_ASSERT( psa_hash_abort( &operation ) );
2612#endif
2613
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002614exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002615 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002616 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002617}
2618/* END_CASE */
2619
2620/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002621void hash_compute_fail( int alg_arg, data_t *input,
2622 int output_size_arg, int expected_status_arg )
2623{
2624 psa_algorithm_t alg = alg_arg;
2625 uint8_t *output = NULL;
2626 size_t output_size = output_size_arg;
2627 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002628 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002629 psa_status_t expected_status = expected_status_arg;
2630 psa_status_t status;
2631
2632 ASSERT_ALLOC( output, output_size );
2633
2634 PSA_ASSERT( psa_crypto_init( ) );
2635
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002636 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002637 status = psa_hash_compute( alg, input->x, input->len,
2638 output, output_size, &output_length );
2639 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002640 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002641
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002642 /* Hash Compute, multi-part */
2643 status = psa_hash_setup( &operation, alg );
2644 if( status == PSA_SUCCESS )
2645 {
2646 status = psa_hash_update( &operation, input->x, input->len );
2647 if( status == PSA_SUCCESS )
2648 {
2649 status = psa_hash_finish( &operation, output, output_size,
2650 &output_length );
2651 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002652 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002653 else
2654 TEST_EQUAL( status, expected_status );
2655 }
2656 else
2657 {
2658 TEST_EQUAL( status, expected_status );
2659 }
2660 }
2661 else
2662 {
2663 TEST_EQUAL( status, expected_status );
2664 }
2665
Gilles Peskine0a749c82019-11-28 19:33:58 +01002666exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002667 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002668 mbedtls_free( output );
2669 PSA_DONE( );
2670}
2671/* END_CASE */
2672
2673/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002674void hash_compare_fail( int alg_arg, data_t *input,
2675 data_t *reference_hash,
2676 int expected_status_arg )
2677{
2678 psa_algorithm_t alg = alg_arg;
2679 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002680 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002681 psa_status_t status;
2682
2683 PSA_ASSERT( psa_crypto_init( ) );
2684
Neil Armstrong55a1be12022-02-07 11:23:20 +01002685 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002686 status = psa_hash_compare( alg, input->x, input->len,
2687 reference_hash->x, reference_hash->len );
2688 TEST_EQUAL( status, expected_status );
2689
Neil Armstrong55a1be12022-02-07 11:23:20 +01002690 /* Hash Compare, multi-part */
2691 status = psa_hash_setup( &operation, alg );
2692 if( status == PSA_SUCCESS )
2693 {
2694 status = psa_hash_update( &operation, input->x, input->len );
2695 if( status == PSA_SUCCESS )
2696 {
2697 status = psa_hash_verify( &operation, reference_hash->x,
2698 reference_hash->len );
2699 TEST_EQUAL( status, expected_status );
2700 }
2701 else
2702 {
2703 TEST_EQUAL( status, expected_status );
2704 }
2705 }
2706 else
2707 {
2708 TEST_EQUAL( status, expected_status );
2709 }
2710
Gilles Peskine88e08462020-01-28 20:43:00 +01002711exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002712 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002713 PSA_DONE( );
2714}
2715/* END_CASE */
2716
2717/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002718void hash_compute_compare( int alg_arg, data_t *input,
2719 data_t *expected_output )
2720{
2721 psa_algorithm_t alg = alg_arg;
2722 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2723 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002724 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002725 size_t i;
2726
2727 PSA_ASSERT( psa_crypto_init( ) );
2728
Neil Armstrongca30a002022-02-07 11:40:23 +01002729 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002730 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002731 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002732 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002733 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002734 ASSERT_COMPARE( output, output_length,
2735 expected_output->x, expected_output->len );
2736
Neil Armstrongca30a002022-02-07 11:40:23 +01002737 /* Compute with tight buffer, multi-part */
2738 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2739 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2740 PSA_ASSERT( psa_hash_finish( &operation, output,
2741 PSA_HASH_LENGTH( alg ),
2742 &output_length ) );
2743 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2744 ASSERT_COMPARE( output, output_length,
2745 expected_output->x, expected_output->len );
2746
2747 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002748 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2749 output, sizeof( output ),
2750 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002751 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002752 ASSERT_COMPARE( output, output_length,
2753 expected_output->x, expected_output->len );
2754
Neil Armstrongca30a002022-02-07 11:40:23 +01002755 /* Compute with larger buffer, multi-part */
2756 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2757 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2758 PSA_ASSERT( psa_hash_finish( &operation, output,
2759 sizeof( output ), &output_length ) );
2760 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2761 ASSERT_COMPARE( output, output_length,
2762 expected_output->x, expected_output->len );
2763
2764 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002765 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2766 output, output_length ) );
2767
Neil Armstrongca30a002022-02-07 11:40:23 +01002768 /* Compare with correct hash, multi-part */
2769 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2770 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2771 PSA_ASSERT( psa_hash_verify( &operation, output,
2772 output_length ) );
2773
2774 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002775 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2776 output, output_length + 1 ),
2777 PSA_ERROR_INVALID_SIGNATURE );
2778
Neil Armstrongca30a002022-02-07 11:40:23 +01002779 /* Compare with trailing garbage, multi-part */
2780 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2781 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2782 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2783 PSA_ERROR_INVALID_SIGNATURE );
2784
2785 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002786 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2787 output, output_length - 1 ),
2788 PSA_ERROR_INVALID_SIGNATURE );
2789
Neil Armstrongca30a002022-02-07 11:40:23 +01002790 /* Compare with truncated hash, multi-part */
2791 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2792 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2793 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2794 PSA_ERROR_INVALID_SIGNATURE );
2795
Gilles Peskine0a749c82019-11-28 19:33:58 +01002796 /* Compare with corrupted value */
2797 for( i = 0; i < output_length; i++ )
2798 {
Chris Jones9634bb12021-01-20 15:56:42 +00002799 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002800 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002801
2802 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002803 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2804 output, output_length ),
2805 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002806
2807 /* Multi-Part */
2808 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2809 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2810 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2811 PSA_ERROR_INVALID_SIGNATURE );
2812
Gilles Peskine0a749c82019-11-28 19:33:58 +01002813 output[i] ^= 1;
2814 }
2815
2816exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002817 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002818 PSA_DONE( );
2819}
2820/* END_CASE */
2821
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002822/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002823void hash_bad_order( )
2824{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002825 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002826 unsigned char input[] = "";
2827 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002828 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002829 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2830 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2831 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002832 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002833 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002834 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002837
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002838 /* Call setup twice in a row. */
2839 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002840 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002841 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2842 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002843 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002844 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002845 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002846
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002847 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002848 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002849 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002850 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002851
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002852 /* Check that update calls abort on error. */
2853 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002854 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002855 ASSERT_OPERATION_IS_ACTIVE( operation );
2856 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2857 PSA_ERROR_BAD_STATE );
2858 ASSERT_OPERATION_IS_INACTIVE( operation );
2859 PSA_ASSERT( psa_hash_abort( &operation ) );
2860 ASSERT_OPERATION_IS_INACTIVE( operation );
2861
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002862 /* Call update after finish. */
2863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2864 PSA_ASSERT( psa_hash_finish( &operation,
2865 hash, sizeof( hash ), &hash_len ) );
2866 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002867 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002868 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002869
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002870 /* Call verify without calling setup beforehand. */
2871 TEST_EQUAL( psa_hash_verify( &operation,
2872 valid_hash, sizeof( valid_hash ) ),
2873 PSA_ERROR_BAD_STATE );
2874 PSA_ASSERT( psa_hash_abort( &operation ) );
2875
2876 /* Call verify after finish. */
2877 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2878 PSA_ASSERT( psa_hash_finish( &operation,
2879 hash, sizeof( hash ), &hash_len ) );
2880 TEST_EQUAL( psa_hash_verify( &operation,
2881 valid_hash, sizeof( valid_hash ) ),
2882 PSA_ERROR_BAD_STATE );
2883 PSA_ASSERT( psa_hash_abort( &operation ) );
2884
2885 /* Call verify twice in a row. */
2886 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002887 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002888 PSA_ASSERT( psa_hash_verify( &operation,
2889 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002890 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002891 TEST_EQUAL( psa_hash_verify( &operation,
2892 valid_hash, sizeof( valid_hash ) ),
2893 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002894 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002895 PSA_ASSERT( psa_hash_abort( &operation ) );
2896
2897 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002898 TEST_EQUAL( psa_hash_finish( &operation,
2899 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002900 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002901 PSA_ASSERT( psa_hash_abort( &operation ) );
2902
2903 /* Call finish twice in a row. */
2904 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2905 PSA_ASSERT( psa_hash_finish( &operation,
2906 hash, sizeof( hash ), &hash_len ) );
2907 TEST_EQUAL( psa_hash_finish( &operation,
2908 hash, sizeof( hash ), &hash_len ),
2909 PSA_ERROR_BAD_STATE );
2910 PSA_ASSERT( psa_hash_abort( &operation ) );
2911
2912 /* Call finish after calling verify. */
2913 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2914 PSA_ASSERT( psa_hash_verify( &operation,
2915 valid_hash, sizeof( valid_hash ) ) );
2916 TEST_EQUAL( psa_hash_finish( &operation,
2917 hash, sizeof( hash ), &hash_len ),
2918 PSA_ERROR_BAD_STATE );
2919 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002920
2921exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002922 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002923}
2924/* END_CASE */
2925
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002926/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002927void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002928{
2929 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002930 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2931 * appended to it */
2932 unsigned char hash[] = {
2933 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2934 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2935 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002936 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002937 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002938
Gilles Peskine8817f612018-12-18 00:18:46 +01002939 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002940
itayzafrir27e69452018-11-01 14:26:34 +02002941 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002942 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002943 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002944 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002945 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002946 ASSERT_OPERATION_IS_INACTIVE( operation );
2947 PSA_ASSERT( psa_hash_abort( &operation ) );
2948 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002949
itayzafrir27e69452018-11-01 14:26:34 +02002950 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002951 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002952 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002953 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002954
itayzafrir27e69452018-11-01 14:26:34 +02002955 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002957 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002958 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002959
itayzafrirec93d302018-10-18 18:01:10 +03002960exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002961 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002962}
2963/* END_CASE */
2964
Ronald Cronee414c72021-03-18 18:50:08 +01002965/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002966void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002967{
2968 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002969 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002970 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002971 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002972 size_t hash_len;
2973
Gilles Peskine8817f612018-12-18 00:18:46 +01002974 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002975
itayzafrir58028322018-10-25 10:22:01 +03002976 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002977 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002978 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002979 hash, expected_size - 1, &hash_len ),
2980 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002981
2982exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002983 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002984}
2985/* END_CASE */
2986
Ronald Cronee414c72021-03-18 18:50:08 +01002987/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002988void hash_clone_source_state( )
2989{
2990 psa_algorithm_t alg = PSA_ALG_SHA_256;
2991 unsigned char hash[PSA_HASH_MAX_SIZE];
2992 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2993 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2994 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2995 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2996 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2997 size_t hash_len;
2998
2999 PSA_ASSERT( psa_crypto_init( ) );
3000 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3001
3002 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3003 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3004 PSA_ASSERT( psa_hash_finish( &op_finished,
3005 hash, sizeof( hash ), &hash_len ) );
3006 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3007 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3008
3009 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3010 PSA_ERROR_BAD_STATE );
3011
3012 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3013 PSA_ASSERT( psa_hash_finish( &op_init,
3014 hash, sizeof( hash ), &hash_len ) );
3015 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3016 PSA_ASSERT( psa_hash_finish( &op_finished,
3017 hash, sizeof( hash ), &hash_len ) );
3018 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3019 PSA_ASSERT( psa_hash_finish( &op_aborted,
3020 hash, sizeof( hash ), &hash_len ) );
3021
3022exit:
3023 psa_hash_abort( &op_source );
3024 psa_hash_abort( &op_init );
3025 psa_hash_abort( &op_setup );
3026 psa_hash_abort( &op_finished );
3027 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003028 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003029}
3030/* END_CASE */
3031
Ronald Cronee414c72021-03-18 18:50:08 +01003032/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003033void hash_clone_target_state( )
3034{
3035 psa_algorithm_t alg = PSA_ALG_SHA_256;
3036 unsigned char hash[PSA_HASH_MAX_SIZE];
3037 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3038 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3039 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3040 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3041 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3042 size_t hash_len;
3043
3044 PSA_ASSERT( psa_crypto_init( ) );
3045
3046 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3047 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3048 PSA_ASSERT( psa_hash_finish( &op_finished,
3049 hash, sizeof( hash ), &hash_len ) );
3050 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3051 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3052
3053 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3054 PSA_ASSERT( psa_hash_finish( &op_target,
3055 hash, sizeof( hash ), &hash_len ) );
3056
3057 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3058 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3059 PSA_ERROR_BAD_STATE );
3060 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3061 PSA_ERROR_BAD_STATE );
3062
3063exit:
3064 psa_hash_abort( &op_target );
3065 psa_hash_abort( &op_init );
3066 psa_hash_abort( &op_setup );
3067 psa_hash_abort( &op_finished );
3068 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003069 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003070}
3071/* END_CASE */
3072
itayzafrir58028322018-10-25 10:22:01 +03003073/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003074void mac_operation_init( )
3075{
Jaeden Amero252ef282019-02-15 14:05:35 +00003076 const uint8_t input[1] = { 0 };
3077
Jaeden Amero769ce272019-01-04 11:48:03 +00003078 /* Test each valid way of initializing the object, except for `= {0}`, as
3079 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3080 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003081 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003082 psa_mac_operation_t func = psa_mac_operation_init( );
3083 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3084 psa_mac_operation_t zero;
3085
3086 memset( &zero, 0, sizeof( zero ) );
3087
Jaeden Amero252ef282019-02-15 14:05:35 +00003088 /* A freshly-initialized MAC operation should not be usable. */
3089 TEST_EQUAL( psa_mac_update( &func,
3090 input, sizeof( input ) ),
3091 PSA_ERROR_BAD_STATE );
3092 TEST_EQUAL( psa_mac_update( &init,
3093 input, sizeof( input ) ),
3094 PSA_ERROR_BAD_STATE );
3095 TEST_EQUAL( psa_mac_update( &zero,
3096 input, sizeof( input ) ),
3097 PSA_ERROR_BAD_STATE );
3098
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003099 /* A default MAC operation should be abortable without error. */
3100 PSA_ASSERT( psa_mac_abort( &func ) );
3101 PSA_ASSERT( psa_mac_abort( &init ) );
3102 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003103}
3104/* END_CASE */
3105
3106/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003107void mac_setup( int key_type_arg,
3108 data_t *key,
3109 int alg_arg,
3110 int expected_status_arg )
3111{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003112 psa_key_type_t key_type = key_type_arg;
3113 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003114 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003115 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003116 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3117#if defined(KNOWN_SUPPORTED_MAC_ALG)
3118 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3119#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003120
Gilles Peskine8817f612018-12-18 00:18:46 +01003121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003122
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003123 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3124 &operation, &status ) )
3125 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003126 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003127
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003128 /* The operation object should be reusable. */
3129#if defined(KNOWN_SUPPORTED_MAC_ALG)
3130 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3131 smoke_test_key_data,
3132 sizeof( smoke_test_key_data ),
3133 KNOWN_SUPPORTED_MAC_ALG,
3134 &operation, &status ) )
3135 goto exit;
3136 TEST_EQUAL( status, PSA_SUCCESS );
3137#endif
3138
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003139exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003140 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003141}
3142/* END_CASE */
3143
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003144/* 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 +00003145void mac_bad_order( )
3146{
Ronald Cron5425a212020-08-04 14:58:35 +02003147 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003148 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3149 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003150 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003151 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3152 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3153 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003154 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003155 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3156 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3157 size_t sign_mac_length = 0;
3158 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3159 const uint8_t verify_mac[] = {
3160 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3161 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3162 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3163
3164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003165 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003166 psa_set_key_algorithm( &attributes, alg );
3167 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003168
Ronald Cron5425a212020-08-04 14:58:35 +02003169 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3170 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003171
Jaeden Amero252ef282019-02-15 14:05:35 +00003172 /* Call update without calling setup beforehand. */
3173 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3174 PSA_ERROR_BAD_STATE );
3175 PSA_ASSERT( psa_mac_abort( &operation ) );
3176
3177 /* Call sign finish without calling setup beforehand. */
3178 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3179 &sign_mac_length),
3180 PSA_ERROR_BAD_STATE );
3181 PSA_ASSERT( psa_mac_abort( &operation ) );
3182
3183 /* Call verify finish without calling setup beforehand. */
3184 TEST_EQUAL( psa_mac_verify_finish( &operation,
3185 verify_mac, sizeof( verify_mac ) ),
3186 PSA_ERROR_BAD_STATE );
3187 PSA_ASSERT( psa_mac_abort( &operation ) );
3188
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003189 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003190 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003191 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003192 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003193 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003194 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003195 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003196 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003197
Jaeden Amero252ef282019-02-15 14:05:35 +00003198 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003199 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003200 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3201 PSA_ASSERT( psa_mac_sign_finish( &operation,
3202 sign_mac, sizeof( sign_mac ),
3203 &sign_mac_length ) );
3204 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3205 PSA_ERROR_BAD_STATE );
3206 PSA_ASSERT( psa_mac_abort( &operation ) );
3207
3208 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003209 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003210 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3211 PSA_ASSERT( psa_mac_verify_finish( &operation,
3212 verify_mac, sizeof( verify_mac ) ) );
3213 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3214 PSA_ERROR_BAD_STATE );
3215 PSA_ASSERT( psa_mac_abort( &operation ) );
3216
3217 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003218 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003219 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3220 PSA_ASSERT( psa_mac_sign_finish( &operation,
3221 sign_mac, sizeof( sign_mac ),
3222 &sign_mac_length ) );
3223 TEST_EQUAL( psa_mac_sign_finish( &operation,
3224 sign_mac, sizeof( sign_mac ),
3225 &sign_mac_length ),
3226 PSA_ERROR_BAD_STATE );
3227 PSA_ASSERT( psa_mac_abort( &operation ) );
3228
3229 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003230 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003231 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3232 PSA_ASSERT( psa_mac_verify_finish( &operation,
3233 verify_mac, sizeof( verify_mac ) ) );
3234 TEST_EQUAL( psa_mac_verify_finish( &operation,
3235 verify_mac, sizeof( verify_mac ) ),
3236 PSA_ERROR_BAD_STATE );
3237 PSA_ASSERT( psa_mac_abort( &operation ) );
3238
3239 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003240 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003241 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003242 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003243 TEST_EQUAL( psa_mac_verify_finish( &operation,
3244 verify_mac, sizeof( verify_mac ) ),
3245 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003246 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003247 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003248 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003249
3250 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003251 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003252 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003253 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003254 TEST_EQUAL( psa_mac_sign_finish( &operation,
3255 sign_mac, sizeof( sign_mac ),
3256 &sign_mac_length ),
3257 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003258 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003259 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003260 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003261
Ronald Cron5425a212020-08-04 14:58:35 +02003262 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003263
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003264exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003265 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003266}
3267/* END_CASE */
3268
3269/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003270void mac_sign_verify_multi( int key_type_arg,
3271 data_t *key_data,
3272 int alg_arg,
3273 data_t *input,
3274 int is_verify,
3275 data_t *expected_mac )
3276{
3277 size_t data_part_len = 0;
3278
3279 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3280 {
3281 /* Split data into length(data_part_len) parts. */
3282 mbedtls_test_set_step( 2000 + data_part_len );
3283
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003284 if( mac_multipart_internal_func( key_type_arg, key_data,
3285 alg_arg,
3286 input, data_part_len,
3287 expected_mac,
3288 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003289 break;
3290
3291 /* length(0) part, length(data_part_len) part, length(0) part... */
3292 mbedtls_test_set_step( 3000 + data_part_len );
3293
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003294 if( mac_multipart_internal_func( key_type_arg, key_data,
3295 alg_arg,
3296 input, data_part_len,
3297 expected_mac,
3298 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003299 break;
3300 }
3301
3302 /* Goto is required to silence warnings about unused labels, as we
3303 * don't actually do any test assertions in this function. */
3304 goto exit;
3305}
3306/* END_CASE */
3307
3308/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003309void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003310 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003311 int alg_arg,
3312 data_t *input,
3313 data_t *expected_mac )
3314{
Ronald Cron5425a212020-08-04 14:58:35 +02003315 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003316 psa_key_type_t key_type = key_type_arg;
3317 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003318 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003320 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003321 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003322 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003323 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003324 const size_t output_sizes_to_test[] = {
3325 0,
3326 1,
3327 expected_mac->len - 1,
3328 expected_mac->len,
3329 expected_mac->len + 1,
3330 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003331
Gilles Peskine7be11a72022-04-14 00:12:57 +02003332 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003333 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003334 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003335
Gilles Peskine8817f612018-12-18 00:18:46 +01003336 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003337
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003338 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003339 psa_set_key_algorithm( &attributes, alg );
3340 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003341
Ronald Cron5425a212020-08-04 14:58:35 +02003342 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3343 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003344
Gilles Peskine8b356b52020-08-25 23:44:59 +02003345 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3346 {
3347 const size_t output_size = output_sizes_to_test[i];
3348 psa_status_t expected_status =
3349 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3350 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003351
Chris Jones9634bb12021-01-20 15:56:42 +00003352 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003353 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003354
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003355 /* Calculate the MAC, one-shot case. */
3356 TEST_EQUAL( psa_mac_compute( key, alg,
3357 input->x, input->len,
3358 actual_mac, output_size, &mac_length ),
3359 expected_status );
3360 if( expected_status == PSA_SUCCESS )
3361 {
3362 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3363 actual_mac, mac_length );
3364 }
3365
3366 if( output_size > 0 )
3367 memset( actual_mac, 0, output_size );
3368
3369 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003370 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003371 PSA_ASSERT( psa_mac_update( &operation,
3372 input->x, input->len ) );
3373 TEST_EQUAL( psa_mac_sign_finish( &operation,
3374 actual_mac, output_size,
3375 &mac_length ),
3376 expected_status );
3377 PSA_ASSERT( psa_mac_abort( &operation ) );
3378
3379 if( expected_status == PSA_SUCCESS )
3380 {
3381 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3382 actual_mac, mac_length );
3383 }
3384 mbedtls_free( actual_mac );
3385 actual_mac = NULL;
3386 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003387
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003388exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003389 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003390 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003391 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003392 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003393}
3394/* END_CASE */
3395
3396/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003397void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003398 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003399 int alg_arg,
3400 data_t *input,
3401 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003402{
Ronald Cron5425a212020-08-04 14:58:35 +02003403 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003404 psa_key_type_t key_type = key_type_arg;
3405 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003406 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003408 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003409
Gilles Peskine7be11a72022-04-14 00:12:57 +02003410 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003411
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003413
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003414 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003415 psa_set_key_algorithm( &attributes, alg );
3416 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003417
Ronald Cron5425a212020-08-04 14:58:35 +02003418 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3419 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003420
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003421 /* Verify correct MAC, one-shot case. */
3422 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3423 expected_mac->x, expected_mac->len ) );
3424
3425 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003426 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003427 PSA_ASSERT( psa_mac_update( &operation,
3428 input->x, input->len ) );
3429 PSA_ASSERT( psa_mac_verify_finish( &operation,
3430 expected_mac->x,
3431 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003432
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003433 /* Test a MAC that's too short, one-shot case. */
3434 TEST_EQUAL( psa_mac_verify( key, alg,
3435 input->x, input->len,
3436 expected_mac->x,
3437 expected_mac->len - 1 ),
3438 PSA_ERROR_INVALID_SIGNATURE );
3439
3440 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003441 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003442 PSA_ASSERT( psa_mac_update( &operation,
3443 input->x, input->len ) );
3444 TEST_EQUAL( psa_mac_verify_finish( &operation,
3445 expected_mac->x,
3446 expected_mac->len - 1 ),
3447 PSA_ERROR_INVALID_SIGNATURE );
3448
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003449 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003450 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3451 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003452 TEST_EQUAL( psa_mac_verify( key, alg,
3453 input->x, input->len,
3454 perturbed_mac, expected_mac->len + 1 ),
3455 PSA_ERROR_INVALID_SIGNATURE );
3456
3457 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003458 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003459 PSA_ASSERT( psa_mac_update( &operation,
3460 input->x, input->len ) );
3461 TEST_EQUAL( psa_mac_verify_finish( &operation,
3462 perturbed_mac,
3463 expected_mac->len + 1 ),
3464 PSA_ERROR_INVALID_SIGNATURE );
3465
3466 /* Test changing one byte. */
3467 for( size_t i = 0; i < expected_mac->len; i++ )
3468 {
Chris Jones9634bb12021-01-20 15:56:42 +00003469 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003470 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003471
3472 TEST_EQUAL( psa_mac_verify( key, alg,
3473 input->x, input->len,
3474 perturbed_mac, expected_mac->len ),
3475 PSA_ERROR_INVALID_SIGNATURE );
3476
Ronald Cron5425a212020-08-04 14:58:35 +02003477 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003478 PSA_ASSERT( psa_mac_update( &operation,
3479 input->x, input->len ) );
3480 TEST_EQUAL( psa_mac_verify_finish( &operation,
3481 perturbed_mac,
3482 expected_mac->len ),
3483 PSA_ERROR_INVALID_SIGNATURE );
3484 perturbed_mac[i] ^= 1;
3485 }
3486
Gilles Peskine8c9def32018-02-08 10:02:12 +01003487exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003488 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003489 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003490 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003491 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003492}
3493/* END_CASE */
3494
3495/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003496void cipher_operation_init( )
3497{
Jaeden Ameroab439972019-02-15 14:12:05 +00003498 const uint8_t input[1] = { 0 };
3499 unsigned char output[1] = { 0 };
3500 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003501 /* Test each valid way of initializing the object, except for `= {0}`, as
3502 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3503 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003504 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003505 psa_cipher_operation_t func = psa_cipher_operation_init( );
3506 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3507 psa_cipher_operation_t zero;
3508
3509 memset( &zero, 0, sizeof( zero ) );
3510
Jaeden Ameroab439972019-02-15 14:12:05 +00003511 /* A freshly-initialized cipher operation should not be usable. */
3512 TEST_EQUAL( psa_cipher_update( &func,
3513 input, sizeof( input ),
3514 output, sizeof( output ),
3515 &output_length ),
3516 PSA_ERROR_BAD_STATE );
3517 TEST_EQUAL( psa_cipher_update( &init,
3518 input, sizeof( input ),
3519 output, sizeof( output ),
3520 &output_length ),
3521 PSA_ERROR_BAD_STATE );
3522 TEST_EQUAL( psa_cipher_update( &zero,
3523 input, sizeof( input ),
3524 output, sizeof( output ),
3525 &output_length ),
3526 PSA_ERROR_BAD_STATE );
3527
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003528 /* A default cipher operation should be abortable without error. */
3529 PSA_ASSERT( psa_cipher_abort( &func ) );
3530 PSA_ASSERT( psa_cipher_abort( &init ) );
3531 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003532}
3533/* END_CASE */
3534
3535/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003536void cipher_setup( int key_type_arg,
3537 data_t *key,
3538 int alg_arg,
3539 int expected_status_arg )
3540{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003541 psa_key_type_t key_type = key_type_arg;
3542 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003543 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003544 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003545 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003546#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003547 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3548#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003549
Gilles Peskine8817f612018-12-18 00:18:46 +01003550 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003551
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003552 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3553 &operation, &status ) )
3554 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003555 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003556
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003557 /* The operation object should be reusable. */
3558#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3559 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3560 smoke_test_key_data,
3561 sizeof( smoke_test_key_data ),
3562 KNOWN_SUPPORTED_CIPHER_ALG,
3563 &operation, &status ) )
3564 goto exit;
3565 TEST_EQUAL( status, PSA_SUCCESS );
3566#endif
3567
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003568exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003569 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003570 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003571}
3572/* END_CASE */
3573
Ronald Cronee414c72021-03-18 18:50:08 +01003574/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003575void cipher_bad_order( )
3576{
Ronald Cron5425a212020-08-04 14:58:35 +02003577 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003578 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3579 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003580 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003581 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003582 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003583 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003584 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3585 0xaa, 0xaa, 0xaa, 0xaa };
3586 const uint8_t text[] = {
3587 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3588 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003589 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003590 size_t length = 0;
3591
3592 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003593 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3594 psa_set_key_algorithm( &attributes, alg );
3595 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003596 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3597 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003598
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003599 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003600 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003601 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003602 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003603 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003604 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003605 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003606 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003607
3608 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003609 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003610 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003611 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003612 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003613 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003614 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003615 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003616
Jaeden Ameroab439972019-02-15 14:12:05 +00003617 /* Generate an IV without calling setup beforehand. */
3618 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3619 buffer, sizeof( buffer ),
3620 &length ),
3621 PSA_ERROR_BAD_STATE );
3622 PSA_ASSERT( psa_cipher_abort( &operation ) );
3623
3624 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003625 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003626 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3627 buffer, sizeof( buffer ),
3628 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003629 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003630 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3631 buffer, sizeof( buffer ),
3632 &length ),
3633 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003634 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003635 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003636 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003637
3638 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003639 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003640 PSA_ASSERT( psa_cipher_set_iv( &operation,
3641 iv, sizeof( iv ) ) );
3642 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3643 buffer, sizeof( buffer ),
3644 &length ),
3645 PSA_ERROR_BAD_STATE );
3646 PSA_ASSERT( psa_cipher_abort( &operation ) );
3647
3648 /* Set an IV without calling setup beforehand. */
3649 TEST_EQUAL( psa_cipher_set_iv( &operation,
3650 iv, sizeof( iv ) ),
3651 PSA_ERROR_BAD_STATE );
3652 PSA_ASSERT( psa_cipher_abort( &operation ) );
3653
3654 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003655 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003656 PSA_ASSERT( psa_cipher_set_iv( &operation,
3657 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003658 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003659 TEST_EQUAL( psa_cipher_set_iv( &operation,
3660 iv, sizeof( iv ) ),
3661 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003662 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003663 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003664 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003665
3666 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003667 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003668 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3669 buffer, sizeof( buffer ),
3670 &length ) );
3671 TEST_EQUAL( psa_cipher_set_iv( &operation,
3672 iv, sizeof( iv ) ),
3673 PSA_ERROR_BAD_STATE );
3674 PSA_ASSERT( psa_cipher_abort( &operation ) );
3675
3676 /* Call update without calling setup beforehand. */
3677 TEST_EQUAL( psa_cipher_update( &operation,
3678 text, sizeof( text ),
3679 buffer, sizeof( buffer ),
3680 &length ),
3681 PSA_ERROR_BAD_STATE );
3682 PSA_ASSERT( psa_cipher_abort( &operation ) );
3683
3684 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003685 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003686 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003687 TEST_EQUAL( psa_cipher_update( &operation,
3688 text, sizeof( text ),
3689 buffer, sizeof( buffer ),
3690 &length ),
3691 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003692 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003693 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003694 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003695
3696 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003697 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003698 PSA_ASSERT( psa_cipher_set_iv( &operation,
3699 iv, sizeof( iv ) ) );
3700 PSA_ASSERT( psa_cipher_finish( &operation,
3701 buffer, sizeof( buffer ), &length ) );
3702 TEST_EQUAL( psa_cipher_update( &operation,
3703 text, sizeof( text ),
3704 buffer, sizeof( buffer ),
3705 &length ),
3706 PSA_ERROR_BAD_STATE );
3707 PSA_ASSERT( psa_cipher_abort( &operation ) );
3708
3709 /* Call finish without calling setup beforehand. */
3710 TEST_EQUAL( psa_cipher_finish( &operation,
3711 buffer, sizeof( buffer ), &length ),
3712 PSA_ERROR_BAD_STATE );
3713 PSA_ASSERT( psa_cipher_abort( &operation ) );
3714
3715 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003716 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003717 /* Not calling update means we are encrypting an empty buffer, which is OK
3718 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003719 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003720 TEST_EQUAL( psa_cipher_finish( &operation,
3721 buffer, sizeof( buffer ), &length ),
3722 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003723 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003724 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003725 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003726
3727 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003728 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003729 PSA_ASSERT( psa_cipher_set_iv( &operation,
3730 iv, sizeof( iv ) ) );
3731 PSA_ASSERT( psa_cipher_finish( &operation,
3732 buffer, sizeof( buffer ), &length ) );
3733 TEST_EQUAL( psa_cipher_finish( &operation,
3734 buffer, sizeof( buffer ), &length ),
3735 PSA_ERROR_BAD_STATE );
3736 PSA_ASSERT( psa_cipher_abort( &operation ) );
3737
Ronald Cron5425a212020-08-04 14:58:35 +02003738 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003739
Jaeden Ameroab439972019-02-15 14:12:05 +00003740exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003741 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003742 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003743}
3744/* END_CASE */
3745
3746/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003747void cipher_encrypt_fail( int alg_arg,
3748 int key_type_arg,
3749 data_t *key_data,
3750 data_t *input,
3751 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003752{
Ronald Cron5425a212020-08-04 14:58:35 +02003753 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003754 psa_status_t status;
3755 psa_key_type_t key_type = key_type_arg;
3756 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003757 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003758 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3759 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3760 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003761 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003762 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003763 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003764 size_t function_output_length;
3765 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3767
3768 if ( PSA_ERROR_BAD_STATE != expected_status )
3769 {
3770 PSA_ASSERT( psa_crypto_init( ) );
3771
3772 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3773 psa_set_key_algorithm( &attributes, alg );
3774 psa_set_key_type( &attributes, key_type );
3775
3776 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3777 input->len );
3778 ASSERT_ALLOC( output, output_buffer_size );
3779
3780 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3781 &key ) );
3782 }
3783
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003784 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003785 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3786 output_buffer_size, &output_length );
3787
3788 TEST_EQUAL( status, expected_status );
3789
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003790 /* Encrypt, multi-part */
3791 status = psa_cipher_encrypt_setup( &operation, key, alg );
3792 if( status == PSA_SUCCESS )
3793 {
3794 if( alg != PSA_ALG_ECB_NO_PADDING )
3795 {
3796 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3797 iv, iv_size,
3798 &iv_length ) );
3799 }
3800
3801 status = psa_cipher_update( &operation, input->x, input->len,
3802 output, output_buffer_size,
3803 &function_output_length );
3804 if( status == PSA_SUCCESS )
3805 {
3806 output_length += function_output_length;
3807
3808 status = psa_cipher_finish( &operation, output + output_length,
3809 output_buffer_size - output_length,
3810 &function_output_length );
3811
3812 TEST_EQUAL( status, expected_status );
3813 }
3814 else
3815 {
3816 TEST_EQUAL( status, expected_status );
3817 }
3818 }
3819 else
3820 {
3821 TEST_EQUAL( status, expected_status );
3822 }
3823
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003824exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003825 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003826 mbedtls_free( output );
3827 psa_destroy_key( key );
3828 PSA_DONE( );
3829}
3830/* END_CASE */
3831
3832/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003833void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3834 data_t *input, int iv_length,
3835 int expected_result )
3836{
3837 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3838 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3840 size_t output_buffer_size = 0;
3841 unsigned char *output = NULL;
3842
3843 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3844 ASSERT_ALLOC( output, output_buffer_size );
3845
3846 PSA_ASSERT( psa_crypto_init( ) );
3847
3848 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3849 psa_set_key_algorithm( &attributes, alg );
3850 psa_set_key_type( &attributes, key_type );
3851
3852 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3853 &key ) );
3854 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3855 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3856 iv_length ) );
3857
3858exit:
3859 psa_cipher_abort( &operation );
3860 mbedtls_free( output );
3861 psa_destroy_key( key );
3862 PSA_DONE( );
3863}
3864/* END_CASE */
3865
3866/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003867void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3868 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003869{
3870 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3871 psa_key_type_t key_type = key_type_arg;
3872 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003873 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3874 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003875 unsigned char *output = NULL;
3876 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003877 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3879
3880 PSA_ASSERT( psa_crypto_init( ) );
3881
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003882 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003883 TEST_LE_U( ciphertext->len,
3884 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3885 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003886 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003887 TEST_LE_U( plaintext->len,
3888 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3889 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3890 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003891
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003892
3893 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003894 psa_set_key_usage_flags( &attributes,
3895 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003896 psa_set_key_algorithm( &attributes, alg );
3897 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003898 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3899 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003900 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3901 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003902 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003903
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003904 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003905 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3906 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3907 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003908 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3909 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003910 PSA_ERROR_BAD_STATE );
3911
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003912 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003913 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3914 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003915 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003916 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003917 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3918 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003919 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003920 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003921
Gilles Peskine286c3142022-04-20 17:09:38 +02003922 /* Multipart encryption */
3923 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3924 output_length = 0;
3925 length = ~0;
3926 PSA_ASSERT( psa_cipher_update( &operation,
3927 plaintext->x, plaintext->len,
3928 output, output_buffer_size,
3929 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003930 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003931 output_length += length;
3932 PSA_ASSERT( psa_cipher_finish( &operation,
3933 output + output_length,
3934 output_buffer_size - output_length,
3935 &length ) );
3936 output_length += length;
3937 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003938 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003939
Gilles Peskine286c3142022-04-20 17:09:38 +02003940 /* Multipart encryption */
3941 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3942 output_length = 0;
3943 length = ~0;
3944 PSA_ASSERT( psa_cipher_update( &operation,
3945 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003946 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003947 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003948 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003949 output_length += length;
3950 PSA_ASSERT( psa_cipher_finish( &operation,
3951 output + output_length,
3952 output_buffer_size - output_length,
3953 &length ) );
3954 output_length += length;
3955 ASSERT_COMPARE( plaintext->x, plaintext->len,
3956 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003957
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003958 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003959 output_length = ~0;
3960 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3961 output, output_buffer_size,
3962 &output_length ) );
3963 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3964 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003965
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003966 /* One-shot decryption */
3967 output_length = ~0;
3968 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
3969 output, output_buffer_size,
3970 &output_length ) );
3971 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003972 output, output_length );
3973
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003974exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003975 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003976 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003977 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003978 psa_destroy_key( key );
3979 PSA_DONE( );
3980}
3981/* END_CASE */
3982
3983/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003984void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3985{
3986 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3987 psa_algorithm_t alg = alg_arg;
3988 psa_key_type_t key_type = key_type_arg;
3989 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3990 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3991 psa_status_t status;
3992
3993 PSA_ASSERT( psa_crypto_init( ) );
3994
3995 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3996 psa_set_key_algorithm( &attributes, alg );
3997 psa_set_key_type( &attributes, key_type );
3998
3999 /* Usage of either of these two size macros would cause divide by zero
4000 * with incorrect key types previously. Input length should be irrelevant
4001 * here. */
4002 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4003 0 );
4004 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4005
4006
4007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4008 &key ) );
4009
4010 /* Should fail due to invalid alg type (to support invalid key type).
4011 * Encrypt or decrypt will end up in the same place. */
4012 status = psa_cipher_encrypt_setup( &operation, key, alg );
4013
4014 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4015
4016exit:
4017 psa_cipher_abort( &operation );
4018 psa_destroy_key( key );
4019 PSA_DONE( );
4020}
4021/* END_CASE */
4022
4023/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004024void cipher_encrypt_validation( int alg_arg,
4025 int key_type_arg,
4026 data_t *key_data,
4027 data_t *input )
4028{
4029 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4030 psa_key_type_t key_type = key_type_arg;
4031 psa_algorithm_t alg = alg_arg;
4032 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4033 unsigned char *output1 = NULL;
4034 size_t output1_buffer_size = 0;
4035 size_t output1_length = 0;
4036 unsigned char *output2 = NULL;
4037 size_t output2_buffer_size = 0;
4038 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004039 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004040 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004041 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004042
Gilles Peskine8817f612018-12-18 00:18:46 +01004043 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004044
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004045 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4046 psa_set_key_algorithm( &attributes, alg );
4047 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004048
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004049 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4050 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4051 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4052 ASSERT_ALLOC( output1, output1_buffer_size );
4053 ASSERT_ALLOC( output2, output2_buffer_size );
4054
Ronald Cron5425a212020-08-04 14:58:35 +02004055 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4056 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004057
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004058 /* The one-shot cipher encryption uses generated iv so validating
4059 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004060 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4061 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004062 TEST_LE_U( output1_length,
4063 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4064 TEST_LE_U( output1_length,
4065 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004066
4067 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4068 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004069
Gilles Peskine8817f612018-12-18 00:18:46 +01004070 PSA_ASSERT( psa_cipher_update( &operation,
4071 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004072 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004073 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004074 TEST_LE_U( function_output_length,
4075 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4076 TEST_LE_U( function_output_length,
4077 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004078 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004079
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004080 PSA_ASSERT( psa_cipher_finish( &operation,
4081 output2 + output2_length,
4082 output2_buffer_size - output2_length,
4083 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004084 TEST_LE_U( function_output_length,
4085 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4086 TEST_LE_U( function_output_length,
4087 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004088 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004089
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004090 PSA_ASSERT( psa_cipher_abort( &operation ) );
4091 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4092 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004093
Gilles Peskine50e586b2018-06-08 14:28:46 +02004094exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004095 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004096 mbedtls_free( output1 );
4097 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004098 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004099 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004100}
4101/* END_CASE */
4102
4103/* BEGIN_CASE */
4104void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004105 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004106 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004107 int first_part_size_arg,
4108 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004109 data_t *expected_output,
4110 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004111{
Ronald Cron5425a212020-08-04 14:58:35 +02004112 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004113 psa_key_type_t key_type = key_type_arg;
4114 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004115 psa_status_t status;
4116 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004117 size_t first_part_size = first_part_size_arg;
4118 size_t output1_length = output1_length_arg;
4119 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004120 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004121 size_t output_buffer_size = 0;
4122 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004123 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004124 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004126
Gilles Peskine8817f612018-12-18 00:18:46 +01004127 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004128
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004129 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4130 psa_set_key_algorithm( &attributes, alg );
4131 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004132
Ronald Cron5425a212020-08-04 14:58:35 +02004133 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4134 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004135
Ronald Cron5425a212020-08-04 14:58:35 +02004136 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004137
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004138 if( iv->len > 0 )
4139 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004140 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004141 }
4142
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004143 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4144 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004145 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004146
Gilles Peskine7be11a72022-04-14 00:12:57 +02004147 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004148 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4149 output, output_buffer_size,
4150 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004151 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004152 TEST_LE_U( function_output_length,
4153 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4154 TEST_LE_U( function_output_length,
4155 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004156 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004157
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004158 if( first_part_size < input->len )
4159 {
4160 PSA_ASSERT( psa_cipher_update( &operation,
4161 input->x + first_part_size,
4162 input->len - first_part_size,
4163 ( output_buffer_size == 0 ? NULL :
4164 output + total_output_length ),
4165 output_buffer_size - total_output_length,
4166 &function_output_length ) );
4167 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004168 TEST_LE_U( function_output_length,
4169 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4170 alg,
4171 input->len - first_part_size ) );
4172 TEST_LE_U( function_output_length,
4173 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004174 total_output_length += function_output_length;
4175 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004176
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004177 status = psa_cipher_finish( &operation,
4178 ( output_buffer_size == 0 ? NULL :
4179 output + total_output_length ),
4180 output_buffer_size - total_output_length,
4181 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004182 TEST_LE_U( function_output_length,
4183 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4184 TEST_LE_U( function_output_length,
4185 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004186 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004187 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004188
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004189 if( expected_status == PSA_SUCCESS )
4190 {
4191 PSA_ASSERT( psa_cipher_abort( &operation ) );
4192
4193 ASSERT_COMPARE( expected_output->x, expected_output->len,
4194 output, total_output_length );
4195 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004196
4197exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004198 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004199 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004200 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004201 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004202}
4203/* END_CASE */
4204
4205/* BEGIN_CASE */
4206void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004207 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004208 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004209 int first_part_size_arg,
4210 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004211 data_t *expected_output,
4212 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004213{
Ronald Cron5425a212020-08-04 14:58:35 +02004214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004215 psa_key_type_t key_type = key_type_arg;
4216 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004217 psa_status_t status;
4218 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004219 size_t first_part_size = first_part_size_arg;
4220 size_t output1_length = output1_length_arg;
4221 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004222 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004223 size_t output_buffer_size = 0;
4224 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004225 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004226 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004228
Gilles Peskine8817f612018-12-18 00:18:46 +01004229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004230
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004231 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4232 psa_set_key_algorithm( &attributes, alg );
4233 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004234
Ronald Cron5425a212020-08-04 14:58:35 +02004235 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4236 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004237
Ronald Cron5425a212020-08-04 14:58:35 +02004238 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004239
Steven Cooreman177deba2020-09-07 17:14:14 +02004240 if( iv->len > 0 )
4241 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004242 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004243 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004244
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004245 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4246 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004247 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004248
Gilles Peskine7be11a72022-04-14 00:12:57 +02004249 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004250 PSA_ASSERT( psa_cipher_update( &operation,
4251 input->x, first_part_size,
4252 output, output_buffer_size,
4253 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004254 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004255 TEST_LE_U( function_output_length,
4256 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4257 TEST_LE_U( function_output_length,
4258 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004259 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004260
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004261 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004262 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004263 PSA_ASSERT( psa_cipher_update( &operation,
4264 input->x + first_part_size,
4265 input->len - first_part_size,
4266 ( output_buffer_size == 0 ? NULL :
4267 output + total_output_length ),
4268 output_buffer_size - total_output_length,
4269 &function_output_length ) );
4270 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004271 TEST_LE_U( function_output_length,
4272 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4273 alg,
4274 input->len - first_part_size ) );
4275 TEST_LE_U( function_output_length,
4276 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004277 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004278 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004279
Gilles Peskine50e586b2018-06-08 14:28:46 +02004280 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004281 ( output_buffer_size == 0 ? NULL :
4282 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004283 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004284 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004285 TEST_LE_U( function_output_length,
4286 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4287 TEST_LE_U( function_output_length,
4288 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004289 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004290 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004291
4292 if( expected_status == PSA_SUCCESS )
4293 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004294 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004295
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004296 ASSERT_COMPARE( expected_output->x, expected_output->len,
4297 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004298 }
4299
Gilles Peskine50e586b2018-06-08 14:28:46 +02004300exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004301 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004302 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004303 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004304 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004305}
4306/* END_CASE */
4307
Gilles Peskine50e586b2018-06-08 14:28:46 +02004308/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004309void cipher_decrypt_fail( int alg_arg,
4310 int key_type_arg,
4311 data_t *key_data,
4312 data_t *iv,
4313 data_t *input_arg,
4314 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004315{
4316 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4317 psa_status_t status;
4318 psa_key_type_t key_type = key_type_arg;
4319 psa_algorithm_t alg = alg_arg;
4320 psa_status_t expected_status = expected_status_arg;
4321 unsigned char *input = NULL;
4322 size_t input_buffer_size = 0;
4323 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004324 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004325 size_t output_buffer_size = 0;
4326 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004327 size_t function_output_length;
4328 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4330
4331 if ( PSA_ERROR_BAD_STATE != expected_status )
4332 {
4333 PSA_ASSERT( psa_crypto_init( ) );
4334
4335 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4336 psa_set_key_algorithm( &attributes, alg );
4337 psa_set_key_type( &attributes, key_type );
4338
4339 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4340 &key ) );
4341 }
4342
4343 /* Allocate input buffer and copy the iv and the plaintext */
4344 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4345 if ( input_buffer_size > 0 )
4346 {
4347 ASSERT_ALLOC( input, input_buffer_size );
4348 memcpy( input, iv->x, iv->len );
4349 memcpy( input + iv->len, input_arg->x, input_arg->len );
4350 }
4351
4352 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4353 ASSERT_ALLOC( output, output_buffer_size );
4354
Neil Armstrong66a479f2022-02-07 15:41:19 +01004355 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004356 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4357 output_buffer_size, &output_length );
4358 TEST_EQUAL( status, expected_status );
4359
Neil Armstrong66a479f2022-02-07 15:41:19 +01004360 /* Decrypt, multi-part */
4361 status = psa_cipher_decrypt_setup( &operation, key, alg );
4362 if( status == PSA_SUCCESS )
4363 {
4364 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4365 input_arg->len ) +
4366 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4367 ASSERT_ALLOC( output_multi, output_buffer_size );
4368
4369 if( iv->len > 0 )
4370 {
4371 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4372
4373 if( status != PSA_SUCCESS )
4374 TEST_EQUAL( status, expected_status );
4375 }
4376
4377 if( status == PSA_SUCCESS )
4378 {
4379 status = psa_cipher_update( &operation,
4380 input_arg->x, input_arg->len,
4381 output_multi, output_buffer_size,
4382 &function_output_length );
4383 if( status == PSA_SUCCESS )
4384 {
4385 output_length = function_output_length;
4386
4387 status = psa_cipher_finish( &operation,
4388 output_multi + output_length,
4389 output_buffer_size - output_length,
4390 &function_output_length );
4391
4392 TEST_EQUAL( status, expected_status );
4393 }
4394 else
4395 {
4396 TEST_EQUAL( status, expected_status );
4397 }
4398 }
4399 else
4400 {
4401 TEST_EQUAL( status, expected_status );
4402 }
4403 }
4404 else
4405 {
4406 TEST_EQUAL( status, expected_status );
4407 }
4408
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004409exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004410 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004411 mbedtls_free( input );
4412 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004413 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004414 psa_destroy_key( key );
4415 PSA_DONE( );
4416}
4417/* END_CASE */
4418
4419/* BEGIN_CASE */
4420void cipher_decrypt( int alg_arg,
4421 int key_type_arg,
4422 data_t *key_data,
4423 data_t *iv,
4424 data_t *input_arg,
4425 data_t *expected_output )
4426{
4427 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4428 psa_key_type_t key_type = key_type_arg;
4429 psa_algorithm_t alg = alg_arg;
4430 unsigned char *input = NULL;
4431 size_t input_buffer_size = 0;
4432 unsigned char *output = NULL;
4433 size_t output_buffer_size = 0;
4434 size_t output_length = 0;
4435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4436
4437 PSA_ASSERT( psa_crypto_init( ) );
4438
4439 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4440 psa_set_key_algorithm( &attributes, alg );
4441 psa_set_key_type( &attributes, key_type );
4442
4443 /* Allocate input buffer and copy the iv and the plaintext */
4444 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4445 if ( input_buffer_size > 0 )
4446 {
4447 ASSERT_ALLOC( input, input_buffer_size );
4448 memcpy( input, iv->x, iv->len );
4449 memcpy( input + iv->len, input_arg->x, input_arg->len );
4450 }
4451
4452 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4453 ASSERT_ALLOC( output, output_buffer_size );
4454
4455 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4456 &key ) );
4457
4458 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4459 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004460 TEST_LE_U( output_length,
4461 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4462 TEST_LE_U( output_length,
4463 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004464
4465 ASSERT_COMPARE( expected_output->x, expected_output->len,
4466 output, output_length );
4467exit:
4468 mbedtls_free( input );
4469 mbedtls_free( output );
4470 psa_destroy_key( key );
4471 PSA_DONE( );
4472}
4473/* END_CASE */
4474
4475/* BEGIN_CASE */
4476void cipher_verify_output( int alg_arg,
4477 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004478 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004479 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004480{
Ronald Cron5425a212020-08-04 14:58:35 +02004481 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004482 psa_key_type_t key_type = key_type_arg;
4483 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004484 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004485 size_t output1_size = 0;
4486 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004487 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004488 size_t output2_size = 0;
4489 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004491
Gilles Peskine8817f612018-12-18 00:18:46 +01004492 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004493
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004494 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4495 psa_set_key_algorithm( &attributes, alg );
4496 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004497
Ronald Cron5425a212020-08-04 14:58:35 +02004498 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4499 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004500 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004501 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004502
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004503 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4504 output1, output1_size,
4505 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004506 TEST_LE_U( output1_length,
4507 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4508 TEST_LE_U( output1_length,
4509 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004510
4511 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004512 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004513
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004514 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4515 output2, output2_size,
4516 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004517 TEST_LE_U( output2_length,
4518 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4519 TEST_LE_U( output2_length,
4520 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004521
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004522 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004523
4524exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004525 mbedtls_free( output1 );
4526 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004527 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004528 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004529}
4530/* END_CASE */
4531
4532/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004533void cipher_verify_output_multipart( int alg_arg,
4534 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004535 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004536 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004537 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004538{
Ronald Cron5425a212020-08-04 14:58:35 +02004539 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004540 psa_key_type_t key_type = key_type_arg;
4541 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004542 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004543 unsigned char iv[16] = {0};
4544 size_t iv_size = 16;
4545 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004546 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004547 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004548 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004549 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004550 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004551 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004552 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004553 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4554 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004556
Gilles Peskine8817f612018-12-18 00:18:46 +01004557 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004558
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4560 psa_set_key_algorithm( &attributes, alg );
4561 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004562
Ronald Cron5425a212020-08-04 14:58:35 +02004563 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4564 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004565
Ronald Cron5425a212020-08-04 14:58:35 +02004566 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4567 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004568
Steven Cooreman177deba2020-09-07 17:14:14 +02004569 if( alg != PSA_ALG_ECB_NO_PADDING )
4570 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004571 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4572 iv, iv_size,
4573 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004574 }
4575
gabor-mezei-armceface22021-01-21 12:26:17 +01004576 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004577 TEST_LE_U( output1_buffer_size,
4578 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004579 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004580
Gilles Peskine7be11a72022-04-14 00:12:57 +02004581 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004582
Gilles Peskine8817f612018-12-18 00:18:46 +01004583 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4584 output1, output1_buffer_size,
4585 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004586 TEST_LE_U( function_output_length,
4587 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4588 TEST_LE_U( function_output_length,
4589 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004590 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004591
Gilles Peskine8817f612018-12-18 00:18:46 +01004592 PSA_ASSERT( psa_cipher_update( &operation1,
4593 input->x + first_part_size,
4594 input->len - first_part_size,
4595 output1, output1_buffer_size,
4596 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004597 TEST_LE_U( function_output_length,
4598 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4599 alg,
4600 input->len - first_part_size ) );
4601 TEST_LE_U( function_output_length,
4602 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004603 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004604
Gilles Peskine8817f612018-12-18 00:18:46 +01004605 PSA_ASSERT( psa_cipher_finish( &operation1,
4606 output1 + output1_length,
4607 output1_buffer_size - output1_length,
4608 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004609 TEST_LE_U( function_output_length,
4610 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4611 TEST_LE_U( function_output_length,
4612 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004613 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004614
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004616
Gilles Peskine048b7f02018-06-08 14:20:49 +02004617 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004618 TEST_LE_U( output2_buffer_size,
4619 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4620 TEST_LE_U( output2_buffer_size,
4621 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004622 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004623
Steven Cooreman177deba2020-09-07 17:14:14 +02004624 if( iv_length > 0 )
4625 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004626 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4627 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004628 }
Moran Pekerded84402018-06-06 16:36:50 +03004629
Gilles Peskine8817f612018-12-18 00:18:46 +01004630 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4631 output2, output2_buffer_size,
4632 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004633 TEST_LE_U( function_output_length,
4634 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4635 TEST_LE_U( function_output_length,
4636 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004637 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004638
Gilles Peskine8817f612018-12-18 00:18:46 +01004639 PSA_ASSERT( psa_cipher_update( &operation2,
4640 output1 + first_part_size,
4641 output1_length - first_part_size,
4642 output2, output2_buffer_size,
4643 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004644 TEST_LE_U( function_output_length,
4645 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4646 alg,
4647 output1_length - first_part_size ) );
4648 TEST_LE_U( function_output_length,
4649 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004650 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004651
Gilles Peskine8817f612018-12-18 00:18:46 +01004652 PSA_ASSERT( psa_cipher_finish( &operation2,
4653 output2 + output2_length,
4654 output2_buffer_size - output2_length,
4655 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004656 TEST_LE_U( function_output_length,
4657 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4658 TEST_LE_U( function_output_length,
4659 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004660 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004661
Gilles Peskine8817f612018-12-18 00:18:46 +01004662 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004663
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004664 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004665
4666exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004667 psa_cipher_abort( &operation1 );
4668 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004669 mbedtls_free( output1 );
4670 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004671 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004672 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004673}
4674/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004675
Gilles Peskine20035e32018-02-03 22:44:14 +01004676/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004677void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004678 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004679 data_t *nonce,
4680 data_t *additional_data,
4681 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004682 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004683{
Ronald Cron5425a212020-08-04 14:58:35 +02004684 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004685 psa_key_type_t key_type = key_type_arg;
4686 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004687 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004688 unsigned char *output_data = NULL;
4689 size_t output_size = 0;
4690 size_t output_length = 0;
4691 unsigned char *output_data2 = NULL;
4692 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004693 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004694 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004696
Gilles Peskine8817f612018-12-18 00:18:46 +01004697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004698
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4700 psa_set_key_algorithm( &attributes, alg );
4701 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004702
Gilles Peskine049c7532019-05-15 20:22:09 +02004703 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004704 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004705 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4706 key_bits = psa_get_key_bits( &attributes );
4707
4708 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4709 alg );
4710 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4711 * should be exact. */
4712 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4713 expected_result != PSA_ERROR_NOT_SUPPORTED )
4714 {
4715 TEST_EQUAL( output_size,
4716 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004717 TEST_LE_U( output_size,
4718 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004719 }
4720 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004721
Steven Cooremanf49478b2021-02-15 15:19:25 +01004722 status = psa_aead_encrypt( key, alg,
4723 nonce->x, nonce->len,
4724 additional_data->x,
4725 additional_data->len,
4726 input_data->x, input_data->len,
4727 output_data, output_size,
4728 &output_length );
4729
4730 /* If the operation is not supported, just skip and not fail in case the
4731 * encryption involves a common limitation of cryptography hardwares and
4732 * an alternative implementation. */
4733 if( status == PSA_ERROR_NOT_SUPPORTED )
4734 {
4735 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4736 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4737 }
4738
4739 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004740
4741 if( PSA_SUCCESS == expected_result )
4742 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004743 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004744
Gilles Peskine003a4a92019-05-14 16:09:40 +02004745 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4746 * should be exact. */
4747 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004748 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004749
Gilles Peskine7be11a72022-04-14 00:12:57 +02004750 TEST_LE_U( input_data->len,
4751 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004752
Ronald Cron5425a212020-08-04 14:58:35 +02004753 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004754 nonce->x, nonce->len,
4755 additional_data->x,
4756 additional_data->len,
4757 output_data, output_length,
4758 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004759 &output_length2 ),
4760 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004761
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004762 ASSERT_COMPARE( input_data->x, input_data->len,
4763 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004764 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004765
Gilles Peskinea1cac842018-06-11 19:33:02 +02004766exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004767 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004768 mbedtls_free( output_data );
4769 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004770 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004771}
4772/* END_CASE */
4773
4774/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004775void aead_encrypt( int key_type_arg, data_t *key_data,
4776 int alg_arg,
4777 data_t *nonce,
4778 data_t *additional_data,
4779 data_t *input_data,
4780 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004781{
Ronald Cron5425a212020-08-04 14:58:35 +02004782 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004783 psa_key_type_t key_type = key_type_arg;
4784 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004785 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004786 unsigned char *output_data = NULL;
4787 size_t output_size = 0;
4788 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004790 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004791
Gilles Peskine8817f612018-12-18 00:18:46 +01004792 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004793
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004794 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4795 psa_set_key_algorithm( &attributes, alg );
4796 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797
Gilles Peskine049c7532019-05-15 20:22:09 +02004798 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004799 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004800 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4801 key_bits = psa_get_key_bits( &attributes );
4802
4803 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4804 alg );
4805 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4806 * should be exact. */
4807 TEST_EQUAL( output_size,
4808 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004809 TEST_LE_U( output_size,
4810 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004811 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004812
Steven Cooremand588ea12021-01-11 19:36:04 +01004813 status = psa_aead_encrypt( key, alg,
4814 nonce->x, nonce->len,
4815 additional_data->x, additional_data->len,
4816 input_data->x, input_data->len,
4817 output_data, output_size,
4818 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004819
Ronald Cron28a45ed2021-02-09 20:35:42 +01004820 /* If the operation is not supported, just skip and not fail in case the
4821 * encryption involves a common limitation of cryptography hardwares and
4822 * an alternative implementation. */
4823 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004824 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004825 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4826 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004827 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004828
4829 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004830 ASSERT_COMPARE( expected_result->x, expected_result->len,
4831 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004832
Gilles Peskinea1cac842018-06-11 19:33:02 +02004833exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004834 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004835 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004836 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004837}
4838/* END_CASE */
4839
4840/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004841void aead_decrypt( int key_type_arg, data_t *key_data,
4842 int alg_arg,
4843 data_t *nonce,
4844 data_t *additional_data,
4845 data_t *input_data,
4846 data_t *expected_data,
4847 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004848{
Ronald Cron5425a212020-08-04 14:58:35 +02004849 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004850 psa_key_type_t key_type = key_type_arg;
4851 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004852 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004853 unsigned char *output_data = NULL;
4854 size_t output_size = 0;
4855 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004856 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004857 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004858 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004859
Gilles Peskine8817f612018-12-18 00:18:46 +01004860 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004861
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004862 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4863 psa_set_key_algorithm( &attributes, alg );
4864 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865
Gilles Peskine049c7532019-05-15 20:22:09 +02004866 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004867 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004868 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4869 key_bits = psa_get_key_bits( &attributes );
4870
4871 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4872 alg );
4873 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4874 expected_result != PSA_ERROR_NOT_SUPPORTED )
4875 {
4876 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4877 * should be exact. */
4878 TEST_EQUAL( output_size,
4879 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004880 TEST_LE_U( output_size,
4881 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004882 }
4883 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004884
Steven Cooremand588ea12021-01-11 19:36:04 +01004885 status = psa_aead_decrypt( key, alg,
4886 nonce->x, nonce->len,
4887 additional_data->x,
4888 additional_data->len,
4889 input_data->x, input_data->len,
4890 output_data, output_size,
4891 &output_length );
4892
Ronald Cron28a45ed2021-02-09 20:35:42 +01004893 /* If the operation is not supported, just skip and not fail in case the
4894 * decryption involves a common limitation of cryptography hardwares and
4895 * an alternative implementation. */
4896 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004897 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004898 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4899 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004900 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004901
4902 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004903
Gilles Peskine2d277862018-06-18 15:41:12 +02004904 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004905 ASSERT_COMPARE( expected_data->x, expected_data->len,
4906 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004907
Gilles Peskinea1cac842018-06-11 19:33:02 +02004908exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004909 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004910 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004911 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004912}
4913/* END_CASE */
4914
4915/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004916void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4917 int alg_arg,
4918 data_t *nonce,
4919 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004920 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004921 int do_set_lengths,
4922 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004923{
Paul Elliottd3f82412021-06-16 16:52:21 +01004924 size_t ad_part_len = 0;
4925 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004926 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004927
Paul Elliott32f46ba2021-09-23 18:24:36 +01004928 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004929 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004930 mbedtls_test_set_step( ad_part_len );
4931
4932 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004933 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004934 if( ad_part_len & 0x01 )
4935 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4936 else
4937 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004938 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004939
4940 /* Split ad into length(ad_part_len) parts. */
4941 if( !aead_multipart_internal_func( key_type_arg, key_data,
4942 alg_arg, nonce,
4943 additional_data,
4944 ad_part_len,
4945 input_data, -1,
4946 set_lengths_method,
4947 expected_output,
4948 1, 0 ) )
4949 break;
4950
4951 /* length(0) part, length(ad_part_len) part, length(0) part... */
4952 mbedtls_test_set_step( 1000 + ad_part_len );
4953
4954 if( !aead_multipart_internal_func( key_type_arg, key_data,
4955 alg_arg, nonce,
4956 additional_data,
4957 ad_part_len,
4958 input_data, -1,
4959 set_lengths_method,
4960 expected_output,
4961 1, 1 ) )
4962 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004963 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004964
Paul Elliott32f46ba2021-09-23 18:24:36 +01004965 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004966 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004967 /* Split data into length(data_part_len) parts. */
4968 mbedtls_test_set_step( 2000 + data_part_len );
4969
4970 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004971 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004972 if( data_part_len & 0x01 )
4973 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4974 else
4975 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004976 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004977
Paul Elliott32f46ba2021-09-23 18:24:36 +01004978 if( !aead_multipart_internal_func( key_type_arg, key_data,
4979 alg_arg, nonce,
4980 additional_data, -1,
4981 input_data, data_part_len,
4982 set_lengths_method,
4983 expected_output,
4984 1, 0 ) )
4985 break;
4986
4987 /* length(0) part, length(data_part_len) part, length(0) part... */
4988 mbedtls_test_set_step( 3000 + data_part_len );
4989
4990 if( !aead_multipart_internal_func( key_type_arg, key_data,
4991 alg_arg, nonce,
4992 additional_data, -1,
4993 input_data, data_part_len,
4994 set_lengths_method,
4995 expected_output,
4996 1, 1 ) )
4997 break;
4998 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004999
Paul Elliott8fc45162021-06-23 16:06:01 +01005000 /* Goto is required to silence warnings about unused labels, as we
5001 * don't actually do any test assertions in this function. */
5002 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005003}
5004/* END_CASE */
5005
5006/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005007void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5008 int alg_arg,
5009 data_t *nonce,
5010 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005011 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005012 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005013 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005014{
Paul Elliottd3f82412021-06-16 16:52:21 +01005015 size_t ad_part_len = 0;
5016 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005017 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005018
Paul Elliott32f46ba2021-09-23 18:24:36 +01005019 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005020 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005021 /* Split ad into length(ad_part_len) parts. */
5022 mbedtls_test_set_step( ad_part_len );
5023
5024 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005025 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005026 if( ad_part_len & 0x01 )
5027 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5028 else
5029 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005030 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005031
5032 if( !aead_multipart_internal_func( key_type_arg, key_data,
5033 alg_arg, nonce,
5034 additional_data,
5035 ad_part_len,
5036 input_data, -1,
5037 set_lengths_method,
5038 expected_output,
5039 0, 0 ) )
5040 break;
5041
5042 /* length(0) part, length(ad_part_len) part, length(0) part... */
5043 mbedtls_test_set_step( 1000 + ad_part_len );
5044
5045 if( !aead_multipart_internal_func( key_type_arg, key_data,
5046 alg_arg, nonce,
5047 additional_data,
5048 ad_part_len,
5049 input_data, -1,
5050 set_lengths_method,
5051 expected_output,
5052 0, 1 ) )
5053 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005054 }
5055
Paul Elliott32f46ba2021-09-23 18:24:36 +01005056 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005057 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005058 /* Split data into length(data_part_len) parts. */
5059 mbedtls_test_set_step( 2000 + data_part_len );
5060
5061 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005062 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005063 if( data_part_len & 0x01 )
5064 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5065 else
5066 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005067 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005068
5069 if( !aead_multipart_internal_func( key_type_arg, key_data,
5070 alg_arg, nonce,
5071 additional_data, -1,
5072 input_data, data_part_len,
5073 set_lengths_method,
5074 expected_output,
5075 0, 0 ) )
5076 break;
5077
5078 /* length(0) part, length(data_part_len) part, length(0) part... */
5079 mbedtls_test_set_step( 3000 + data_part_len );
5080
5081 if( !aead_multipart_internal_func( key_type_arg, key_data,
5082 alg_arg, nonce,
5083 additional_data, -1,
5084 input_data, data_part_len,
5085 set_lengths_method,
5086 expected_output,
5087 0, 1 ) )
5088 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005089 }
5090
Paul Elliott8fc45162021-06-23 16:06:01 +01005091 /* Goto is required to silence warnings about unused labels, as we
5092 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005093 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005094}
5095/* END_CASE */
5096
5097/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005098void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5099 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005100 int nonce_length,
5101 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005102 data_t *additional_data,
5103 data_t *input_data,
5104 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005105{
5106
5107 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5108 psa_key_type_t key_type = key_type_arg;
5109 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005110 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005111 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5113 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005114 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005115 size_t actual_nonce_length = 0;
5116 size_t expected_nonce_length = expected_nonce_length_arg;
5117 unsigned char *output = NULL;
5118 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005119 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005120 size_t ciphertext_size = 0;
5121 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005122 size_t tag_length = 0;
5123 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005124
5125 PSA_ASSERT( psa_crypto_init( ) );
5126
5127 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5128 psa_set_key_algorithm( & attributes, alg );
5129 psa_set_key_type( & attributes, key_type );
5130
5131 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5132 &key ) );
5133
5134 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5135
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005136 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5137
Paul Elliottf1277632021-08-24 18:11:37 +01005138 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005139
Paul Elliottf1277632021-08-24 18:11:37 +01005140 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005141
Gilles Peskine7be11a72022-04-14 00:12:57 +02005142 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005143
Paul Elliottf1277632021-08-24 18:11:37 +01005144 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005145
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005146 status = psa_aead_encrypt_setup( &operation, key, alg );
5147
5148 /* If the operation is not supported, just skip and not fail in case the
5149 * encryption involves a common limitation of cryptography hardwares and
5150 * an alternative implementation. */
5151 if( status == PSA_ERROR_NOT_SUPPORTED )
5152 {
5153 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005154 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005155 }
5156
5157 PSA_ASSERT( status );
5158
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005159 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005160 nonce_length,
5161 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005162
Paul Elliott693bf312021-07-23 17:40:41 +01005163 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005164
Paul Elliottf1277632021-08-24 18:11:37 +01005165 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005166
Paul Elliott88ecbe12021-09-22 17:23:03 +01005167 if( expected_status == PSA_SUCCESS )
5168 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5169 alg ) );
5170
Gilles Peskine7be11a72022-04-14 00:12:57 +02005171 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005172
Paul Elliott693bf312021-07-23 17:40:41 +01005173 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005174 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005175 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005176 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5177 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005178
5179 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5180 additional_data->len ) );
5181
5182 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005183 output, output_size,
5184 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005185
Paul Elliottf1277632021-08-24 18:11:37 +01005186 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5187 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005188 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5189 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005190
5191exit:
5192 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005193 mbedtls_free( output );
5194 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005195 psa_aead_abort( &operation );
5196 PSA_DONE( );
5197}
5198/* END_CASE */
5199
5200/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005201void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5202 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005203 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005204 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005205 data_t *additional_data,
5206 data_t *input_data,
5207 int expected_status_arg )
5208{
5209
5210 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5211 psa_key_type_t key_type = key_type_arg;
5212 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005213 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005214 uint8_t *nonce_buffer = NULL;
5215 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5216 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5217 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005218 unsigned char *output = NULL;
5219 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005220 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005221 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005222 size_t ciphertext_size = 0;
5223 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005224 size_t tag_length = 0;
5225 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005226 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005227 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005228
5229 PSA_ASSERT( psa_crypto_init( ) );
5230
5231 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5232 psa_set_key_algorithm( &attributes, alg );
5233 psa_set_key_type( &attributes, key_type );
5234
5235 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5236 &key ) );
5237
5238 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5239
5240 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5241
Paul Elliott6f0e7202021-08-25 12:57:18 +01005242 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005243
Paul Elliott6f0e7202021-08-25 12:57:18 +01005244 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005245
Gilles Peskine7be11a72022-04-14 00:12:57 +02005246 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005247
Paul Elliott6f0e7202021-08-25 12:57:18 +01005248 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Paul Elliott863864a2021-07-23 17:28:31 +01005250 status = psa_aead_encrypt_setup( &operation, key, alg );
5251
5252 /* If the operation is not supported, just skip and not fail in case the
5253 * encryption involves a common limitation of cryptography hardwares and
5254 * an alternative implementation. */
5255 if( status == PSA_ERROR_NOT_SUPPORTED )
5256 {
5257 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005258 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005259 }
5260
5261 PSA_ASSERT( status );
5262
Paul Elliott4023ffd2021-09-10 16:21:22 +01005263 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5264 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005265 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005266 /* Arbitrary size buffer, to test zero length valid buffer. */
5267 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005268 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005269 }
5270 else
5271 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005272 /* If length is zero, then this will return NULL. */
5273 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005274 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005275
Paul Elliott4023ffd2021-09-10 16:21:22 +01005276 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005277 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005278 for( index = 0; index < nonce_length - 1; ++index )
5279 {
5280 nonce_buffer[index] = 'a' + index;
5281 }
Paul Elliott66696b52021-08-16 18:42:41 +01005282 }
Paul Elliott863864a2021-07-23 17:28:31 +01005283 }
5284
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005285 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5286 {
5287 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5288 input_data->len ) );
5289 }
5290
Paul Elliott6f0e7202021-08-25 12:57:18 +01005291 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005292
Paul Elliott693bf312021-07-23 17:40:41 +01005293 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005294
5295 if( expected_status == PSA_SUCCESS )
5296 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005297 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5298 {
5299 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5300 input_data->len ) );
5301 }
5302 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5303 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005304
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005305 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5306 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5307 additional_data->len ),
5308 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005309
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005310 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005311 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005312 &ciphertext_length ),
5313 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005314
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005315 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005316 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005317 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5318 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005319 }
5320
5321exit:
5322 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005323 mbedtls_free( output );
5324 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005325 mbedtls_free( nonce_buffer );
5326 psa_aead_abort( &operation );
5327 PSA_DONE( );
5328}
5329/* END_CASE */
5330
5331/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005332void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5333 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005334 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005335 data_t *nonce,
5336 data_t *additional_data,
5337 data_t *input_data,
5338 int expected_status_arg )
5339{
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 Elliott43fbda62021-07-23 18:30:59 +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;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005348 unsigned char *output = NULL;
5349 unsigned char *ciphertext = NULL;
5350 size_t output_size = output_size_arg;
5351 size_t ciphertext_size = 0;
5352 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005353 size_t tag_length = 0;
5354 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5355
5356 PSA_ASSERT( psa_crypto_init( ) );
5357
5358 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5359 psa_set_key_algorithm( &attributes, alg );
5360 psa_set_key_type( &attributes, key_type );
5361
5362 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5363 &key ) );
5364
5365 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5366
Paul Elliottc6d11d02021-09-01 12:04:23 +01005367 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005368
Paul Elliottc6d11d02021-09-01 12:04:23 +01005369 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005370
Paul Elliottc6d11d02021-09-01 12:04:23 +01005371 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005372
Paul Elliott43fbda62021-07-23 18:30:59 +01005373 status = psa_aead_encrypt_setup( &operation, key, alg );
5374
5375 /* If the operation is not supported, just skip and not fail in case the
5376 * encryption involves a common limitation of cryptography hardwares and
5377 * an alternative implementation. */
5378 if( status == PSA_ERROR_NOT_SUPPORTED )
5379 {
5380 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5381 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5382 }
5383
5384 PSA_ASSERT( status );
5385
Paul Elliott47b9a142021-10-07 15:04:57 +01005386 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5387 input_data->len ) );
5388
Paul Elliott43fbda62021-07-23 18:30:59 +01005389 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5390
5391 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5392 additional_data->len ) );
5393
5394 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005395 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005396
5397 TEST_EQUAL( status, expected_status );
5398
5399 if( expected_status == PSA_SUCCESS )
5400 {
5401 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005402 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5403 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005404 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5405 }
5406
5407exit:
5408 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005409 mbedtls_free( output );
5410 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005411 psa_aead_abort( &operation );
5412 PSA_DONE( );
5413}
5414/* END_CASE */
5415
Paul Elliott91b021e2021-07-23 18:52:31 +01005416/* BEGIN_CASE */
5417void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5418 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005419 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005420 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005421 data_t *nonce,
5422 data_t *additional_data,
5423 data_t *input_data,
5424 int expected_status_arg )
5425{
5426
5427 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5428 psa_key_type_t key_type = key_type_arg;
5429 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005430 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5432 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5433 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005434 unsigned char *ciphertext = NULL;
5435 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005436 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005437 size_t ciphertext_size = 0;
5438 size_t ciphertext_length = 0;
5439 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005440 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005441 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005442
5443 PSA_ASSERT( psa_crypto_init( ) );
5444
5445 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5446 psa_set_key_algorithm( &attributes, alg );
5447 psa_set_key_type( &attributes, key_type );
5448
5449 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5450 &key ) );
5451
5452 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5453
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005454 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005455
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005456 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005457
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005458 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
Paul Elliott719c1322021-09-13 18:27:22 +01005460 ASSERT_ALLOC( tag_buffer, tag_size );
5461
Paul Elliott91b021e2021-07-23 18:52:31 +01005462 status = psa_aead_encrypt_setup( &operation, key, alg );
5463
5464 /* If the operation is not supported, just skip and not fail in case the
5465 * encryption involves a common limitation of cryptography hardwares and
5466 * an alternative implementation. */
5467 if( status == PSA_ERROR_NOT_SUPPORTED )
5468 {
5469 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5470 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5471 }
5472
5473 PSA_ASSERT( status );
5474
5475 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5476
Paul Elliott76bda482021-10-07 17:07:23 +01005477 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5478 input_data->len ) );
5479
Paul Elliott91b021e2021-07-23 18:52:31 +01005480 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5481 additional_data->len ) );
5482
5483 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005484 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005485
5486 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005487 status = psa_aead_finish( &operation, finish_ciphertext,
5488 finish_ciphertext_size,
5489 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005490 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005491
5492 TEST_EQUAL( status, expected_status );
5493
5494exit:
5495 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005496 mbedtls_free( ciphertext );
5497 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005498 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005499 psa_aead_abort( &operation );
5500 PSA_DONE( );
5501}
5502/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005503
5504/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005505void aead_multipart_verify( int key_type_arg, data_t *key_data,
5506 int alg_arg,
5507 data_t *nonce,
5508 data_t *additional_data,
5509 data_t *input_data,
5510 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005511 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005512 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005513 int expected_status_arg )
5514{
5515 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5516 psa_key_type_t key_type = key_type_arg;
5517 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005518 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5520 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5521 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005522 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005523 unsigned char *plaintext = NULL;
5524 unsigned char *finish_plaintext = NULL;
5525 size_t plaintext_size = 0;
5526 size_t plaintext_length = 0;
5527 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005528 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005529 unsigned char *tag_buffer = NULL;
5530 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005531
5532 PSA_ASSERT( psa_crypto_init( ) );
5533
5534 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5535 psa_set_key_algorithm( &attributes, alg );
5536 psa_set_key_type( &attributes, key_type );
5537
5538 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5539 &key ) );
5540
5541 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5542
5543 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5544 input_data->len );
5545
5546 ASSERT_ALLOC( plaintext, plaintext_size );
5547
5548 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5549
5550 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5551
Paul Elliott9961a662021-09-17 19:19:02 +01005552 status = psa_aead_decrypt_setup( &operation, key, alg );
5553
5554 /* If the operation is not supported, just skip and not fail in case the
5555 * encryption involves a common limitation of cryptography hardwares and
5556 * an alternative implementation. */
5557 if( status == PSA_ERROR_NOT_SUPPORTED )
5558 {
5559 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5560 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5561 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005562 TEST_EQUAL( status, expected_setup_status );
5563
5564 if( status != PSA_SUCCESS )
5565 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005566
5567 PSA_ASSERT( status );
5568
5569 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5570
Paul Elliottfec6f372021-10-06 17:15:02 +01005571 status = psa_aead_set_lengths( &operation, additional_data->len,
5572 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005573 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005574
Paul Elliott9961a662021-09-17 19:19:02 +01005575 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5576 additional_data->len ) );
5577
5578 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5579 input_data->len,
5580 plaintext, plaintext_size,
5581 &plaintext_length ) );
5582
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005583 if( tag_usage == USE_GIVEN_TAG )
5584 {
5585 tag_buffer = tag->x;
5586 tag_size = tag->len;
5587 }
5588
Paul Elliott9961a662021-09-17 19:19:02 +01005589 status = psa_aead_verify( &operation, finish_plaintext,
5590 verify_plaintext_size,
5591 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005592 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005593
5594 TEST_EQUAL( status, expected_status );
5595
5596exit:
5597 psa_destroy_key( key );
5598 mbedtls_free( plaintext );
5599 mbedtls_free( finish_plaintext );
5600 psa_aead_abort( &operation );
5601 PSA_DONE( );
5602}
5603/* END_CASE */
5604
Paul Elliott9961a662021-09-17 19:19:02 +01005605/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005606void aead_multipart_setup( int key_type_arg, data_t *key_data,
5607 int alg_arg, int expected_status_arg )
5608{
5609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5610 psa_key_type_t key_type = key_type_arg;
5611 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005612 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5614 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5615 psa_status_t expected_status = expected_status_arg;
5616
5617 PSA_ASSERT( psa_crypto_init( ) );
5618
5619 psa_set_key_usage_flags( &attributes,
5620 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5621 psa_set_key_algorithm( &attributes, alg );
5622 psa_set_key_type( &attributes, key_type );
5623
5624 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5625 &key ) );
5626
Paul Elliott5221ef62021-09-19 17:33:03 +01005627 status = psa_aead_encrypt_setup( &operation, key, alg );
5628
5629 TEST_EQUAL( status, expected_status );
5630
5631 psa_aead_abort( &operation );
5632
Paul Elliott5221ef62021-09-19 17:33:03 +01005633 status = psa_aead_decrypt_setup( &operation, key, alg );
5634
5635 TEST_EQUAL(status, expected_status );
5636
5637exit:
5638 psa_destroy_key( key );
5639 psa_aead_abort( &operation );
5640 PSA_DONE( );
5641}
5642/* END_CASE */
5643
5644/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005645void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5646 int alg_arg,
5647 data_t *nonce,
5648 data_t *additional_data,
5649 data_t *input_data )
5650{
5651 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5652 psa_key_type_t key_type = key_type_arg;
5653 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005654 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005655 unsigned char *output_data = NULL;
5656 unsigned char *final_data = NULL;
5657 size_t output_size = 0;
5658 size_t finish_output_size = 0;
5659 size_t output_length = 0;
5660 size_t key_bits = 0;
5661 size_t tag_length = 0;
5662 size_t tag_size = 0;
5663 size_t nonce_length = 0;
5664 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5665 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5666 size_t output_part_length = 0;
5667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5668
5669 PSA_ASSERT( psa_crypto_init( ) );
5670
5671 psa_set_key_usage_flags( & attributes,
5672 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5673 psa_set_key_algorithm( & attributes, alg );
5674 psa_set_key_type( & attributes, key_type );
5675
5676 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5677 &key ) );
5678
5679 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5680 key_bits = psa_get_key_bits( &attributes );
5681
5682 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5683
Gilles Peskine7be11a72022-04-14 00:12:57 +02005684 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005685
5686 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5687
5688 ASSERT_ALLOC( output_data, output_size );
5689
5690 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5691
Gilles Peskine7be11a72022-04-14 00:12:57 +02005692 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005693
5694 ASSERT_ALLOC( final_data, finish_output_size );
5695
5696 /* Test all operations error without calling setup first. */
5697
Paul Elliottc23a9a02021-06-21 18:32:46 +01005698 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5699 PSA_ERROR_BAD_STATE );
5700
5701 psa_aead_abort( &operation );
5702
Paul Elliottc23a9a02021-06-21 18:32:46 +01005703 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5704 PSA_AEAD_NONCE_MAX_SIZE,
5705 &nonce_length ),
5706 PSA_ERROR_BAD_STATE );
5707
5708 psa_aead_abort( &operation );
5709
Paul Elliott481be342021-07-16 17:38:47 +01005710 /* ------------------------------------------------------- */
5711
Paul Elliottc23a9a02021-06-21 18:32:46 +01005712 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5713 input_data->len ),
5714 PSA_ERROR_BAD_STATE );
5715
5716 psa_aead_abort( &operation );
5717
Paul Elliott481be342021-07-16 17:38:47 +01005718 /* ------------------------------------------------------- */
5719
Paul Elliottc23a9a02021-06-21 18:32:46 +01005720 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5721 additional_data->len ),
5722 PSA_ERROR_BAD_STATE );
5723
5724 psa_aead_abort( &operation );
5725
Paul Elliott481be342021-07-16 17:38:47 +01005726 /* ------------------------------------------------------- */
5727
Paul Elliottc23a9a02021-06-21 18:32:46 +01005728 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5729 input_data->len, output_data,
5730 output_size, &output_length ),
5731 PSA_ERROR_BAD_STATE );
5732
5733 psa_aead_abort( &operation );
5734
Paul Elliott481be342021-07-16 17:38:47 +01005735 /* ------------------------------------------------------- */
5736
Paul Elliottc23a9a02021-06-21 18:32:46 +01005737 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5738 finish_output_size,
5739 &output_part_length,
5740 tag_buffer, tag_length,
5741 &tag_size ),
5742 PSA_ERROR_BAD_STATE );
5743
5744 psa_aead_abort( &operation );
5745
Paul Elliott481be342021-07-16 17:38:47 +01005746 /* ------------------------------------------------------- */
5747
Paul Elliottc23a9a02021-06-21 18:32:46 +01005748 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5749 finish_output_size,
5750 &output_part_length,
5751 tag_buffer,
5752 tag_length ),
5753 PSA_ERROR_BAD_STATE );
5754
5755 psa_aead_abort( &operation );
5756
5757 /* Test for double setups. */
5758
Paul Elliottc23a9a02021-06-21 18:32:46 +01005759 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5760
5761 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5762 PSA_ERROR_BAD_STATE );
5763
5764 psa_aead_abort( &operation );
5765
Paul Elliott481be342021-07-16 17:38:47 +01005766 /* ------------------------------------------------------- */
5767
Paul Elliottc23a9a02021-06-21 18:32:46 +01005768 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5769
5770 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5771 PSA_ERROR_BAD_STATE );
5772
5773 psa_aead_abort( &operation );
5774
Paul Elliott374a2be2021-07-16 17:53:40 +01005775 /* ------------------------------------------------------- */
5776
Paul Elliott374a2be2021-07-16 17:53:40 +01005777 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5778
5779 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5780 PSA_ERROR_BAD_STATE );
5781
5782 psa_aead_abort( &operation );
5783
5784 /* ------------------------------------------------------- */
5785
Paul Elliott374a2be2021-07-16 17:53:40 +01005786 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5787
5788 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5789 PSA_ERROR_BAD_STATE );
5790
5791 psa_aead_abort( &operation );
5792
Paul Elliottc23a9a02021-06-21 18:32:46 +01005793 /* Test for not setting a nonce. */
5794
Paul Elliottc23a9a02021-06-21 18:32:46 +01005795 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5796
5797 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5798 additional_data->len ),
5799 PSA_ERROR_BAD_STATE );
5800
5801 psa_aead_abort( &operation );
5802
Paul Elliott7f628422021-09-01 12:08:29 +01005803 /* ------------------------------------------------------- */
5804
Paul Elliott7f628422021-09-01 12:08:29 +01005805 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5806
5807 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5808 input_data->len, output_data,
5809 output_size, &output_length ),
5810 PSA_ERROR_BAD_STATE );
5811
5812 psa_aead_abort( &operation );
5813
Paul Elliottbdc2c682021-09-21 18:37:10 +01005814 /* ------------------------------------------------------- */
5815
Paul Elliottbdc2c682021-09-21 18:37:10 +01005816 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5817
5818 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5819 finish_output_size,
5820 &output_part_length,
5821 tag_buffer, tag_length,
5822 &tag_size ),
5823 PSA_ERROR_BAD_STATE );
5824
5825 psa_aead_abort( &operation );
5826
5827 /* ------------------------------------------------------- */
5828
Paul Elliottbdc2c682021-09-21 18:37:10 +01005829 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5830
5831 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5832 finish_output_size,
5833 &output_part_length,
5834 tag_buffer,
5835 tag_length ),
5836 PSA_ERROR_BAD_STATE );
5837
5838 psa_aead_abort( &operation );
5839
Paul Elliottc23a9a02021-06-21 18:32:46 +01005840 /* Test for double setting nonce. */
5841
Paul Elliottc23a9a02021-06-21 18:32:46 +01005842 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5843
5844 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5845
5846 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5847 PSA_ERROR_BAD_STATE );
5848
5849 psa_aead_abort( &operation );
5850
Paul Elliott374a2be2021-07-16 17:53:40 +01005851 /* Test for double generating nonce. */
5852
Paul Elliott374a2be2021-07-16 17:53:40 +01005853 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5854
5855 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5856 PSA_AEAD_NONCE_MAX_SIZE,
5857 &nonce_length ) );
5858
5859 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5860 PSA_AEAD_NONCE_MAX_SIZE,
5861 &nonce_length ),
5862 PSA_ERROR_BAD_STATE );
5863
5864
5865 psa_aead_abort( &operation );
5866
5867 /* Test for generate nonce then set and vice versa */
5868
Paul Elliott374a2be2021-07-16 17:53:40 +01005869 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5870
5871 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5872 PSA_AEAD_NONCE_MAX_SIZE,
5873 &nonce_length ) );
5874
5875 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5876 PSA_ERROR_BAD_STATE );
5877
5878 psa_aead_abort( &operation );
5879
Andrzej Kurekad837522021-12-15 15:28:49 +01005880 /* Test for generating nonce after calling set lengths */
5881
5882 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5883
5884 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5885 input_data->len ) );
5886
5887 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5888 PSA_AEAD_NONCE_MAX_SIZE,
5889 &nonce_length ) );
5890
5891 psa_aead_abort( &operation );
5892
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005893 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005894
5895 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5896
5897 if( operation.alg == PSA_ALG_CCM )
5898 {
5899 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5900 input_data->len ),
5901 PSA_ERROR_INVALID_ARGUMENT );
5902 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5903 PSA_AEAD_NONCE_MAX_SIZE,
5904 &nonce_length ),
5905 PSA_ERROR_BAD_STATE );
5906 }
5907 else
5908 {
5909 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5910 input_data->len ) );
5911 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5912 PSA_AEAD_NONCE_MAX_SIZE,
5913 &nonce_length ) );
5914 }
5915
5916 psa_aead_abort( &operation );
5917
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005918 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005919#if SIZE_MAX > UINT32_MAX
5920 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5921
5922 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5923 {
5924 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5925 input_data->len ),
5926 PSA_ERROR_INVALID_ARGUMENT );
5927 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5928 PSA_AEAD_NONCE_MAX_SIZE,
5929 &nonce_length ),
5930 PSA_ERROR_BAD_STATE );
5931 }
5932 else
5933 {
5934 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5935 input_data->len ) );
5936 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5937 PSA_AEAD_NONCE_MAX_SIZE,
5938 &nonce_length ) );
5939 }
5940
5941 psa_aead_abort( &operation );
5942#endif
5943
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005944 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005945
5946 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5947
5948 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5949 PSA_AEAD_NONCE_MAX_SIZE,
5950 &nonce_length ) );
5951
5952 if( operation.alg == PSA_ALG_CCM )
5953 {
5954 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5955 input_data->len ),
5956 PSA_ERROR_INVALID_ARGUMENT );
5957 }
5958 else
5959 {
5960 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5961 input_data->len ) );
5962 }
5963
5964 psa_aead_abort( &operation );
5965
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005966 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005967 /* Test for setting nonce after calling set lengths */
5968
5969 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5970
5971 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5972 input_data->len ) );
5973
5974 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5975
5976 psa_aead_abort( &operation );
5977
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005978 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005979
5980 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5981
5982 if( operation.alg == PSA_ALG_CCM )
5983 {
5984 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5985 input_data->len ),
5986 PSA_ERROR_INVALID_ARGUMENT );
5987 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5988 PSA_ERROR_BAD_STATE );
5989 }
5990 else
5991 {
5992 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5993 input_data->len ) );
5994 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5995 }
5996
5997 psa_aead_abort( &operation );
5998
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005999 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006000#if SIZE_MAX > UINT32_MAX
6001 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6002
6003 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6004 {
6005 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6006 input_data->len ),
6007 PSA_ERROR_INVALID_ARGUMENT );
6008 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6009 PSA_ERROR_BAD_STATE );
6010 }
6011 else
6012 {
6013 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6014 input_data->len ) );
6015 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6016 }
6017
6018 psa_aead_abort( &operation );
6019#endif
6020
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006021 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006022
6023 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6024
6025 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6026
6027 if( operation.alg == PSA_ALG_CCM )
6028 {
6029 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6030 input_data->len ),
6031 PSA_ERROR_INVALID_ARGUMENT );
6032 }
6033 else
6034 {
6035 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6036 input_data->len ) );
6037 }
6038
6039 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006040
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006041 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006042#if SIZE_MAX > UINT32_MAX
6043 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6044
6045 if( operation.alg == PSA_ALG_GCM )
6046 {
6047 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6048 SIZE_MAX ),
6049 PSA_ERROR_INVALID_ARGUMENT );
6050 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6051 PSA_ERROR_BAD_STATE );
6052 }
6053 else if ( operation.alg != PSA_ALG_CCM )
6054 {
6055 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6056 SIZE_MAX ) );
6057 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6058 }
6059
6060 psa_aead_abort( &operation );
6061
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006062 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006063 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6064
6065 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6066
6067 if( operation.alg == PSA_ALG_GCM )
6068 {
6069 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6070 SIZE_MAX ),
6071 PSA_ERROR_INVALID_ARGUMENT );
6072 }
6073 else if ( operation.alg != PSA_ALG_CCM )
6074 {
6075 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6076 SIZE_MAX ) );
6077 }
6078
6079 psa_aead_abort( &operation );
6080#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006081
6082 /* ------------------------------------------------------- */
6083
Paul Elliott374a2be2021-07-16 17:53:40 +01006084 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6085
6086 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6087
6088 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6089 PSA_AEAD_NONCE_MAX_SIZE,
6090 &nonce_length ),
6091 PSA_ERROR_BAD_STATE );
6092
6093 psa_aead_abort( &operation );
6094
Paul Elliott7220cae2021-06-22 17:25:57 +01006095 /* Test for generating nonce in decrypt setup. */
6096
Paul Elliott7220cae2021-06-22 17:25:57 +01006097 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6098
6099 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6100 PSA_AEAD_NONCE_MAX_SIZE,
6101 &nonce_length ),
6102 PSA_ERROR_BAD_STATE );
6103
6104 psa_aead_abort( &operation );
6105
Paul Elliottc23a9a02021-06-21 18:32:46 +01006106 /* Test for setting lengths twice. */
6107
Paul Elliottc23a9a02021-06-21 18:32:46 +01006108 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6109
6110 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6111
6112 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6113 input_data->len ) );
6114
6115 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6116 input_data->len ),
6117 PSA_ERROR_BAD_STATE );
6118
6119 psa_aead_abort( &operation );
6120
Andrzej Kurekad837522021-12-15 15:28:49 +01006121 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006122
Paul Elliottc23a9a02021-06-21 18:32:46 +01006123 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6124
6125 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6126
Andrzej Kurekad837522021-12-15 15:28:49 +01006127 if( operation.alg == PSA_ALG_CCM )
6128 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006129
Andrzej Kurekad837522021-12-15 15:28:49 +01006130 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6131 additional_data->len ),
6132 PSA_ERROR_BAD_STATE );
6133 }
6134 else
6135 {
6136 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6137 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006138
Andrzej Kurekad837522021-12-15 15:28:49 +01006139 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6140 input_data->len ),
6141 PSA_ERROR_BAD_STATE );
6142 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006143 psa_aead_abort( &operation );
6144
6145 /* ------------------------------------------------------- */
6146
Paul Elliottf94bd992021-09-19 18:15:59 +01006147 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6148
6149 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6150
Andrzej Kurekad837522021-12-15 15:28:49 +01006151 if( operation.alg == PSA_ALG_CCM )
6152 {
6153 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6154 input_data->len, output_data,
6155 output_size, &output_length ),
6156 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006157
Andrzej Kurekad837522021-12-15 15:28:49 +01006158 }
6159 else
6160 {
6161 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6162 input_data->len, output_data,
6163 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006164
Andrzej Kurekad837522021-12-15 15:28:49 +01006165 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6166 input_data->len ),
6167 PSA_ERROR_BAD_STATE );
6168 }
6169 psa_aead_abort( &operation );
6170
6171 /* ------------------------------------------------------- */
6172
6173 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6174
6175 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6176
6177 if( operation.alg == PSA_ALG_CCM )
6178 {
6179 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6180 finish_output_size,
6181 &output_part_length,
6182 tag_buffer, tag_length,
6183 &tag_size ) );
6184 }
6185 else
6186 {
6187 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6188 finish_output_size,
6189 &output_part_length,
6190 tag_buffer, tag_length,
6191 &tag_size ) );
6192
6193 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6194 input_data->len ),
6195 PSA_ERROR_BAD_STATE );
6196 }
6197 psa_aead_abort( &operation );
6198
6199 /* Test for setting lengths after generating nonce + already starting data. */
6200
6201 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6202
6203 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6204 PSA_AEAD_NONCE_MAX_SIZE,
6205 &nonce_length ) );
6206 if( operation.alg == PSA_ALG_CCM )
6207 {
6208
6209 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6210 additional_data->len ),
6211 PSA_ERROR_BAD_STATE );
6212 }
6213 else
6214 {
6215 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6216 additional_data->len ) );
6217
6218 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6219 input_data->len ),
6220 PSA_ERROR_BAD_STATE );
6221 }
6222 psa_aead_abort( &operation );
6223
6224 /* ------------------------------------------------------- */
6225
6226 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6227
6228 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6229 PSA_AEAD_NONCE_MAX_SIZE,
6230 &nonce_length ) );
6231 if( operation.alg == PSA_ALG_CCM )
6232 {
6233 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6234 input_data->len, output_data,
6235 output_size, &output_length ),
6236 PSA_ERROR_BAD_STATE );
6237
6238 }
6239 else
6240 {
6241 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6242 input_data->len, output_data,
6243 output_size, &output_length ) );
6244
6245 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6246 input_data->len ),
6247 PSA_ERROR_BAD_STATE );
6248 }
6249 psa_aead_abort( &operation );
6250
6251 /* ------------------------------------------------------- */
6252
6253 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6254
6255 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6256 PSA_AEAD_NONCE_MAX_SIZE,
6257 &nonce_length ) );
6258 if( operation.alg == PSA_ALG_CCM )
6259 {
6260 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6261 finish_output_size,
6262 &output_part_length,
6263 tag_buffer, tag_length,
6264 &tag_size ) );
6265 }
6266 else
6267 {
6268 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6269 finish_output_size,
6270 &output_part_length,
6271 tag_buffer, tag_length,
6272 &tag_size ) );
6273
6274 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6275 input_data->len ),
6276 PSA_ERROR_BAD_STATE );
6277 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006278 psa_aead_abort( &operation );
6279
Paul Elliott243080c2021-07-21 19:01:17 +01006280 /* Test for not sending any additional data or data after setting non zero
6281 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006282
Paul Elliottc23a9a02021-06-21 18:32:46 +01006283 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6284
6285 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6286
6287 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6288 input_data->len ) );
6289
6290 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6291 finish_output_size,
6292 &output_part_length,
6293 tag_buffer, tag_length,
6294 &tag_size ),
6295 PSA_ERROR_INVALID_ARGUMENT );
6296
6297 psa_aead_abort( &operation );
6298
Paul Elliott243080c2021-07-21 19:01:17 +01006299 /* Test for not sending any additional data or data after setting non-zero
6300 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006301
Paul Elliottc23a9a02021-06-21 18:32:46 +01006302 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6303
6304 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6305
6306 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6307 input_data->len ) );
6308
6309 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6310 finish_output_size,
6311 &output_part_length,
6312 tag_buffer,
6313 tag_length ),
6314 PSA_ERROR_INVALID_ARGUMENT );
6315
6316 psa_aead_abort( &operation );
6317
Paul Elliott243080c2021-07-21 19:01:17 +01006318 /* Test for not sending any additional data after setting a non-zero length
6319 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006320
Paul Elliottc23a9a02021-06-21 18:32:46 +01006321 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6322
6323 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6324
6325 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6326 input_data->len ) );
6327
6328 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6329 input_data->len, output_data,
6330 output_size, &output_length ),
6331 PSA_ERROR_INVALID_ARGUMENT );
6332
6333 psa_aead_abort( &operation );
6334
Paul Elliottf94bd992021-09-19 18:15:59 +01006335 /* Test for not sending any data after setting a non-zero length for it.*/
6336
Paul Elliottf94bd992021-09-19 18:15:59 +01006337 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6338
6339 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6340
6341 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6342 input_data->len ) );
6343
6344 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6345 additional_data->len ) );
6346
6347 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6348 finish_output_size,
6349 &output_part_length,
6350 tag_buffer, tag_length,
6351 &tag_size ),
6352 PSA_ERROR_INVALID_ARGUMENT );
6353
6354 psa_aead_abort( &operation );
6355
Paul Elliottb0450fe2021-09-01 15:06:26 +01006356 /* Test for sending too much additional data after setting lengths. */
6357
Paul Elliottb0450fe2021-09-01 15:06:26 +01006358 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6359
6360 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6361
6362 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6363
6364
6365 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6366 additional_data->len ),
6367 PSA_ERROR_INVALID_ARGUMENT );
6368
6369 psa_aead_abort( &operation );
6370
Paul Elliotta2a09b02021-09-22 14:56:40 +01006371 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006372
6373 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6374
6375 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6376
6377 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6378 input_data->len ) );
6379
6380 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6381 additional_data->len ) );
6382
6383 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6384 1 ),
6385 PSA_ERROR_INVALID_ARGUMENT );
6386
6387 psa_aead_abort( &operation );
6388
Paul Elliottb0450fe2021-09-01 15:06:26 +01006389 /* Test for sending too much data after setting lengths. */
6390
Paul Elliottb0450fe2021-09-01 15:06:26 +01006391 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6392
6393 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6394
6395 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6396
6397 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6398 input_data->len, output_data,
6399 output_size, &output_length ),
6400 PSA_ERROR_INVALID_ARGUMENT );
6401
6402 psa_aead_abort( &operation );
6403
Paul Elliotta2a09b02021-09-22 14:56:40 +01006404 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006405
6406 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6407
6408 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6409
6410 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6411 input_data->len ) );
6412
6413 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6414 additional_data->len ) );
6415
6416 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6417 input_data->len, output_data,
6418 output_size, &output_length ) );
6419
6420 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6421 1, output_data,
6422 output_size, &output_length ),
6423 PSA_ERROR_INVALID_ARGUMENT );
6424
6425 psa_aead_abort( &operation );
6426
Paul Elliottc23a9a02021-06-21 18:32:46 +01006427 /* Test sending additional data after data. */
6428
Paul Elliottc23a9a02021-06-21 18:32:46 +01006429 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6430
6431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6432
Andrzej Kurekad837522021-12-15 15:28:49 +01006433 if( operation.alg != PSA_ALG_CCM )
6434 {
6435 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6436 input_data->len, output_data,
6437 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006438
Andrzej Kurekad837522021-12-15 15:28:49 +01006439 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6440 additional_data->len ),
6441 PSA_ERROR_BAD_STATE );
6442 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006443 psa_aead_abort( &operation );
6444
Paul Elliott534d0b42021-06-22 19:15:20 +01006445 /* Test calling finish on decryption. */
6446
Paul Elliott534d0b42021-06-22 19:15:20 +01006447 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6448
6449 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6450
6451 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6452 finish_output_size,
6453 &output_part_length,
6454 tag_buffer, tag_length,
6455 &tag_size ),
6456 PSA_ERROR_BAD_STATE );
6457
6458 psa_aead_abort( &operation );
6459
6460 /* Test calling verify on encryption. */
6461
Paul Elliott534d0b42021-06-22 19:15:20 +01006462 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6463
6464 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6465
6466 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6467 finish_output_size,
6468 &output_part_length,
6469 tag_buffer,
6470 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006471 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006472
6473 psa_aead_abort( &operation );
6474
6475
Paul Elliottc23a9a02021-06-21 18:32:46 +01006476exit:
6477 psa_destroy_key( key );
6478 psa_aead_abort( &operation );
6479 mbedtls_free( output_data );
6480 mbedtls_free( final_data );
6481 PSA_DONE( );
6482}
6483/* END_CASE */
6484
6485/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006486void signature_size( int type_arg,
6487 int bits,
6488 int alg_arg,
6489 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006490{
6491 psa_key_type_t type = type_arg;
6492 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006493 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006494
Gilles Peskinefe11b722018-12-18 00:24:04 +01006495 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006496
Gilles Peskinee59236f2018-01-27 23:32:46 +01006497exit:
6498 ;
6499}
6500/* END_CASE */
6501
6502/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006503void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6504 int alg_arg, data_t *input_data,
6505 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006506{
Ronald Cron5425a212020-08-04 14:58:35 +02006507 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006508 psa_key_type_t key_type = key_type_arg;
6509 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006510 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006511 unsigned char *signature = NULL;
6512 size_t signature_size;
6513 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006515
Gilles Peskine8817f612018-12-18 00:18:46 +01006516 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006517
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006518 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006519 psa_set_key_algorithm( &attributes, alg );
6520 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006521
Gilles Peskine049c7532019-05-15 20:22:09 +02006522 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006523 &key ) );
6524 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006525 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006526
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006527 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006528 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006529 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006530 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006531 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006532 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006533 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006534
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006535 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006536 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006537 input_data->x, input_data->len,
6538 signature, signature_size,
6539 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006540 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006541 ASSERT_COMPARE( output_data->x, output_data->len,
6542 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006543
6544exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006545 /*
6546 * Key attributes may have been returned by psa_get_key_attributes()
6547 * thus reset them as required.
6548 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006549 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006550
Ronald Cron5425a212020-08-04 14:58:35 +02006551 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006552 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006553 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006554}
6555/* END_CASE */
6556
6557/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006558void sign_hash_fail( int key_type_arg, data_t *key_data,
6559 int alg_arg, data_t *input_data,
6560 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006561{
Ronald Cron5425a212020-08-04 14:58:35 +02006562 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006563 psa_key_type_t key_type = key_type_arg;
6564 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006565 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006566 psa_status_t actual_status;
6567 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006568 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006569 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006570 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006571
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006572 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006573
Gilles Peskine8817f612018-12-18 00:18:46 +01006574 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006575
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006576 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006577 psa_set_key_algorithm( &attributes, alg );
6578 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006579
Gilles Peskine049c7532019-05-15 20:22:09 +02006580 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006581 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006582
Ronald Cron5425a212020-08-04 14:58:35 +02006583 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006584 input_data->x, input_data->len,
6585 signature, signature_size,
6586 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006587 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006588 /* The value of *signature_length is unspecified on error, but
6589 * whatever it is, it should be less than signature_size, so that
6590 * if the caller tries to read *signature_length bytes without
6591 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006592 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006593
6594exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006595 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006596 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006597 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006598 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006599}
6600/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006601
6602/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006603void sign_verify_hash( int key_type_arg, data_t *key_data,
6604 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006605{
Ronald Cron5425a212020-08-04 14:58:35 +02006606 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006607 psa_key_type_t key_type = key_type_arg;
6608 psa_algorithm_t alg = alg_arg;
6609 size_t key_bits;
6610 unsigned char *signature = NULL;
6611 size_t signature_size;
6612 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006614
Gilles Peskine8817f612018-12-18 00:18:46 +01006615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006616
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006618 psa_set_key_algorithm( &attributes, alg );
6619 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006620
Gilles Peskine049c7532019-05-15 20:22:09 +02006621 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006622 &key ) );
6623 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006624 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006625
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006626 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006627 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006628 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006629 key_bits, alg );
6630 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006631 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006632 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006633
6634 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006635 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006636 input_data->x, input_data->len,
6637 signature, signature_size,
6638 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006639 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006640 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006641 TEST_ASSERT( signature_length > 0 );
6642
6643 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006644 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006645 input_data->x, input_data->len,
6646 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006647
6648 if( input_data->len != 0 )
6649 {
6650 /* Flip a bit in the input and verify that the signature is now
6651 * detected as invalid. Flip a bit at the beginning, not at the end,
6652 * because ECDSA may ignore the last few bits of the input. */
6653 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006654 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006655 input_data->x, input_data->len,
6656 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006657 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006658 }
6659
6660exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006661 /*
6662 * Key attributes may have been returned by psa_get_key_attributes()
6663 * thus reset them as required.
6664 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006665 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006666
Ronald Cron5425a212020-08-04 14:58:35 +02006667 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006668 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006669 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006670}
6671/* END_CASE */
6672
6673/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006674void verify_hash( int key_type_arg, data_t *key_data,
6675 int alg_arg, data_t *hash_data,
6676 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006677{
Ronald Cron5425a212020-08-04 14:58:35 +02006678 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006679 psa_key_type_t key_type = key_type_arg;
6680 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006681 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006682
Gilles Peskine7be11a72022-04-14 00:12:57 +02006683 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006684
Gilles Peskine8817f612018-12-18 00:18:46 +01006685 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006686
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006687 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006688 psa_set_key_algorithm( &attributes, alg );
6689 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006690
Gilles Peskine049c7532019-05-15 20:22:09 +02006691 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006692 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006693
Ronald Cron5425a212020-08-04 14:58:35 +02006694 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006695 hash_data->x, hash_data->len,
6696 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006697
itayzafrir5c753392018-05-08 11:18:38 +03006698exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006699 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006700 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006701 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006702}
6703/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006704
6705/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006706void verify_hash_fail( int key_type_arg, data_t *key_data,
6707 int alg_arg, data_t *hash_data,
6708 data_t *signature_data,
6709 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006710{
Ronald Cron5425a212020-08-04 14:58:35 +02006711 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006712 psa_key_type_t key_type = key_type_arg;
6713 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006714 psa_status_t actual_status;
6715 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006717
Gilles Peskine8817f612018-12-18 00:18:46 +01006718 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006719
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006720 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006721 psa_set_key_algorithm( &attributes, alg );
6722 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006723
Gilles Peskine049c7532019-05-15 20:22:09 +02006724 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006725 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006726
Ronald Cron5425a212020-08-04 14:58:35 +02006727 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006728 hash_data->x, hash_data->len,
6729 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006730 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006731
6732exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006733 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006734 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006735 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006736}
6737/* END_CASE */
6738
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006739/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006740void sign_message_deterministic( int key_type_arg,
6741 data_t *key_data,
6742 int alg_arg,
6743 data_t *input_data,
6744 data_t *output_data )
6745{
6746 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6747 psa_key_type_t key_type = key_type_arg;
6748 psa_algorithm_t alg = alg_arg;
6749 size_t key_bits;
6750 unsigned char *signature = NULL;
6751 size_t signature_size;
6752 size_t signature_length = 0xdeadbeef;
6753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6754
6755 PSA_ASSERT( psa_crypto_init( ) );
6756
6757 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6758 psa_set_key_algorithm( &attributes, alg );
6759 psa_set_key_type( &attributes, key_type );
6760
6761 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6762 &key ) );
6763 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6764 key_bits = psa_get_key_bits( &attributes );
6765
6766 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6767 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006768 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006769 ASSERT_ALLOC( signature, signature_size );
6770
6771 PSA_ASSERT( psa_sign_message( key, alg,
6772 input_data->x, input_data->len,
6773 signature, signature_size,
6774 &signature_length ) );
6775
6776 ASSERT_COMPARE( output_data->x, output_data->len,
6777 signature, signature_length );
6778
6779exit:
6780 psa_reset_key_attributes( &attributes );
6781
6782 psa_destroy_key( key );
6783 mbedtls_free( signature );
6784 PSA_DONE( );
6785
6786}
6787/* END_CASE */
6788
6789/* BEGIN_CASE */
6790void sign_message_fail( int key_type_arg,
6791 data_t *key_data,
6792 int alg_arg,
6793 data_t *input_data,
6794 int signature_size_arg,
6795 int expected_status_arg )
6796{
6797 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6798 psa_key_type_t key_type = key_type_arg;
6799 psa_algorithm_t alg = alg_arg;
6800 size_t signature_size = signature_size_arg;
6801 psa_status_t actual_status;
6802 psa_status_t expected_status = expected_status_arg;
6803 unsigned char *signature = NULL;
6804 size_t signature_length = 0xdeadbeef;
6805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6806
6807 ASSERT_ALLOC( signature, signature_size );
6808
6809 PSA_ASSERT( psa_crypto_init( ) );
6810
6811 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6812 psa_set_key_algorithm( &attributes, alg );
6813 psa_set_key_type( &attributes, key_type );
6814
6815 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6816 &key ) );
6817
6818 actual_status = psa_sign_message( key, alg,
6819 input_data->x, input_data->len,
6820 signature, signature_size,
6821 &signature_length );
6822 TEST_EQUAL( actual_status, expected_status );
6823 /* The value of *signature_length is unspecified on error, but
6824 * whatever it is, it should be less than signature_size, so that
6825 * if the caller tries to read *signature_length bytes without
6826 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006827 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006828
6829exit:
6830 psa_reset_key_attributes( &attributes );
6831 psa_destroy_key( key );
6832 mbedtls_free( signature );
6833 PSA_DONE( );
6834}
6835/* END_CASE */
6836
6837/* BEGIN_CASE */
6838void sign_verify_message( int key_type_arg,
6839 data_t *key_data,
6840 int alg_arg,
6841 data_t *input_data )
6842{
6843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6844 psa_key_type_t key_type = key_type_arg;
6845 psa_algorithm_t alg = alg_arg;
6846 size_t key_bits;
6847 unsigned char *signature = NULL;
6848 size_t signature_size;
6849 size_t signature_length = 0xdeadbeef;
6850 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6851
6852 PSA_ASSERT( psa_crypto_init( ) );
6853
6854 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6855 PSA_KEY_USAGE_VERIFY_MESSAGE );
6856 psa_set_key_algorithm( &attributes, alg );
6857 psa_set_key_type( &attributes, key_type );
6858
6859 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6860 &key ) );
6861 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6862 key_bits = psa_get_key_bits( &attributes );
6863
6864 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6865 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006866 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006867 ASSERT_ALLOC( signature, signature_size );
6868
6869 PSA_ASSERT( psa_sign_message( key, alg,
6870 input_data->x, input_data->len,
6871 signature, signature_size,
6872 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006873 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006874 TEST_ASSERT( signature_length > 0 );
6875
6876 PSA_ASSERT( psa_verify_message( key, alg,
6877 input_data->x, input_data->len,
6878 signature, signature_length ) );
6879
6880 if( input_data->len != 0 )
6881 {
6882 /* Flip a bit in the input and verify that the signature is now
6883 * detected as invalid. Flip a bit at the beginning, not at the end,
6884 * because ECDSA may ignore the last few bits of the input. */
6885 input_data->x[0] ^= 1;
6886 TEST_EQUAL( psa_verify_message( key, alg,
6887 input_data->x, input_data->len,
6888 signature, signature_length ),
6889 PSA_ERROR_INVALID_SIGNATURE );
6890 }
6891
6892exit:
6893 psa_reset_key_attributes( &attributes );
6894
6895 psa_destroy_key( key );
6896 mbedtls_free( signature );
6897 PSA_DONE( );
6898}
6899/* END_CASE */
6900
6901/* BEGIN_CASE */
6902void verify_message( int key_type_arg,
6903 data_t *key_data,
6904 int alg_arg,
6905 data_t *input_data,
6906 data_t *signature_data )
6907{
6908 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6909 psa_key_type_t key_type = key_type_arg;
6910 psa_algorithm_t alg = alg_arg;
6911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6912
Gilles Peskine7be11a72022-04-14 00:12:57 +02006913 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006914
6915 PSA_ASSERT( psa_crypto_init( ) );
6916
6917 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6918 psa_set_key_algorithm( &attributes, alg );
6919 psa_set_key_type( &attributes, key_type );
6920
6921 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6922 &key ) );
6923
6924 PSA_ASSERT( psa_verify_message( key, alg,
6925 input_data->x, input_data->len,
6926 signature_data->x, signature_data->len ) );
6927
6928exit:
6929 psa_reset_key_attributes( &attributes );
6930 psa_destroy_key( key );
6931 PSA_DONE( );
6932}
6933/* END_CASE */
6934
6935/* BEGIN_CASE */
6936void verify_message_fail( int key_type_arg,
6937 data_t *key_data,
6938 int alg_arg,
6939 data_t *hash_data,
6940 data_t *signature_data,
6941 int expected_status_arg )
6942{
6943 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6944 psa_key_type_t key_type = key_type_arg;
6945 psa_algorithm_t alg = alg_arg;
6946 psa_status_t actual_status;
6947 psa_status_t expected_status = expected_status_arg;
6948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6949
6950 PSA_ASSERT( psa_crypto_init( ) );
6951
6952 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6953 psa_set_key_algorithm( &attributes, alg );
6954 psa_set_key_type( &attributes, key_type );
6955
6956 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6957 &key ) );
6958
6959 actual_status = psa_verify_message( key, alg,
6960 hash_data->x, hash_data->len,
6961 signature_data->x,
6962 signature_data->len );
6963 TEST_EQUAL( actual_status, expected_status );
6964
6965exit:
6966 psa_reset_key_attributes( &attributes );
6967 psa_destroy_key( key );
6968 PSA_DONE( );
6969}
6970/* END_CASE */
6971
6972/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006973void asymmetric_encrypt( int key_type_arg,
6974 data_t *key_data,
6975 int alg_arg,
6976 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006977 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006978 int expected_output_length_arg,
6979 int expected_status_arg )
6980{
Ronald Cron5425a212020-08-04 14:58:35 +02006981 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006982 psa_key_type_t key_type = key_type_arg;
6983 psa_algorithm_t alg = alg_arg;
6984 size_t expected_output_length = expected_output_length_arg;
6985 size_t key_bits;
6986 unsigned char *output = NULL;
6987 size_t output_size;
6988 size_t output_length = ~0;
6989 psa_status_t actual_status;
6990 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006992
Gilles Peskine8817f612018-12-18 00:18:46 +01006993 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006994
Gilles Peskine656896e2018-06-29 19:12:28 +02006995 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006996 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6997 psa_set_key_algorithm( &attributes, alg );
6998 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006999 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007000 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007001
7002 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007003 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007004 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007005
Gilles Peskine656896e2018-06-29 19:12:28 +02007006 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007007 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007008 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007009
7010 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007011 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007012 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007013 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007014 output, output_size,
7015 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007016 TEST_EQUAL( actual_status, expected_status );
7017 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007018
Gilles Peskine68428122018-06-30 18:42:41 +02007019 /* If the label is empty, the test framework puts a non-null pointer
7020 * in label->x. Test that a null pointer works as well. */
7021 if( label->len == 0 )
7022 {
7023 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007024 if( output_size != 0 )
7025 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007026 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007027 input_data->x, input_data->len,
7028 NULL, label->len,
7029 output, output_size,
7030 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007031 TEST_EQUAL( actual_status, expected_status );
7032 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007033 }
7034
Gilles Peskine656896e2018-06-29 19:12:28 +02007035exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007036 /*
7037 * Key attributes may have been returned by psa_get_key_attributes()
7038 * thus reset them as required.
7039 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007040 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007041
Ronald Cron5425a212020-08-04 14:58:35 +02007042 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007043 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007044 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007045}
7046/* END_CASE */
7047
7048/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007049void asymmetric_encrypt_decrypt( int key_type_arg,
7050 data_t *key_data,
7051 int alg_arg,
7052 data_t *input_data,
7053 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007054{
Ronald Cron5425a212020-08-04 14:58:35 +02007055 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007056 psa_key_type_t key_type = key_type_arg;
7057 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007058 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007059 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007060 size_t output_size;
7061 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007062 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007063 size_t output2_size;
7064 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007065 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007066
Gilles Peskine8817f612018-12-18 00:18:46 +01007067 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007068
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007069 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7070 psa_set_key_algorithm( &attributes, alg );
7071 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007072
Gilles Peskine049c7532019-05-15 20:22:09 +02007073 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007074 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007075
7076 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007077 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007078 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007079
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007080 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007081 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007082 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007083
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007084 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007085 TEST_LE_U( output2_size,
7086 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7087 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007088 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007089
Gilles Peskineeebd7382018-06-08 18:11:54 +02007090 /* We test encryption by checking that encrypt-then-decrypt gives back
7091 * the original plaintext because of the non-optional random
7092 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007093 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007094 input_data->x, input_data->len,
7095 label->x, label->len,
7096 output, output_size,
7097 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007098 /* We don't know what ciphertext length to expect, but check that
7099 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007100 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007101
Ronald Cron5425a212020-08-04 14:58:35 +02007102 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007103 output, output_length,
7104 label->x, label->len,
7105 output2, output2_size,
7106 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007107 ASSERT_COMPARE( input_data->x, input_data->len,
7108 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007109
7110exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007111 /*
7112 * Key attributes may have been returned by psa_get_key_attributes()
7113 * thus reset them as required.
7114 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007115 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007116
Ronald Cron5425a212020-08-04 14:58:35 +02007117 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007118 mbedtls_free( output );
7119 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007120 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007121}
7122/* END_CASE */
7123
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007124/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007125void asymmetric_decrypt( int key_type_arg,
7126 data_t *key_data,
7127 int alg_arg,
7128 data_t *input_data,
7129 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007130 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007131{
Ronald Cron5425a212020-08-04 14:58:35 +02007132 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007133 psa_key_type_t key_type = key_type_arg;
7134 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007135 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007136 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007137 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007138 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007139 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007140
Gilles Peskine8817f612018-12-18 00:18:46 +01007141 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007142
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007143 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7144 psa_set_key_algorithm( &attributes, alg );
7145 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007146
Gilles Peskine049c7532019-05-15 20:22:09 +02007147 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007148 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007149
gabor-mezei-armceface22021-01-21 12:26:17 +01007150 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7151 key_bits = psa_get_key_bits( &attributes );
7152
7153 /* Determine the maximum ciphertext length */
7154 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007155 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007156 ASSERT_ALLOC( output, output_size );
7157
Ronald Cron5425a212020-08-04 14:58:35 +02007158 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007159 input_data->x, input_data->len,
7160 label->x, label->len,
7161 output,
7162 output_size,
7163 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007164 ASSERT_COMPARE( expected_data->x, expected_data->len,
7165 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007166
Gilles Peskine68428122018-06-30 18:42:41 +02007167 /* If the label is empty, the test framework puts a non-null pointer
7168 * in label->x. Test that a null pointer works as well. */
7169 if( label->len == 0 )
7170 {
7171 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007172 if( output_size != 0 )
7173 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007174 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007175 input_data->x, input_data->len,
7176 NULL, label->len,
7177 output,
7178 output_size,
7179 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007180 ASSERT_COMPARE( expected_data->x, expected_data->len,
7181 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007182 }
7183
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007184exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007185 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007186 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007187 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007188 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007189}
7190/* END_CASE */
7191
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007192/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007193void asymmetric_decrypt_fail( int key_type_arg,
7194 data_t *key_data,
7195 int alg_arg,
7196 data_t *input_data,
7197 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007198 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007199 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007200{
Ronald Cron5425a212020-08-04 14:58:35 +02007201 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007202 psa_key_type_t key_type = key_type_arg;
7203 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007204 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007205 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007206 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007207 psa_status_t actual_status;
7208 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007209 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007210
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007211 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007212
Gilles Peskine8817f612018-12-18 00:18:46 +01007213 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007214
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007215 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7216 psa_set_key_algorithm( &attributes, alg );
7217 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007218
Gilles Peskine049c7532019-05-15 20:22:09 +02007219 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007220 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007221
Ronald Cron5425a212020-08-04 14:58:35 +02007222 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007223 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007224 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007225 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007226 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007227 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007228 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007229
Gilles Peskine68428122018-06-30 18:42:41 +02007230 /* If the label is empty, the test framework puts a non-null pointer
7231 * in label->x. Test that a null pointer works as well. */
7232 if( label->len == 0 )
7233 {
7234 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007235 if( output_size != 0 )
7236 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007237 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007238 input_data->x, input_data->len,
7239 NULL, label->len,
7240 output, output_size,
7241 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007242 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007243 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007244 }
7245
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007246exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007247 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007248 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007249 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007250 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007251}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007252/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007253
7254/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007255void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007256{
7257 /* Test each valid way of initializing the object, except for `= {0}`, as
7258 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7259 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007260 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007261 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007262 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7263 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7264 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007265
7266 memset( &zero, 0, sizeof( zero ) );
7267
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007268 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007269 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007270 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007271 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007272 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007273 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007274 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007275
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007276 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007277 PSA_ASSERT( psa_key_derivation_abort(&func) );
7278 PSA_ASSERT( psa_key_derivation_abort(&init) );
7279 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007280}
7281/* END_CASE */
7282
Janos Follath16de4a42019-06-13 16:32:24 +01007283/* BEGIN_CASE */
7284void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007285{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007286 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007287 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007288 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007289
Gilles Peskine8817f612018-12-18 00:18:46 +01007290 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007291
Janos Follath16de4a42019-06-13 16:32:24 +01007292 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007293 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007294
7295exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007296 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007297 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007298}
7299/* END_CASE */
7300
Janos Follathaf3c2a02019-06-12 12:34:34 +01007301/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007302void derive_set_capacity( int alg_arg, int capacity_arg,
7303 int expected_status_arg )
7304{
7305 psa_algorithm_t alg = alg_arg;
7306 size_t capacity = capacity_arg;
7307 psa_status_t expected_status = expected_status_arg;
7308 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7309
7310 PSA_ASSERT( psa_crypto_init( ) );
7311
7312 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7313
7314 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7315 expected_status );
7316
7317exit:
7318 psa_key_derivation_abort( &operation );
7319 PSA_DONE( );
7320}
7321/* END_CASE */
7322
7323/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007324void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007325 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007326 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007327 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007328 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007329 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007330 int expected_status_arg3,
7331 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007332{
7333 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007334 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7335 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007336 psa_status_t expected_statuses[] = {expected_status_arg1,
7337 expected_status_arg2,
7338 expected_status_arg3};
7339 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007340 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7341 MBEDTLS_SVC_KEY_ID_INIT,
7342 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007343 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7344 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7345 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007346 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007347 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007348 psa_status_t expected_output_status = expected_output_status_arg;
7349 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007350
7351 PSA_ASSERT( psa_crypto_init( ) );
7352
7353 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7354 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007355
7356 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7357
7358 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7359 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007360 mbedtls_test_set_step( i );
7361 if( steps[i] == 0 )
7362 {
7363 /* Skip this step */
7364 }
7365 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007366 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007367 psa_set_key_type( &attributes, key_types[i] );
7368 PSA_ASSERT( psa_import_key( &attributes,
7369 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007370 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007371 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7372 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7373 {
7374 // When taking a private key as secret input, use key agreement
7375 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007376 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7377 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007378 expected_statuses[i] );
7379 }
7380 else
7381 {
7382 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007383 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007384 expected_statuses[i] );
7385 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007386 }
7387 else
7388 {
7389 TEST_EQUAL( psa_key_derivation_input_bytes(
7390 &operation, steps[i],
7391 inputs[i]->x, inputs[i]->len ),
7392 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007393 }
7394 }
7395
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007396 if( output_key_type != PSA_KEY_TYPE_NONE )
7397 {
7398 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007399 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007400 psa_set_key_bits( &attributes, 8 );
7401 actual_output_status =
7402 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007403 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007404 }
7405 else
7406 {
7407 uint8_t buffer[1];
7408 actual_output_status =
7409 psa_key_derivation_output_bytes( &operation,
7410 buffer, sizeof( buffer ) );
7411 }
7412 TEST_EQUAL( actual_output_status, expected_output_status );
7413
Janos Follathaf3c2a02019-06-12 12:34:34 +01007414exit:
7415 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007416 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7417 psa_destroy_key( keys[i] );
7418 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007419 PSA_DONE( );
7420}
7421/* END_CASE */
7422
Janos Follathd958bb72019-07-03 15:02:16 +01007423/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007424void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007425{
Janos Follathd958bb72019-07-03 15:02:16 +01007426 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007427 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007428 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007429 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007430 unsigned char input1[] = "Input 1";
7431 size_t input1_length = sizeof( input1 );
7432 unsigned char input2[] = "Input 2";
7433 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007434 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007435 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007436 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7437 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7438 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007440
Gilles Peskine8817f612018-12-18 00:18:46 +01007441 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007442
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7444 psa_set_key_algorithm( &attributes, alg );
7445 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007446
Gilles Peskine73676cb2019-05-15 20:15:10 +02007447 PSA_ASSERT( psa_import_key( &attributes,
7448 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007449 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007450
7451 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007452 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7453 input1, input1_length,
7454 input2, input2_length,
7455 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007456 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007457
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007458 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007459 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007460 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007461
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007462 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007463
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007464 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007465 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007466
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007467exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007468 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007469 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007470 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007471}
7472/* END_CASE */
7473
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007474/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007475void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007476{
7477 uint8_t output_buffer[16];
7478 size_t buffer_size = 16;
7479 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007480 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007481
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007482 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7483 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007484 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007485
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007486 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007487 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007488
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007489 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007490
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007491 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7492 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007493 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007495 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007496 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007497
7498exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007499 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007500}
7501/* END_CASE */
7502
7503/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007504void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007505 int step1_arg, data_t *input1, int expected_status_arg1,
7506 int step2_arg, data_t *input2, int expected_status_arg2,
7507 int step3_arg, data_t *input3, int expected_status_arg3,
7508 int step4_arg, data_t *input4, int expected_status_arg4,
7509 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007510 int requested_capacity_arg,
7511 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007512 data_t *expected_output2,
7513 int other_key_input_type,
7514 int key_input_type,
7515 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007516{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007517 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007518 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7519 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007520 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7521 MBEDTLS_SVC_KEY_ID_INIT,
7522 MBEDTLS_SVC_KEY_ID_INIT,
7523 MBEDTLS_SVC_KEY_ID_INIT};
7524 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7525 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007526 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007527 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007528 uint8_t *expected_outputs[2] =
7529 {expected_output1->x, expected_output2->x};
7530 size_t output_sizes[2] =
7531 {expected_output1->len, expected_output2->len};
7532 size_t output_buffer_size = 0;
7533 uint8_t *output_buffer = NULL;
7534 size_t expected_capacity;
7535 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007536 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7537 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7538 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7539 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007540 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007541 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007542 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007543
7544 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7545 {
7546 if( output_sizes[i] > output_buffer_size )
7547 output_buffer_size = output_sizes[i];
7548 if( output_sizes[i] == 0 )
7549 expected_outputs[i] = NULL;
7550 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007551 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007552 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007553
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007554 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007555 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7556 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7557 requested_capacity ) );
7558 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007559 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007560 switch( steps[i] )
7561 {
7562 case 0:
7563 break;
7564 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007565 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007566 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007567 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007568 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007569 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007570 inputs[i]->x, inputs[i]->len ),
7571 statuses[i] );
7572
7573 if( statuses[i] != PSA_SUCCESS )
7574 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007575 break;
7576 case 1: // input key
7577 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7578 psa_set_key_algorithm( &attributes1, alg );
7579 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7580
7581 PSA_ASSERT( psa_import_key( &attributes1,
7582 inputs[i]->x, inputs[i]->len,
7583 &keys[i] ) );
7584
Przemek Stekiel38647de2022-04-19 13:27:47 +02007585 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007586 {
7587 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007588 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7589 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007590 }
7591
Przemek Stekiel38647de2022-04-19 13:27:47 +02007592 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7593 steps[i],
7594 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007595 break;
7596 default:
7597 TEST_ASSERT( ! "default case not supported" );
7598 break;
7599 }
7600 break;
7601 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007602 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007603 {
7604 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007605 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7606 steps[i],
7607 inputs[i]->x,
7608 inputs[i]->len ),
7609 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007610 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007611 case 1: // input key, type DERIVE
7612 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007613 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7614 psa_set_key_algorithm( &attributes2, alg );
7615 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7616
7617 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007618 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007619 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7620
7621 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007622 inputs[i]->x, inputs[i]->len,
7623 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007624
Przemek Stekiel38647de2022-04-19 13:27:47 +02007625 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7626 steps[i],
7627 keys[i] ),
7628 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007629 break;
7630 case 2: // key agreement
7631 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7632 psa_set_key_algorithm( &attributes3, alg );
7633 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7634
7635 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007636 inputs[i]->x, inputs[i]->len,
7637 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007638
7639 TEST_EQUAL( psa_key_derivation_key_agreement(
7640 &operation,
7641 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7642 keys[i], key_agreement_peer_key->x,
7643 key_agreement_peer_key->len ), statuses[i] );
7644 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007645 default:
7646 TEST_ASSERT( ! "default case not supported" );
7647 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007648 }
7649
Przemek Stekiel38647de2022-04-19 13:27:47 +02007650 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007651 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007652 break;
7653 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007654 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007655 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007656 inputs[i]->x, inputs[i]->len ), statuses[i] );
7657
7658 if( statuses[i] != PSA_SUCCESS )
7659 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007660 break;
7661 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007662 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007663
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007664 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007665 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007666 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007667 expected_capacity = requested_capacity;
7668
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007669 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007670 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007671 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7672
7673 /* For output key derivation secret must be provided using
7674 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007675 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007676 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007677
7678 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7679 psa_set_key_algorithm( &attributes4, alg );
7680 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007681 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007682
7683 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007684 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007685 }
7686 else // output bytes
7687 {
7688 /* Expansion phase. */
7689 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007690 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007691 /* Read some bytes. */
7692 status = psa_key_derivation_output_bytes( &operation,
7693 output_buffer, output_sizes[i] );
7694 if( expected_capacity == 0 && output_sizes[i] == 0 )
7695 {
7696 /* Reading 0 bytes when 0 bytes are available can go either way. */
7697 TEST_ASSERT( status == PSA_SUCCESS ||
7698 status == PSA_ERROR_INSUFFICIENT_DATA );
7699 continue;
7700 }
7701 else if( expected_capacity == 0 ||
7702 output_sizes[i] > expected_capacity )
7703 {
7704 /* Capacity exceeded. */
7705 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7706 expected_capacity = 0;
7707 continue;
7708 }
7709 /* Success. Check the read data. */
7710 PSA_ASSERT( status );
7711 if( output_sizes[i] != 0 )
7712 ASSERT_COMPARE( output_buffer, output_sizes[i],
7713 expected_outputs[i], output_sizes[i] );
7714 /* Check the operation status. */
7715 expected_capacity -= output_sizes[i];
7716 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7717 &current_capacity ) );
7718 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007719 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007720 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007721 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007722
7723exit:
7724 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007725 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007726 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7727 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007728 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007729 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007730}
7731/* END_CASE */
7732
7733/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007734void derive_full( int alg_arg,
7735 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007736 data_t *input1,
7737 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007738 int requested_capacity_arg )
7739{
Ronald Cron5425a212020-08-04 14:58:35 +02007740 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007741 psa_algorithm_t alg = alg_arg;
7742 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007743 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007744 unsigned char output_buffer[16];
7745 size_t expected_capacity = requested_capacity;
7746 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007748
Gilles Peskine8817f612018-12-18 00:18:46 +01007749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007750
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7752 psa_set_key_algorithm( &attributes, alg );
7753 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007754
Gilles Peskine049c7532019-05-15 20:22:09 +02007755 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007756 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007757
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007758 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7759 input1->x, input1->len,
7760 input2->x, input2->len,
7761 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007762 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007763
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007764 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007765 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007766 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007767
7768 /* Expansion phase. */
7769 while( current_capacity > 0 )
7770 {
7771 size_t read_size = sizeof( output_buffer );
7772 if( read_size > current_capacity )
7773 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007774 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007775 output_buffer,
7776 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007777 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007778 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007779 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007780 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007781 }
7782
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007783 /* Check that the operation refuses to go over capacity. */
7784 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007785 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007786
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007787 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007788
7789exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007790 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007791 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007792 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007793}
7794/* END_CASE */
7795
Janos Follathe60c9052019-07-03 13:51:30 +01007796/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007797void derive_key_exercise( int alg_arg,
7798 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007799 data_t *input1,
7800 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007801 int derived_type_arg,
7802 int derived_bits_arg,
7803 int derived_usage_arg,
7804 int derived_alg_arg )
7805{
Ronald Cron5425a212020-08-04 14:58:35 +02007806 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7807 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007808 psa_algorithm_t alg = alg_arg;
7809 psa_key_type_t derived_type = derived_type_arg;
7810 size_t derived_bits = derived_bits_arg;
7811 psa_key_usage_t derived_usage = derived_usage_arg;
7812 psa_algorithm_t derived_alg = derived_alg_arg;
7813 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007814 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007816 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007817
Gilles Peskine8817f612018-12-18 00:18:46 +01007818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007819
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007820 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7821 psa_set_key_algorithm( &attributes, alg );
7822 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007823 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007824 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007825
7826 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007827 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7828 input1->x, input1->len,
7829 input2->x, input2->len,
7830 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007831 goto exit;
7832
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007833 psa_set_key_usage_flags( &attributes, derived_usage );
7834 psa_set_key_algorithm( &attributes, derived_alg );
7835 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007836 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007837 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007838 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007839
7840 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007841 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007842 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7843 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007844
7845 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007846 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007847 goto exit;
7848
7849exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007850 /*
7851 * Key attributes may have been returned by psa_get_key_attributes()
7852 * thus reset them as required.
7853 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007854 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007855
7856 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007857 psa_destroy_key( base_key );
7858 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007859 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007860}
7861/* END_CASE */
7862
Janos Follath42fd8882019-07-03 14:17:09 +01007863/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007864void derive_key_export( int alg_arg,
7865 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007866 data_t *input1,
7867 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007868 int bytes1_arg,
7869 int bytes2_arg )
7870{
Ronald Cron5425a212020-08-04 14:58:35 +02007871 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7872 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007873 psa_algorithm_t alg = alg_arg;
7874 size_t bytes1 = bytes1_arg;
7875 size_t bytes2 = bytes2_arg;
7876 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007877 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007878 uint8_t *output_buffer = NULL;
7879 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007880 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7881 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007882 size_t length;
7883
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007884 ASSERT_ALLOC( output_buffer, capacity );
7885 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007886 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007887
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007888 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7889 psa_set_key_algorithm( &base_attributes, alg );
7890 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007891 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007892 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007893
7894 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007895 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7896 input1->x, input1->len,
7897 input2->x, input2->len,
7898 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007899 goto exit;
7900
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007901 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007902 output_buffer,
7903 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007904 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007905
7906 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007907 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7908 input1->x, input1->len,
7909 input2->x, input2->len,
7910 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007911 goto exit;
7912
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007913 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7914 psa_set_key_algorithm( &derived_attributes, 0 );
7915 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007916 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007917 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007918 &derived_key ) );
7919 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007920 export_buffer, bytes1,
7921 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007922 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007923 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007924 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007925 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007926 &derived_key ) );
7927 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007928 export_buffer + bytes1, bytes2,
7929 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007930 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007931
7932 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007933 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7934 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007935
7936exit:
7937 mbedtls_free( output_buffer );
7938 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007939 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007940 psa_destroy_key( base_key );
7941 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007942 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007943}
7944/* END_CASE */
7945
7946/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007947void derive_key_type( int alg_arg,
7948 data_t *key_data,
7949 data_t *input1,
7950 data_t *input2,
7951 int key_type_arg, int bits_arg,
7952 data_t *expected_export )
7953{
7954 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7955 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7956 const psa_algorithm_t alg = alg_arg;
7957 const psa_key_type_t key_type = key_type_arg;
7958 const size_t bits = bits_arg;
7959 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7960 const size_t export_buffer_size =
7961 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
7962 uint8_t *export_buffer = NULL;
7963 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7964 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7965 size_t export_length;
7966
7967 ASSERT_ALLOC( export_buffer, export_buffer_size );
7968 PSA_ASSERT( psa_crypto_init( ) );
7969
7970 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7971 psa_set_key_algorithm( &base_attributes, alg );
7972 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7973 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
7974 &base_key ) );
7975
Przemek Stekielc85f0912022-03-08 11:37:54 +01007976 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007977 &operation, base_key, alg,
7978 input1->x, input1->len,
7979 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01007980 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007981 goto exit;
7982
7983 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7984 psa_set_key_algorithm( &derived_attributes, 0 );
7985 psa_set_key_type( &derived_attributes, key_type );
7986 psa_set_key_bits( &derived_attributes, bits );
7987 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
7988 &derived_key ) );
7989
7990 PSA_ASSERT( psa_export_key( derived_key,
7991 export_buffer, export_buffer_size,
7992 &export_length ) );
7993 ASSERT_COMPARE( export_buffer, export_length,
7994 expected_export->x, expected_export->len );
7995
7996exit:
7997 mbedtls_free( export_buffer );
7998 psa_key_derivation_abort( &operation );
7999 psa_destroy_key( base_key );
8000 psa_destroy_key( derived_key );
8001 PSA_DONE( );
8002}
8003/* END_CASE */
8004
8005/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008006void derive_key( int alg_arg,
8007 data_t *key_data, data_t *input1, data_t *input2,
8008 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008009 int expected_status_arg,
8010 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008011{
Ronald Cron5425a212020-08-04 14:58:35 +02008012 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8013 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008014 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008015 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008016 size_t bits = bits_arg;
8017 psa_status_t expected_status = expected_status_arg;
8018 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8019 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8020 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8021
8022 PSA_ASSERT( psa_crypto_init( ) );
8023
8024 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8025 psa_set_key_algorithm( &base_attributes, alg );
8026 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8027 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008028 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008029
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008030 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8031 input1->x, input1->len,
8032 input2->x, input2->len,
8033 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008034 goto exit;
8035
8036 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8037 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008038 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008039 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008040
8041 psa_status_t status =
8042 psa_key_derivation_output_key( &derived_attributes,
8043 &operation,
8044 &derived_key );
8045 if( is_large_output > 0 )
8046 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8047 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008048
8049exit:
8050 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008051 psa_destroy_key( base_key );
8052 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008053 PSA_DONE( );
8054}
8055/* END_CASE */
8056
8057/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008058void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008059 int our_key_type_arg, int our_key_alg_arg,
8060 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008061 int expected_status_arg )
8062{
Ronald Cron5425a212020-08-04 14:58:35 +02008063 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008064 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008065 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008066 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008067 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008068 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008069 psa_status_t expected_status = expected_status_arg;
8070 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008071
Gilles Peskine8817f612018-12-18 00:18:46 +01008072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008073
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008074 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008075 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008076 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008077 PSA_ASSERT( psa_import_key( &attributes,
8078 our_key_data->x, our_key_data->len,
8079 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008080
Gilles Peskine77f40d82019-04-11 21:27:06 +02008081 /* The tests currently include inputs that should fail at either step.
8082 * Test cases that fail at the setup step should be changed to call
8083 * key_derivation_setup instead, and this function should be renamed
8084 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008085 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008086 if( status == PSA_SUCCESS )
8087 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008088 TEST_EQUAL( psa_key_derivation_key_agreement(
8089 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8090 our_key,
8091 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008092 expected_status );
8093 }
8094 else
8095 {
8096 TEST_ASSERT( status == expected_status );
8097 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008098
8099exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008100 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008101 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008102 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008103}
8104/* END_CASE */
8105
8106/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008107void raw_key_agreement( int alg_arg,
8108 int our_key_type_arg, data_t *our_key_data,
8109 data_t *peer_key_data,
8110 data_t *expected_output )
8111{
Ronald Cron5425a212020-08-04 14:58:35 +02008112 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008113 psa_algorithm_t alg = alg_arg;
8114 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008115 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008116 unsigned char *output = NULL;
8117 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008118 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008119
Gilles Peskinef0cba732019-04-11 22:12:38 +02008120 PSA_ASSERT( psa_crypto_init( ) );
8121
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008122 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8123 psa_set_key_algorithm( &attributes, alg );
8124 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008125 PSA_ASSERT( psa_import_key( &attributes,
8126 our_key_data->x, our_key_data->len,
8127 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008128
gabor-mezei-armceface22021-01-21 12:26:17 +01008129 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8130 key_bits = psa_get_key_bits( &attributes );
8131
Gilles Peskine992bee82022-04-13 23:25:52 +02008132 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008133 TEST_LE_U( expected_output->len,
8134 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8135 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8136 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008137
8138 /* Good case with exact output size */
8139 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008140 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8141 peer_key_data->x, peer_key_data->len,
8142 output, expected_output->len,
8143 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008144 ASSERT_COMPARE( output, output_length,
8145 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008146 mbedtls_free( output );
8147 output = NULL;
8148 output_length = ~0;
8149
8150 /* Larger buffer */
8151 ASSERT_ALLOC( output, expected_output->len + 1 );
8152 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8153 peer_key_data->x, peer_key_data->len,
8154 output, expected_output->len + 1,
8155 &output_length ) );
8156 ASSERT_COMPARE( output, output_length,
8157 expected_output->x, expected_output->len );
8158 mbedtls_free( output );
8159 output = NULL;
8160 output_length = ~0;
8161
8162 /* Buffer too small */
8163 ASSERT_ALLOC( output, expected_output->len - 1 );
8164 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8165 peer_key_data->x, peer_key_data->len,
8166 output, expected_output->len - 1,
8167 &output_length ),
8168 PSA_ERROR_BUFFER_TOO_SMALL );
8169 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008170 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008171 mbedtls_free( output );
8172 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008173
8174exit:
8175 mbedtls_free( output );
8176 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008177 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008178}
8179/* END_CASE */
8180
8181/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008182void key_agreement_capacity( int alg_arg,
8183 int our_key_type_arg, data_t *our_key_data,
8184 data_t *peer_key_data,
8185 int expected_capacity_arg )
8186{
Ronald Cron5425a212020-08-04 14:58:35 +02008187 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008188 psa_algorithm_t alg = alg_arg;
8189 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008190 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008192 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008193 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008194
Gilles Peskine8817f612018-12-18 00:18:46 +01008195 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008196
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008197 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8198 psa_set_key_algorithm( &attributes, alg );
8199 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008200 PSA_ASSERT( psa_import_key( &attributes,
8201 our_key_data->x, our_key_data->len,
8202 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008203
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008204 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008205 PSA_ASSERT( psa_key_derivation_key_agreement(
8206 &operation,
8207 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8208 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008209 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8210 {
8211 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008212 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008213 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008214 NULL, 0 ) );
8215 }
Gilles Peskine59685592018-09-18 12:11:34 +02008216
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008217 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008218 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008219 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008220 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008221
Gilles Peskinebf491972018-10-25 22:36:12 +02008222 /* Test the actual capacity by reading the output. */
8223 while( actual_capacity > sizeof( output ) )
8224 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008225 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008226 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008227 actual_capacity -= sizeof( output );
8228 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008229 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008230 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008231 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008232 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008233
Gilles Peskine59685592018-09-18 12:11:34 +02008234exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008235 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008236 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008237 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008238}
8239/* END_CASE */
8240
8241/* BEGIN_CASE */
8242void key_agreement_output( int alg_arg,
8243 int our_key_type_arg, data_t *our_key_data,
8244 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008245 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008246{
Ronald Cron5425a212020-08-04 14:58:35 +02008247 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008248 psa_algorithm_t alg = alg_arg;
8249 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008250 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008252 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008253
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008254 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8255 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008256
Gilles Peskine8817f612018-12-18 00:18:46 +01008257 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008258
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008259 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8260 psa_set_key_algorithm( &attributes, alg );
8261 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008262 PSA_ASSERT( psa_import_key( &attributes,
8263 our_key_data->x, our_key_data->len,
8264 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008265
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008266 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008267 PSA_ASSERT( psa_key_derivation_key_agreement(
8268 &operation,
8269 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8270 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008271 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8272 {
8273 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008274 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008275 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008276 NULL, 0 ) );
8277 }
Gilles Peskine59685592018-09-18 12:11:34 +02008278
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008279 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008280 actual_output,
8281 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008282 ASSERT_COMPARE( actual_output, expected_output1->len,
8283 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008284 if( expected_output2->len != 0 )
8285 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008286 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008287 actual_output,
8288 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008289 ASSERT_COMPARE( actual_output, expected_output2->len,
8290 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008291 }
Gilles Peskine59685592018-09-18 12:11:34 +02008292
8293exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008294 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008295 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008296 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008297 mbedtls_free( actual_output );
8298}
8299/* END_CASE */
8300
8301/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008302void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008303{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008304 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008305 unsigned char *output = NULL;
8306 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008307 size_t i;
8308 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008309
Simon Butcher49f8e312020-03-03 15:51:50 +00008310 TEST_ASSERT( bytes_arg >= 0 );
8311
Gilles Peskine91892022021-02-08 19:50:26 +01008312 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008313 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008314
Gilles Peskine8817f612018-12-18 00:18:46 +01008315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008316
Gilles Peskinea50d7392018-06-21 10:22:13 +02008317 /* Run several times, to ensure that every output byte will be
8318 * nonzero at least once with overwhelming probability
8319 * (2^(-8*number_of_runs)). */
8320 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008321 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008322 if( bytes != 0 )
8323 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008324 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008325
Gilles Peskinea50d7392018-06-21 10:22:13 +02008326 for( i = 0; i < bytes; i++ )
8327 {
8328 if( output[i] != 0 )
8329 ++changed[i];
8330 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008331 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008332
8333 /* Check that every byte was changed to nonzero at least once. This
8334 * validates that psa_generate_random is overwriting every byte of
8335 * the output buffer. */
8336 for( i = 0; i < bytes; i++ )
8337 {
8338 TEST_ASSERT( changed[i] != 0 );
8339 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008340
8341exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008342 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008343 mbedtls_free( output );
8344 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008345}
8346/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008347
8348/* BEGIN_CASE */
8349void generate_key( int type_arg,
8350 int bits_arg,
8351 int usage_arg,
8352 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008353 int expected_status_arg,
8354 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008355{
Ronald Cron5425a212020-08-04 14:58:35 +02008356 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008357 psa_key_type_t type = type_arg;
8358 psa_key_usage_t usage = usage_arg;
8359 size_t bits = bits_arg;
8360 psa_algorithm_t alg = alg_arg;
8361 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008362 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008363 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008364
Gilles Peskine8817f612018-12-18 00:18:46 +01008365 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008366
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008367 psa_set_key_usage_flags( &attributes, usage );
8368 psa_set_key_algorithm( &attributes, alg );
8369 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008370 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008371
8372 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008373 psa_status_t status = psa_generate_key( &attributes, &key );
8374
8375 if( is_large_key > 0 )
8376 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8377 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008378 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008379 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008380
8381 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008382 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008383 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8384 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008385
Gilles Peskine818ca122018-06-20 18:16:48 +02008386 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008387 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008388 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008389
8390exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008391 /*
8392 * Key attributes may have been returned by psa_get_key_attributes()
8393 * thus reset them as required.
8394 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008395 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008396
Ronald Cron5425a212020-08-04 14:58:35 +02008397 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008398 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008399}
8400/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008401
Ronald Cronee414c72021-03-18 18:50:08 +01008402/* 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 +02008403void generate_key_rsa( int bits_arg,
8404 data_t *e_arg,
8405 int expected_status_arg )
8406{
Ronald Cron5425a212020-08-04 14:58:35 +02008407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008408 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008409 size_t bits = bits_arg;
8410 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8411 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8412 psa_status_t expected_status = expected_status_arg;
8413 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8414 uint8_t *exported = NULL;
8415 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008416 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008417 size_t exported_length = SIZE_MAX;
8418 uint8_t *e_read_buffer = NULL;
8419 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008420 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008421 size_t e_read_length = SIZE_MAX;
8422
8423 if( e_arg->len == 0 ||
8424 ( e_arg->len == 3 &&
8425 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8426 {
8427 is_default_public_exponent = 1;
8428 e_read_size = 0;
8429 }
8430 ASSERT_ALLOC( e_read_buffer, e_read_size );
8431 ASSERT_ALLOC( exported, exported_size );
8432
8433 PSA_ASSERT( psa_crypto_init( ) );
8434
8435 psa_set_key_usage_flags( &attributes, usage );
8436 psa_set_key_algorithm( &attributes, alg );
8437 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8438 e_arg->x, e_arg->len ) );
8439 psa_set_key_bits( &attributes, bits );
8440
8441 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008442 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008443 if( expected_status != PSA_SUCCESS )
8444 goto exit;
8445
8446 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008447 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008448 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8449 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8450 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8451 e_read_buffer, e_read_size,
8452 &e_read_length ) );
8453 if( is_default_public_exponent )
8454 TEST_EQUAL( e_read_length, 0 );
8455 else
8456 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8457
8458 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008459 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008460 goto exit;
8461
8462 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008463 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008464 exported, exported_size,
8465 &exported_length ) );
8466 {
8467 uint8_t *p = exported;
8468 uint8_t *end = exported + exported_length;
8469 size_t len;
8470 /* RSAPublicKey ::= SEQUENCE {
8471 * modulus INTEGER, -- n
8472 * publicExponent INTEGER } -- e
8473 */
8474 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008475 MBEDTLS_ASN1_SEQUENCE |
8476 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008477 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008478 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8479 MBEDTLS_ASN1_INTEGER ) );
8480 if( len >= 1 && p[0] == 0 )
8481 {
8482 ++p;
8483 --len;
8484 }
8485 if( e_arg->len == 0 )
8486 {
8487 TEST_EQUAL( len, 3 );
8488 TEST_EQUAL( p[0], 1 );
8489 TEST_EQUAL( p[1], 0 );
8490 TEST_EQUAL( p[2], 1 );
8491 }
8492 else
8493 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8494 }
8495
8496exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008497 /*
8498 * Key attributes may have been returned by psa_get_key_attributes() or
8499 * set by psa_set_key_domain_parameters() thus reset them as required.
8500 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008501 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008502
Ronald Cron5425a212020-08-04 14:58:35 +02008503 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008504 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008505 mbedtls_free( e_read_buffer );
8506 mbedtls_free( exported );
8507}
8508/* END_CASE */
8509
Darryl Greend49a4992018-06-18 17:27:26 +01008510/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008511void persistent_key_load_key_from_storage( data_t *data,
8512 int type_arg, int bits_arg,
8513 int usage_flags_arg, int alg_arg,
8514 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008515{
Ronald Cron71016a92020-08-28 19:01:50 +02008516 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008518 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8519 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008520 psa_key_type_t type = type_arg;
8521 size_t bits = bits_arg;
8522 psa_key_usage_t usage_flags = usage_flags_arg;
8523 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008524 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008525 unsigned char *first_export = NULL;
8526 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008527 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008528 size_t first_exported_length;
8529 size_t second_exported_length;
8530
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008531 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8532 {
8533 ASSERT_ALLOC( first_export, export_size );
8534 ASSERT_ALLOC( second_export, export_size );
8535 }
Darryl Greend49a4992018-06-18 17:27:26 +01008536
Gilles Peskine8817f612018-12-18 00:18:46 +01008537 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008538
Gilles Peskinec87af662019-05-15 16:12:22 +02008539 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008540 psa_set_key_usage_flags( &attributes, usage_flags );
8541 psa_set_key_algorithm( &attributes, alg );
8542 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008543 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008544
Darryl Green0c6575a2018-11-07 16:05:30 +00008545 switch( generation_method )
8546 {
8547 case IMPORT_KEY:
8548 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008549 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008550 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008551 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008552
Darryl Green0c6575a2018-11-07 16:05:30 +00008553 case GENERATE_KEY:
8554 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008555 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008556 break;
8557
8558 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008559#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008560 {
8561 /* Create base key */
8562 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8563 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8564 psa_set_key_usage_flags( &base_attributes,
8565 PSA_KEY_USAGE_DERIVE );
8566 psa_set_key_algorithm( &base_attributes, derive_alg );
8567 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008568 PSA_ASSERT( psa_import_key( &base_attributes,
8569 data->x, data->len,
8570 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008571 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008572 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008573 PSA_ASSERT( psa_key_derivation_input_key(
8574 &operation,
8575 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008576 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008577 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008578 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008579 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8580 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008581 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008582 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008583 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008584 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008585 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008586#else
8587 TEST_ASSUME( ! "KDF not supported in this configuration" );
8588#endif
8589 break;
8590
8591 default:
8592 TEST_ASSERT( ! "generation_method not implemented in test" );
8593 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008594 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008595 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008596
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008597 /* Export the key if permitted by the key policy. */
8598 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8599 {
Ronald Cron5425a212020-08-04 14:58:35 +02008600 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008601 first_export, export_size,
8602 &first_exported_length ) );
8603 if( generation_method == IMPORT_KEY )
8604 ASSERT_COMPARE( data->x, data->len,
8605 first_export, first_exported_length );
8606 }
Darryl Greend49a4992018-06-18 17:27:26 +01008607
8608 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008609 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008610 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008611 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008612
Darryl Greend49a4992018-06-18 17:27:26 +01008613 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008614 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008615 TEST_ASSERT( mbedtls_svc_key_id_equal(
8616 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008617 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8618 PSA_KEY_LIFETIME_PERSISTENT );
8619 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8620 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008621 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008622 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008623 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008624
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008625 /* Export the key again if permitted by the key policy. */
8626 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008627 {
Ronald Cron5425a212020-08-04 14:58:35 +02008628 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008629 second_export, export_size,
8630 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008631 ASSERT_COMPARE( first_export, first_exported_length,
8632 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008633 }
8634
8635 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008636 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008637 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008638
8639exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008640 /*
8641 * Key attributes may have been returned by psa_get_key_attributes()
8642 * thus reset them as required.
8643 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008644 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008645
Darryl Greend49a4992018-06-18 17:27:26 +01008646 mbedtls_free( first_export );
8647 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008648 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008649 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008650 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008651 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008652}
8653/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008654
Neil Armstronga557cb82022-06-10 08:58:32 +02008655/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008656void ecjpake_setup( int alg_arg, int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008657 int input_first, data_t *pw_data,
Neil Armstrongd597bc72022-05-25 11:28:39 +02008658 int expected_status_arg )
8659{
8660 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8661 psa_pake_operation_t operation = psa_pake_operation_init();
8662 psa_algorithm_t alg = alg_arg;
8663 psa_algorithm_t hash_alg = hash_arg;
8664 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008665 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8666 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8667 psa_status_t expected_status = expected_status_arg;
8668 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8669 unsigned char *output_buffer = NULL;
8670 size_t output_len = 0;
8671
8672 PSA_INIT( );
8673
8674 ASSERT_ALLOC( output_buffer,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008675 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8676 PSA_PAKE_STEP_KEY_SHARE) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008677
8678 if( pw_data->len > 0 )
8679 {
8680 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8681 psa_set_key_algorithm( &attributes, alg );
8682 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8683 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8684 &key ) );
8685 }
8686
8687 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8688 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8689 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8690
Neil Armstrong645cccd2022-06-08 17:36:23 +02008691 PSA_ASSERT( psa_pake_abort( &operation ) );
8692
8693 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8694 PSA_ERROR_BAD_STATE );
8695 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8696 PSA_ERROR_BAD_STATE );
8697 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8698 PSA_ERROR_BAD_STATE );
8699 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8700 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008701 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8702 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008703 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008704 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008705 PSA_ERROR_BAD_STATE );
8706
8707 PSA_ASSERT( psa_pake_abort( &operation ) );
8708
Neil Armstrongd597bc72022-05-25 11:28:39 +02008709 status = psa_pake_setup( &operation, &cipher_suite );
8710 if( status != PSA_SUCCESS )
8711 {
8712 TEST_EQUAL( status, expected_status );
8713 goto exit;
8714 }
8715 else
8716 PSA_ASSERT( status );
8717
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008718 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8719 PSA_ERROR_BAD_STATE );
8720
Neil Armstrongd597bc72022-05-25 11:28:39 +02008721 status = psa_pake_set_role( &operation, role );
8722 if( status != PSA_SUCCESS )
8723 {
8724 TEST_EQUAL( status, expected_status );
8725 goto exit;
8726 }
8727 else
8728 PSA_ASSERT( status );
8729
8730 if( pw_data->len > 0 )
8731 {
8732 status = psa_pake_set_password_key( &operation, key );
8733 if( status != PSA_SUCCESS )
8734 {
8735 TEST_EQUAL( status, expected_status );
8736 goto exit;
8737 }
8738 else
8739 PSA_ASSERT( status );
8740 }
8741
Neil Armstrong707d9572022-06-08 17:31:49 +02008742 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8743 PSA_ERROR_INVALID_ARGUMENT );
8744 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8745 PSA_ERROR_INVALID_ARGUMENT );
8746
8747 const uint8_t unsupported_id[] = "abcd";
8748
8749 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8750 PSA_ERROR_NOT_SUPPORTED );
8751 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8752 PSA_ERROR_NOT_SUPPORTED );
8753
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008754 /* First round */
8755 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008756 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008757 /* Invalid parameters */
8758 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8759 NULL, 0 ),
8760 PSA_ERROR_INVALID_ARGUMENT );
8761 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8762 output_buffer, 66 ),
8763 PSA_ERROR_INVALID_ARGUMENT );
8764 /* Invalid first step */
8765 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8766 output_buffer, 66 ),
8767 PSA_ERROR_BAD_STATE );
8768
8769 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8770 output_buffer, 66 ),
8771 expected_status);
8772
8773 if( expected_status == PSA_SUCCESS )
8774 {
8775 /* Buffer too large */
8776 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8777 output_buffer, 512 ),
8778 PSA_ERROR_INSUFFICIENT_MEMORY );
8779
8780 /* The operation should be aborted at this point */
8781 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8782 output_buffer, 66 ),
8783 PSA_ERROR_BAD_STATE );
8784 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008785 }
8786 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008787 {
8788 /* Invalid parameters */
8789 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8790 NULL, 0, NULL ),
8791 PSA_ERROR_INVALID_ARGUMENT );
8792 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8793 output_buffer, 512, &output_len ),
8794 PSA_ERROR_INVALID_ARGUMENT );
8795 /* Invalid first step */
8796 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8797 output_buffer, 512, &output_len ),
8798 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008799
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008800 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8801 output_buffer, 512, &output_len ),
8802 expected_status );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008803
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008804 if( expected_status == PSA_SUCCESS )
8805 {
8806 TEST_ASSERT( output_len > 0 );
8807
8808 /* Buffer too small */
8809 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8810 output_buffer, 5, &output_len ),
8811 PSA_ERROR_BUFFER_TOO_SMALL );
8812
8813 /* The operation should be aborted at this point */
8814 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8815 output_buffer, 512, &output_len ),
8816 PSA_ERROR_BAD_STATE );
8817 }
8818 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008819
8820exit:
8821 PSA_ASSERT( psa_destroy_key( key ) );
8822 PSA_ASSERT( psa_pake_abort( &operation ) );
8823 mbedtls_free( output_buffer );
8824 PSA_DONE( );
8825}
8826/* END_CASE */
8827
Neil Armstronga557cb82022-06-10 08:58:32 +02008828/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008829void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8830 int client_input_first, int inject_error,
8831 data_t *pw_data )
8832{
8833 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8834 psa_pake_operation_t server = psa_pake_operation_init();
8835 psa_pake_operation_t client = psa_pake_operation_init();
8836 psa_algorithm_t alg = alg_arg;
8837 psa_algorithm_t hash_alg = hash_arg;
8838 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8839 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8840
8841 PSA_INIT( );
8842
8843 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8844 psa_set_key_algorithm( &attributes, alg );
8845 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8846 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8847 &key ) );
8848
8849 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8850 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8851 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8852
8853
8854 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8855 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8856
8857 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8858 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8859
8860 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8861 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8862
8863 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8864 client_input_first, 1,
8865 inject_error ), 1 );
8866
8867 if( inject_error == 1 || inject_error == 2 )
8868 goto exit;
8869
8870 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8871 client_input_first, 2,
8872 inject_error ), 1 );
8873
8874exit:
8875 psa_destroy_key( key );
8876 psa_pake_abort( &server );
8877 psa_pake_abort( &client );
8878 PSA_DONE( );
8879}
8880/* END_CASE */
8881
8882/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008883void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008884 int derive_alg_arg, data_t *pw_data,
8885 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008886{
8887 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8888 psa_pake_operation_t server = psa_pake_operation_init();
8889 psa_pake_operation_t client = psa_pake_operation_init();
8890 psa_algorithm_t alg = alg_arg;
8891 psa_algorithm_t hash_alg = hash_arg;
8892 psa_algorithm_t derive_alg = derive_alg_arg;
8893 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8895 psa_key_derivation_operation_t server_derive =
8896 PSA_KEY_DERIVATION_OPERATION_INIT;
8897 psa_key_derivation_operation_t client_derive =
8898 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008899
8900 PSA_INIT( );
8901
Neil Armstrongd597bc72022-05-25 11:28:39 +02008902 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8903 psa_set_key_algorithm( &attributes, alg );
8904 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8905 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8906 &key ) );
8907
8908 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8909 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8910 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8911
Neil Armstrong1e855602022-06-15 11:32:11 +02008912 /* Get shared key */
8913 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
8914 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
8915
8916 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
8917 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
8918 {
8919 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
8920 PSA_KEY_DERIVATION_INPUT_SEED,
8921 (const uint8_t*) "", 0) );
8922 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
8923 PSA_KEY_DERIVATION_INPUT_SEED,
8924 (const uint8_t*) "", 0) );
8925 }
8926
Neil Armstrongd597bc72022-05-25 11:28:39 +02008927 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8928 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8929
8930 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8931 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8932
8933 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8934 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8935
Neil Armstrong1e855602022-06-15 11:32:11 +02008936 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8937 PSA_ERROR_BAD_STATE );
8938 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8939 PSA_ERROR_BAD_STATE );
8940
Neil Armstrongf983caf2022-06-15 15:27:48 +02008941 /* First round */
8942 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8943 client_input_first, 1, 0 ), 1 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008944
Neil Armstrong1e855602022-06-15 11:32:11 +02008945 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8946 PSA_ERROR_BAD_STATE );
8947 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8948 PSA_ERROR_BAD_STATE );
8949
Neil Armstrongf983caf2022-06-15 15:27:48 +02008950 /* Second round */
8951 TEST_EQUAL( ecjpake_do_round( alg, primitive_arg, &server, &client,
8952 client_input_first, 2, 0 ), 1 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008953
Neil Armstrongd597bc72022-05-25 11:28:39 +02008954 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
8955 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
8956
8957exit:
8958 psa_key_derivation_abort( &server_derive );
8959 psa_key_derivation_abort( &client_derive );
8960 psa_destroy_key( key );
8961 psa_pake_abort( &server );
8962 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008963 PSA_DONE( );
8964}
8965/* END_CASE */