blob: caa5987405b08f88845c5f5ccc7b6e5fb486cf51 [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
Gilles Peskinee59236f2018-01-27 23:32:46 +0100708/* END_HEADER */
709
710/* BEGIN_DEPENDENCIES
711 * depends_on:MBEDTLS_PSA_CRYPTO_C
712 * END_DEPENDENCIES
713 */
714
715/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200716void static_checks( )
717{
718 size_t max_truncated_mac_size =
719 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
720
721 /* Check that the length for a truncated MAC always fits in the algorithm
722 * encoding. The shifted mask is the maximum truncated value. The
723 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +0200724 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200725}
726/* END_CASE */
727
728/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200729void import_with_policy( int type_arg,
730 int usage_arg, int alg_arg,
731 int expected_status_arg )
732{
733 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
734 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200735 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200736 psa_key_type_t type = type_arg;
737 psa_key_usage_t usage = usage_arg;
738 psa_algorithm_t alg = alg_arg;
739 psa_status_t expected_status = expected_status_arg;
740 const uint8_t key_material[16] = {0};
741 psa_status_t status;
742
743 PSA_ASSERT( psa_crypto_init( ) );
744
745 psa_set_key_type( &attributes, type );
746 psa_set_key_usage_flags( &attributes, usage );
747 psa_set_key_algorithm( &attributes, alg );
748
749 status = psa_import_key( &attributes,
750 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200751 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200752 TEST_EQUAL( status, expected_status );
753 if( status != PSA_SUCCESS )
754 goto exit;
755
Ronald Cron5425a212020-08-04 14:58:35 +0200756 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200757 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200758 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +0200759 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200760 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200761 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200762
Ronald Cron5425a212020-08-04 14:58:35 +0200763 PSA_ASSERT( psa_destroy_key( key ) );
764 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200765
766exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100767 /*
768 * Key attributes may have been returned by psa_get_key_attributes()
769 * thus reset them as required.
770 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200771 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100772
773 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200774 PSA_DONE( );
775}
776/* END_CASE */
777
778/* BEGIN_CASE */
779void import_with_data( data_t *data, int type_arg,
780 int attr_bits_arg,
781 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200782{
783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
784 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200785 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200786 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200787 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200788 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790
Gilles Peskine8817f612018-12-18 00:18:46 +0100791 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100792
Gilles Peskine4747d192019-04-17 15:05:45 +0200793 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200794 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200795
Ronald Cron5425a212020-08-04 14:58:35 +0200796 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100797 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200798 if( status != PSA_SUCCESS )
799 goto exit;
800
Ronald Cron5425a212020-08-04 14:58:35 +0200801 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200802 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200803 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200804 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200805 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200806
Ronald Cron5425a212020-08-04 14:58:35 +0200807 PSA_ASSERT( psa_destroy_key( key ) );
808 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100809
810exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100811 /*
812 * Key attributes may have been returned by psa_get_key_attributes()
813 * thus reset them as required.
814 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200815 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100816
817 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200818 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819}
820/* END_CASE */
821
822/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200823void import_large_key( int type_arg, int byte_size_arg,
824 int expected_status_arg )
825{
826 psa_key_type_t type = type_arg;
827 size_t byte_size = byte_size_arg;
828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
829 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200830 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200831 psa_status_t status;
832 uint8_t *buffer = NULL;
833 size_t buffer_size = byte_size + 1;
834 size_t n;
835
Steven Cooreman69967ce2021-01-18 18:01:08 +0100836 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800837 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +0100838 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200839 memset( buffer, 'K', byte_size );
840
841 PSA_ASSERT( psa_crypto_init( ) );
842
843 /* Try importing the key */
844 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
845 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200846 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100847 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200848 TEST_EQUAL( status, expected_status );
849
850 if( status == PSA_SUCCESS )
851 {
Ronald Cron5425a212020-08-04 14:58:35 +0200852 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200853 TEST_EQUAL( psa_get_key_type( &attributes ), type );
854 TEST_EQUAL( psa_get_key_bits( &attributes ),
855 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200856 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200857 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200858 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200859 for( n = 0; n < byte_size; n++ )
860 TEST_EQUAL( buffer[n], 'K' );
861 for( n = byte_size; n < buffer_size; n++ )
862 TEST_EQUAL( buffer[n], 0 );
863 }
864
865exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100866 /*
867 * Key attributes may have been returned by psa_get_key_attributes()
868 * thus reset them as required.
869 */
870 psa_reset_key_attributes( &attributes );
871
Ronald Cron5425a212020-08-04 14:58:35 +0200872 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200873 PSA_DONE( );
874 mbedtls_free( buffer );
875}
876/* END_CASE */
877
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100878/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200879void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
880{
Ronald Cron5425a212020-08-04 14:58:35 +0200881 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200882 size_t bits = bits_arg;
883 psa_status_t expected_status = expected_status_arg;
884 psa_status_t status;
885 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200886 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200887 size_t buffer_size = /* Slight overapproximations */
888 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200889 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200890 unsigned char *p;
891 int ret;
892 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200893 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200894
Gilles Peskine8817f612018-12-18 00:18:46 +0100895 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200896 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200897
898 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
899 bits, keypair ) ) >= 0 );
900 length = ret;
901
902 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200903 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200904 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100905 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200906
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200907 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200908 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200909
910exit:
911 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200912 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200913}
914/* END_CASE */
915
916/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300917void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300918 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200919 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +0530920 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921 int expected_bits,
922 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200923 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100924 int canonical_input )
925{
Ronald Cron5425a212020-08-04 14:58:35 +0200926 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100927 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200928 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200929 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100930 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +0530931 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100932 unsigned char *exported = NULL;
933 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100934 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100935 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100936 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200938 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939
Moran Pekercb088e72018-07-17 17:36:59 +0300940 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200941 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200943 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945
Archana4d7ae1d2021-07-07 02:50:22 +0530946 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +0200947 psa_set_key_usage_flags( &attributes, usage_arg );
948 psa_set_key_algorithm( &attributes, alg );
949 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700950
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200952 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953
954 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200955 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200956 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
957 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200958 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959
960 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200961 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100962 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100963
964 /* The exported length must be set by psa_export_key() to a value between 0
965 * and export_size. On errors, the exported length must be 0. */
966 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
967 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200968 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100969
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200970 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200971 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200973 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100974 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200976 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977
Gilles Peskineea38a922021-02-13 00:05:16 +0100978 /* Run sanity checks on the exported key. For non-canonical inputs,
979 * this validates the canonical representations. For canonical inputs,
980 * this doesn't directly validate the implementation, but it still helps
981 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +0530982 if( !psa_key_lifetime_is_external( lifetime ) )
983 {
984 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
985 goto exit;
986 }
Gilles Peskine8f609232018-08-11 01:24:55 +0200987
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200989 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990 else
991 {
Ronald Cron5425a212020-08-04 14:58:35 +0200992 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200993 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200994 &key2 ) );
995 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100996 reexported,
997 export_size,
998 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200999 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301000 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001001 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001003 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301004 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301005 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001006 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007
1008destroy:
1009 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001010 PSA_ASSERT( psa_destroy_key( key ) );
1011 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012
1013exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001014 /*
1015 * Key attributes may have been returned by psa_get_key_attributes()
1016 * thus reset them as required.
1017 */
1018 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301019 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001020 mbedtls_free( exported );
1021 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001022 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023}
1024/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001025
Moran Pekerf709f4a2018-06-06 17:26:04 +03001026/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001027void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001028 int type_arg,
1029 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301030 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001031 int export_size_delta,
1032 int expected_export_status_arg,
1033 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001034{
Ronald Cron5425a212020-08-04 14:58:35 +02001035 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001036 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001037 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001038 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001039 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301040 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001041 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001042 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001043 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045
Gilles Peskine8817f612018-12-18 00:18:46 +01001046 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001047
Archana4d7ae1d2021-07-07 02:50:22 +05301048 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001049 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1050 psa_set_key_algorithm( &attributes, alg );
1051 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052
1053 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001054 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001055
Gilles Peskine49c25912018-10-29 15:15:31 +01001056 /* Export the public key */
1057 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001058 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001059 exported, export_size,
1060 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001061 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001062 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001063 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001064 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001065 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001066 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001067 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001068 TEST_LE_U( expected_public_key->len,
1069 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1070 TEST_LE_U( expected_public_key->len,
1071 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1072 TEST_LE_U( expected_public_key->len,
1073 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001074 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1075 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001076 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001077exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001078 /*
1079 * Key attributes may have been returned by psa_get_key_attributes()
1080 * thus reset them as required.
1081 */
1082 psa_reset_key_attributes( &attributes );
1083
itayzafrir3e02b3b2018-06-12 17:06:52 +03001084 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001085 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001086 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001087}
1088/* END_CASE */
1089
Gilles Peskine20035e32018-02-03 22:44:14 +01001090/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001091void import_and_exercise_key( data_t *data,
1092 int type_arg,
1093 int bits_arg,
1094 int alg_arg )
1095{
Ronald Cron5425a212020-08-04 14:58:35 +02001096 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001097 psa_key_type_t type = type_arg;
1098 size_t bits = bits_arg;
1099 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001100 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001101 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001102 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001103
Gilles Peskine8817f612018-12-18 00:18:46 +01001104 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001105
Gilles Peskine4747d192019-04-17 15:05:45 +02001106 psa_set_key_usage_flags( &attributes, usage );
1107 psa_set_key_algorithm( &attributes, alg );
1108 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001109
1110 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001111 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001112
1113 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001114 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001115 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1116 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001117
1118 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001119 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001120 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001121
Ronald Cron5425a212020-08-04 14:58:35 +02001122 PSA_ASSERT( psa_destroy_key( key ) );
1123 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001124
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001125exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001126 /*
1127 * Key attributes may have been returned by psa_get_key_attributes()
1128 * thus reset them as required.
1129 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001130 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001131
1132 psa_reset_key_attributes( &attributes );
1133 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001134 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001135}
1136/* END_CASE */
1137
1138/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001139void effective_key_attributes( int type_arg, int expected_type_arg,
1140 int bits_arg, int expected_bits_arg,
1141 int usage_arg, int expected_usage_arg,
1142 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001143{
Ronald Cron5425a212020-08-04 14:58:35 +02001144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001145 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001146 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001147 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001148 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001149 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001150 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001151 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001152 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001154
Gilles Peskine8817f612018-12-18 00:18:46 +01001155 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001156
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001157 psa_set_key_usage_flags( &attributes, usage );
1158 psa_set_key_algorithm( &attributes, alg );
1159 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001160 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001161
Ronald Cron5425a212020-08-04 14:58:35 +02001162 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001163 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001164
Ronald Cron5425a212020-08-04 14:58:35 +02001165 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001166 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1167 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1168 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1169 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001170
1171exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001172 /*
1173 * Key attributes may have been returned by psa_get_key_attributes()
1174 * thus reset them as required.
1175 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001176 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001177
1178 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001179 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001180}
1181/* END_CASE */
1182
1183/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001184void check_key_policy( int type_arg, int bits_arg,
1185 int usage_arg, int alg_arg )
1186{
1187 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001188 usage_arg,
1189 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001190 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001191 goto exit;
1192}
1193/* END_CASE */
1194
1195/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001196void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001197{
1198 /* Test each valid way of initializing the object, except for `= {0}`, as
1199 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1200 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001201 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001202 psa_key_attributes_t func = psa_key_attributes_init( );
1203 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1204 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001205
1206 memset( &zero, 0, sizeof( zero ) );
1207
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001208 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1209 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1210 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001211
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001212 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1213 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1214 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1215
1216 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1217 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1218 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1219
1220 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1221 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1222 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1223
1224 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1225 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1226 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001231void mac_key_policy( int policy_usage_arg,
1232 int policy_alg_arg,
1233 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001234 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001235 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001236 int expected_status_sign_arg,
1237 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001238{
Ronald Cron5425a212020-08-04 14:58:35 +02001239 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001241 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001242 psa_key_type_t key_type = key_type_arg;
1243 psa_algorithm_t policy_alg = policy_alg_arg;
1244 psa_algorithm_t exercise_alg = exercise_alg_arg;
1245 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001246 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001247 psa_status_t expected_status_sign = expected_status_sign_arg;
1248 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001249 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001250
Gilles Peskine8817f612018-12-18 00:18:46 +01001251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001252
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001253 psa_set_key_usage_flags( &attributes, policy_usage );
1254 psa_set_key_algorithm( &attributes, policy_alg );
1255 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001256
Gilles Peskine049c7532019-05-15 20:22:09 +02001257 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001258 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001259
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001260 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1261 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001262
Ronald Cron5425a212020-08-04 14:58:35 +02001263 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001264 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001265
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001266 /* Calculate the MAC, one-shot case. */
1267 uint8_t input[128] = {0};
1268 size_t mac_len;
1269 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1270 input, 128,
1271 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1272 expected_status_sign );
1273
Neil Armstrong3af9b972022-02-07 12:20:21 +01001274 /* Calculate the MAC, multi-part case. */
1275 PSA_ASSERT( psa_mac_abort( &operation ) );
1276 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1277 if( status == PSA_SUCCESS )
1278 {
1279 status = psa_mac_update( &operation, input, 128 );
1280 if( status == PSA_SUCCESS )
1281 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1282 &mac_len ),
1283 expected_status_sign );
1284 else
1285 TEST_EQUAL( status, expected_status_sign );
1286 }
1287 else
1288 {
1289 TEST_EQUAL( status, expected_status_sign );
1290 }
1291 PSA_ASSERT( psa_mac_abort( &operation ) );
1292
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001293 /* Verify correct MAC, one-shot case. */
1294 status = psa_mac_verify( key, exercise_alg, input, 128,
1295 mac, mac_len );
1296
1297 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1298 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001299 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001300 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001301
Neil Armstrong3af9b972022-02-07 12:20:21 +01001302 /* Verify correct MAC, multi-part case. */
1303 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1304 if( status == PSA_SUCCESS )
1305 {
1306 status = psa_mac_update( &operation, input, 128 );
1307 if( status == PSA_SUCCESS )
1308 {
1309 status = psa_mac_verify_finish( &operation, mac, mac_len );
1310 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1311 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1312 else
1313 TEST_EQUAL( status, expected_status_verify );
1314 }
1315 else
1316 {
1317 TEST_EQUAL( status, expected_status_verify );
1318 }
1319 }
1320 else
1321 {
1322 TEST_EQUAL( status, expected_status_verify );
1323 }
1324
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001325 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001326
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001327 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001328 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001329 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
1331exit:
1332 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001333 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001334 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001335}
1336/* END_CASE */
1337
1338/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001339void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001340 int policy_alg,
1341 int key_type,
1342 data_t *key_data,
1343 int exercise_alg )
1344{
Ronald Cron5425a212020-08-04 14:58:35 +02001345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001346 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001347 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001348 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001349 size_t output_buffer_size = 0;
1350 size_t input_buffer_size = 0;
1351 size_t output_length = 0;
1352 uint8_t *output = NULL;
1353 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354 psa_status_t status;
1355
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001356 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1357 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1358 input_buffer_size );
1359
1360 ASSERT_ALLOC( input, input_buffer_size );
1361 ASSERT_ALLOC( output, output_buffer_size );
1362
Gilles Peskine8817f612018-12-18 00:18:46 +01001363 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001365 psa_set_key_usage_flags( &attributes, policy_usage );
1366 psa_set_key_algorithm( &attributes, policy_alg );
1367 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001368
Gilles Peskine049c7532019-05-15 20:22:09 +02001369 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001370 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001371
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001372 /* Check if no key usage flag implication is done */
1373 TEST_EQUAL( policy_usage,
1374 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001375
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001376 /* Encrypt check, one-shot */
1377 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1378 output, output_buffer_size,
1379 &output_length);
1380 if( policy_alg == exercise_alg &&
1381 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1382 PSA_ASSERT( status );
1383 else
1384 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1385
1386 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001387 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001388 if( policy_alg == exercise_alg &&
1389 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001391 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001392 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001393 psa_cipher_abort( &operation );
1394
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001395 /* Decrypt check, one-shot */
1396 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1397 input, input_buffer_size,
1398 &output_length);
1399 if( policy_alg == exercise_alg &&
1400 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1401 PSA_ASSERT( status );
1402 else
1403 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1404
1405 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001406 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001407 if( policy_alg == exercise_alg &&
1408 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001410 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001411 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001412
1413exit:
1414 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001415 mbedtls_free( input );
1416 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001417 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001418 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001419}
1420/* END_CASE */
1421
1422/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001423void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001424 int policy_alg,
1425 int key_type,
1426 data_t *key_data,
1427 int nonce_length_arg,
1428 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001429 int exercise_alg,
1430 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001431{
Ronald Cron5425a212020-08-04 14:58:35 +02001432 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001433 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001434 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001435 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001437 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438 unsigned char nonce[16] = {0};
1439 size_t nonce_length = nonce_length_arg;
1440 unsigned char tag[16];
1441 size_t tag_length = tag_length_arg;
1442 size_t output_length;
1443
Gilles Peskine7be11a72022-04-14 00:12:57 +02001444 TEST_LE_U( nonce_length, sizeof( nonce ) );
1445 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001446
Gilles Peskine8817f612018-12-18 00:18:46 +01001447 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001448
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001449 psa_set_key_usage_flags( &attributes, policy_usage );
1450 psa_set_key_algorithm( &attributes, policy_alg );
1451 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001452
Gilles Peskine049c7532019-05-15 20:22:09 +02001453 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001454 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001455
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001456 /* Check if no key usage implication is done */
1457 TEST_EQUAL( policy_usage,
1458 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001459
Neil Armstrong752d8112022-02-07 14:51:11 +01001460 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02001461 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001462 nonce, nonce_length,
1463 NULL, 0,
1464 NULL, 0,
1465 tag, tag_length,
1466 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001467 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1468 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001470 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471
Neil Armstrong752d8112022-02-07 14:51:11 +01001472 /* Encrypt check, multi-part */
1473 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
1474 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1475 TEST_EQUAL( status, expected_status );
1476 else
1477 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1478
1479 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001480 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001481 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 nonce, nonce_length,
1483 NULL, 0,
1484 tag, tag_length,
1485 NULL, 0,
1486 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001487 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1488 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1489 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001490 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001491 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001492 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001493
Neil Armstrong752d8112022-02-07 14:51:11 +01001494 /* Decrypt check, multi-part */
1495 PSA_ASSERT( psa_aead_abort( &operation ) );
1496 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
1497 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
1498 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1499 else
1500 TEST_EQUAL( status, expected_status );
1501
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001502exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01001503 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001504 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001505 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001510void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 int policy_alg,
1512 int key_type,
1513 data_t *key_data,
1514 int exercise_alg )
1515{
Ronald Cron5425a212020-08-04 14:58:35 +02001516 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001517 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001518 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001519 psa_status_t status;
1520 size_t key_bits;
1521 size_t buffer_length;
1522 unsigned char *buffer = NULL;
1523 size_t output_length;
1524
Gilles Peskine8817f612018-12-18 00:18:46 +01001525 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001527 psa_set_key_usage_flags( &attributes, policy_usage );
1528 psa_set_key_algorithm( &attributes, policy_alg );
1529 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001530
Gilles Peskine049c7532019-05-15 20:22:09 +02001531 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001532 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001533
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001534 /* Check if no key usage implication is done */
1535 TEST_EQUAL( policy_usage,
1536 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001537
Ronald Cron5425a212020-08-04 14:58:35 +02001538 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001539 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1541 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001542 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001543
Ronald Cron5425a212020-08-04 14:58:35 +02001544 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001545 NULL, 0,
1546 NULL, 0,
1547 buffer, buffer_length,
1548 &output_length );
1549 if( policy_alg == exercise_alg &&
1550 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001551 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001552 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001553 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001554
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001555 if( buffer_length != 0 )
1556 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001557 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558 buffer, buffer_length,
1559 NULL, 0,
1560 buffer, buffer_length,
1561 &output_length );
1562 if( policy_alg == exercise_alg &&
1563 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001564 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001565 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001566 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001567
1568exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001569 /*
1570 * Key attributes may have been returned by psa_get_key_attributes()
1571 * thus reset them as required.
1572 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001573 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001574
1575 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001576 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 mbedtls_free( buffer );
1578}
1579/* END_CASE */
1580
1581/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001582void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001583 int policy_alg,
1584 int key_type,
1585 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001586 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001587 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001588 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001589{
Ronald Cron5425a212020-08-04 14:58:35 +02001590 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001591 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001592 psa_key_usage_t policy_usage = policy_usage_arg;
1593 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001594 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001595 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1596 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1597 * compatible with the policy and `payload_length_arg` is supposed to be
1598 * a valid input length to sign. If `payload_length_arg <= 0`,
1599 * `exercise_alg` is supposed to be forbidden by the policy. */
1600 int compatible_alg = payload_length_arg > 0;
1601 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001602 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 size_t signature_length;
1604
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001605 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001606 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001607 TEST_EQUAL( expected_usage,
1608 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001609
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001612 psa_set_key_usage_flags( &attributes, policy_usage );
1613 psa_set_key_algorithm( &attributes, policy_alg );
1614 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615
Gilles Peskine049c7532019-05-15 20:22:09 +02001616 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001617 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001618
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001619 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1620
Ronald Cron5425a212020-08-04 14:58:35 +02001621 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001622 payload, payload_length,
1623 signature, sizeof( signature ),
1624 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001625 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001626 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001628 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629
1630 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001631 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001632 payload, payload_length,
1633 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001634 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001635 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001637 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001638
Gilles Peskinef7b41372021-09-22 16:15:05 +02001639 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02001640 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001641 {
1642 status = psa_sign_message( key, exercise_alg,
1643 payload, payload_length,
1644 signature, sizeof( signature ),
1645 &signature_length );
1646 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
1647 PSA_ASSERT( status );
1648 else
1649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1650
1651 memset( signature, 0, sizeof( signature ) );
1652 status = psa_verify_message( key, exercise_alg,
1653 payload, payload_length,
1654 signature, sizeof( signature ) );
1655 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
1656 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1657 else
1658 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1659 }
1660
Gilles Peskined5b33222018-06-18 22:20:03 +02001661exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001662 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001663 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001664}
1665/* END_CASE */
1666
Janos Follathba3fab92019-06-11 14:50:16 +01001667/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001668void derive_key_policy( int policy_usage,
1669 int policy_alg,
1670 int key_type,
1671 data_t *key_data,
1672 int exercise_alg )
1673{
Ronald Cron5425a212020-08-04 14:58:35 +02001674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001675 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001676 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001677 psa_status_t status;
1678
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001680
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001681 psa_set_key_usage_flags( &attributes, policy_usage );
1682 psa_set_key_algorithm( &attributes, policy_alg );
1683 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001684
Gilles Peskine049c7532019-05-15 20:22:09 +02001685 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001686 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001687
Janos Follathba3fab92019-06-11 14:50:16 +01001688 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1689
1690 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1691 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001692 {
Janos Follathba3fab92019-06-11 14:50:16 +01001693 PSA_ASSERT( psa_key_derivation_input_bytes(
1694 &operation,
1695 PSA_KEY_DERIVATION_INPUT_SEED,
1696 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001697 }
Janos Follathba3fab92019-06-11 14:50:16 +01001698
1699 status = psa_key_derivation_input_key( &operation,
1700 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001701 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001702
Gilles Peskineea0fb492018-07-12 17:17:20 +02001703 if( policy_alg == exercise_alg &&
1704 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001706 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001707 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001708
1709exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001710 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001711 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001712 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001713}
1714/* END_CASE */
1715
1716/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001717void agreement_key_policy( int policy_usage,
1718 int policy_alg,
1719 int key_type_arg,
1720 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001721 int exercise_alg,
1722 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001723{
Ronald Cron5425a212020-08-04 14:58:35 +02001724 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001726 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001727 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001728 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001729 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001730
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001732
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001733 psa_set_key_usage_flags( &attributes, policy_usage );
1734 psa_set_key_algorithm( &attributes, policy_alg );
1735 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001736
Gilles Peskine049c7532019-05-15 20:22:09 +02001737 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001738 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001739
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001740 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001741 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001742
Steven Cooremance48e852020-10-05 16:02:45 +02001743 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001744
1745exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001746 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001747 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001748 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001749}
1750/* END_CASE */
1751
1752/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001753void key_policy_alg2( int key_type_arg, data_t *key_data,
1754 int usage_arg, int alg_arg, int alg2_arg )
1755{
Ronald Cron5425a212020-08-04 14:58:35 +02001756 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001757 psa_key_type_t key_type = key_type_arg;
1758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1759 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1760 psa_key_usage_t usage = usage_arg;
1761 psa_algorithm_t alg = alg_arg;
1762 psa_algorithm_t alg2 = alg2_arg;
1763
1764 PSA_ASSERT( psa_crypto_init( ) );
1765
1766 psa_set_key_usage_flags( &attributes, usage );
1767 psa_set_key_algorithm( &attributes, alg );
1768 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1769 psa_set_key_type( &attributes, key_type );
1770 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001771 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001772
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001773 /* Update the usage flags to obtain implicit usage flags */
1774 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001775 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001776 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1777 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1778 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1779
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001780 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001781 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001782 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001783 goto exit;
1784
1785exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001786 /*
1787 * Key attributes may have been returned by psa_get_key_attributes()
1788 * thus reset them as required.
1789 */
1790 psa_reset_key_attributes( &got_attributes );
1791
Ronald Cron5425a212020-08-04 14:58:35 +02001792 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001793 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001794}
1795/* END_CASE */
1796
1797/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001798void raw_agreement_key_policy( int policy_usage,
1799 int policy_alg,
1800 int key_type_arg,
1801 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001802 int exercise_alg,
1803 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001804{
Ronald Cron5425a212020-08-04 14:58:35 +02001805 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001807 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001808 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001809 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001810 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001811
1812 PSA_ASSERT( psa_crypto_init( ) );
1813
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814 psa_set_key_usage_flags( &attributes, policy_usage );
1815 psa_set_key_algorithm( &attributes, policy_alg );
1816 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001817
Gilles Peskine049c7532019-05-15 20:22:09 +02001818 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001819 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001820
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001821 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001822
Steven Cooremance48e852020-10-05 16:02:45 +02001823 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001824
1825exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001826 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001827 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001828 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001829}
1830/* END_CASE */
1831
1832/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001833void copy_success( int source_usage_arg,
1834 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301835 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001836 int type_arg, data_t *material,
1837 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001838 int target_usage_arg,
1839 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301840 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001841 int expected_usage_arg,
1842 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001843{
Gilles Peskineca25db92019-04-19 11:43:08 +02001844 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1845 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001846 psa_key_usage_t expected_usage = expected_usage_arg;
1847 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001848 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05301849 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
1850 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001851 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1852 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001853 uint8_t *export_buffer = NULL;
1854
Gilles Peskine57ab7212019-01-28 13:03:09 +01001855 PSA_ASSERT( psa_crypto_init( ) );
1856
Gilles Peskineca25db92019-04-19 11:43:08 +02001857 /* Prepare the source key. */
1858 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1859 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001860 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001861 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301862 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02001863 PSA_ASSERT( psa_import_key( &source_attributes,
1864 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001865 &source_key ) );
1866 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001867
Gilles Peskineca25db92019-04-19 11:43:08 +02001868 /* Prepare the target attributes. */
1869 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001870 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001871 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001872 }
Archana8a180362021-07-05 02:18:48 +05301873 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02001874
Gilles Peskineca25db92019-04-19 11:43:08 +02001875 if( target_usage_arg != -1 )
1876 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1877 if( target_alg_arg != -1 )
1878 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001879 if( target_alg2_arg != -1 )
1880 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001881
Archana8a180362021-07-05 02:18:48 +05301882
Gilles Peskine57ab7212019-01-28 13:03:09 +01001883 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001884 PSA_ASSERT( psa_copy_key( source_key,
1885 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001886
1887 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001888 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001889
1890 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001891 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001892 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1893 psa_get_key_type( &target_attributes ) );
1894 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1895 psa_get_key_bits( &target_attributes ) );
1896 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1897 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001898 TEST_EQUAL( expected_alg2,
1899 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001900 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1901 {
1902 size_t length;
1903 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001904 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001905 material->len, &length ) );
1906 ASSERT_COMPARE( material->x, material->len,
1907 export_buffer, length );
1908 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001909
Archana8a180362021-07-05 02:18:48 +05301910 if( !psa_key_lifetime_is_external( target_lifetime ) )
1911 {
1912 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
1913 goto exit;
1914 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
1915 goto exit;
1916 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01001917
Ronald Cron5425a212020-08-04 14:58:35 +02001918 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001919
1920exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001921 /*
1922 * Source and target key attributes may have been returned by
1923 * psa_get_key_attributes() thus reset them as required.
1924 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001925 psa_reset_key_attributes( &source_attributes );
1926 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001927
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001928 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001929 mbedtls_free( export_buffer );
1930}
1931/* END_CASE */
1932
1933/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001934void copy_fail( int source_usage_arg,
1935 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05301936 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001937 int type_arg, data_t *material,
1938 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001939 int target_usage_arg,
1940 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001941 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001942 int expected_status_arg )
1943{
1944 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1945 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001946 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1947 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001948 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001949
1950 PSA_ASSERT( psa_crypto_init( ) );
1951
1952 /* Prepare the source key. */
1953 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1954 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001955 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001956 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05301957 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001958 PSA_ASSERT( psa_import_key( &source_attributes,
1959 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001960 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001961
1962 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001963 psa_set_key_id( &target_attributes, key_id );
1964 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001965 psa_set_key_type( &target_attributes, target_type_arg );
1966 psa_set_key_bits( &target_attributes, target_bits_arg );
1967 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1968 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001969 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001970
1971 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001972 TEST_EQUAL( psa_copy_key( source_key,
1973 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001974 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001975
Ronald Cron5425a212020-08-04 14:58:35 +02001976 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001977
Gilles Peskine4a644642019-05-03 17:14:08 +02001978exit:
1979 psa_reset_key_attributes( &source_attributes );
1980 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001981 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001982}
1983/* END_CASE */
1984
1985/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001986void hash_operation_init( )
1987{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001988 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001989 /* Test each valid way of initializing the object, except for `= {0}`, as
1990 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1991 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001992 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001993 psa_hash_operation_t func = psa_hash_operation_init( );
1994 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1995 psa_hash_operation_t zero;
1996
1997 memset( &zero, 0, sizeof( zero ) );
1998
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001999 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002000 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2001 PSA_ERROR_BAD_STATE );
2002 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2003 PSA_ERROR_BAD_STATE );
2004 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2005 PSA_ERROR_BAD_STATE );
2006
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002007 /* A default hash operation should be abortable without error. */
2008 PSA_ASSERT( psa_hash_abort( &func ) );
2009 PSA_ASSERT( psa_hash_abort( &init ) );
2010 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002011}
2012/* END_CASE */
2013
2014/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002015void hash_setup( int alg_arg,
2016 int expected_status_arg )
2017{
2018 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002019 uint8_t *output = NULL;
2020 size_t output_size = 0;
2021 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002022 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002023 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002024 psa_status_t status;
2025
Gilles Peskine8817f612018-12-18 00:18:46 +01002026 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002027
Neil Armstrongedb20862022-02-07 15:47:44 +01002028 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002029 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002030 ASSERT_ALLOC( output, output_size );
2031
2032 status = psa_hash_compute( alg, NULL, 0,
2033 output, output_size, &output_length );
2034 TEST_EQUAL( status, expected_status );
2035
2036 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002037 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002038 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002039
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002040 /* Whether setup succeeded or failed, abort must succeed. */
2041 PSA_ASSERT( psa_hash_abort( &operation ) );
2042
2043 /* If setup failed, reproduce the failure, so as to
2044 * test the resulting state of the operation object. */
2045 if( status != PSA_SUCCESS )
2046 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2047
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002048 /* Now the operation object should be reusable. */
2049#if defined(KNOWN_SUPPORTED_HASH_ALG)
2050 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2051 PSA_ASSERT( psa_hash_abort( &operation ) );
2052#endif
2053
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002054exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002055 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002056 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002061void hash_compute_fail( int alg_arg, data_t *input,
2062 int output_size_arg, int expected_status_arg )
2063{
2064 psa_algorithm_t alg = alg_arg;
2065 uint8_t *output = NULL;
2066 size_t output_size = output_size_arg;
2067 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002068 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002069 psa_status_t expected_status = expected_status_arg;
2070 psa_status_t status;
2071
2072 ASSERT_ALLOC( output, output_size );
2073
2074 PSA_ASSERT( psa_crypto_init( ) );
2075
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002076 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002077 status = psa_hash_compute( alg, input->x, input->len,
2078 output, output_size, &output_length );
2079 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002080 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002081
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002082 /* Hash Compute, multi-part */
2083 status = psa_hash_setup( &operation, alg );
2084 if( status == PSA_SUCCESS )
2085 {
2086 status = psa_hash_update( &operation, input->x, input->len );
2087 if( status == PSA_SUCCESS )
2088 {
2089 status = psa_hash_finish( &operation, output, output_size,
2090 &output_length );
2091 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002092 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002093 else
2094 TEST_EQUAL( status, expected_status );
2095 }
2096 else
2097 {
2098 TEST_EQUAL( status, expected_status );
2099 }
2100 }
2101 else
2102 {
2103 TEST_EQUAL( status, expected_status );
2104 }
2105
Gilles Peskine0a749c82019-11-28 19:33:58 +01002106exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002107 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002108 mbedtls_free( output );
2109 PSA_DONE( );
2110}
2111/* END_CASE */
2112
2113/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002114void hash_compare_fail( int alg_arg, data_t *input,
2115 data_t *reference_hash,
2116 int expected_status_arg )
2117{
2118 psa_algorithm_t alg = alg_arg;
2119 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002120 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002121 psa_status_t status;
2122
2123 PSA_ASSERT( psa_crypto_init( ) );
2124
Neil Armstrong55a1be12022-02-07 11:23:20 +01002125 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002126 status = psa_hash_compare( alg, input->x, input->len,
2127 reference_hash->x, reference_hash->len );
2128 TEST_EQUAL( status, expected_status );
2129
Neil Armstrong55a1be12022-02-07 11:23:20 +01002130 /* Hash Compare, multi-part */
2131 status = psa_hash_setup( &operation, alg );
2132 if( status == PSA_SUCCESS )
2133 {
2134 status = psa_hash_update( &operation, input->x, input->len );
2135 if( status == PSA_SUCCESS )
2136 {
2137 status = psa_hash_verify( &operation, reference_hash->x,
2138 reference_hash->len );
2139 TEST_EQUAL( status, expected_status );
2140 }
2141 else
2142 {
2143 TEST_EQUAL( status, expected_status );
2144 }
2145 }
2146 else
2147 {
2148 TEST_EQUAL( status, expected_status );
2149 }
2150
Gilles Peskine88e08462020-01-28 20:43:00 +01002151exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002152 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002153 PSA_DONE( );
2154}
2155/* END_CASE */
2156
2157/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002158void hash_compute_compare( int alg_arg, data_t *input,
2159 data_t *expected_output )
2160{
2161 psa_algorithm_t alg = alg_arg;
2162 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2163 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002164 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002165 size_t i;
2166
2167 PSA_ASSERT( psa_crypto_init( ) );
2168
Neil Armstrongca30a002022-02-07 11:40:23 +01002169 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002170 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002171 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002172 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002173 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002174 ASSERT_COMPARE( output, output_length,
2175 expected_output->x, expected_output->len );
2176
Neil Armstrongca30a002022-02-07 11:40:23 +01002177 /* Compute with tight buffer, multi-part */
2178 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2179 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2180 PSA_ASSERT( psa_hash_finish( &operation, output,
2181 PSA_HASH_LENGTH( alg ),
2182 &output_length ) );
2183 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2184 ASSERT_COMPARE( output, output_length,
2185 expected_output->x, expected_output->len );
2186
2187 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002188 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2189 output, sizeof( output ),
2190 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002191 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002192 ASSERT_COMPARE( output, output_length,
2193 expected_output->x, expected_output->len );
2194
Neil Armstrongca30a002022-02-07 11:40:23 +01002195 /* Compute with larger buffer, multi-part */
2196 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2197 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2198 PSA_ASSERT( psa_hash_finish( &operation, output,
2199 sizeof( output ), &output_length ) );
2200 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2201 ASSERT_COMPARE( output, output_length,
2202 expected_output->x, expected_output->len );
2203
2204 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002205 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2206 output, output_length ) );
2207
Neil Armstrongca30a002022-02-07 11:40:23 +01002208 /* Compare with correct hash, multi-part */
2209 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2210 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2211 PSA_ASSERT( psa_hash_verify( &operation, output,
2212 output_length ) );
2213
2214 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002215 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2216 output, output_length + 1 ),
2217 PSA_ERROR_INVALID_SIGNATURE );
2218
Neil Armstrongca30a002022-02-07 11:40:23 +01002219 /* Compare with trailing garbage, multi-part */
2220 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2221 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2222 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2223 PSA_ERROR_INVALID_SIGNATURE );
2224
2225 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002226 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2227 output, output_length - 1 ),
2228 PSA_ERROR_INVALID_SIGNATURE );
2229
Neil Armstrongca30a002022-02-07 11:40:23 +01002230 /* Compare with truncated hash, multi-part */
2231 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2232 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2233 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2234 PSA_ERROR_INVALID_SIGNATURE );
2235
Gilles Peskine0a749c82019-11-28 19:33:58 +01002236 /* Compare with corrupted value */
2237 for( i = 0; i < output_length; i++ )
2238 {
Chris Jones9634bb12021-01-20 15:56:42 +00002239 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002240 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002241
2242 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002243 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2244 output, output_length ),
2245 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002246
2247 /* Multi-Part */
2248 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2249 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2250 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2251 PSA_ERROR_INVALID_SIGNATURE );
2252
Gilles Peskine0a749c82019-11-28 19:33:58 +01002253 output[i] ^= 1;
2254 }
2255
2256exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002257 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002258 PSA_DONE( );
2259}
2260/* END_CASE */
2261
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002262/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002263void hash_bad_order( )
2264{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002265 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002266 unsigned char input[] = "";
2267 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002268 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002269 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2270 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2271 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002272 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002273 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002274 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002275
Gilles Peskine8817f612018-12-18 00:18:46 +01002276 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002277
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002278 /* Call setup twice in a row. */
2279 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002280 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002281 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2282 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002283 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002284 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002285 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002286
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002287 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002288 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002289 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002290 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002291
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002292 /* Check that update calls abort on error. */
2293 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002294 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002295 ASSERT_OPERATION_IS_ACTIVE( operation );
2296 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2297 PSA_ERROR_BAD_STATE );
2298 ASSERT_OPERATION_IS_INACTIVE( operation );
2299 PSA_ASSERT( psa_hash_abort( &operation ) );
2300 ASSERT_OPERATION_IS_INACTIVE( operation );
2301
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002302 /* Call update after finish. */
2303 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2304 PSA_ASSERT( psa_hash_finish( &operation,
2305 hash, sizeof( hash ), &hash_len ) );
2306 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002307 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002308 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002309
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002310 /* Call verify without calling setup beforehand. */
2311 TEST_EQUAL( psa_hash_verify( &operation,
2312 valid_hash, sizeof( valid_hash ) ),
2313 PSA_ERROR_BAD_STATE );
2314 PSA_ASSERT( psa_hash_abort( &operation ) );
2315
2316 /* Call verify after finish. */
2317 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2318 PSA_ASSERT( psa_hash_finish( &operation,
2319 hash, sizeof( hash ), &hash_len ) );
2320 TEST_EQUAL( psa_hash_verify( &operation,
2321 valid_hash, sizeof( valid_hash ) ),
2322 PSA_ERROR_BAD_STATE );
2323 PSA_ASSERT( psa_hash_abort( &operation ) );
2324
2325 /* Call verify twice in a row. */
2326 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002327 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002328 PSA_ASSERT( psa_hash_verify( &operation,
2329 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002330 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002331 TEST_EQUAL( psa_hash_verify( &operation,
2332 valid_hash, sizeof( valid_hash ) ),
2333 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002335 PSA_ASSERT( psa_hash_abort( &operation ) );
2336
2337 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002338 TEST_EQUAL( psa_hash_finish( &operation,
2339 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002340 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002341 PSA_ASSERT( psa_hash_abort( &operation ) );
2342
2343 /* Call finish twice in a row. */
2344 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2345 PSA_ASSERT( psa_hash_finish( &operation,
2346 hash, sizeof( hash ), &hash_len ) );
2347 TEST_EQUAL( psa_hash_finish( &operation,
2348 hash, sizeof( hash ), &hash_len ),
2349 PSA_ERROR_BAD_STATE );
2350 PSA_ASSERT( psa_hash_abort( &operation ) );
2351
2352 /* Call finish after calling verify. */
2353 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2354 PSA_ASSERT( psa_hash_verify( &operation,
2355 valid_hash, sizeof( valid_hash ) ) );
2356 TEST_EQUAL( psa_hash_finish( &operation,
2357 hash, sizeof( hash ), &hash_len ),
2358 PSA_ERROR_BAD_STATE );
2359 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002360
2361exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002362 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002363}
2364/* END_CASE */
2365
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002366/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002367void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002368{
2369 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002370 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2371 * appended to it */
2372 unsigned char hash[] = {
2373 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2374 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2375 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002376 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002377 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002378
Gilles Peskine8817f612018-12-18 00:18:46 +01002379 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002380
itayzafrir27e69452018-11-01 14:26:34 +02002381 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002382 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002383 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002384 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002385 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002386 ASSERT_OPERATION_IS_INACTIVE( operation );
2387 PSA_ASSERT( psa_hash_abort( &operation ) );
2388 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002389
itayzafrir27e69452018-11-01 14:26:34 +02002390 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002391 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002392 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002393 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002394
itayzafrir27e69452018-11-01 14:26:34 +02002395 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002396 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002397 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002398 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002399
itayzafrirec93d302018-10-18 18:01:10 +03002400exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002401 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002402}
2403/* END_CASE */
2404
Ronald Cronee414c72021-03-18 18:50:08 +01002405/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002406void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002407{
2408 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002409 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002410 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002411 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002412 size_t hash_len;
2413
Gilles Peskine8817f612018-12-18 00:18:46 +01002414 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002415
itayzafrir58028322018-10-25 10:22:01 +03002416 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002417 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002418 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002419 hash, expected_size - 1, &hash_len ),
2420 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002421
2422exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002423 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002424}
2425/* END_CASE */
2426
Ronald Cronee414c72021-03-18 18:50:08 +01002427/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002428void hash_clone_source_state( )
2429{
2430 psa_algorithm_t alg = PSA_ALG_SHA_256;
2431 unsigned char hash[PSA_HASH_MAX_SIZE];
2432 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2433 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2434 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2435 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2436 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2437 size_t hash_len;
2438
2439 PSA_ASSERT( psa_crypto_init( ) );
2440 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2441
2442 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2443 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2444 PSA_ASSERT( psa_hash_finish( &op_finished,
2445 hash, sizeof( hash ), &hash_len ) );
2446 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2447 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2448
2449 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2450 PSA_ERROR_BAD_STATE );
2451
2452 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2453 PSA_ASSERT( psa_hash_finish( &op_init,
2454 hash, sizeof( hash ), &hash_len ) );
2455 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2456 PSA_ASSERT( psa_hash_finish( &op_finished,
2457 hash, sizeof( hash ), &hash_len ) );
2458 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2459 PSA_ASSERT( psa_hash_finish( &op_aborted,
2460 hash, sizeof( hash ), &hash_len ) );
2461
2462exit:
2463 psa_hash_abort( &op_source );
2464 psa_hash_abort( &op_init );
2465 psa_hash_abort( &op_setup );
2466 psa_hash_abort( &op_finished );
2467 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002468 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469}
2470/* END_CASE */
2471
Ronald Cronee414c72021-03-18 18:50:08 +01002472/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002473void hash_clone_target_state( )
2474{
2475 psa_algorithm_t alg = PSA_ALG_SHA_256;
2476 unsigned char hash[PSA_HASH_MAX_SIZE];
2477 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2478 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2479 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2480 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2481 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2482 size_t hash_len;
2483
2484 PSA_ASSERT( psa_crypto_init( ) );
2485
2486 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2487 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2488 PSA_ASSERT( psa_hash_finish( &op_finished,
2489 hash, sizeof( hash ), &hash_len ) );
2490 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2491 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2492
2493 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2494 PSA_ASSERT( psa_hash_finish( &op_target,
2495 hash, sizeof( hash ), &hash_len ) );
2496
2497 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2498 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2499 PSA_ERROR_BAD_STATE );
2500 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2501 PSA_ERROR_BAD_STATE );
2502
2503exit:
2504 psa_hash_abort( &op_target );
2505 psa_hash_abort( &op_init );
2506 psa_hash_abort( &op_setup );
2507 psa_hash_abort( &op_finished );
2508 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002509 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002510}
2511/* END_CASE */
2512
itayzafrir58028322018-10-25 10:22:01 +03002513/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002514void mac_operation_init( )
2515{
Jaeden Amero252ef282019-02-15 14:05:35 +00002516 const uint8_t input[1] = { 0 };
2517
Jaeden Amero769ce272019-01-04 11:48:03 +00002518 /* Test each valid way of initializing the object, except for `= {0}`, as
2519 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2520 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002521 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00002522 psa_mac_operation_t func = psa_mac_operation_init( );
2523 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2524 psa_mac_operation_t zero;
2525
2526 memset( &zero, 0, sizeof( zero ) );
2527
Jaeden Amero252ef282019-02-15 14:05:35 +00002528 /* A freshly-initialized MAC operation should not be usable. */
2529 TEST_EQUAL( psa_mac_update( &func,
2530 input, sizeof( input ) ),
2531 PSA_ERROR_BAD_STATE );
2532 TEST_EQUAL( psa_mac_update( &init,
2533 input, sizeof( input ) ),
2534 PSA_ERROR_BAD_STATE );
2535 TEST_EQUAL( psa_mac_update( &zero,
2536 input, sizeof( input ) ),
2537 PSA_ERROR_BAD_STATE );
2538
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002539 /* A default MAC operation should be abortable without error. */
2540 PSA_ASSERT( psa_mac_abort( &func ) );
2541 PSA_ASSERT( psa_mac_abort( &init ) );
2542 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002543}
2544/* END_CASE */
2545
2546/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002547void mac_setup( int key_type_arg,
2548 data_t *key,
2549 int alg_arg,
2550 int expected_status_arg )
2551{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002552 psa_key_type_t key_type = key_type_arg;
2553 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002554 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002555 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002556 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2557#if defined(KNOWN_SUPPORTED_MAC_ALG)
2558 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2559#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560
Gilles Peskine8817f612018-12-18 00:18:46 +01002561 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002562
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002563 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2564 &operation, &status ) )
2565 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002566 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002567
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002568 /* The operation object should be reusable. */
2569#if defined(KNOWN_SUPPORTED_MAC_ALG)
2570 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2571 smoke_test_key_data,
2572 sizeof( smoke_test_key_data ),
2573 KNOWN_SUPPORTED_MAC_ALG,
2574 &operation, &status ) )
2575 goto exit;
2576 TEST_EQUAL( status, PSA_SUCCESS );
2577#endif
2578
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002579exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002580 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581}
2582/* END_CASE */
2583
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002584/* 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 +00002585void mac_bad_order( )
2586{
Ronald Cron5425a212020-08-04 14:58:35 +02002587 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002588 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2589 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02002590 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00002591 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2592 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2593 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002594 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002595 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2596 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2597 size_t sign_mac_length = 0;
2598 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2599 const uint8_t verify_mac[] = {
2600 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2601 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2602 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2603
2604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002605 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002606 psa_set_key_algorithm( &attributes, alg );
2607 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002608
Ronald Cron5425a212020-08-04 14:58:35 +02002609 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2610 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002611
Jaeden Amero252ef282019-02-15 14:05:35 +00002612 /* Call update without calling setup beforehand. */
2613 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2614 PSA_ERROR_BAD_STATE );
2615 PSA_ASSERT( psa_mac_abort( &operation ) );
2616
2617 /* Call sign finish without calling setup beforehand. */
2618 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2619 &sign_mac_length),
2620 PSA_ERROR_BAD_STATE );
2621 PSA_ASSERT( psa_mac_abort( &operation ) );
2622
2623 /* Call verify finish without calling setup beforehand. */
2624 TEST_EQUAL( psa_mac_verify_finish( &operation,
2625 verify_mac, sizeof( verify_mac ) ),
2626 PSA_ERROR_BAD_STATE );
2627 PSA_ASSERT( psa_mac_abort( &operation ) );
2628
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002629 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002630 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002631 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002632 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002633 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002634 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002635 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002636 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002637
Jaeden Amero252ef282019-02-15 14:05:35 +00002638 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002639 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002640 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2641 PSA_ASSERT( psa_mac_sign_finish( &operation,
2642 sign_mac, sizeof( sign_mac ),
2643 &sign_mac_length ) );
2644 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2645 PSA_ERROR_BAD_STATE );
2646 PSA_ASSERT( psa_mac_abort( &operation ) );
2647
2648 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002649 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002650 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2651 PSA_ASSERT( psa_mac_verify_finish( &operation,
2652 verify_mac, sizeof( verify_mac ) ) );
2653 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2654 PSA_ERROR_BAD_STATE );
2655 PSA_ASSERT( psa_mac_abort( &operation ) );
2656
2657 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002658 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002659 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2660 PSA_ASSERT( psa_mac_sign_finish( &operation,
2661 sign_mac, sizeof( sign_mac ),
2662 &sign_mac_length ) );
2663 TEST_EQUAL( psa_mac_sign_finish( &operation,
2664 sign_mac, sizeof( sign_mac ),
2665 &sign_mac_length ),
2666 PSA_ERROR_BAD_STATE );
2667 PSA_ASSERT( psa_mac_abort( &operation ) );
2668
2669 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002670 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002671 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2672 PSA_ASSERT( psa_mac_verify_finish( &operation,
2673 verify_mac, sizeof( verify_mac ) ) );
2674 TEST_EQUAL( psa_mac_verify_finish( &operation,
2675 verify_mac, sizeof( verify_mac ) ),
2676 PSA_ERROR_BAD_STATE );
2677 PSA_ASSERT( psa_mac_abort( &operation ) );
2678
2679 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02002680 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002681 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002682 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002683 TEST_EQUAL( psa_mac_verify_finish( &operation,
2684 verify_mac, sizeof( verify_mac ) ),
2685 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002686 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002687 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002688 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002689
2690 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02002691 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002692 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002693 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002694 TEST_EQUAL( psa_mac_sign_finish( &operation,
2695 sign_mac, sizeof( sign_mac ),
2696 &sign_mac_length ),
2697 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002698 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00002699 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002700 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002701
Ronald Cron5425a212020-08-04 14:58:35 +02002702 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002703
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002704exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002705 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002706}
2707/* END_CASE */
2708
2709/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01002710void mac_sign_verify_multi( int key_type_arg,
2711 data_t *key_data,
2712 int alg_arg,
2713 data_t *input,
2714 int is_verify,
2715 data_t *expected_mac )
2716{
2717 size_t data_part_len = 0;
2718
2719 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
2720 {
2721 /* Split data into length(data_part_len) parts. */
2722 mbedtls_test_set_step( 2000 + data_part_len );
2723
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01002724 if( mac_multipart_internal_func( key_type_arg, key_data,
2725 alg_arg,
2726 input, data_part_len,
2727 expected_mac,
2728 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01002729 break;
2730
2731 /* length(0) part, length(data_part_len) part, length(0) part... */
2732 mbedtls_test_set_step( 3000 + data_part_len );
2733
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01002734 if( mac_multipart_internal_func( key_type_arg, key_data,
2735 alg_arg,
2736 input, data_part_len,
2737 expected_mac,
2738 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01002739 break;
2740 }
2741
2742 /* Goto is required to silence warnings about unused labels, as we
2743 * don't actually do any test assertions in this function. */
2744 goto exit;
2745}
2746/* END_CASE */
2747
2748/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002749void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002750 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002751 int alg_arg,
2752 data_t *input,
2753 data_t *expected_mac )
2754{
Ronald Cron5425a212020-08-04 14:58:35 +02002755 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002756 psa_key_type_t key_type = key_type_arg;
2757 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002758 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002760 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002761 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002762 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002763 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02002764 const size_t output_sizes_to_test[] = {
2765 0,
2766 1,
2767 expected_mac->len - 1,
2768 expected_mac->len,
2769 expected_mac->len + 1,
2770 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002771
Gilles Peskine7be11a72022-04-14 00:12:57 +02002772 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002773 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002774 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002777
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002778 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002779 psa_set_key_algorithm( &attributes, alg );
2780 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002781
Ronald Cron5425a212020-08-04 14:58:35 +02002782 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2783 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002784
Gilles Peskine8b356b52020-08-25 23:44:59 +02002785 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2786 {
2787 const size_t output_size = output_sizes_to_test[i];
2788 psa_status_t expected_status =
2789 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2790 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002791
Chris Jones9634bb12021-01-20 15:56:42 +00002792 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002793 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002794
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002795 /* Calculate the MAC, one-shot case. */
2796 TEST_EQUAL( psa_mac_compute( key, alg,
2797 input->x, input->len,
2798 actual_mac, output_size, &mac_length ),
2799 expected_status );
2800 if( expected_status == PSA_SUCCESS )
2801 {
2802 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2803 actual_mac, mac_length );
2804 }
2805
2806 if( output_size > 0 )
2807 memset( actual_mac, 0, output_size );
2808
2809 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002810 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002811 PSA_ASSERT( psa_mac_update( &operation,
2812 input->x, input->len ) );
2813 TEST_EQUAL( psa_mac_sign_finish( &operation,
2814 actual_mac, output_size,
2815 &mac_length ),
2816 expected_status );
2817 PSA_ASSERT( psa_mac_abort( &operation ) );
2818
2819 if( expected_status == PSA_SUCCESS )
2820 {
2821 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2822 actual_mac, mac_length );
2823 }
2824 mbedtls_free( actual_mac );
2825 actual_mac = NULL;
2826 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002827
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002828exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002829 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002830 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002831 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002832 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002833}
2834/* END_CASE */
2835
2836/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002837void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002838 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002839 int alg_arg,
2840 data_t *input,
2841 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842{
Ronald Cron5425a212020-08-04 14:58:35 +02002843 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844 psa_key_type_t key_type = key_type_arg;
2845 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002846 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002848 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849
Gilles Peskine7be11a72022-04-14 00:12:57 +02002850 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02002851
Gilles Peskine8817f612018-12-18 00:18:46 +01002852 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002854 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002855 psa_set_key_algorithm( &attributes, alg );
2856 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002857
Ronald Cron5425a212020-08-04 14:58:35 +02002858 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2859 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002860
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002861 /* Verify correct MAC, one-shot case. */
2862 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2863 expected_mac->x, expected_mac->len ) );
2864
2865 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002866 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_mac_update( &operation,
2868 input->x, input->len ) );
2869 PSA_ASSERT( psa_mac_verify_finish( &operation,
2870 expected_mac->x,
2871 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002872
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002873 /* Test a MAC that's too short, one-shot case. */
2874 TEST_EQUAL( psa_mac_verify( key, alg,
2875 input->x, input->len,
2876 expected_mac->x,
2877 expected_mac->len - 1 ),
2878 PSA_ERROR_INVALID_SIGNATURE );
2879
2880 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002881 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002882 PSA_ASSERT( psa_mac_update( &operation,
2883 input->x, input->len ) );
2884 TEST_EQUAL( psa_mac_verify_finish( &operation,
2885 expected_mac->x,
2886 expected_mac->len - 1 ),
2887 PSA_ERROR_INVALID_SIGNATURE );
2888
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002889 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002890 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2891 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002892 TEST_EQUAL( psa_mac_verify( key, alg,
2893 input->x, input->len,
2894 perturbed_mac, expected_mac->len + 1 ),
2895 PSA_ERROR_INVALID_SIGNATURE );
2896
2897 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002898 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002899 PSA_ASSERT( psa_mac_update( &operation,
2900 input->x, input->len ) );
2901 TEST_EQUAL( psa_mac_verify_finish( &operation,
2902 perturbed_mac,
2903 expected_mac->len + 1 ),
2904 PSA_ERROR_INVALID_SIGNATURE );
2905
2906 /* Test changing one byte. */
2907 for( size_t i = 0; i < expected_mac->len; i++ )
2908 {
Chris Jones9634bb12021-01-20 15:56:42 +00002909 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002910 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002911
2912 TEST_EQUAL( psa_mac_verify( key, alg,
2913 input->x, input->len,
2914 perturbed_mac, expected_mac->len ),
2915 PSA_ERROR_INVALID_SIGNATURE );
2916
Ronald Cron5425a212020-08-04 14:58:35 +02002917 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002918 PSA_ASSERT( psa_mac_update( &operation,
2919 input->x, input->len ) );
2920 TEST_EQUAL( psa_mac_verify_finish( &operation,
2921 perturbed_mac,
2922 expected_mac->len ),
2923 PSA_ERROR_INVALID_SIGNATURE );
2924 perturbed_mac[i] ^= 1;
2925 }
2926
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002928 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002929 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002930 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002931 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002932}
2933/* END_CASE */
2934
2935/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002936void cipher_operation_init( )
2937{
Jaeden Ameroab439972019-02-15 14:12:05 +00002938 const uint8_t input[1] = { 0 };
2939 unsigned char output[1] = { 0 };
2940 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002941 /* Test each valid way of initializing the object, except for `= {0}`, as
2942 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2943 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002944 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002945 psa_cipher_operation_t func = psa_cipher_operation_init( );
2946 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2947 psa_cipher_operation_t zero;
2948
2949 memset( &zero, 0, sizeof( zero ) );
2950
Jaeden Ameroab439972019-02-15 14:12:05 +00002951 /* A freshly-initialized cipher operation should not be usable. */
2952 TEST_EQUAL( psa_cipher_update( &func,
2953 input, sizeof( input ),
2954 output, sizeof( output ),
2955 &output_length ),
2956 PSA_ERROR_BAD_STATE );
2957 TEST_EQUAL( psa_cipher_update( &init,
2958 input, sizeof( input ),
2959 output, sizeof( output ),
2960 &output_length ),
2961 PSA_ERROR_BAD_STATE );
2962 TEST_EQUAL( psa_cipher_update( &zero,
2963 input, sizeof( input ),
2964 output, sizeof( output ),
2965 &output_length ),
2966 PSA_ERROR_BAD_STATE );
2967
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002968 /* A default cipher operation should be abortable without error. */
2969 PSA_ASSERT( psa_cipher_abort( &func ) );
2970 PSA_ASSERT( psa_cipher_abort( &init ) );
2971 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002972}
2973/* END_CASE */
2974
2975/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976void cipher_setup( int key_type_arg,
2977 data_t *key,
2978 int alg_arg,
2979 int expected_status_arg )
2980{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002981 psa_key_type_t key_type = key_type_arg;
2982 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002983 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002984 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002985 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002986#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002987 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2988#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002991
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002992 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2993 &operation, &status ) )
2994 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002995 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002996
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002997 /* The operation object should be reusable. */
2998#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2999 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3000 smoke_test_key_data,
3001 sizeof( smoke_test_key_data ),
3002 KNOWN_SUPPORTED_CIPHER_ALG,
3003 &operation, &status ) )
3004 goto exit;
3005 TEST_EQUAL( status, PSA_SUCCESS );
3006#endif
3007
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003008exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003009 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003010 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003011}
3012/* END_CASE */
3013
Ronald Cronee414c72021-03-18 18:50:08 +01003014/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003015void cipher_bad_order( )
3016{
Ronald Cron5425a212020-08-04 14:58:35 +02003017 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003018 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3019 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003021 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003022 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003023 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003024 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3025 0xaa, 0xaa, 0xaa, 0xaa };
3026 const uint8_t text[] = {
3027 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3028 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003029 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003030 size_t length = 0;
3031
3032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003033 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3034 psa_set_key_algorithm( &attributes, alg );
3035 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003036 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3037 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003038
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003039 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003040 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003041 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003042 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003043 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003044 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003045 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003046 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003047
3048 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003049 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003050 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003051 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003052 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003053 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003054 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003055 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003056
Jaeden Ameroab439972019-02-15 14:12:05 +00003057 /* Generate an IV without calling setup beforehand. */
3058 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3059 buffer, sizeof( buffer ),
3060 &length ),
3061 PSA_ERROR_BAD_STATE );
3062 PSA_ASSERT( psa_cipher_abort( &operation ) );
3063
3064 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003065 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003066 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3067 buffer, sizeof( buffer ),
3068 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003069 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003070 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3071 buffer, sizeof( buffer ),
3072 &length ),
3073 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003074 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003075 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003076 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003077
3078 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003079 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003080 PSA_ASSERT( psa_cipher_set_iv( &operation,
3081 iv, sizeof( iv ) ) );
3082 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3083 buffer, sizeof( buffer ),
3084 &length ),
3085 PSA_ERROR_BAD_STATE );
3086 PSA_ASSERT( psa_cipher_abort( &operation ) );
3087
3088 /* Set an IV without calling setup beforehand. */
3089 TEST_EQUAL( psa_cipher_set_iv( &operation,
3090 iv, sizeof( iv ) ),
3091 PSA_ERROR_BAD_STATE );
3092 PSA_ASSERT( psa_cipher_abort( &operation ) );
3093
3094 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003095 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003096 PSA_ASSERT( psa_cipher_set_iv( &operation,
3097 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003098 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003099 TEST_EQUAL( psa_cipher_set_iv( &operation,
3100 iv, sizeof( iv ) ),
3101 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003102 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003103 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003104 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003105
3106 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003107 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003108 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3109 buffer, sizeof( buffer ),
3110 &length ) );
3111 TEST_EQUAL( psa_cipher_set_iv( &operation,
3112 iv, sizeof( iv ) ),
3113 PSA_ERROR_BAD_STATE );
3114 PSA_ASSERT( psa_cipher_abort( &operation ) );
3115
3116 /* Call update without calling setup beforehand. */
3117 TEST_EQUAL( psa_cipher_update( &operation,
3118 text, sizeof( text ),
3119 buffer, sizeof( buffer ),
3120 &length ),
3121 PSA_ERROR_BAD_STATE );
3122 PSA_ASSERT( psa_cipher_abort( &operation ) );
3123
3124 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003125 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003126 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003127 TEST_EQUAL( psa_cipher_update( &operation,
3128 text, sizeof( text ),
3129 buffer, sizeof( buffer ),
3130 &length ),
3131 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003132 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003133 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003134 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003135
3136 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003137 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003138 PSA_ASSERT( psa_cipher_set_iv( &operation,
3139 iv, sizeof( iv ) ) );
3140 PSA_ASSERT( psa_cipher_finish( &operation,
3141 buffer, sizeof( buffer ), &length ) );
3142 TEST_EQUAL( psa_cipher_update( &operation,
3143 text, sizeof( text ),
3144 buffer, sizeof( buffer ),
3145 &length ),
3146 PSA_ERROR_BAD_STATE );
3147 PSA_ASSERT( psa_cipher_abort( &operation ) );
3148
3149 /* Call finish without calling setup beforehand. */
3150 TEST_EQUAL( psa_cipher_finish( &operation,
3151 buffer, sizeof( buffer ), &length ),
3152 PSA_ERROR_BAD_STATE );
3153 PSA_ASSERT( psa_cipher_abort( &operation ) );
3154
3155 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003156 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003157 /* Not calling update means we are encrypting an empty buffer, which is OK
3158 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003159 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003160 TEST_EQUAL( psa_cipher_finish( &operation,
3161 buffer, sizeof( buffer ), &length ),
3162 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003163 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003164 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003165 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003166
3167 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003168 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003169 PSA_ASSERT( psa_cipher_set_iv( &operation,
3170 iv, sizeof( iv ) ) );
3171 PSA_ASSERT( psa_cipher_finish( &operation,
3172 buffer, sizeof( buffer ), &length ) );
3173 TEST_EQUAL( psa_cipher_finish( &operation,
3174 buffer, sizeof( buffer ), &length ),
3175 PSA_ERROR_BAD_STATE );
3176 PSA_ASSERT( psa_cipher_abort( &operation ) );
3177
Ronald Cron5425a212020-08-04 14:58:35 +02003178 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003179
Jaeden Ameroab439972019-02-15 14:12:05 +00003180exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003181 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003182 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003183}
3184/* END_CASE */
3185
3186/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003187void cipher_encrypt_fail( int alg_arg,
3188 int key_type_arg,
3189 data_t *key_data,
3190 data_t *input,
3191 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003192{
Ronald Cron5425a212020-08-04 14:58:35 +02003193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194 psa_status_t status;
3195 psa_key_type_t key_type = key_type_arg;
3196 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003197 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003198 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3199 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3200 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003201 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003202 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003203 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003204 size_t function_output_length;
3205 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3207
3208 if ( PSA_ERROR_BAD_STATE != expected_status )
3209 {
3210 PSA_ASSERT( psa_crypto_init( ) );
3211
3212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3213 psa_set_key_algorithm( &attributes, alg );
3214 psa_set_key_type( &attributes, key_type );
3215
3216 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3217 input->len );
3218 ASSERT_ALLOC( output, output_buffer_size );
3219
3220 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3221 &key ) );
3222 }
3223
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003224 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003225 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3226 output_buffer_size, &output_length );
3227
3228 TEST_EQUAL( status, expected_status );
3229
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003230 /* Encrypt, multi-part */
3231 status = psa_cipher_encrypt_setup( &operation, key, alg );
3232 if( status == PSA_SUCCESS )
3233 {
3234 if( alg != PSA_ALG_ECB_NO_PADDING )
3235 {
3236 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3237 iv, iv_size,
3238 &iv_length ) );
3239 }
3240
3241 status = psa_cipher_update( &operation, input->x, input->len,
3242 output, output_buffer_size,
3243 &function_output_length );
3244 if( status == PSA_SUCCESS )
3245 {
3246 output_length += function_output_length;
3247
3248 status = psa_cipher_finish( &operation, output + output_length,
3249 output_buffer_size - output_length,
3250 &function_output_length );
3251
3252 TEST_EQUAL( status, expected_status );
3253 }
3254 else
3255 {
3256 TEST_EQUAL( status, expected_status );
3257 }
3258 }
3259 else
3260 {
3261 TEST_EQUAL( status, expected_status );
3262 }
3263
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003264exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003265 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003266 mbedtls_free( output );
3267 psa_destroy_key( key );
3268 PSA_DONE( );
3269}
3270/* END_CASE */
3271
3272/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003273void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3274 data_t *input, int iv_length,
3275 int expected_result )
3276{
3277 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3278 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3280 size_t output_buffer_size = 0;
3281 unsigned char *output = NULL;
3282
3283 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3284 ASSERT_ALLOC( output, output_buffer_size );
3285
3286 PSA_ASSERT( psa_crypto_init( ) );
3287
3288 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3289 psa_set_key_algorithm( &attributes, alg );
3290 psa_set_key_type( &attributes, key_type );
3291
3292 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3293 &key ) );
3294 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3295 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3296 iv_length ) );
3297
3298exit:
3299 psa_cipher_abort( &operation );
3300 mbedtls_free( output );
3301 psa_destroy_key( key );
3302 PSA_DONE( );
3303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003307void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3308 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003309{
3310 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3311 psa_key_type_t key_type = key_type_arg;
3312 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003313 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3314 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003315 unsigned char *output = NULL;
3316 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003317 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3319
3320 PSA_ASSERT( psa_crypto_init( ) );
3321
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003322 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003323 TEST_LE_U( ciphertext->len,
3324 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3325 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003326 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003327 TEST_LE_U( plaintext->len,
3328 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3329 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3330 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003331
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003332
3333 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003334 psa_set_key_usage_flags( &attributes,
3335 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003336 psa_set_key_algorithm( &attributes, alg );
3337 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003338 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3339 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003340 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3341 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003342 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003343
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003344 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003345 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3346 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3347 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003348 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3349 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003350 PSA_ERROR_BAD_STATE );
3351
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003352 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003353 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3354 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003355 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003356 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003357 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3358 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003359 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003360 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003361
Gilles Peskine286c3142022-04-20 17:09:38 +02003362 /* Multipart encryption */
3363 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3364 output_length = 0;
3365 length = ~0;
3366 PSA_ASSERT( psa_cipher_update( &operation,
3367 plaintext->x, plaintext->len,
3368 output, output_buffer_size,
3369 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003370 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003371 output_length += length;
3372 PSA_ASSERT( psa_cipher_finish( &operation,
3373 output + output_length,
3374 output_buffer_size - output_length,
3375 &length ) );
3376 output_length += length;
3377 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003378 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003379
Gilles Peskine286c3142022-04-20 17:09:38 +02003380 /* Multipart encryption */
3381 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3382 output_length = 0;
3383 length = ~0;
3384 PSA_ASSERT( psa_cipher_update( &operation,
3385 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003386 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003387 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003388 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003389 output_length += length;
3390 PSA_ASSERT( psa_cipher_finish( &operation,
3391 output + output_length,
3392 output_buffer_size - output_length,
3393 &length ) );
3394 output_length += length;
3395 ASSERT_COMPARE( plaintext->x, plaintext->len,
3396 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003397
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003398 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003399 output_length = ~0;
3400 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3401 output, output_buffer_size,
3402 &output_length ) );
3403 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3404 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003405
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003406 /* One-shot decryption */
3407 output_length = ~0;
3408 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
3409 output, output_buffer_size,
3410 &output_length ) );
3411 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003412 output, output_length );
3413
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003414exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003415 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003416 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003417 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003418 psa_destroy_key( key );
3419 PSA_DONE( );
3420}
3421/* END_CASE */
3422
3423/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003424void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3425{
3426 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3427 psa_algorithm_t alg = alg_arg;
3428 psa_key_type_t key_type = key_type_arg;
3429 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3430 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3431 psa_status_t status;
3432
3433 PSA_ASSERT( psa_crypto_init( ) );
3434
3435 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3436 psa_set_key_algorithm( &attributes, alg );
3437 psa_set_key_type( &attributes, key_type );
3438
3439 /* Usage of either of these two size macros would cause divide by zero
3440 * with incorrect key types previously. Input length should be irrelevant
3441 * here. */
3442 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
3443 0 );
3444 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
3445
3446
3447 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3448 &key ) );
3449
3450 /* Should fail due to invalid alg type (to support invalid key type).
3451 * Encrypt or decrypt will end up in the same place. */
3452 status = psa_cipher_encrypt_setup( &operation, key, alg );
3453
3454 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
3455
3456exit:
3457 psa_cipher_abort( &operation );
3458 psa_destroy_key( key );
3459 PSA_DONE( );
3460}
3461/* END_CASE */
3462
3463/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003464void cipher_encrypt_validation( int alg_arg,
3465 int key_type_arg,
3466 data_t *key_data,
3467 data_t *input )
3468{
3469 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3470 psa_key_type_t key_type = key_type_arg;
3471 psa_algorithm_t alg = alg_arg;
3472 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
3473 unsigned char *output1 = NULL;
3474 size_t output1_buffer_size = 0;
3475 size_t output1_length = 0;
3476 unsigned char *output2 = NULL;
3477 size_t output2_buffer_size = 0;
3478 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003479 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003480 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003484
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3486 psa_set_key_algorithm( &attributes, alg );
3487 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003488
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003489 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3490 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3491 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3492 ASSERT_ALLOC( output1, output1_buffer_size );
3493 ASSERT_ALLOC( output2, output2_buffer_size );
3494
Ronald Cron5425a212020-08-04 14:58:35 +02003495 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3496 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003497
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02003498 /* The one-shot cipher encryption uses generated iv so validating
3499 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003500 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
3501 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003502 TEST_LE_U( output1_length,
3503 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3504 TEST_LE_U( output1_length,
3505 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003506
3507 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3508 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003509
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_cipher_update( &operation,
3511 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003512 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003514 TEST_LE_U( function_output_length,
3515 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
3516 TEST_LE_U( function_output_length,
3517 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003518 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003519
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003520 PSA_ASSERT( psa_cipher_finish( &operation,
3521 output2 + output2_length,
3522 output2_buffer_size - output2_length,
3523 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003524 TEST_LE_U( function_output_length,
3525 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3526 TEST_LE_U( function_output_length,
3527 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003528 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003529
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003530 PSA_ASSERT( psa_cipher_abort( &operation ) );
3531 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
3532 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003533
Gilles Peskine50e586b2018-06-08 14:28:46 +02003534exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003535 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003536 mbedtls_free( output1 );
3537 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003538 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003539 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003540}
3541/* END_CASE */
3542
3543/* BEGIN_CASE */
3544void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003545 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003546 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003547 int first_part_size_arg,
3548 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003549 data_t *expected_output,
3550 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551{
Ronald Cron5425a212020-08-04 14:58:35 +02003552 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003553 psa_key_type_t key_type = key_type_arg;
3554 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003555 psa_status_t status;
3556 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003557 size_t first_part_size = first_part_size_arg;
3558 size_t output1_length = output1_length_arg;
3559 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003560 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003561 size_t output_buffer_size = 0;
3562 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003563 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003564 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003565 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566
Gilles Peskine8817f612018-12-18 00:18:46 +01003567 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003568
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003569 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3570 psa_set_key_algorithm( &attributes, alg );
3571 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003572
Ronald Cron5425a212020-08-04 14:58:35 +02003573 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3574 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575
Ronald Cron5425a212020-08-04 14:58:35 +02003576 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003577
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003578 if( iv->len > 0 )
3579 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003580 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003581 }
3582
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003583 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3584 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003585 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003586
Gilles Peskine7be11a72022-04-14 00:12:57 +02003587 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3589 output, output_buffer_size,
3590 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003591 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003592 TEST_LE_U( function_output_length,
3593 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3594 TEST_LE_U( function_output_length,
3595 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003596 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003597
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003598 if( first_part_size < input->len )
3599 {
3600 PSA_ASSERT( psa_cipher_update( &operation,
3601 input->x + first_part_size,
3602 input->len - first_part_size,
3603 ( output_buffer_size == 0 ? NULL :
3604 output + total_output_length ),
3605 output_buffer_size - total_output_length,
3606 &function_output_length ) );
3607 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003608 TEST_LE_U( function_output_length,
3609 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3610 alg,
3611 input->len - first_part_size ) );
3612 TEST_LE_U( function_output_length,
3613 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003614 total_output_length += function_output_length;
3615 }
gabor-mezei-armceface22021-01-21 12:26:17 +01003616
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003617 status = psa_cipher_finish( &operation,
3618 ( output_buffer_size == 0 ? NULL :
3619 output + total_output_length ),
3620 output_buffer_size - total_output_length,
3621 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003622 TEST_LE_U( function_output_length,
3623 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3624 TEST_LE_U( function_output_length,
3625 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003626 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003627 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003628
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003629 if( expected_status == PSA_SUCCESS )
3630 {
3631 PSA_ASSERT( psa_cipher_abort( &operation ) );
3632
3633 ASSERT_COMPARE( expected_output->x, expected_output->len,
3634 output, total_output_length );
3635 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003636
3637exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003638 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003639 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003640 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003641 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003642}
3643/* END_CASE */
3644
3645/* BEGIN_CASE */
3646void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003647 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003648 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003649 int first_part_size_arg,
3650 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003651 data_t *expected_output,
3652 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003653{
Ronald Cron5425a212020-08-04 14:58:35 +02003654 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003655 psa_key_type_t key_type = key_type_arg;
3656 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003657 psa_status_t status;
3658 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003659 size_t first_part_size = first_part_size_arg;
3660 size_t output1_length = output1_length_arg;
3661 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003662 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003663 size_t output_buffer_size = 0;
3664 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003665 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003666 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003667 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003668
Gilles Peskine8817f612018-12-18 00:18:46 +01003669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003670
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3672 psa_set_key_algorithm( &attributes, alg );
3673 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003674
Ronald Cron5425a212020-08-04 14:58:35 +02003675 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3676 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003677
Ronald Cron5425a212020-08-04 14:58:35 +02003678 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003679
Steven Cooreman177deba2020-09-07 17:14:14 +02003680 if( iv->len > 0 )
3681 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003682 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003683 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003684
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003685 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
3686 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003687 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003688
Gilles Peskine7be11a72022-04-14 00:12:57 +02003689 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003690 PSA_ASSERT( psa_cipher_update( &operation,
3691 input->x, first_part_size,
3692 output, output_buffer_size,
3693 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003694 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003695 TEST_LE_U( function_output_length,
3696 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
3697 TEST_LE_U( function_output_length,
3698 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003699 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01003700
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003701 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02003702 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003703 PSA_ASSERT( psa_cipher_update( &operation,
3704 input->x + first_part_size,
3705 input->len - first_part_size,
3706 ( output_buffer_size == 0 ? NULL :
3707 output + total_output_length ),
3708 output_buffer_size - total_output_length,
3709 &function_output_length ) );
3710 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003711 TEST_LE_U( function_output_length,
3712 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
3713 alg,
3714 input->len - first_part_size ) );
3715 TEST_LE_U( function_output_length,
3716 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003717 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003718 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003719
Gilles Peskine50e586b2018-06-08 14:28:46 +02003720 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01003721 ( output_buffer_size == 0 ? NULL :
3722 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01003723 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003724 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003725 TEST_LE_U( function_output_length,
3726 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
3727 TEST_LE_U( function_output_length,
3728 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003729 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003730 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003731
3732 if( expected_status == PSA_SUCCESS )
3733 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003734 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02003735
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003736 ASSERT_COMPARE( expected_output->x, expected_output->len,
3737 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003738 }
3739
Gilles Peskine50e586b2018-06-08 14:28:46 +02003740exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003741 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003742 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02003743 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003744 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003745}
3746/* END_CASE */
3747
Gilles Peskine50e586b2018-06-08 14:28:46 +02003748/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003749void cipher_decrypt_fail( int alg_arg,
3750 int key_type_arg,
3751 data_t *key_data,
3752 data_t *iv,
3753 data_t *input_arg,
3754 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003755{
3756 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3757 psa_status_t status;
3758 psa_key_type_t key_type = key_type_arg;
3759 psa_algorithm_t alg = alg_arg;
3760 psa_status_t expected_status = expected_status_arg;
3761 unsigned char *input = NULL;
3762 size_t input_buffer_size = 0;
3763 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003764 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003765 size_t output_buffer_size = 0;
3766 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01003767 size_t function_output_length;
3768 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003769 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3770
3771 if ( PSA_ERROR_BAD_STATE != expected_status )
3772 {
3773 PSA_ASSERT( psa_crypto_init( ) );
3774
3775 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3776 psa_set_key_algorithm( &attributes, alg );
3777 psa_set_key_type( &attributes, key_type );
3778
3779 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3780 &key ) );
3781 }
3782
3783 /* Allocate input buffer and copy the iv and the plaintext */
3784 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3785 if ( input_buffer_size > 0 )
3786 {
3787 ASSERT_ALLOC( input, input_buffer_size );
3788 memcpy( input, iv->x, iv->len );
3789 memcpy( input + iv->len, input_arg->x, input_arg->len );
3790 }
3791
3792 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3793 ASSERT_ALLOC( output, output_buffer_size );
3794
Neil Armstrong66a479f2022-02-07 15:41:19 +01003795 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003796 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3797 output_buffer_size, &output_length );
3798 TEST_EQUAL( status, expected_status );
3799
Neil Armstrong66a479f2022-02-07 15:41:19 +01003800 /* Decrypt, multi-part */
3801 status = psa_cipher_decrypt_setup( &operation, key, alg );
3802 if( status == PSA_SUCCESS )
3803 {
3804 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
3805 input_arg->len ) +
3806 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
3807 ASSERT_ALLOC( output_multi, output_buffer_size );
3808
3809 if( iv->len > 0 )
3810 {
3811 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
3812
3813 if( status != PSA_SUCCESS )
3814 TEST_EQUAL( status, expected_status );
3815 }
3816
3817 if( status == PSA_SUCCESS )
3818 {
3819 status = psa_cipher_update( &operation,
3820 input_arg->x, input_arg->len,
3821 output_multi, output_buffer_size,
3822 &function_output_length );
3823 if( status == PSA_SUCCESS )
3824 {
3825 output_length = function_output_length;
3826
3827 status = psa_cipher_finish( &operation,
3828 output_multi + output_length,
3829 output_buffer_size - output_length,
3830 &function_output_length );
3831
3832 TEST_EQUAL( status, expected_status );
3833 }
3834 else
3835 {
3836 TEST_EQUAL( status, expected_status );
3837 }
3838 }
3839 else
3840 {
3841 TEST_EQUAL( status, expected_status );
3842 }
3843 }
3844 else
3845 {
3846 TEST_EQUAL( status, expected_status );
3847 }
3848
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003849exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01003850 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003851 mbedtls_free( input );
3852 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01003853 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003854 psa_destroy_key( key );
3855 PSA_DONE( );
3856}
3857/* END_CASE */
3858
3859/* BEGIN_CASE */
3860void cipher_decrypt( int alg_arg,
3861 int key_type_arg,
3862 data_t *key_data,
3863 data_t *iv,
3864 data_t *input_arg,
3865 data_t *expected_output )
3866{
3867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3868 psa_key_type_t key_type = key_type_arg;
3869 psa_algorithm_t alg = alg_arg;
3870 unsigned char *input = NULL;
3871 size_t input_buffer_size = 0;
3872 unsigned char *output = NULL;
3873 size_t output_buffer_size = 0;
3874 size_t output_length = 0;
3875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3876
3877 PSA_ASSERT( psa_crypto_init( ) );
3878
3879 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3880 psa_set_key_algorithm( &attributes, alg );
3881 psa_set_key_type( &attributes, key_type );
3882
3883 /* Allocate input buffer and copy the iv and the plaintext */
3884 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
3885 if ( input_buffer_size > 0 )
3886 {
3887 ASSERT_ALLOC( input, input_buffer_size );
3888 memcpy( input, iv->x, iv->len );
3889 memcpy( input + iv->len, input_arg->x, input_arg->len );
3890 }
3891
3892 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
3893 ASSERT_ALLOC( output, output_buffer_size );
3894
3895 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3896 &key ) );
3897
3898 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
3899 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003900 TEST_LE_U( output_length,
3901 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
3902 TEST_LE_U( output_length,
3903 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003904
3905 ASSERT_COMPARE( expected_output->x, expected_output->len,
3906 output, output_length );
3907exit:
3908 mbedtls_free( input );
3909 mbedtls_free( output );
3910 psa_destroy_key( key );
3911 PSA_DONE( );
3912}
3913/* END_CASE */
3914
3915/* BEGIN_CASE */
3916void cipher_verify_output( int alg_arg,
3917 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003918 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003919 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003920{
Ronald Cron5425a212020-08-04 14:58:35 +02003921 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003922 psa_key_type_t key_type = key_type_arg;
3923 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003924 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003925 size_t output1_size = 0;
3926 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003927 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003928 size_t output2_size = 0;
3929 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003931
Gilles Peskine8817f612018-12-18 00:18:46 +01003932 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003933
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3935 psa_set_key_algorithm( &attributes, alg );
3936 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003937
Ronald Cron5425a212020-08-04 14:58:35 +02003938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3939 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01003940 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003941 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003942
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003943 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
3944 output1, output1_size,
3945 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003946 TEST_LE_U( output1_length,
3947 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
3948 TEST_LE_U( output1_length,
3949 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003950
3951 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003952 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003953
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003954 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
3955 output2, output2_size,
3956 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003957 TEST_LE_U( output2_length,
3958 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
3959 TEST_LE_U( output2_length,
3960 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003961
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003962 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003963
3964exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003965 mbedtls_free( output1 );
3966 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003967 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003968 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003969}
3970/* END_CASE */
3971
3972/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003973void cipher_verify_output_multipart( int alg_arg,
3974 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003975 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003976 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003977 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003978{
Ronald Cron5425a212020-08-04 14:58:35 +02003979 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003980 psa_key_type_t key_type = key_type_arg;
3981 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003982 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003983 unsigned char iv[16] = {0};
3984 size_t iv_size = 16;
3985 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003986 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003987 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003988 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003989 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003990 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003991 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003992 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003993 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3994 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003996
Gilles Peskine8817f612018-12-18 00:18:46 +01003997 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003998
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003999 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4000 psa_set_key_algorithm( &attributes, alg );
4001 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004002
Ronald Cron5425a212020-08-04 14:58:35 +02004003 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4004 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004005
Ronald Cron5425a212020-08-04 14:58:35 +02004006 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4007 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004008
Steven Cooreman177deba2020-09-07 17:14:14 +02004009 if( alg != PSA_ALG_ECB_NO_PADDING )
4010 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004011 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4012 iv, iv_size,
4013 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004014 }
4015
gabor-mezei-armceface22021-01-21 12:26:17 +01004016 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004017 TEST_LE_U( output1_buffer_size,
4018 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004019 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004020
Gilles Peskine7be11a72022-04-14 00:12:57 +02004021 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004022
Gilles Peskine8817f612018-12-18 00:18:46 +01004023 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4024 output1, output1_buffer_size,
4025 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004026 TEST_LE_U( function_output_length,
4027 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4028 TEST_LE_U( function_output_length,
4029 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004030 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004031
Gilles Peskine8817f612018-12-18 00:18:46 +01004032 PSA_ASSERT( psa_cipher_update( &operation1,
4033 input->x + first_part_size,
4034 input->len - first_part_size,
4035 output1, output1_buffer_size,
4036 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004037 TEST_LE_U( function_output_length,
4038 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4039 alg,
4040 input->len - first_part_size ) );
4041 TEST_LE_U( function_output_length,
4042 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004043 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004044
Gilles Peskine8817f612018-12-18 00:18:46 +01004045 PSA_ASSERT( psa_cipher_finish( &operation1,
4046 output1 + output1_length,
4047 output1_buffer_size - output1_length,
4048 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004049 TEST_LE_U( function_output_length,
4050 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4051 TEST_LE_U( function_output_length,
4052 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004053 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004054
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004056
Gilles Peskine048b7f02018-06-08 14:20:49 +02004057 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004058 TEST_LE_U( output2_buffer_size,
4059 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4060 TEST_LE_U( output2_buffer_size,
4061 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004062 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004063
Steven Cooreman177deba2020-09-07 17:14:14 +02004064 if( iv_length > 0 )
4065 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004066 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4067 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004068 }
Moran Pekerded84402018-06-06 16:36:50 +03004069
Gilles Peskine8817f612018-12-18 00:18:46 +01004070 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4071 output2, output2_buffer_size,
4072 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004073 TEST_LE_U( function_output_length,
4074 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4075 TEST_LE_U( function_output_length,
4076 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004077 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004078
Gilles Peskine8817f612018-12-18 00:18:46 +01004079 PSA_ASSERT( psa_cipher_update( &operation2,
4080 output1 + first_part_size,
4081 output1_length - first_part_size,
4082 output2, output2_buffer_size,
4083 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004084 TEST_LE_U( function_output_length,
4085 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4086 alg,
4087 output1_length - first_part_size ) );
4088 TEST_LE_U( function_output_length,
4089 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004090 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004091
Gilles Peskine8817f612018-12-18 00:18:46 +01004092 PSA_ASSERT( psa_cipher_finish( &operation2,
4093 output2 + output2_length,
4094 output2_buffer_size - output2_length,
4095 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004096 TEST_LE_U( function_output_length,
4097 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4098 TEST_LE_U( function_output_length,
4099 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004100 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004101
Gilles Peskine8817f612018-12-18 00:18:46 +01004102 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004103
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004104 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004105
4106exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004107 psa_cipher_abort( &operation1 );
4108 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004109 mbedtls_free( output1 );
4110 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004111 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004112 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004113}
4114/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004115
Gilles Peskine20035e32018-02-03 22:44:14 +01004116/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004117void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004118 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004119 data_t *nonce,
4120 data_t *additional_data,
4121 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004122 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004123{
Ronald Cron5425a212020-08-04 14:58:35 +02004124 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004125 psa_key_type_t key_type = key_type_arg;
4126 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004127 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004128 unsigned char *output_data = NULL;
4129 size_t output_size = 0;
4130 size_t output_length = 0;
4131 unsigned char *output_data2 = NULL;
4132 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004133 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004134 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004136
Gilles Peskine8817f612018-12-18 00:18:46 +01004137 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004138
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004139 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4140 psa_set_key_algorithm( &attributes, alg );
4141 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004142
Gilles Peskine049c7532019-05-15 20:22:09 +02004143 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004144 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004145 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4146 key_bits = psa_get_key_bits( &attributes );
4147
4148 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4149 alg );
4150 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4151 * should be exact. */
4152 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4153 expected_result != PSA_ERROR_NOT_SUPPORTED )
4154 {
4155 TEST_EQUAL( output_size,
4156 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004157 TEST_LE_U( output_size,
4158 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004159 }
4160 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004161
Steven Cooremanf49478b2021-02-15 15:19:25 +01004162 status = psa_aead_encrypt( key, alg,
4163 nonce->x, nonce->len,
4164 additional_data->x,
4165 additional_data->len,
4166 input_data->x, input_data->len,
4167 output_data, output_size,
4168 &output_length );
4169
4170 /* If the operation is not supported, just skip and not fail in case the
4171 * encryption involves a common limitation of cryptography hardwares and
4172 * an alternative implementation. */
4173 if( status == PSA_ERROR_NOT_SUPPORTED )
4174 {
4175 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4176 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4177 }
4178
4179 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004180
4181 if( PSA_SUCCESS == expected_result )
4182 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004183 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004184
Gilles Peskine003a4a92019-05-14 16:09:40 +02004185 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4186 * should be exact. */
4187 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004188 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004189
Gilles Peskine7be11a72022-04-14 00:12:57 +02004190 TEST_LE_U( input_data->len,
4191 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004192
Ronald Cron5425a212020-08-04 14:58:35 +02004193 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004194 nonce->x, nonce->len,
4195 additional_data->x,
4196 additional_data->len,
4197 output_data, output_length,
4198 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004199 &output_length2 ),
4200 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004201
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004202 ASSERT_COMPARE( input_data->x, input_data->len,
4203 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004204 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004205
Gilles Peskinea1cac842018-06-11 19:33:02 +02004206exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004207 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004208 mbedtls_free( output_data );
4209 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004210 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004211}
4212/* END_CASE */
4213
4214/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004215void aead_encrypt( int key_type_arg, data_t *key_data,
4216 int alg_arg,
4217 data_t *nonce,
4218 data_t *additional_data,
4219 data_t *input_data,
4220 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004221{
Ronald Cron5425a212020-08-04 14:58:35 +02004222 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004223 psa_key_type_t key_type = key_type_arg;
4224 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004225 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004226 unsigned char *output_data = NULL;
4227 size_t output_size = 0;
4228 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004229 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004230 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004231
Gilles Peskine8817f612018-12-18 00:18:46 +01004232 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004233
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004234 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4235 psa_set_key_algorithm( &attributes, alg );
4236 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004237
Gilles Peskine049c7532019-05-15 20:22:09 +02004238 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004239 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004240 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4241 key_bits = psa_get_key_bits( &attributes );
4242
4243 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4244 alg );
4245 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4246 * should be exact. */
4247 TEST_EQUAL( output_size,
4248 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004249 TEST_LE_U( output_size,
4250 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004251 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004252
Steven Cooremand588ea12021-01-11 19:36:04 +01004253 status = psa_aead_encrypt( key, alg,
4254 nonce->x, nonce->len,
4255 additional_data->x, additional_data->len,
4256 input_data->x, input_data->len,
4257 output_data, output_size,
4258 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004259
Ronald Cron28a45ed2021-02-09 20:35:42 +01004260 /* If the operation is not supported, just skip and not fail in case the
4261 * encryption involves a common limitation of cryptography hardwares and
4262 * an alternative implementation. */
4263 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004264 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004265 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4266 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004267 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004268
4269 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004270 ASSERT_COMPARE( expected_result->x, expected_result->len,
4271 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004272
Gilles Peskinea1cac842018-06-11 19:33:02 +02004273exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004274 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004275 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004276 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004277}
4278/* END_CASE */
4279
4280/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004281void aead_decrypt( int key_type_arg, data_t *key_data,
4282 int alg_arg,
4283 data_t *nonce,
4284 data_t *additional_data,
4285 data_t *input_data,
4286 data_t *expected_data,
4287 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004288{
Ronald Cron5425a212020-08-04 14:58:35 +02004289 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004290 psa_key_type_t key_type = key_type_arg;
4291 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004292 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004293 unsigned char *output_data = NULL;
4294 size_t output_size = 0;
4295 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004296 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004297 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004298 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004299
Gilles Peskine8817f612018-12-18 00:18:46 +01004300 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004301
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004302 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4303 psa_set_key_algorithm( &attributes, alg );
4304 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004305
Gilles Peskine049c7532019-05-15 20:22:09 +02004306 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004307 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004308 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4309 key_bits = psa_get_key_bits( &attributes );
4310
4311 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4312 alg );
4313 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4314 expected_result != PSA_ERROR_NOT_SUPPORTED )
4315 {
4316 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4317 * should be exact. */
4318 TEST_EQUAL( output_size,
4319 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004320 TEST_LE_U( output_size,
4321 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004322 }
4323 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004324
Steven Cooremand588ea12021-01-11 19:36:04 +01004325 status = psa_aead_decrypt( key, alg,
4326 nonce->x, nonce->len,
4327 additional_data->x,
4328 additional_data->len,
4329 input_data->x, input_data->len,
4330 output_data, output_size,
4331 &output_length );
4332
Ronald Cron28a45ed2021-02-09 20:35:42 +01004333 /* If the operation is not supported, just skip and not fail in case the
4334 * decryption involves a common limitation of cryptography hardwares and
4335 * an alternative implementation. */
4336 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004337 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004338 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4339 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004340 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004341
4342 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004343
Gilles Peskine2d277862018-06-18 15:41:12 +02004344 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004345 ASSERT_COMPARE( expected_data->x, expected_data->len,
4346 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004347
Gilles Peskinea1cac842018-06-11 19:33:02 +02004348exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004349 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004350 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004351 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004352}
4353/* END_CASE */
4354
4355/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004356void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4357 int alg_arg,
4358 data_t *nonce,
4359 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004360 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004361 int do_set_lengths,
4362 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004363{
Paul Elliottd3f82412021-06-16 16:52:21 +01004364 size_t ad_part_len = 0;
4365 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004366 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004367
Paul Elliott32f46ba2021-09-23 18:24:36 +01004368 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004369 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004370 mbedtls_test_set_step( ad_part_len );
4371
4372 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004373 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004374 if( ad_part_len & 0x01 )
4375 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4376 else
4377 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004378 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004379
4380 /* Split ad into length(ad_part_len) parts. */
4381 if( !aead_multipart_internal_func( key_type_arg, key_data,
4382 alg_arg, nonce,
4383 additional_data,
4384 ad_part_len,
4385 input_data, -1,
4386 set_lengths_method,
4387 expected_output,
4388 1, 0 ) )
4389 break;
4390
4391 /* length(0) part, length(ad_part_len) part, length(0) part... */
4392 mbedtls_test_set_step( 1000 + ad_part_len );
4393
4394 if( !aead_multipart_internal_func( key_type_arg, key_data,
4395 alg_arg, nonce,
4396 additional_data,
4397 ad_part_len,
4398 input_data, -1,
4399 set_lengths_method,
4400 expected_output,
4401 1, 1 ) )
4402 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004403 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004404
Paul Elliott32f46ba2021-09-23 18:24:36 +01004405 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004406 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004407 /* Split data into length(data_part_len) parts. */
4408 mbedtls_test_set_step( 2000 + data_part_len );
4409
4410 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004411 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004412 if( data_part_len & 0x01 )
4413 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4414 else
4415 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004416 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004417
Paul Elliott32f46ba2021-09-23 18:24:36 +01004418 if( !aead_multipart_internal_func( key_type_arg, key_data,
4419 alg_arg, nonce,
4420 additional_data, -1,
4421 input_data, data_part_len,
4422 set_lengths_method,
4423 expected_output,
4424 1, 0 ) )
4425 break;
4426
4427 /* length(0) part, length(data_part_len) part, length(0) part... */
4428 mbedtls_test_set_step( 3000 + data_part_len );
4429
4430 if( !aead_multipart_internal_func( key_type_arg, key_data,
4431 alg_arg, nonce,
4432 additional_data, -1,
4433 input_data, data_part_len,
4434 set_lengths_method,
4435 expected_output,
4436 1, 1 ) )
4437 break;
4438 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004439
Paul Elliott8fc45162021-06-23 16:06:01 +01004440 /* Goto is required to silence warnings about unused labels, as we
4441 * don't actually do any test assertions in this function. */
4442 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004443}
4444/* END_CASE */
4445
4446/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004447void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
4448 int alg_arg,
4449 data_t *nonce,
4450 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004451 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004452 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01004453 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004454{
Paul Elliottd3f82412021-06-16 16:52:21 +01004455 size_t ad_part_len = 0;
4456 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004457 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004458
Paul Elliott32f46ba2021-09-23 18:24:36 +01004459 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004460 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004461 /* Split ad into length(ad_part_len) parts. */
4462 mbedtls_test_set_step( ad_part_len );
4463
4464 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004465 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004466 if( ad_part_len & 0x01 )
4467 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4468 else
4469 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004470 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004471
4472 if( !aead_multipart_internal_func( key_type_arg, key_data,
4473 alg_arg, nonce,
4474 additional_data,
4475 ad_part_len,
4476 input_data, -1,
4477 set_lengths_method,
4478 expected_output,
4479 0, 0 ) )
4480 break;
4481
4482 /* length(0) part, length(ad_part_len) part, length(0) part... */
4483 mbedtls_test_set_step( 1000 + ad_part_len );
4484
4485 if( !aead_multipart_internal_func( key_type_arg, key_data,
4486 alg_arg, nonce,
4487 additional_data,
4488 ad_part_len,
4489 input_data, -1,
4490 set_lengths_method,
4491 expected_output,
4492 0, 1 ) )
4493 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004494 }
4495
Paul Elliott32f46ba2021-09-23 18:24:36 +01004496 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004497 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004498 /* Split data into length(data_part_len) parts. */
4499 mbedtls_test_set_step( 2000 + data_part_len );
4500
4501 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004502 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004503 if( data_part_len & 0x01 )
4504 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4505 else
4506 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004507 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004508
4509 if( !aead_multipart_internal_func( key_type_arg, key_data,
4510 alg_arg, nonce,
4511 additional_data, -1,
4512 input_data, data_part_len,
4513 set_lengths_method,
4514 expected_output,
4515 0, 0 ) )
4516 break;
4517
4518 /* length(0) part, length(data_part_len) part, length(0) part... */
4519 mbedtls_test_set_step( 3000 + data_part_len );
4520
4521 if( !aead_multipart_internal_func( key_type_arg, key_data,
4522 alg_arg, nonce,
4523 additional_data, -1,
4524 input_data, data_part_len,
4525 set_lengths_method,
4526 expected_output,
4527 0, 1 ) )
4528 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004529 }
4530
Paul Elliott8fc45162021-06-23 16:06:01 +01004531 /* Goto is required to silence warnings about unused labels, as we
4532 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01004533 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004534}
4535/* END_CASE */
4536
4537/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004538void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
4539 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01004540 int nonce_length,
4541 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004542 data_t *additional_data,
4543 data_t *input_data,
4544 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004545{
4546
4547 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4548 psa_key_type_t key_type = key_type_arg;
4549 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004550 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004551 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
4552 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4553 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01004554 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01004555 size_t actual_nonce_length = 0;
4556 size_t expected_nonce_length = expected_nonce_length_arg;
4557 unsigned char *output = NULL;
4558 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004559 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01004560 size_t ciphertext_size = 0;
4561 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004562 size_t tag_length = 0;
4563 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004564
4565 PSA_ASSERT( psa_crypto_init( ) );
4566
4567 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
4568 psa_set_key_algorithm( & attributes, alg );
4569 psa_set_key_type( & attributes, key_type );
4570
4571 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4572 &key ) );
4573
4574 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4575
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004576 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4577
Paul Elliottf1277632021-08-24 18:11:37 +01004578 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004579
Paul Elliottf1277632021-08-24 18:11:37 +01004580 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004581
Gilles Peskine7be11a72022-04-14 00:12:57 +02004582 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004583
Paul Elliottf1277632021-08-24 18:11:37 +01004584 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004585
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004586 status = psa_aead_encrypt_setup( &operation, key, alg );
4587
4588 /* If the operation is not supported, just skip and not fail in case the
4589 * encryption involves a common limitation of cryptography hardwares and
4590 * an alternative implementation. */
4591 if( status == PSA_ERROR_NOT_SUPPORTED )
4592 {
4593 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01004594 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004595 }
4596
4597 PSA_ASSERT( status );
4598
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004599 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01004600 nonce_length,
4601 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004602
Paul Elliott693bf312021-07-23 17:40:41 +01004603 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004604
Paul Elliottf1277632021-08-24 18:11:37 +01004605 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01004606
Paul Elliott88ecbe12021-09-22 17:23:03 +01004607 if( expected_status == PSA_SUCCESS )
4608 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
4609 alg ) );
4610
Gilles Peskine7be11a72022-04-14 00:12:57 +02004611 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01004612
Paul Elliott693bf312021-07-23 17:40:41 +01004613 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004614 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004615 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01004616 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4617 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004618
4619 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4620 additional_data->len ) );
4621
4622 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01004623 output, output_size,
4624 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004625
Paul Elliottf1277632021-08-24 18:11:37 +01004626 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4627 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01004628 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4629 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004630
4631exit:
4632 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01004633 mbedtls_free( output );
4634 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01004635 psa_aead_abort( &operation );
4636 PSA_DONE( );
4637}
4638/* END_CASE */
4639
4640/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01004641void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
4642 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01004643 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004644 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01004645 data_t *additional_data,
4646 data_t *input_data,
4647 int expected_status_arg )
4648{
4649
4650 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4651 psa_key_type_t key_type = key_type_arg;
4652 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004653 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01004654 uint8_t *nonce_buffer = NULL;
4655 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4656 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4657 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004658 unsigned char *output = NULL;
4659 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01004660 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01004661 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004662 size_t ciphertext_size = 0;
4663 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01004664 size_t tag_length = 0;
4665 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01004666 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004667 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01004668
4669 PSA_ASSERT( psa_crypto_init( ) );
4670
4671 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4672 psa_set_key_algorithm( &attributes, alg );
4673 psa_set_key_type( &attributes, key_type );
4674
4675 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4676 &key ) );
4677
4678 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4679
4680 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
4681
Paul Elliott6f0e7202021-08-25 12:57:18 +01004682 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004683
Paul Elliott6f0e7202021-08-25 12:57:18 +01004684 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01004685
Gilles Peskine7be11a72022-04-14 00:12:57 +02004686 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01004687
Paul Elliott6f0e7202021-08-25 12:57:18 +01004688 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01004689
Paul Elliott863864a2021-07-23 17:28:31 +01004690 status = psa_aead_encrypt_setup( &operation, key, alg );
4691
4692 /* If the operation is not supported, just skip and not fail in case the
4693 * encryption involves a common limitation of cryptography hardwares and
4694 * an alternative implementation. */
4695 if( status == PSA_ERROR_NOT_SUPPORTED )
4696 {
4697 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004698 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01004699 }
4700
4701 PSA_ASSERT( status );
4702
Paul Elliott4023ffd2021-09-10 16:21:22 +01004703 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
4704 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01004705 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01004706 /* Arbitrary size buffer, to test zero length valid buffer. */
4707 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01004708 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01004709 }
4710 else
4711 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004712 /* If length is zero, then this will return NULL. */
4713 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01004714 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01004715
Paul Elliott4023ffd2021-09-10 16:21:22 +01004716 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01004717 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01004718 for( index = 0; index < nonce_length - 1; ++index )
4719 {
4720 nonce_buffer[index] = 'a' + index;
4721 }
Paul Elliott66696b52021-08-16 18:42:41 +01004722 }
Paul Elliott863864a2021-07-23 17:28:31 +01004723 }
4724
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004725 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
4726 {
4727 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4728 input_data->len ) );
4729 }
4730
Paul Elliott6f0e7202021-08-25 12:57:18 +01004731 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01004732
Paul Elliott693bf312021-07-23 17:40:41 +01004733 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004734
4735 if( expected_status == PSA_SUCCESS )
4736 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004737 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
4738 {
4739 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4740 input_data->len ) );
4741 }
4742 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
4743 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01004744
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004745 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
4746 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
4747 additional_data->len ),
4748 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004749
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004750 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004751 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004752 &ciphertext_length ),
4753 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004754
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004755 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01004756 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01004757 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
4758 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01004759 }
4760
4761exit:
4762 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01004763 mbedtls_free( output );
4764 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01004765 mbedtls_free( nonce_buffer );
4766 psa_aead_abort( &operation );
4767 PSA_DONE( );
4768}
4769/* END_CASE */
4770
4771/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004772void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
4773 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004774 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01004775 data_t *nonce,
4776 data_t *additional_data,
4777 data_t *input_data,
4778 int expected_status_arg )
4779{
4780
4781 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4782 psa_key_type_t key_type = key_type_arg;
4783 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004784 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01004785 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4786 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4787 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01004788 unsigned char *output = NULL;
4789 unsigned char *ciphertext = NULL;
4790 size_t output_size = output_size_arg;
4791 size_t ciphertext_size = 0;
4792 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01004793 size_t tag_length = 0;
4794 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
4795
4796 PSA_ASSERT( psa_crypto_init( ) );
4797
4798 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4799 psa_set_key_algorithm( &attributes, alg );
4800 psa_set_key_type( &attributes, key_type );
4801
4802 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4803 &key ) );
4804
4805 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4806
Paul Elliottc6d11d02021-09-01 12:04:23 +01004807 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004808
Paul Elliottc6d11d02021-09-01 12:04:23 +01004809 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01004810
Paul Elliottc6d11d02021-09-01 12:04:23 +01004811 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01004812
Paul Elliott43fbda62021-07-23 18:30:59 +01004813 status = psa_aead_encrypt_setup( &operation, key, alg );
4814
4815 /* If the operation is not supported, just skip and not fail in case the
4816 * encryption involves a common limitation of cryptography hardwares and
4817 * an alternative implementation. */
4818 if( status == PSA_ERROR_NOT_SUPPORTED )
4819 {
4820 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4821 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4822 }
4823
4824 PSA_ASSERT( status );
4825
Paul Elliott47b9a142021-10-07 15:04:57 +01004826 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4827 input_data->len ) );
4828
Paul Elliott43fbda62021-07-23 18:30:59 +01004829 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4830
4831 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4832 additional_data->len ) );
4833
4834 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01004835 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01004836
4837 TEST_EQUAL( status, expected_status );
4838
4839 if( expected_status == PSA_SUCCESS )
4840 {
4841 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01004842 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
4843 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01004844 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
4845 }
4846
4847exit:
4848 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01004849 mbedtls_free( output );
4850 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01004851 psa_aead_abort( &operation );
4852 PSA_DONE( );
4853}
4854/* END_CASE */
4855
Paul Elliott91b021e2021-07-23 18:52:31 +01004856/* BEGIN_CASE */
4857void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
4858 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004859 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01004860 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01004861 data_t *nonce,
4862 data_t *additional_data,
4863 data_t *input_data,
4864 int expected_status_arg )
4865{
4866
4867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4868 psa_key_type_t key_type = key_type_arg;
4869 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004870 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01004871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4872 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4873 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004874 unsigned char *ciphertext = NULL;
4875 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01004876 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004877 size_t ciphertext_size = 0;
4878 size_t ciphertext_length = 0;
4879 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01004880 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01004881 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01004882
4883 PSA_ASSERT( psa_crypto_init( ) );
4884
4885 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4886 psa_set_key_algorithm( &attributes, alg );
4887 psa_set_key_type( &attributes, key_type );
4888
4889 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4890 &key ) );
4891
4892 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4893
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004894 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01004895
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004896 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004897
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004898 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01004899
Paul Elliott719c1322021-09-13 18:27:22 +01004900 ASSERT_ALLOC( tag_buffer, tag_size );
4901
Paul Elliott91b021e2021-07-23 18:52:31 +01004902 status = psa_aead_encrypt_setup( &operation, key, alg );
4903
4904 /* If the operation is not supported, just skip and not fail in case the
4905 * encryption involves a common limitation of cryptography hardwares and
4906 * an alternative implementation. */
4907 if( status == PSA_ERROR_NOT_SUPPORTED )
4908 {
4909 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4910 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4911 }
4912
4913 PSA_ASSERT( status );
4914
4915 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
4916
Paul Elliott76bda482021-10-07 17:07:23 +01004917 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
4918 input_data->len ) );
4919
Paul Elliott91b021e2021-07-23 18:52:31 +01004920 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
4921 additional_data->len ) );
4922
4923 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004924 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01004925
4926 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004927 status = psa_aead_finish( &operation, finish_ciphertext,
4928 finish_ciphertext_size,
4929 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01004930 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01004931
4932 TEST_EQUAL( status, expected_status );
4933
4934exit:
4935 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01004936 mbedtls_free( ciphertext );
4937 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01004938 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01004939 psa_aead_abort( &operation );
4940 PSA_DONE( );
4941}
4942/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01004943
4944/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01004945void aead_multipart_verify( int key_type_arg, data_t *key_data,
4946 int alg_arg,
4947 data_t *nonce,
4948 data_t *additional_data,
4949 data_t *input_data,
4950 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004951 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01004952 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01004953 int expected_status_arg )
4954{
4955 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4956 psa_key_type_t key_type = key_type_arg;
4957 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01004958 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01004959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4960 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
4961 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01004962 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01004963 unsigned char *plaintext = NULL;
4964 unsigned char *finish_plaintext = NULL;
4965 size_t plaintext_size = 0;
4966 size_t plaintext_length = 0;
4967 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004968 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01004969 unsigned char *tag_buffer = NULL;
4970 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01004971
4972 PSA_ASSERT( psa_crypto_init( ) );
4973
4974 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4975 psa_set_key_algorithm( &attributes, alg );
4976 psa_set_key_type( &attributes, key_type );
4977
4978 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4979 &key ) );
4980
4981 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4982
4983 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
4984 input_data->len );
4985
4986 ASSERT_ALLOC( plaintext, plaintext_size );
4987
4988 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
4989
4990 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
4991
Paul Elliott9961a662021-09-17 19:19:02 +01004992 status = psa_aead_decrypt_setup( &operation, key, alg );
4993
4994 /* If the operation is not supported, just skip and not fail in case the
4995 * encryption involves a common limitation of cryptography hardwares and
4996 * an alternative implementation. */
4997 if( status == PSA_ERROR_NOT_SUPPORTED )
4998 {
4999 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5000 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5001 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005002 TEST_EQUAL( status, expected_setup_status );
5003
5004 if( status != PSA_SUCCESS )
5005 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005006
5007 PSA_ASSERT( status );
5008
5009 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5010
Paul Elliottfec6f372021-10-06 17:15:02 +01005011 status = psa_aead_set_lengths( &operation, additional_data->len,
5012 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005013 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005014
Paul Elliott9961a662021-09-17 19:19:02 +01005015 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5016 additional_data->len ) );
5017
5018 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5019 input_data->len,
5020 plaintext, plaintext_size,
5021 &plaintext_length ) );
5022
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005023 if( tag_usage == USE_GIVEN_TAG )
5024 {
5025 tag_buffer = tag->x;
5026 tag_size = tag->len;
5027 }
5028
Paul Elliott9961a662021-09-17 19:19:02 +01005029 status = psa_aead_verify( &operation, finish_plaintext,
5030 verify_plaintext_size,
5031 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005032 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005033
5034 TEST_EQUAL( status, expected_status );
5035
5036exit:
5037 psa_destroy_key( key );
5038 mbedtls_free( plaintext );
5039 mbedtls_free( finish_plaintext );
5040 psa_aead_abort( &operation );
5041 PSA_DONE( );
5042}
5043/* END_CASE */
5044
Paul Elliott9961a662021-09-17 19:19:02 +01005045/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005046void aead_multipart_setup( int key_type_arg, data_t *key_data,
5047 int alg_arg, int expected_status_arg )
5048{
5049 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5050 psa_key_type_t key_type = key_type_arg;
5051 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005052 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5054 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5055 psa_status_t expected_status = expected_status_arg;
5056
5057 PSA_ASSERT( psa_crypto_init( ) );
5058
5059 psa_set_key_usage_flags( &attributes,
5060 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5061 psa_set_key_algorithm( &attributes, alg );
5062 psa_set_key_type( &attributes, key_type );
5063
5064 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5065 &key ) );
5066
Paul Elliott5221ef62021-09-19 17:33:03 +01005067 status = psa_aead_encrypt_setup( &operation, key, alg );
5068
5069 TEST_EQUAL( status, expected_status );
5070
5071 psa_aead_abort( &operation );
5072
Paul Elliott5221ef62021-09-19 17:33:03 +01005073 status = psa_aead_decrypt_setup( &operation, key, alg );
5074
5075 TEST_EQUAL(status, expected_status );
5076
5077exit:
5078 psa_destroy_key( key );
5079 psa_aead_abort( &operation );
5080 PSA_DONE( );
5081}
5082/* END_CASE */
5083
5084/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005085void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5086 int alg_arg,
5087 data_t *nonce,
5088 data_t *additional_data,
5089 data_t *input_data )
5090{
5091 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5092 psa_key_type_t key_type = key_type_arg;
5093 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005094 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005095 unsigned char *output_data = NULL;
5096 unsigned char *final_data = NULL;
5097 size_t output_size = 0;
5098 size_t finish_output_size = 0;
5099 size_t output_length = 0;
5100 size_t key_bits = 0;
5101 size_t tag_length = 0;
5102 size_t tag_size = 0;
5103 size_t nonce_length = 0;
5104 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5105 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5106 size_t output_part_length = 0;
5107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5108
5109 PSA_ASSERT( psa_crypto_init( ) );
5110
5111 psa_set_key_usage_flags( & attributes,
5112 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5113 psa_set_key_algorithm( & attributes, alg );
5114 psa_set_key_type( & attributes, key_type );
5115
5116 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5117 &key ) );
5118
5119 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5120 key_bits = psa_get_key_bits( &attributes );
5121
5122 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5123
Gilles Peskine7be11a72022-04-14 00:12:57 +02005124 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005125
5126 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5127
5128 ASSERT_ALLOC( output_data, output_size );
5129
5130 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5131
Gilles Peskine7be11a72022-04-14 00:12:57 +02005132 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005133
5134 ASSERT_ALLOC( final_data, finish_output_size );
5135
5136 /* Test all operations error without calling setup first. */
5137
Paul Elliottc23a9a02021-06-21 18:32:46 +01005138 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5139 PSA_ERROR_BAD_STATE );
5140
5141 psa_aead_abort( &operation );
5142
Paul Elliottc23a9a02021-06-21 18:32:46 +01005143 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5144 PSA_AEAD_NONCE_MAX_SIZE,
5145 &nonce_length ),
5146 PSA_ERROR_BAD_STATE );
5147
5148 psa_aead_abort( &operation );
5149
Paul Elliott481be342021-07-16 17:38:47 +01005150 /* ------------------------------------------------------- */
5151
Paul Elliottc23a9a02021-06-21 18:32:46 +01005152 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5153 input_data->len ),
5154 PSA_ERROR_BAD_STATE );
5155
5156 psa_aead_abort( &operation );
5157
Paul Elliott481be342021-07-16 17:38:47 +01005158 /* ------------------------------------------------------- */
5159
Paul Elliottc23a9a02021-06-21 18:32:46 +01005160 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5161 additional_data->len ),
5162 PSA_ERROR_BAD_STATE );
5163
5164 psa_aead_abort( &operation );
5165
Paul Elliott481be342021-07-16 17:38:47 +01005166 /* ------------------------------------------------------- */
5167
Paul Elliottc23a9a02021-06-21 18:32:46 +01005168 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5169 input_data->len, output_data,
5170 output_size, &output_length ),
5171 PSA_ERROR_BAD_STATE );
5172
5173 psa_aead_abort( &operation );
5174
Paul Elliott481be342021-07-16 17:38:47 +01005175 /* ------------------------------------------------------- */
5176
Paul Elliottc23a9a02021-06-21 18:32:46 +01005177 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5178 finish_output_size,
5179 &output_part_length,
5180 tag_buffer, tag_length,
5181 &tag_size ),
5182 PSA_ERROR_BAD_STATE );
5183
5184 psa_aead_abort( &operation );
5185
Paul Elliott481be342021-07-16 17:38:47 +01005186 /* ------------------------------------------------------- */
5187
Paul Elliottc23a9a02021-06-21 18:32:46 +01005188 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5189 finish_output_size,
5190 &output_part_length,
5191 tag_buffer,
5192 tag_length ),
5193 PSA_ERROR_BAD_STATE );
5194
5195 psa_aead_abort( &operation );
5196
5197 /* Test for double setups. */
5198
Paul Elliottc23a9a02021-06-21 18:32:46 +01005199 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5200
5201 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5202 PSA_ERROR_BAD_STATE );
5203
5204 psa_aead_abort( &operation );
5205
Paul Elliott481be342021-07-16 17:38:47 +01005206 /* ------------------------------------------------------- */
5207
Paul Elliottc23a9a02021-06-21 18:32:46 +01005208 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5209
5210 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5211 PSA_ERROR_BAD_STATE );
5212
5213 psa_aead_abort( &operation );
5214
Paul Elliott374a2be2021-07-16 17:53:40 +01005215 /* ------------------------------------------------------- */
5216
Paul Elliott374a2be2021-07-16 17:53:40 +01005217 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5218
5219 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5220 PSA_ERROR_BAD_STATE );
5221
5222 psa_aead_abort( &operation );
5223
5224 /* ------------------------------------------------------- */
5225
Paul Elliott374a2be2021-07-16 17:53:40 +01005226 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5227
5228 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5229 PSA_ERROR_BAD_STATE );
5230
5231 psa_aead_abort( &operation );
5232
Paul Elliottc23a9a02021-06-21 18:32:46 +01005233 /* Test for not setting a nonce. */
5234
Paul Elliottc23a9a02021-06-21 18:32:46 +01005235 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5236
5237 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5238 additional_data->len ),
5239 PSA_ERROR_BAD_STATE );
5240
5241 psa_aead_abort( &operation );
5242
Paul Elliott7f628422021-09-01 12:08:29 +01005243 /* ------------------------------------------------------- */
5244
Paul Elliott7f628422021-09-01 12:08:29 +01005245 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5246
5247 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5248 input_data->len, output_data,
5249 output_size, &output_length ),
5250 PSA_ERROR_BAD_STATE );
5251
5252 psa_aead_abort( &operation );
5253
Paul Elliottbdc2c682021-09-21 18:37:10 +01005254 /* ------------------------------------------------------- */
5255
Paul Elliottbdc2c682021-09-21 18:37:10 +01005256 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5257
5258 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5259 finish_output_size,
5260 &output_part_length,
5261 tag_buffer, tag_length,
5262 &tag_size ),
5263 PSA_ERROR_BAD_STATE );
5264
5265 psa_aead_abort( &operation );
5266
5267 /* ------------------------------------------------------- */
5268
Paul Elliottbdc2c682021-09-21 18:37:10 +01005269 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5270
5271 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5272 finish_output_size,
5273 &output_part_length,
5274 tag_buffer,
5275 tag_length ),
5276 PSA_ERROR_BAD_STATE );
5277
5278 psa_aead_abort( &operation );
5279
Paul Elliottc23a9a02021-06-21 18:32:46 +01005280 /* Test for double setting nonce. */
5281
Paul Elliottc23a9a02021-06-21 18:32:46 +01005282 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5283
5284 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5285
5286 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5287 PSA_ERROR_BAD_STATE );
5288
5289 psa_aead_abort( &operation );
5290
Paul Elliott374a2be2021-07-16 17:53:40 +01005291 /* Test for double generating nonce. */
5292
Paul Elliott374a2be2021-07-16 17:53:40 +01005293 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5294
5295 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5296 PSA_AEAD_NONCE_MAX_SIZE,
5297 &nonce_length ) );
5298
5299 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5300 PSA_AEAD_NONCE_MAX_SIZE,
5301 &nonce_length ),
5302 PSA_ERROR_BAD_STATE );
5303
5304
5305 psa_aead_abort( &operation );
5306
5307 /* Test for generate nonce then set and vice versa */
5308
Paul Elliott374a2be2021-07-16 17:53:40 +01005309 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5310
5311 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5312 PSA_AEAD_NONCE_MAX_SIZE,
5313 &nonce_length ) );
5314
5315 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5316 PSA_ERROR_BAD_STATE );
5317
5318 psa_aead_abort( &operation );
5319
Andrzej Kurekad837522021-12-15 15:28:49 +01005320 /* Test for generating nonce after calling set lengths */
5321
5322 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5323
5324 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5325 input_data->len ) );
5326
5327 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5328 PSA_AEAD_NONCE_MAX_SIZE,
5329 &nonce_length ) );
5330
5331 psa_aead_abort( &operation );
5332
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005333 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005334
5335 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5336
5337 if( operation.alg == PSA_ALG_CCM )
5338 {
5339 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5340 input_data->len ),
5341 PSA_ERROR_INVALID_ARGUMENT );
5342 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5343 PSA_AEAD_NONCE_MAX_SIZE,
5344 &nonce_length ),
5345 PSA_ERROR_BAD_STATE );
5346 }
5347 else
5348 {
5349 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5350 input_data->len ) );
5351 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5352 PSA_AEAD_NONCE_MAX_SIZE,
5353 &nonce_length ) );
5354 }
5355
5356 psa_aead_abort( &operation );
5357
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005358 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005359#if SIZE_MAX > UINT32_MAX
5360 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5361
5362 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5363 {
5364 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5365 input_data->len ),
5366 PSA_ERROR_INVALID_ARGUMENT );
5367 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5368 PSA_AEAD_NONCE_MAX_SIZE,
5369 &nonce_length ),
5370 PSA_ERROR_BAD_STATE );
5371 }
5372 else
5373 {
5374 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5375 input_data->len ) );
5376 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5377 PSA_AEAD_NONCE_MAX_SIZE,
5378 &nonce_length ) );
5379 }
5380
5381 psa_aead_abort( &operation );
5382#endif
5383
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005384 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005385
5386 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5387
5388 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5389 PSA_AEAD_NONCE_MAX_SIZE,
5390 &nonce_length ) );
5391
5392 if( operation.alg == PSA_ALG_CCM )
5393 {
5394 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5395 input_data->len ),
5396 PSA_ERROR_INVALID_ARGUMENT );
5397 }
5398 else
5399 {
5400 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5401 input_data->len ) );
5402 }
5403
5404 psa_aead_abort( &operation );
5405
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005406 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005407 /* Test for setting nonce after calling set lengths */
5408
5409 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5410
5411 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5412 input_data->len ) );
5413
5414 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5415
5416 psa_aead_abort( &operation );
5417
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005418 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005419
5420 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5421
5422 if( operation.alg == PSA_ALG_CCM )
5423 {
5424 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5425 input_data->len ),
5426 PSA_ERROR_INVALID_ARGUMENT );
5427 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5428 PSA_ERROR_BAD_STATE );
5429 }
5430 else
5431 {
5432 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5433 input_data->len ) );
5434 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5435 }
5436
5437 psa_aead_abort( &operation );
5438
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005439 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005440#if SIZE_MAX > UINT32_MAX
5441 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5442
5443 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5444 {
5445 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5446 input_data->len ),
5447 PSA_ERROR_INVALID_ARGUMENT );
5448 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5449 PSA_ERROR_BAD_STATE );
5450 }
5451 else
5452 {
5453 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5454 input_data->len ) );
5455 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5456 }
5457
5458 psa_aead_abort( &operation );
5459#endif
5460
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005461 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005462
5463 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5464
5465 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5466
5467 if( operation.alg == PSA_ALG_CCM )
5468 {
5469 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5470 input_data->len ),
5471 PSA_ERROR_INVALID_ARGUMENT );
5472 }
5473 else
5474 {
5475 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5476 input_data->len ) );
5477 }
5478
5479 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01005480
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005481 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005482#if SIZE_MAX > UINT32_MAX
5483 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5484
5485 if( operation.alg == PSA_ALG_GCM )
5486 {
5487 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5488 SIZE_MAX ),
5489 PSA_ERROR_INVALID_ARGUMENT );
5490 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5491 PSA_ERROR_BAD_STATE );
5492 }
5493 else if ( operation.alg != PSA_ALG_CCM )
5494 {
5495 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5496 SIZE_MAX ) );
5497 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5498 }
5499
5500 psa_aead_abort( &operation );
5501
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005502 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005503 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5504
5505 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5506
5507 if( operation.alg == PSA_ALG_GCM )
5508 {
5509 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5510 SIZE_MAX ),
5511 PSA_ERROR_INVALID_ARGUMENT );
5512 }
5513 else if ( operation.alg != PSA_ALG_CCM )
5514 {
5515 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5516 SIZE_MAX ) );
5517 }
5518
5519 psa_aead_abort( &operation );
5520#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01005521
5522 /* ------------------------------------------------------- */
5523
Paul Elliott374a2be2021-07-16 17:53:40 +01005524 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5525
5526 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5527
5528 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5529 PSA_AEAD_NONCE_MAX_SIZE,
5530 &nonce_length ),
5531 PSA_ERROR_BAD_STATE );
5532
5533 psa_aead_abort( &operation );
5534
Paul Elliott7220cae2021-06-22 17:25:57 +01005535 /* Test for generating nonce in decrypt setup. */
5536
Paul Elliott7220cae2021-06-22 17:25:57 +01005537 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5538
5539 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5540 PSA_AEAD_NONCE_MAX_SIZE,
5541 &nonce_length ),
5542 PSA_ERROR_BAD_STATE );
5543
5544 psa_aead_abort( &operation );
5545
Paul Elliottc23a9a02021-06-21 18:32:46 +01005546 /* Test for setting lengths twice. */
5547
Paul Elliottc23a9a02021-06-21 18:32:46 +01005548 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5549
5550 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5551
5552 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5553 input_data->len ) );
5554
5555 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5556 input_data->len ),
5557 PSA_ERROR_BAD_STATE );
5558
5559 psa_aead_abort( &operation );
5560
Andrzej Kurekad837522021-12-15 15:28:49 +01005561 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005562
Paul Elliottc23a9a02021-06-21 18:32:46 +01005563 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5564
5565 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5566
Andrzej Kurekad837522021-12-15 15:28:49 +01005567 if( operation.alg == PSA_ALG_CCM )
5568 {
Paul Elliottf94bd992021-09-19 18:15:59 +01005569
Andrzej Kurekad837522021-12-15 15:28:49 +01005570 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5571 additional_data->len ),
5572 PSA_ERROR_BAD_STATE );
5573 }
5574 else
5575 {
5576 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5577 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01005578
Andrzej Kurekad837522021-12-15 15:28:49 +01005579 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5580 input_data->len ),
5581 PSA_ERROR_BAD_STATE );
5582 }
Paul Elliottf94bd992021-09-19 18:15:59 +01005583 psa_aead_abort( &operation );
5584
5585 /* ------------------------------------------------------- */
5586
Paul Elliottf94bd992021-09-19 18:15:59 +01005587 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5588
5589 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5590
Andrzej Kurekad837522021-12-15 15:28:49 +01005591 if( operation.alg == PSA_ALG_CCM )
5592 {
5593 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5594 input_data->len, output_data,
5595 output_size, &output_length ),
5596 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005597
Andrzej Kurekad837522021-12-15 15:28:49 +01005598 }
5599 else
5600 {
5601 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5602 input_data->len, output_data,
5603 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005604
Andrzej Kurekad837522021-12-15 15:28:49 +01005605 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5606 input_data->len ),
5607 PSA_ERROR_BAD_STATE );
5608 }
5609 psa_aead_abort( &operation );
5610
5611 /* ------------------------------------------------------- */
5612
5613 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5614
5615 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5616
5617 if( operation.alg == PSA_ALG_CCM )
5618 {
5619 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5620 finish_output_size,
5621 &output_part_length,
5622 tag_buffer, tag_length,
5623 &tag_size ) );
5624 }
5625 else
5626 {
5627 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5628 finish_output_size,
5629 &output_part_length,
5630 tag_buffer, tag_length,
5631 &tag_size ) );
5632
5633 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5634 input_data->len ),
5635 PSA_ERROR_BAD_STATE );
5636 }
5637 psa_aead_abort( &operation );
5638
5639 /* Test for setting lengths after generating nonce + already starting data. */
5640
5641 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5642
5643 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5644 PSA_AEAD_NONCE_MAX_SIZE,
5645 &nonce_length ) );
5646 if( operation.alg == PSA_ALG_CCM )
5647 {
5648
5649 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5650 additional_data->len ),
5651 PSA_ERROR_BAD_STATE );
5652 }
5653 else
5654 {
5655 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5656 additional_data->len ) );
5657
5658 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5659 input_data->len ),
5660 PSA_ERROR_BAD_STATE );
5661 }
5662 psa_aead_abort( &operation );
5663
5664 /* ------------------------------------------------------- */
5665
5666 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5667
5668 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5669 PSA_AEAD_NONCE_MAX_SIZE,
5670 &nonce_length ) );
5671 if( operation.alg == PSA_ALG_CCM )
5672 {
5673 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5674 input_data->len, output_data,
5675 output_size, &output_length ),
5676 PSA_ERROR_BAD_STATE );
5677
5678 }
5679 else
5680 {
5681 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5682 input_data->len, output_data,
5683 output_size, &output_length ) );
5684
5685 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5686 input_data->len ),
5687 PSA_ERROR_BAD_STATE );
5688 }
5689 psa_aead_abort( &operation );
5690
5691 /* ------------------------------------------------------- */
5692
5693 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5694
5695 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5696 PSA_AEAD_NONCE_MAX_SIZE,
5697 &nonce_length ) );
5698 if( operation.alg == PSA_ALG_CCM )
5699 {
5700 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5701 finish_output_size,
5702 &output_part_length,
5703 tag_buffer, tag_length,
5704 &tag_size ) );
5705 }
5706 else
5707 {
5708 PSA_ASSERT( psa_aead_finish( &operation, final_data,
5709 finish_output_size,
5710 &output_part_length,
5711 tag_buffer, tag_length,
5712 &tag_size ) );
5713
5714 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5715 input_data->len ),
5716 PSA_ERROR_BAD_STATE );
5717 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005718 psa_aead_abort( &operation );
5719
Paul Elliott243080c2021-07-21 19:01:17 +01005720 /* Test for not sending any additional data or data after setting non zero
5721 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005722
Paul Elliottc23a9a02021-06-21 18:32:46 +01005723 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5724
5725 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5726
5727 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5728 input_data->len ) );
5729
5730 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5731 finish_output_size,
5732 &output_part_length,
5733 tag_buffer, tag_length,
5734 &tag_size ),
5735 PSA_ERROR_INVALID_ARGUMENT );
5736
5737 psa_aead_abort( &operation );
5738
Paul Elliott243080c2021-07-21 19:01:17 +01005739 /* Test for not sending any additional data or data after setting non-zero
5740 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005741
Paul Elliottc23a9a02021-06-21 18:32:46 +01005742 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5743
5744 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5745
5746 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5747 input_data->len ) );
5748
5749 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5750 finish_output_size,
5751 &output_part_length,
5752 tag_buffer,
5753 tag_length ),
5754 PSA_ERROR_INVALID_ARGUMENT );
5755
5756 psa_aead_abort( &operation );
5757
Paul Elliott243080c2021-07-21 19:01:17 +01005758 /* Test for not sending any additional data after setting a non-zero length
5759 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005760
Paul Elliottc23a9a02021-06-21 18:32:46 +01005761 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5762
5763 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5764
5765 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5766 input_data->len ) );
5767
5768 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5769 input_data->len, output_data,
5770 output_size, &output_length ),
5771 PSA_ERROR_INVALID_ARGUMENT );
5772
5773 psa_aead_abort( &operation );
5774
Paul Elliottf94bd992021-09-19 18:15:59 +01005775 /* Test for not sending any data after setting a non-zero length for it.*/
5776
Paul Elliottf94bd992021-09-19 18:15:59 +01005777 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5778
5779 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5780
5781 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5782 input_data->len ) );
5783
5784 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5785 additional_data->len ) );
5786
5787 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5788 finish_output_size,
5789 &output_part_length,
5790 tag_buffer, tag_length,
5791 &tag_size ),
5792 PSA_ERROR_INVALID_ARGUMENT );
5793
5794 psa_aead_abort( &operation );
5795
Paul Elliottb0450fe2021-09-01 15:06:26 +01005796 /* Test for sending too much additional data after setting lengths. */
5797
Paul Elliottb0450fe2021-09-01 15:06:26 +01005798 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5799
5800 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5801
5802 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5803
5804
5805 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5806 additional_data->len ),
5807 PSA_ERROR_INVALID_ARGUMENT );
5808
5809 psa_aead_abort( &operation );
5810
Paul Elliotta2a09b02021-09-22 14:56:40 +01005811 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005812
5813 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5814
5815 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5816
5817 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5818 input_data->len ) );
5819
5820 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5821 additional_data->len ) );
5822
5823 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5824 1 ),
5825 PSA_ERROR_INVALID_ARGUMENT );
5826
5827 psa_aead_abort( &operation );
5828
Paul Elliottb0450fe2021-09-01 15:06:26 +01005829 /* Test for sending too much data after setting lengths. */
5830
Paul Elliottb0450fe2021-09-01 15:06:26 +01005831 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5832
5833 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5834
5835 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
5836
5837 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5838 input_data->len, output_data,
5839 output_size, &output_length ),
5840 PSA_ERROR_INVALID_ARGUMENT );
5841
5842 psa_aead_abort( &operation );
5843
Paul Elliotta2a09b02021-09-22 14:56:40 +01005844 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01005845
5846 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5847
5848 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5849
5850 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5851 input_data->len ) );
5852
5853 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5854 additional_data->len ) );
5855
5856 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5857 input_data->len, output_data,
5858 output_size, &output_length ) );
5859
5860 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5861 1, output_data,
5862 output_size, &output_length ),
5863 PSA_ERROR_INVALID_ARGUMENT );
5864
5865 psa_aead_abort( &operation );
5866
Paul Elliottc23a9a02021-06-21 18:32:46 +01005867 /* Test sending additional data after data. */
5868
Paul Elliottc23a9a02021-06-21 18:32:46 +01005869 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5870
5871 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5872
Andrzej Kurekad837522021-12-15 15:28:49 +01005873 if( operation.alg != PSA_ALG_CCM )
5874 {
5875 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5876 input_data->len, output_data,
5877 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005878
Andrzej Kurekad837522021-12-15 15:28:49 +01005879 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5880 additional_data->len ),
5881 PSA_ERROR_BAD_STATE );
5882 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01005883 psa_aead_abort( &operation );
5884
Paul Elliott534d0b42021-06-22 19:15:20 +01005885 /* Test calling finish on decryption. */
5886
Paul Elliott534d0b42021-06-22 19:15:20 +01005887 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5888
5889 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5890
5891 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5892 finish_output_size,
5893 &output_part_length,
5894 tag_buffer, tag_length,
5895 &tag_size ),
5896 PSA_ERROR_BAD_STATE );
5897
5898 psa_aead_abort( &operation );
5899
5900 /* Test calling verify on encryption. */
5901
Paul Elliott534d0b42021-06-22 19:15:20 +01005902 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5903
5904 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5905
5906 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5907 finish_output_size,
5908 &output_part_length,
5909 tag_buffer,
5910 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01005911 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01005912
5913 psa_aead_abort( &operation );
5914
5915
Paul Elliottc23a9a02021-06-21 18:32:46 +01005916exit:
5917 psa_destroy_key( key );
5918 psa_aead_abort( &operation );
5919 mbedtls_free( output_data );
5920 mbedtls_free( final_data );
5921 PSA_DONE( );
5922}
5923/* END_CASE */
5924
5925/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005926void signature_size( int type_arg,
5927 int bits,
5928 int alg_arg,
5929 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005930{
5931 psa_key_type_t type = type_arg;
5932 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005933 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005934
Gilles Peskinefe11b722018-12-18 00:24:04 +01005935 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01005936
Gilles Peskinee59236f2018-01-27 23:32:46 +01005937exit:
5938 ;
5939}
5940/* END_CASE */
5941
5942/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005943void sign_hash_deterministic( int key_type_arg, data_t *key_data,
5944 int alg_arg, data_t *input_data,
5945 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005946{
Ronald Cron5425a212020-08-04 14:58:35 +02005947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005948 psa_key_type_t key_type = key_type_arg;
5949 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01005950 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01005951 unsigned char *signature = NULL;
5952 size_t signature_size;
5953 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005954 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01005955
Gilles Peskine8817f612018-12-18 00:18:46 +01005956 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01005957
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005958 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02005959 psa_set_key_algorithm( &attributes, alg );
5960 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07005961
Gilles Peskine049c7532019-05-15 20:22:09 +02005962 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005963 &key ) );
5964 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005965 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01005966
Shaun Case8b0ecbc2021-12-20 21:14:10 -08005967 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005968 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005969 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02005970 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01005971 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02005972 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005973 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01005974
Gilles Peskine860ce9d2018-06-28 12:23:00 +02005975 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02005976 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01005977 input_data->x, input_data->len,
5978 signature, signature_size,
5979 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02005980 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02005981 ASSERT_COMPARE( output_data->x, output_data->len,
5982 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01005983
5984exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005985 /*
5986 * Key attributes may have been returned by psa_get_key_attributes()
5987 * thus reset them as required.
5988 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005989 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005990
Ronald Cron5425a212020-08-04 14:58:35 +02005991 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01005992 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005993 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01005994}
5995/* END_CASE */
5996
5997/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02005998void sign_hash_fail( int key_type_arg, data_t *key_data,
5999 int alg_arg, data_t *input_data,
6000 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006001{
Ronald Cron5425a212020-08-04 14:58:35 +02006002 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006003 psa_key_type_t key_type = key_type_arg;
6004 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006005 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006006 psa_status_t actual_status;
6007 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006008 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006009 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006011
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006012 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006013
Gilles Peskine8817f612018-12-18 00:18:46 +01006014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006015
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006016 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006017 psa_set_key_algorithm( &attributes, alg );
6018 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006019
Gilles Peskine049c7532019-05-15 20:22:09 +02006020 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006021 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006022
Ronald Cron5425a212020-08-04 14:58:35 +02006023 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006024 input_data->x, input_data->len,
6025 signature, signature_size,
6026 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006027 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006028 /* The value of *signature_length is unspecified on error, but
6029 * whatever it is, it should be less than signature_size, so that
6030 * if the caller tries to read *signature_length bytes without
6031 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006032 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006033
6034exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006035 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006036 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006037 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006038 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006039}
6040/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006041
6042/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006043void sign_verify_hash( int key_type_arg, data_t *key_data,
6044 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006045{
Ronald Cron5425a212020-08-04 14:58:35 +02006046 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006047 psa_key_type_t key_type = key_type_arg;
6048 psa_algorithm_t alg = alg_arg;
6049 size_t key_bits;
6050 unsigned char *signature = NULL;
6051 size_t signature_size;
6052 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006054
Gilles Peskine8817f612018-12-18 00:18:46 +01006055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006056
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006058 psa_set_key_algorithm( &attributes, alg );
6059 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006060
Gilles Peskine049c7532019-05-15 20:22:09 +02006061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006062 &key ) );
6063 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006064 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006065
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006066 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006067 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006068 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006069 key_bits, alg );
6070 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006071 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006072 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006073
6074 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006075 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006076 input_data->x, input_data->len,
6077 signature, signature_size,
6078 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006079 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006080 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006081 TEST_ASSERT( signature_length > 0 );
6082
6083 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006084 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006085 input_data->x, input_data->len,
6086 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006087
6088 if( input_data->len != 0 )
6089 {
6090 /* Flip a bit in the input and verify that the signature is now
6091 * detected as invalid. Flip a bit at the beginning, not at the end,
6092 * because ECDSA may ignore the last few bits of the input. */
6093 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006094 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006095 input_data->x, input_data->len,
6096 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006097 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006098 }
6099
6100exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006101 /*
6102 * Key attributes may have been returned by psa_get_key_attributes()
6103 * thus reset them as required.
6104 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006105 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006106
Ronald Cron5425a212020-08-04 14:58:35 +02006107 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006108 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006109 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006110}
6111/* END_CASE */
6112
6113/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006114void verify_hash( int key_type_arg, data_t *key_data,
6115 int alg_arg, data_t *hash_data,
6116 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006117{
Ronald Cron5425a212020-08-04 14:58:35 +02006118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006119 psa_key_type_t key_type = key_type_arg;
6120 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006122
Gilles Peskine7be11a72022-04-14 00:12:57 +02006123 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006124
Gilles Peskine8817f612018-12-18 00:18:46 +01006125 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006126
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006128 psa_set_key_algorithm( &attributes, alg );
6129 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006130
Gilles Peskine049c7532019-05-15 20:22:09 +02006131 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006132 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006133
Ronald Cron5425a212020-08-04 14:58:35 +02006134 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006135 hash_data->x, hash_data->len,
6136 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006137
itayzafrir5c753392018-05-08 11:18:38 +03006138exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006139 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006140 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006141 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006142}
6143/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006144
6145/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006146void verify_hash_fail( int key_type_arg, data_t *key_data,
6147 int alg_arg, data_t *hash_data,
6148 data_t *signature_data,
6149 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006150{
Ronald Cron5425a212020-08-04 14:58:35 +02006151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006152 psa_key_type_t key_type = key_type_arg;
6153 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006154 psa_status_t actual_status;
6155 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006157
Gilles Peskine8817f612018-12-18 00:18:46 +01006158 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006159
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006161 psa_set_key_algorithm( &attributes, alg );
6162 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006163
Gilles Peskine049c7532019-05-15 20:22:09 +02006164 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006165 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006166
Ronald Cron5425a212020-08-04 14:58:35 +02006167 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006168 hash_data->x, hash_data->len,
6169 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006170 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006171
6172exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006173 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006174 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006175 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006176}
6177/* END_CASE */
6178
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006179/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006180void sign_message_deterministic( int key_type_arg,
6181 data_t *key_data,
6182 int alg_arg,
6183 data_t *input_data,
6184 data_t *output_data )
6185{
6186 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6187 psa_key_type_t key_type = key_type_arg;
6188 psa_algorithm_t alg = alg_arg;
6189 size_t key_bits;
6190 unsigned char *signature = NULL;
6191 size_t signature_size;
6192 size_t signature_length = 0xdeadbeef;
6193 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6194
6195 PSA_ASSERT( psa_crypto_init( ) );
6196
6197 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6198 psa_set_key_algorithm( &attributes, alg );
6199 psa_set_key_type( &attributes, key_type );
6200
6201 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6202 &key ) );
6203 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6204 key_bits = psa_get_key_bits( &attributes );
6205
6206 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6207 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006208 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006209 ASSERT_ALLOC( signature, signature_size );
6210
6211 PSA_ASSERT( psa_sign_message( key, alg,
6212 input_data->x, input_data->len,
6213 signature, signature_size,
6214 &signature_length ) );
6215
6216 ASSERT_COMPARE( output_data->x, output_data->len,
6217 signature, signature_length );
6218
6219exit:
6220 psa_reset_key_attributes( &attributes );
6221
6222 psa_destroy_key( key );
6223 mbedtls_free( signature );
6224 PSA_DONE( );
6225
6226}
6227/* END_CASE */
6228
6229/* BEGIN_CASE */
6230void sign_message_fail( int key_type_arg,
6231 data_t *key_data,
6232 int alg_arg,
6233 data_t *input_data,
6234 int signature_size_arg,
6235 int expected_status_arg )
6236{
6237 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6238 psa_key_type_t key_type = key_type_arg;
6239 psa_algorithm_t alg = alg_arg;
6240 size_t signature_size = signature_size_arg;
6241 psa_status_t actual_status;
6242 psa_status_t expected_status = expected_status_arg;
6243 unsigned char *signature = NULL;
6244 size_t signature_length = 0xdeadbeef;
6245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6246
6247 ASSERT_ALLOC( signature, signature_size );
6248
6249 PSA_ASSERT( psa_crypto_init( ) );
6250
6251 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6252 psa_set_key_algorithm( &attributes, alg );
6253 psa_set_key_type( &attributes, key_type );
6254
6255 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6256 &key ) );
6257
6258 actual_status = psa_sign_message( key, alg,
6259 input_data->x, input_data->len,
6260 signature, signature_size,
6261 &signature_length );
6262 TEST_EQUAL( actual_status, expected_status );
6263 /* The value of *signature_length is unspecified on error, but
6264 * whatever it is, it should be less than signature_size, so that
6265 * if the caller tries to read *signature_length bytes without
6266 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006267 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006268
6269exit:
6270 psa_reset_key_attributes( &attributes );
6271 psa_destroy_key( key );
6272 mbedtls_free( signature );
6273 PSA_DONE( );
6274}
6275/* END_CASE */
6276
6277/* BEGIN_CASE */
6278void sign_verify_message( int key_type_arg,
6279 data_t *key_data,
6280 int alg_arg,
6281 data_t *input_data )
6282{
6283 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6284 psa_key_type_t key_type = key_type_arg;
6285 psa_algorithm_t alg = alg_arg;
6286 size_t key_bits;
6287 unsigned char *signature = NULL;
6288 size_t signature_size;
6289 size_t signature_length = 0xdeadbeef;
6290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6291
6292 PSA_ASSERT( psa_crypto_init( ) );
6293
6294 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6295 PSA_KEY_USAGE_VERIFY_MESSAGE );
6296 psa_set_key_algorithm( &attributes, alg );
6297 psa_set_key_type( &attributes, key_type );
6298
6299 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6300 &key ) );
6301 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6302 key_bits = psa_get_key_bits( &attributes );
6303
6304 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6305 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006306 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006307 ASSERT_ALLOC( signature, signature_size );
6308
6309 PSA_ASSERT( psa_sign_message( key, alg,
6310 input_data->x, input_data->len,
6311 signature, signature_size,
6312 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006313 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006314 TEST_ASSERT( signature_length > 0 );
6315
6316 PSA_ASSERT( psa_verify_message( key, alg,
6317 input_data->x, input_data->len,
6318 signature, signature_length ) );
6319
6320 if( input_data->len != 0 )
6321 {
6322 /* Flip a bit in the input and verify that the signature is now
6323 * detected as invalid. Flip a bit at the beginning, not at the end,
6324 * because ECDSA may ignore the last few bits of the input. */
6325 input_data->x[0] ^= 1;
6326 TEST_EQUAL( psa_verify_message( key, alg,
6327 input_data->x, input_data->len,
6328 signature, signature_length ),
6329 PSA_ERROR_INVALID_SIGNATURE );
6330 }
6331
6332exit:
6333 psa_reset_key_attributes( &attributes );
6334
6335 psa_destroy_key( key );
6336 mbedtls_free( signature );
6337 PSA_DONE( );
6338}
6339/* END_CASE */
6340
6341/* BEGIN_CASE */
6342void verify_message( int key_type_arg,
6343 data_t *key_data,
6344 int alg_arg,
6345 data_t *input_data,
6346 data_t *signature_data )
6347{
6348 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6349 psa_key_type_t key_type = key_type_arg;
6350 psa_algorithm_t alg = alg_arg;
6351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6352
Gilles Peskine7be11a72022-04-14 00:12:57 +02006353 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006354
6355 PSA_ASSERT( psa_crypto_init( ) );
6356
6357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6358 psa_set_key_algorithm( &attributes, alg );
6359 psa_set_key_type( &attributes, key_type );
6360
6361 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6362 &key ) );
6363
6364 PSA_ASSERT( psa_verify_message( key, alg,
6365 input_data->x, input_data->len,
6366 signature_data->x, signature_data->len ) );
6367
6368exit:
6369 psa_reset_key_attributes( &attributes );
6370 psa_destroy_key( key );
6371 PSA_DONE( );
6372}
6373/* END_CASE */
6374
6375/* BEGIN_CASE */
6376void verify_message_fail( int key_type_arg,
6377 data_t *key_data,
6378 int alg_arg,
6379 data_t *hash_data,
6380 data_t *signature_data,
6381 int expected_status_arg )
6382{
6383 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6384 psa_key_type_t key_type = key_type_arg;
6385 psa_algorithm_t alg = alg_arg;
6386 psa_status_t actual_status;
6387 psa_status_t expected_status = expected_status_arg;
6388 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6389
6390 PSA_ASSERT( psa_crypto_init( ) );
6391
6392 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6393 psa_set_key_algorithm( &attributes, alg );
6394 psa_set_key_type( &attributes, key_type );
6395
6396 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6397 &key ) );
6398
6399 actual_status = psa_verify_message( key, alg,
6400 hash_data->x, hash_data->len,
6401 signature_data->x,
6402 signature_data->len );
6403 TEST_EQUAL( actual_status, expected_status );
6404
6405exit:
6406 psa_reset_key_attributes( &attributes );
6407 psa_destroy_key( key );
6408 PSA_DONE( );
6409}
6410/* END_CASE */
6411
6412/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006413void asymmetric_encrypt( int key_type_arg,
6414 data_t *key_data,
6415 int alg_arg,
6416 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006417 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006418 int expected_output_length_arg,
6419 int expected_status_arg )
6420{
Ronald Cron5425a212020-08-04 14:58:35 +02006421 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006422 psa_key_type_t key_type = key_type_arg;
6423 psa_algorithm_t alg = alg_arg;
6424 size_t expected_output_length = expected_output_length_arg;
6425 size_t key_bits;
6426 unsigned char *output = NULL;
6427 size_t output_size;
6428 size_t output_length = ~0;
6429 psa_status_t actual_status;
6430 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006432
Gilles Peskine8817f612018-12-18 00:18:46 +01006433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006434
Gilles Peskine656896e2018-06-29 19:12:28 +02006435 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006436 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
6437 psa_set_key_algorithm( &attributes, alg );
6438 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02006439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006440 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02006441
6442 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02006443 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006444 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006445
Gilles Peskine656896e2018-06-29 19:12:28 +02006446 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006447 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006448 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02006449
6450 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02006451 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02006452 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006453 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02006454 output, output_size,
6455 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006456 TEST_EQUAL( actual_status, expected_status );
6457 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02006458
Gilles Peskine68428122018-06-30 18:42:41 +02006459 /* If the label is empty, the test framework puts a non-null pointer
6460 * in label->x. Test that a null pointer works as well. */
6461 if( label->len == 0 )
6462 {
6463 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006464 if( output_size != 0 )
6465 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006466 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006467 input_data->x, input_data->len,
6468 NULL, label->len,
6469 output, output_size,
6470 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006471 TEST_EQUAL( actual_status, expected_status );
6472 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006473 }
6474
Gilles Peskine656896e2018-06-29 19:12:28 +02006475exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006476 /*
6477 * Key attributes may have been returned by psa_get_key_attributes()
6478 * thus reset them as required.
6479 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006480 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006481
Ronald Cron5425a212020-08-04 14:58:35 +02006482 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02006483 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006484 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02006485}
6486/* END_CASE */
6487
6488/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006489void asymmetric_encrypt_decrypt( int key_type_arg,
6490 data_t *key_data,
6491 int alg_arg,
6492 data_t *input_data,
6493 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006494{
Ronald Cron5425a212020-08-04 14:58:35 +02006495 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006496 psa_key_type_t key_type = key_type_arg;
6497 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006498 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006499 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006500 size_t output_size;
6501 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006502 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006503 size_t output2_size;
6504 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006506
Gilles Peskine8817f612018-12-18 00:18:46 +01006507 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006508
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
6510 psa_set_key_algorithm( &attributes, alg );
6511 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006512
Gilles Peskine049c7532019-05-15 20:22:09 +02006513 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006514 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006515
6516 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02006517 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006518 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01006519
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006520 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006521 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006522 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01006523
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006524 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02006525 TEST_LE_U( output2_size,
6526 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
6527 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006528 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006529
Gilles Peskineeebd7382018-06-08 18:11:54 +02006530 /* We test encryption by checking that encrypt-then-decrypt gives back
6531 * the original plaintext because of the non-optional random
6532 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02006533 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006534 input_data->x, input_data->len,
6535 label->x, label->len,
6536 output, output_size,
6537 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006538 /* We don't know what ciphertext length to expect, but check that
6539 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006540 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03006541
Ronald Cron5425a212020-08-04 14:58:35 +02006542 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006543 output, output_length,
6544 label->x, label->len,
6545 output2, output2_size,
6546 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006547 ASSERT_COMPARE( input_data->x, input_data->len,
6548 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006549
6550exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006551 /*
6552 * Key attributes may have been returned by psa_get_key_attributes()
6553 * thus reset them as required.
6554 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006555 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006556
Ronald Cron5425a212020-08-04 14:58:35 +02006557 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006558 mbedtls_free( output );
6559 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006560 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006561}
6562/* END_CASE */
6563
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006564/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006565void asymmetric_decrypt( int key_type_arg,
6566 data_t *key_data,
6567 int alg_arg,
6568 data_t *input_data,
6569 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02006570 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006571{
Ronald Cron5425a212020-08-04 14:58:35 +02006572 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006573 psa_key_type_t key_type = key_type_arg;
6574 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01006575 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006576 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03006577 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006578 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006580
Gilles Peskine8817f612018-12-18 00:18:46 +01006581 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006582
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6584 psa_set_key_algorithm( &attributes, alg );
6585 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006586
Gilles Peskine049c7532019-05-15 20:22:09 +02006587 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006588 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006589
gabor-mezei-armceface22021-01-21 12:26:17 +01006590 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6591 key_bits = psa_get_key_bits( &attributes );
6592
6593 /* Determine the maximum ciphertext length */
6594 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006595 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01006596 ASSERT_ALLOC( output, output_size );
6597
Ronald Cron5425a212020-08-04 14:58:35 +02006598 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006599 input_data->x, input_data->len,
6600 label->x, label->len,
6601 output,
6602 output_size,
6603 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006604 ASSERT_COMPARE( expected_data->x, expected_data->len,
6605 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006606
Gilles Peskine68428122018-06-30 18:42:41 +02006607 /* If the label is empty, the test framework puts a non-null pointer
6608 * in label->x. Test that a null pointer works as well. */
6609 if( label->len == 0 )
6610 {
6611 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006612 if( output_size != 0 )
6613 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006614 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01006615 input_data->x, input_data->len,
6616 NULL, label->len,
6617 output,
6618 output_size,
6619 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006620 ASSERT_COMPARE( expected_data->x, expected_data->len,
6621 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02006622 }
6623
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006624exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006625 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006626 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03006627 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006628 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006629}
6630/* END_CASE */
6631
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006632/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02006633void asymmetric_decrypt_fail( int key_type_arg,
6634 data_t *key_data,
6635 int alg_arg,
6636 data_t *input_data,
6637 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00006638 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02006639 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006640{
Ronald Cron5425a212020-08-04 14:58:35 +02006641 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006642 psa_key_type_t key_type = key_type_arg;
6643 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006644 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00006645 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02006646 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006647 psa_status_t actual_status;
6648 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006650
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006651 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006652
Gilles Peskine8817f612018-12-18 00:18:46 +01006653 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
6656 psa_set_key_algorithm( &attributes, alg );
6657 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006658
Gilles Peskine049c7532019-05-15 20:22:09 +02006659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006660 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006661
Ronald Cron5425a212020-08-04 14:58:35 +02006662 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006663 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02006664 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02006665 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02006666 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006667 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006668 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006669
Gilles Peskine68428122018-06-30 18:42:41 +02006670 /* If the label is empty, the test framework puts a non-null pointer
6671 * in label->x. Test that a null pointer works as well. */
6672 if( label->len == 0 )
6673 {
6674 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02006675 if( output_size != 0 )
6676 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02006677 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02006678 input_data->x, input_data->len,
6679 NULL, label->len,
6680 output, output_size,
6681 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006682 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006683 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02006684 }
6685
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006686exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006687 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006688 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02006689 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006690 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006691}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02006692/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02006693
6694/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02006695void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00006696{
6697 /* Test each valid way of initializing the object, except for `= {0}`, as
6698 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
6699 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006700 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006701 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006702 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
6703 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
6704 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00006705
6706 memset( &zero, 0, sizeof( zero ) );
6707
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006708 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006709 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006710 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006711 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006712 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006713 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006714 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00006715
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006716 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006717 PSA_ASSERT( psa_key_derivation_abort(&func) );
6718 PSA_ASSERT( psa_key_derivation_abort(&init) );
6719 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00006720}
6721/* END_CASE */
6722
Janos Follath16de4a42019-06-13 16:32:24 +01006723/* BEGIN_CASE */
6724void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02006725{
Gilles Peskineea0fb492018-07-12 17:17:20 +02006726 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006727 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006728 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02006729
Gilles Peskine8817f612018-12-18 00:18:46 +01006730 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006731
Janos Follath16de4a42019-06-13 16:32:24 +01006732 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006733 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006734
6735exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006736 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006737 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02006738}
6739/* END_CASE */
6740
Janos Follathaf3c2a02019-06-12 12:34:34 +01006741/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01006742void derive_set_capacity( int alg_arg, int capacity_arg,
6743 int expected_status_arg )
6744{
6745 psa_algorithm_t alg = alg_arg;
6746 size_t capacity = capacity_arg;
6747 psa_status_t expected_status = expected_status_arg;
6748 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6749
6750 PSA_ASSERT( psa_crypto_init( ) );
6751
6752 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6753
6754 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
6755 expected_status );
6756
6757exit:
6758 psa_key_derivation_abort( &operation );
6759 PSA_DONE( );
6760}
6761/* END_CASE */
6762
6763/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01006764void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02006765 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006766 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02006767 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01006768 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02006769 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006770 int expected_status_arg3,
6771 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006772{
6773 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02006774 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
6775 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01006776 psa_status_t expected_statuses[] = {expected_status_arg1,
6777 expected_status_arg2,
6778 expected_status_arg3};
6779 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02006780 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
6781 MBEDTLS_SVC_KEY_ID_INIT,
6782 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01006783 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
6784 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6785 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006786 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006787 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006788 psa_status_t expected_output_status = expected_output_status_arg;
6789 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01006790
6791 PSA_ASSERT( psa_crypto_init( ) );
6792
6793 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6794 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006795
6796 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6797
6798 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
6799 {
Gilles Peskine4023c012021-05-27 13:21:20 +02006800 mbedtls_test_set_step( i );
6801 if( steps[i] == 0 )
6802 {
6803 /* Skip this step */
6804 }
6805 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01006806 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02006807 psa_set_key_type( &attributes, key_types[i] );
6808 PSA_ASSERT( psa_import_key( &attributes,
6809 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006810 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006811 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
6812 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
6813 {
6814 // When taking a private key as secret input, use key agreement
6815 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006816 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
6817 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006818 expected_statuses[i] );
6819 }
6820 else
6821 {
6822 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02006823 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02006824 expected_statuses[i] );
6825 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02006826 }
6827 else
6828 {
6829 TEST_EQUAL( psa_key_derivation_input_bytes(
6830 &operation, steps[i],
6831 inputs[i]->x, inputs[i]->len ),
6832 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006833 }
6834 }
6835
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006836 if( output_key_type != PSA_KEY_TYPE_NONE )
6837 {
6838 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00006839 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006840 psa_set_key_bits( &attributes, 8 );
6841 actual_output_status =
6842 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02006843 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02006844 }
6845 else
6846 {
6847 uint8_t buffer[1];
6848 actual_output_status =
6849 psa_key_derivation_output_bytes( &operation,
6850 buffer, sizeof( buffer ) );
6851 }
6852 TEST_EQUAL( actual_output_status, expected_output_status );
6853
Janos Follathaf3c2a02019-06-12 12:34:34 +01006854exit:
6855 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006856 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
6857 psa_destroy_key( keys[i] );
6858 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01006859 PSA_DONE( );
6860}
6861/* END_CASE */
6862
Janos Follathd958bb72019-07-03 15:02:16 +01006863/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006864void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006865{
Janos Follathd958bb72019-07-03 15:02:16 +01006866 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02006867 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02006868 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006869 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01006870 unsigned char input1[] = "Input 1";
6871 size_t input1_length = sizeof( input1 );
6872 unsigned char input2[] = "Input 2";
6873 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006874 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02006875 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02006876 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6877 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
6878 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006880
Gilles Peskine8817f612018-12-18 00:18:46 +01006881 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006882
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006883 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
6884 psa_set_key_algorithm( &attributes, alg );
6885 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006886
Gilles Peskine73676cb2019-05-15 20:15:10 +02006887 PSA_ASSERT( psa_import_key( &attributes,
6888 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02006889 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006890
6891 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01006892 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
6893 input1, input1_length,
6894 input2, input2_length,
6895 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01006896 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006897
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006898 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01006899 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006900 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006901
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006902 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006903
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006904 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02006905 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006906
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006907exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006908 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02006909 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006910 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006911}
6912/* END_CASE */
6913
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006914/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02006915void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006916{
6917 uint8_t output_buffer[16];
6918 size_t buffer_size = 16;
6919 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006920 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006921
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006922 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6923 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006924 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006925
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006926 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006927 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006928
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006929 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006930
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006931 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
6932 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006933 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006934
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006935 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00006936 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03006937
6938exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006939 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03006940}
6941/* END_CASE */
6942
6943/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006944void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006945 int step1_arg, data_t *input1, int expected_status_arg1,
6946 int step2_arg, data_t *input2, int expected_status_arg2,
6947 int step3_arg, data_t *input3, int expected_status_arg3,
6948 int step4_arg, data_t *input4, int expected_status_arg4,
6949 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006950 int requested_capacity_arg,
6951 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006952 data_t *expected_output2,
6953 int other_key_input_type,
6954 int key_input_type,
6955 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006956{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006957 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02006958 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
6959 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006960 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
6961 MBEDTLS_SVC_KEY_ID_INIT,
6962 MBEDTLS_SVC_KEY_ID_INIT,
6963 MBEDTLS_SVC_KEY_ID_INIT};
6964 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
6965 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006966 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006967 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006968 uint8_t *expected_outputs[2] =
6969 {expected_output1->x, expected_output2->x};
6970 size_t output_sizes[2] =
6971 {expected_output1->len, expected_output2->len};
6972 size_t output_buffer_size = 0;
6973 uint8_t *output_buffer = NULL;
6974 size_t expected_capacity;
6975 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02006976 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
6977 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
6978 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
6979 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02006980 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006981 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02006982 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006983
6984 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
6985 {
6986 if( output_sizes[i] > output_buffer_size )
6987 output_buffer_size = output_sizes[i];
6988 if( output_sizes[i] == 0 )
6989 expected_outputs[i] = NULL;
6990 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006991 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01006992 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006993
Gilles Peskine96ee5c72018-07-12 17:24:54 +02006994 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02006995 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
6996 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
6997 requested_capacity ) );
6998 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006999 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007000 switch( steps[i] )
7001 {
7002 case 0:
7003 break;
7004 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007005 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007006 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007007 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007008 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007009 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007010 inputs[i]->x, inputs[i]->len ),
7011 statuses[i] );
7012
7013 if( statuses[i] != PSA_SUCCESS )
7014 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007015 break;
7016 case 1: // input key
7017 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7018 psa_set_key_algorithm( &attributes1, alg );
7019 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7020
7021 PSA_ASSERT( psa_import_key( &attributes1,
7022 inputs[i]->x, inputs[i]->len,
7023 &keys[i] ) );
7024
Przemek Stekiel38647de2022-04-19 13:27:47 +02007025 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007026 {
7027 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007028 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7029 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007030 }
7031
Przemek Stekiel38647de2022-04-19 13:27:47 +02007032 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7033 steps[i],
7034 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007035 break;
7036 default:
7037 TEST_ASSERT( ! "default case not supported" );
7038 break;
7039 }
7040 break;
7041 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007042 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007043 {
7044 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007045 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7046 steps[i],
7047 inputs[i]->x,
7048 inputs[i]->len ),
7049 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007050 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007051 case 1: // input key, type DERIVE
7052 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007053 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7054 psa_set_key_algorithm( &attributes2, alg );
7055 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7056
7057 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007058 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007059 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7060
7061 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007062 inputs[i]->x, inputs[i]->len,
7063 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007064
Przemek Stekiel38647de2022-04-19 13:27:47 +02007065 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7066 steps[i],
7067 keys[i] ),
7068 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007069 break;
7070 case 2: // key agreement
7071 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7072 psa_set_key_algorithm( &attributes3, alg );
7073 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7074
7075 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007076 inputs[i]->x, inputs[i]->len,
7077 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007078
7079 TEST_EQUAL( psa_key_derivation_key_agreement(
7080 &operation,
7081 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7082 keys[i], key_agreement_peer_key->x,
7083 key_agreement_peer_key->len ), statuses[i] );
7084 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007085 default:
7086 TEST_ASSERT( ! "default case not supported" );
7087 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007088 }
7089
Przemek Stekiel38647de2022-04-19 13:27:47 +02007090 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007091 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007092 break;
7093 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007094 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007095 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007096 inputs[i]->x, inputs[i]->len ), statuses[i] );
7097
7098 if( statuses[i] != PSA_SUCCESS )
7099 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007100 break;
7101 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007102 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007103
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007104 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007105 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007106 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007107 expected_capacity = requested_capacity;
7108
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007109 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007110 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007111 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7112
7113 /* For output key derivation secret must be provided using
7114 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007115 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007116 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007117
7118 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7119 psa_set_key_algorithm( &attributes4, alg );
7120 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007121 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007122
7123 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007124 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007125 }
7126 else // output bytes
7127 {
7128 /* Expansion phase. */
7129 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007130 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007131 /* Read some bytes. */
7132 status = psa_key_derivation_output_bytes( &operation,
7133 output_buffer, output_sizes[i] );
7134 if( expected_capacity == 0 && output_sizes[i] == 0 )
7135 {
7136 /* Reading 0 bytes when 0 bytes are available can go either way. */
7137 TEST_ASSERT( status == PSA_SUCCESS ||
7138 status == PSA_ERROR_INSUFFICIENT_DATA );
7139 continue;
7140 }
7141 else if( expected_capacity == 0 ||
7142 output_sizes[i] > expected_capacity )
7143 {
7144 /* Capacity exceeded. */
7145 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7146 expected_capacity = 0;
7147 continue;
7148 }
7149 /* Success. Check the read data. */
7150 PSA_ASSERT( status );
7151 if( output_sizes[i] != 0 )
7152 ASSERT_COMPARE( output_buffer, output_sizes[i],
7153 expected_outputs[i], output_sizes[i] );
7154 /* Check the operation status. */
7155 expected_capacity -= output_sizes[i];
7156 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7157 &current_capacity ) );
7158 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007159 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007160 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007161 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007162
7163exit:
7164 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007165 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007166 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7167 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007168 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007169 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007170}
7171/* END_CASE */
7172
7173/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007174void derive_full( int alg_arg,
7175 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007176 data_t *input1,
7177 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007178 int requested_capacity_arg )
7179{
Ronald Cron5425a212020-08-04 14:58:35 +02007180 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007181 psa_algorithm_t alg = alg_arg;
7182 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007183 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007184 unsigned char output_buffer[16];
7185 size_t expected_capacity = requested_capacity;
7186 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007188
Gilles Peskine8817f612018-12-18 00:18:46 +01007189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007190
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007191 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7192 psa_set_key_algorithm( &attributes, alg );
7193 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007194
Gilles Peskine049c7532019-05-15 20:22:09 +02007195 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007196 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007197
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007198 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7199 input1->x, input1->len,
7200 input2->x, input2->len,
7201 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007202 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007203
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007204 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007205 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007206 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007207
7208 /* Expansion phase. */
7209 while( current_capacity > 0 )
7210 {
7211 size_t read_size = sizeof( output_buffer );
7212 if( read_size > current_capacity )
7213 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007214 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007215 output_buffer,
7216 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007217 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007218 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007219 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007220 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007221 }
7222
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007223 /* Check that the operation refuses to go over capacity. */
7224 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007225 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007226
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007227 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007228
7229exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007230 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007231 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007232 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007233}
7234/* END_CASE */
7235
Janos Follathe60c9052019-07-03 13:51:30 +01007236/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007237void derive_key_exercise( int alg_arg,
7238 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007239 data_t *input1,
7240 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007241 int derived_type_arg,
7242 int derived_bits_arg,
7243 int derived_usage_arg,
7244 int derived_alg_arg )
7245{
Ronald Cron5425a212020-08-04 14:58:35 +02007246 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7247 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007248 psa_algorithm_t alg = alg_arg;
7249 psa_key_type_t derived_type = derived_type_arg;
7250 size_t derived_bits = derived_bits_arg;
7251 psa_key_usage_t derived_usage = derived_usage_arg;
7252 psa_algorithm_t derived_alg = derived_alg_arg;
7253 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007254 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007255 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007256 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007257
Gilles Peskine8817f612018-12-18 00:18:46 +01007258 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007259
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7261 psa_set_key_algorithm( &attributes, alg );
7262 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007263 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007264 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007265
7266 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007267 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7268 input1->x, input1->len,
7269 input2->x, input2->len,
7270 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007271 goto exit;
7272
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007273 psa_set_key_usage_flags( &attributes, derived_usage );
7274 psa_set_key_algorithm( &attributes, derived_alg );
7275 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007276 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007277 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007278 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007279
7280 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007281 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007282 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7283 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007284
7285 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007286 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007287 goto exit;
7288
7289exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007290 /*
7291 * Key attributes may have been returned by psa_get_key_attributes()
7292 * thus reset them as required.
7293 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007294 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007295
7296 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007297 psa_destroy_key( base_key );
7298 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007299 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007300}
7301/* END_CASE */
7302
Janos Follath42fd8882019-07-03 14:17:09 +01007303/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007304void derive_key_export( int alg_arg,
7305 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007306 data_t *input1,
7307 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007308 int bytes1_arg,
7309 int bytes2_arg )
7310{
Ronald Cron5425a212020-08-04 14:58:35 +02007311 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7312 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007313 psa_algorithm_t alg = alg_arg;
7314 size_t bytes1 = bytes1_arg;
7315 size_t bytes2 = bytes2_arg;
7316 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007317 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007318 uint8_t *output_buffer = NULL;
7319 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007320 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7321 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007322 size_t length;
7323
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007324 ASSERT_ALLOC( output_buffer, capacity );
7325 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007326 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007327
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007328 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7329 psa_set_key_algorithm( &base_attributes, alg );
7330 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007331 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007332 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007333
7334 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007335 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7336 input1->x, input1->len,
7337 input2->x, input2->len,
7338 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007339 goto exit;
7340
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007341 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007342 output_buffer,
7343 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007344 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007345
7346 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007347 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7348 input1->x, input1->len,
7349 input2->x, input2->len,
7350 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007351 goto exit;
7352
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007353 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7354 psa_set_key_algorithm( &derived_attributes, 0 );
7355 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007356 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007357 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007358 &derived_key ) );
7359 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007360 export_buffer, bytes1,
7361 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007362 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007363 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007364 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007365 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007366 &derived_key ) );
7367 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007368 export_buffer + bytes1, bytes2,
7369 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007370 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007371
7372 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007373 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7374 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007375
7376exit:
7377 mbedtls_free( output_buffer );
7378 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007379 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007380 psa_destroy_key( base_key );
7381 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007382 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007383}
7384/* END_CASE */
7385
7386/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007387void derive_key_type( int alg_arg,
7388 data_t *key_data,
7389 data_t *input1,
7390 data_t *input2,
7391 int key_type_arg, int bits_arg,
7392 data_t *expected_export )
7393{
7394 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7395 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
7396 const psa_algorithm_t alg = alg_arg;
7397 const psa_key_type_t key_type = key_type_arg;
7398 const size_t bits = bits_arg;
7399 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7400 const size_t export_buffer_size =
7401 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
7402 uint8_t *export_buffer = NULL;
7403 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7404 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7405 size_t export_length;
7406
7407 ASSERT_ALLOC( export_buffer, export_buffer_size );
7408 PSA_ASSERT( psa_crypto_init( ) );
7409
7410 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7411 psa_set_key_algorithm( &base_attributes, alg );
7412 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7413 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
7414 &base_key ) );
7415
Przemek Stekielc85f0912022-03-08 11:37:54 +01007416 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007417 &operation, base_key, alg,
7418 input1->x, input1->len,
7419 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01007420 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007421 goto exit;
7422
7423 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7424 psa_set_key_algorithm( &derived_attributes, 0 );
7425 psa_set_key_type( &derived_attributes, key_type );
7426 psa_set_key_bits( &derived_attributes, bits );
7427 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
7428 &derived_key ) );
7429
7430 PSA_ASSERT( psa_export_key( derived_key,
7431 export_buffer, export_buffer_size,
7432 &export_length ) );
7433 ASSERT_COMPARE( export_buffer, export_length,
7434 expected_export->x, expected_export->len );
7435
7436exit:
7437 mbedtls_free( export_buffer );
7438 psa_key_derivation_abort( &operation );
7439 psa_destroy_key( base_key );
7440 psa_destroy_key( derived_key );
7441 PSA_DONE( );
7442}
7443/* END_CASE */
7444
7445/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007446void derive_key( int alg_arg,
7447 data_t *key_data, data_t *input1, data_t *input2,
7448 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007449 int expected_status_arg,
7450 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02007451{
Ronald Cron5425a212020-08-04 14:58:35 +02007452 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7453 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02007454 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007455 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02007456 size_t bits = bits_arg;
7457 psa_status_t expected_status = expected_status_arg;
7458 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7459 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7460 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
7461
7462 PSA_ASSERT( psa_crypto_init( ) );
7463
7464 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7465 psa_set_key_algorithm( &base_attributes, alg );
7466 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
7467 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007468 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02007469
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007470 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7471 input1->x, input1->len,
7472 input2->x, input2->len,
7473 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02007474 goto exit;
7475
7476 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7477 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02007478 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02007479 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01007480
7481 psa_status_t status =
7482 psa_key_derivation_output_key( &derived_attributes,
7483 &operation,
7484 &derived_key );
7485 if( is_large_output > 0 )
7486 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7487 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02007488
7489exit:
7490 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007491 psa_destroy_key( base_key );
7492 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02007493 PSA_DONE( );
7494}
7495/* END_CASE */
7496
7497/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02007498void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02007499 int our_key_type_arg, int our_key_alg_arg,
7500 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02007501 int expected_status_arg )
7502{
Ronald Cron5425a212020-08-04 14:58:35 +02007503 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007504 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007505 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007506 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007507 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02007509 psa_status_t expected_status = expected_status_arg;
7510 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02007511
Gilles Peskine8817f612018-12-18 00:18:46 +01007512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007513
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007514 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02007515 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007516 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007517 PSA_ASSERT( psa_import_key( &attributes,
7518 our_key_data->x, our_key_data->len,
7519 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007520
Gilles Peskine77f40d82019-04-11 21:27:06 +02007521 /* The tests currently include inputs that should fail at either step.
7522 * Test cases that fail at the setup step should be changed to call
7523 * key_derivation_setup instead, and this function should be renamed
7524 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007525 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02007526 if( status == PSA_SUCCESS )
7527 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007528 TEST_EQUAL( psa_key_derivation_key_agreement(
7529 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
7530 our_key,
7531 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02007532 expected_status );
7533 }
7534 else
7535 {
7536 TEST_ASSERT( status == expected_status );
7537 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02007538
7539exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007540 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007541 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007542 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02007543}
7544/* END_CASE */
7545
7546/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02007547void raw_key_agreement( int alg_arg,
7548 int our_key_type_arg, data_t *our_key_data,
7549 data_t *peer_key_data,
7550 data_t *expected_output )
7551{
Ronald Cron5425a212020-08-04 14:58:35 +02007552 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007553 psa_algorithm_t alg = alg_arg;
7554 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007556 unsigned char *output = NULL;
7557 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01007558 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007559
Gilles Peskinef0cba732019-04-11 22:12:38 +02007560 PSA_ASSERT( psa_crypto_init( ) );
7561
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007562 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7563 psa_set_key_algorithm( &attributes, alg );
7564 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007565 PSA_ASSERT( psa_import_key( &attributes,
7566 our_key_data->x, our_key_data->len,
7567 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007568
gabor-mezei-armceface22021-01-21 12:26:17 +01007569 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
7570 key_bits = psa_get_key_bits( &attributes );
7571
Gilles Peskine992bee82022-04-13 23:25:52 +02007572 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007573 TEST_LE_U( expected_output->len,
7574 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
7575 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
7576 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02007577
7578 /* Good case with exact output size */
7579 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02007580 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7581 peer_key_data->x, peer_key_data->len,
7582 output, expected_output->len,
7583 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007584 ASSERT_COMPARE( output, output_length,
7585 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02007586 mbedtls_free( output );
7587 output = NULL;
7588 output_length = ~0;
7589
7590 /* Larger buffer */
7591 ASSERT_ALLOC( output, expected_output->len + 1 );
7592 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
7593 peer_key_data->x, peer_key_data->len,
7594 output, expected_output->len + 1,
7595 &output_length ) );
7596 ASSERT_COMPARE( output, output_length,
7597 expected_output->x, expected_output->len );
7598 mbedtls_free( output );
7599 output = NULL;
7600 output_length = ~0;
7601
7602 /* Buffer too small */
7603 ASSERT_ALLOC( output, expected_output->len - 1 );
7604 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
7605 peer_key_data->x, peer_key_data->len,
7606 output, expected_output->len - 1,
7607 &output_length ),
7608 PSA_ERROR_BUFFER_TOO_SMALL );
7609 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007610 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02007611 mbedtls_free( output );
7612 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02007613
7614exit:
7615 mbedtls_free( output );
7616 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007617 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02007618}
7619/* END_CASE */
7620
7621/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02007622void key_agreement_capacity( int alg_arg,
7623 int our_key_type_arg, data_t *our_key_data,
7624 data_t *peer_key_data,
7625 int expected_capacity_arg )
7626{
Ronald Cron5425a212020-08-04 14:58:35 +02007627 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007628 psa_algorithm_t alg = alg_arg;
7629 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007630 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007632 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02007633 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02007634
Gilles Peskine8817f612018-12-18 00:18:46 +01007635 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007636
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007637 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7638 psa_set_key_algorithm( &attributes, alg );
7639 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007640 PSA_ASSERT( psa_import_key( &attributes,
7641 our_key_data->x, our_key_data->len,
7642 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007643
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007644 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007645 PSA_ASSERT( psa_key_derivation_key_agreement(
7646 &operation,
7647 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7648 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007649 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7650 {
7651 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007652 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007653 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007654 NULL, 0 ) );
7655 }
Gilles Peskine59685592018-09-18 12:11:34 +02007656
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007657 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007658 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007659 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007660 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02007661
Gilles Peskinebf491972018-10-25 22:36:12 +02007662 /* Test the actual capacity by reading the output. */
7663 while( actual_capacity > sizeof( output ) )
7664 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007665 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007666 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02007667 actual_capacity -= sizeof( output );
7668 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007669 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007670 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007671 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007672 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02007673
Gilles Peskine59685592018-09-18 12:11:34 +02007674exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007675 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007676 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007677 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007678}
7679/* END_CASE */
7680
7681/* BEGIN_CASE */
7682void key_agreement_output( int alg_arg,
7683 int our_key_type_arg, data_t *our_key_data,
7684 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007685 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02007686{
Ronald Cron5425a212020-08-04 14:58:35 +02007687 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02007688 psa_algorithm_t alg = alg_arg;
7689 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007690 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007691 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007692 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02007693
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007694 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
7695 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007696
Gilles Peskine8817f612018-12-18 00:18:46 +01007697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007698
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7700 psa_set_key_algorithm( &attributes, alg );
7701 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007702 PSA_ASSERT( psa_import_key( &attributes,
7703 our_key_data->x, our_key_data->len,
7704 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02007705
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007706 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007707 PSA_ASSERT( psa_key_derivation_key_agreement(
7708 &operation,
7709 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
7710 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007711 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
7712 {
7713 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007714 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02007715 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02007716 NULL, 0 ) );
7717 }
Gilles Peskine59685592018-09-18 12:11:34 +02007718
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007719 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007720 actual_output,
7721 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007722 ASSERT_COMPARE( actual_output, expected_output1->len,
7723 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007724 if( expected_output2->len != 0 )
7725 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007726 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007727 actual_output,
7728 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007729 ASSERT_COMPARE( actual_output, expected_output2->len,
7730 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02007731 }
Gilles Peskine59685592018-09-18 12:11:34 +02007732
7733exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007734 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02007735 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007736 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02007737 mbedtls_free( actual_output );
7738}
7739/* END_CASE */
7740
7741/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02007742void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02007743{
Gilles Peskinea50d7392018-06-21 10:22:13 +02007744 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007745 unsigned char *output = NULL;
7746 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02007747 size_t i;
7748 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02007749
Simon Butcher49f8e312020-03-03 15:51:50 +00007750 TEST_ASSERT( bytes_arg >= 0 );
7751
Gilles Peskine91892022021-02-08 19:50:26 +01007752 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007753 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02007754
Gilles Peskine8817f612018-12-18 00:18:46 +01007755 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02007756
Gilles Peskinea50d7392018-06-21 10:22:13 +02007757 /* Run several times, to ensure that every output byte will be
7758 * nonzero at least once with overwhelming probability
7759 * (2^(-8*number_of_runs)). */
7760 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02007761 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007762 if( bytes != 0 )
7763 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01007764 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007765
Gilles Peskinea50d7392018-06-21 10:22:13 +02007766 for( i = 0; i < bytes; i++ )
7767 {
7768 if( output[i] != 0 )
7769 ++changed[i];
7770 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007771 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02007772
7773 /* Check that every byte was changed to nonzero at least once. This
7774 * validates that psa_generate_random is overwriting every byte of
7775 * the output buffer. */
7776 for( i = 0; i < bytes; i++ )
7777 {
7778 TEST_ASSERT( changed[i] != 0 );
7779 }
Gilles Peskine05d69892018-06-19 22:00:52 +02007780
7781exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007782 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02007783 mbedtls_free( output );
7784 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02007785}
7786/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02007787
7788/* BEGIN_CASE */
7789void generate_key( int type_arg,
7790 int bits_arg,
7791 int usage_arg,
7792 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01007793 int expected_status_arg,
7794 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02007795{
Ronald Cron5425a212020-08-04 14:58:35 +02007796 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007797 psa_key_type_t type = type_arg;
7798 psa_key_usage_t usage = usage_arg;
7799 size_t bits = bits_arg;
7800 psa_algorithm_t alg = alg_arg;
7801 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007802 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007803 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007804
Gilles Peskine8817f612018-12-18 00:18:46 +01007805 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007806
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007807 psa_set_key_usage_flags( &attributes, usage );
7808 psa_set_key_algorithm( &attributes, alg );
7809 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007810 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007811
7812 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01007813 psa_status_t status = psa_generate_key( &attributes, &key );
7814
7815 if( is_large_key > 0 )
7816 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
7817 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007818 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007819 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007820
7821 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007822 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007823 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
7824 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007825
Gilles Peskine818ca122018-06-20 18:16:48 +02007826 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007827 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02007828 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02007829
7830exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007831 /*
7832 * Key attributes may have been returned by psa_get_key_attributes()
7833 * thus reset them as required.
7834 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007835 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007836
Ronald Cron5425a212020-08-04 14:58:35 +02007837 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007838 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02007839}
7840/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03007841
Ronald Cronee414c72021-03-18 18:50:08 +01007842/* 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 +02007843void generate_key_rsa( int bits_arg,
7844 data_t *e_arg,
7845 int expected_status_arg )
7846{
Ronald Cron5425a212020-08-04 14:58:35 +02007847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02007848 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02007849 size_t bits = bits_arg;
7850 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
7851 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
7852 psa_status_t expected_status = expected_status_arg;
7853 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7854 uint8_t *exported = NULL;
7855 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007856 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007857 size_t exported_length = SIZE_MAX;
7858 uint8_t *e_read_buffer = NULL;
7859 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02007860 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007861 size_t e_read_length = SIZE_MAX;
7862
7863 if( e_arg->len == 0 ||
7864 ( e_arg->len == 3 &&
7865 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
7866 {
7867 is_default_public_exponent = 1;
7868 e_read_size = 0;
7869 }
7870 ASSERT_ALLOC( e_read_buffer, e_read_size );
7871 ASSERT_ALLOC( exported, exported_size );
7872
7873 PSA_ASSERT( psa_crypto_init( ) );
7874
7875 psa_set_key_usage_flags( &attributes, usage );
7876 psa_set_key_algorithm( &attributes, alg );
7877 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
7878 e_arg->x, e_arg->len ) );
7879 psa_set_key_bits( &attributes, bits );
7880
7881 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007882 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007883 if( expected_status != PSA_SUCCESS )
7884 goto exit;
7885
7886 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007887 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007888 TEST_EQUAL( psa_get_key_type( &attributes ), type );
7889 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
7890 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
7891 e_read_buffer, e_read_size,
7892 &e_read_length ) );
7893 if( is_default_public_exponent )
7894 TEST_EQUAL( e_read_length, 0 );
7895 else
7896 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
7897
7898 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007899 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02007900 goto exit;
7901
7902 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02007903 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02007904 exported, exported_size,
7905 &exported_length ) );
7906 {
7907 uint8_t *p = exported;
7908 uint8_t *end = exported + exported_length;
7909 size_t len;
7910 /* RSAPublicKey ::= SEQUENCE {
7911 * modulus INTEGER, -- n
7912 * publicExponent INTEGER } -- e
7913 */
7914 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007915 MBEDTLS_ASN1_SEQUENCE |
7916 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01007917 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007918 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
7919 MBEDTLS_ASN1_INTEGER ) );
7920 if( len >= 1 && p[0] == 0 )
7921 {
7922 ++p;
7923 --len;
7924 }
7925 if( e_arg->len == 0 )
7926 {
7927 TEST_EQUAL( len, 3 );
7928 TEST_EQUAL( p[0], 1 );
7929 TEST_EQUAL( p[1], 0 );
7930 TEST_EQUAL( p[2], 1 );
7931 }
7932 else
7933 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
7934 }
7935
7936exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007937 /*
7938 * Key attributes may have been returned by psa_get_key_attributes() or
7939 * set by psa_set_key_domain_parameters() thus reset them as required.
7940 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02007941 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007942
Ronald Cron5425a212020-08-04 14:58:35 +02007943 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007944 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02007945 mbedtls_free( e_read_buffer );
7946 mbedtls_free( exported );
7947}
7948/* END_CASE */
7949
Darryl Greend49a4992018-06-18 17:27:26 +01007950/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007951void persistent_key_load_key_from_storage( data_t *data,
7952 int type_arg, int bits_arg,
7953 int usage_flags_arg, int alg_arg,
7954 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01007955{
Ronald Cron71016a92020-08-28 19:01:50 +02007956 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02007958 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
7959 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007960 psa_key_type_t type = type_arg;
7961 size_t bits = bits_arg;
7962 psa_key_usage_t usage_flags = usage_flags_arg;
7963 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007964 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01007965 unsigned char *first_export = NULL;
7966 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01007967 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007968 size_t first_exported_length;
7969 size_t second_exported_length;
7970
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007971 if( usage_flags & PSA_KEY_USAGE_EXPORT )
7972 {
7973 ASSERT_ALLOC( first_export, export_size );
7974 ASSERT_ALLOC( second_export, export_size );
7975 }
Darryl Greend49a4992018-06-18 17:27:26 +01007976
Gilles Peskine8817f612018-12-18 00:18:46 +01007977 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01007978
Gilles Peskinec87af662019-05-15 16:12:22 +02007979 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02007980 psa_set_key_usage_flags( &attributes, usage_flags );
7981 psa_set_key_algorithm( &attributes, alg );
7982 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007983 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01007984
Darryl Green0c6575a2018-11-07 16:05:30 +00007985 switch( generation_method )
7986 {
7987 case IMPORT_KEY:
7988 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02007989 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007990 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007991 break;
Darryl Greend49a4992018-06-18 17:27:26 +01007992
Darryl Green0c6575a2018-11-07 16:05:30 +00007993 case GENERATE_KEY:
7994 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02007995 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00007996 break;
7997
7998 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01007999#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008000 {
8001 /* Create base key */
8002 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8003 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8004 psa_set_key_usage_flags( &base_attributes,
8005 PSA_KEY_USAGE_DERIVE );
8006 psa_set_key_algorithm( &base_attributes, derive_alg );
8007 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008008 PSA_ASSERT( psa_import_key( &base_attributes,
8009 data->x, data->len,
8010 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008011 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008012 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008013 PSA_ASSERT( psa_key_derivation_input_key(
8014 &operation,
8015 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008016 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008017 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008018 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008019 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8020 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008021 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008022 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008023 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008024 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008025 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008026#else
8027 TEST_ASSUME( ! "KDF not supported in this configuration" );
8028#endif
8029 break;
8030
8031 default:
8032 TEST_ASSERT( ! "generation_method not implemented in test" );
8033 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008034 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008035 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008036
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008037 /* Export the key if permitted by the key policy. */
8038 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8039 {
Ronald Cron5425a212020-08-04 14:58:35 +02008040 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008041 first_export, export_size,
8042 &first_exported_length ) );
8043 if( generation_method == IMPORT_KEY )
8044 ASSERT_COMPARE( data->x, data->len,
8045 first_export, first_exported_length );
8046 }
Darryl Greend49a4992018-06-18 17:27:26 +01008047
8048 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008049 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008050 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008051 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008052
Darryl Greend49a4992018-06-18 17:27:26 +01008053 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008054 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008055 TEST_ASSERT( mbedtls_svc_key_id_equal(
8056 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008057 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8058 PSA_KEY_LIFETIME_PERSISTENT );
8059 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8060 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008061 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008062 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008063 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008064
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008065 /* Export the key again if permitted by the key policy. */
8066 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008067 {
Ronald Cron5425a212020-08-04 14:58:35 +02008068 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008069 second_export, export_size,
8070 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008071 ASSERT_COMPARE( first_export, first_exported_length,
8072 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008073 }
8074
8075 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008076 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008077 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008078
8079exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008080 /*
8081 * Key attributes may have been returned by psa_get_key_attributes()
8082 * thus reset them as required.
8083 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008084 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008085
Darryl Greend49a4992018-06-18 17:27:26 +01008086 mbedtls_free( first_export );
8087 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008088 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008089 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008090 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008091 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008092}
8093/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008094
Neil Armstrongebd9a032022-06-08 17:24:56 +02008095/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECJPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008096void ecjpake_setup( int alg_arg, int primitive_arg, int hash_arg, int role_arg,
8097 int output_step_arg, data_t *pw_data,
8098 int expected_status_arg )
8099{
8100 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8101 psa_pake_operation_t operation = psa_pake_operation_init();
8102 psa_algorithm_t alg = alg_arg;
8103 psa_algorithm_t hash_alg = hash_arg;
8104 psa_pake_role_t role = role_arg;
8105 psa_pake_step_t step = output_step_arg;
8106 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8107 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8108 psa_status_t expected_status = expected_status_arg;
8109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8110 unsigned char *output_buffer = NULL;
8111 size_t output_len = 0;
8112
8113 PSA_INIT( );
8114
8115 ASSERT_ALLOC( output_buffer,
8116 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, step) );
8117
8118 if( pw_data->len > 0 )
8119 {
8120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8121 psa_set_key_algorithm( &attributes, alg );
8122 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8123 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8124 &key ) );
8125 }
8126
8127 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8128 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8129 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8130
8131 status = psa_pake_setup( &operation, &cipher_suite );
8132 if( status != PSA_SUCCESS )
8133 {
8134 TEST_EQUAL( status, expected_status );
8135 goto exit;
8136 }
8137 else
8138 PSA_ASSERT( status );
8139
8140 status = psa_pake_set_role( &operation, role );
8141 if( status != PSA_SUCCESS )
8142 {
8143 TEST_EQUAL( status, expected_status );
8144 goto exit;
8145 }
8146 else
8147 PSA_ASSERT( status );
8148
8149 if( pw_data->len > 0 )
8150 {
8151 status = psa_pake_set_password_key( &operation, key );
8152 if( status != PSA_SUCCESS )
8153 {
8154 TEST_EQUAL( status, expected_status );
8155 goto exit;
8156 }
8157 else
8158 PSA_ASSERT( status );
8159 }
8160
Neil Armstrong707d9572022-06-08 17:31:49 +02008161 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8162 PSA_ERROR_INVALID_ARGUMENT );
8163 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8164 PSA_ERROR_INVALID_ARGUMENT );
8165
8166 const uint8_t unsupported_id[] = "abcd";
8167
8168 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8169 PSA_ERROR_NOT_SUPPORTED );
8170 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8171 PSA_ERROR_NOT_SUPPORTED );
8172
Neil Armstrongd597bc72022-05-25 11:28:39 +02008173 /* First round Output */
8174 status = psa_pake_output( &operation, step, output_buffer,
8175 512, &output_len );
8176 if( status != PSA_SUCCESS )
8177 {
8178 TEST_EQUAL( status, expected_status );
8179 goto exit;
8180 }
8181 else
8182 PSA_ASSERT( status );
8183
8184 TEST_ASSERT( output_len > 0 );
8185
8186exit:
8187 PSA_ASSERT( psa_destroy_key( key ) );
8188 PSA_ASSERT( psa_pake_abort( &operation ) );
8189 mbedtls_free( output_buffer );
8190 PSA_DONE( );
8191}
8192/* END_CASE */
8193
Neil Armstrongebd9a032022-06-08 17:24:56 +02008194/* BEGIN_CASE depends_on:PSA_WANT_ALG_ECJPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008195void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
8196 int derive_alg_arg, data_t *pw_data )
8197{
8198 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8199 psa_pake_operation_t server = psa_pake_operation_init();
8200 psa_pake_operation_t client = psa_pake_operation_init();
8201 psa_algorithm_t alg = alg_arg;
8202 psa_algorithm_t hash_alg = hash_arg;
8203 psa_algorithm_t derive_alg = derive_alg_arg;
8204 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8206 psa_key_derivation_operation_t server_derive =
8207 PSA_KEY_DERIVATION_OPERATION_INIT;
8208 psa_key_derivation_operation_t client_derive =
8209 PSA_KEY_DERIVATION_OPERATION_INIT;
8210 unsigned char *buffer0 = NULL, *buffer1 = NULL;
8211 size_t buffer_length = (
8212 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, PSA_PAKE_STEP_KEY_SHARE) +
8213 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, PSA_PAKE_STEP_ZK_PUBLIC) +
8214 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, PSA_PAKE_STEP_ZK_PROOF)) * 2;
8215 size_t buffer0_off = 0;
8216 size_t buffer1_off = 0;
8217 size_t s_g1_len, s_g2_len, s_a_len;
8218 size_t s_g1_off, s_g2_off, s_a_off;
8219 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
8220 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
8221 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
8222 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
8223 size_t c_g1_len, c_g2_len, c_a_len;
8224 size_t c_g1_off, c_g2_off, c_a_off;
8225 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
8226 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
8227 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
8228 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
8229
8230 PSA_INIT( );
8231
8232 ASSERT_ALLOC( buffer0, buffer_length );
8233 ASSERT_ALLOC( buffer1, buffer_length );
8234
8235 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8236 psa_set_key_algorithm( &attributes, alg );
8237 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8238 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8239 &key ) );
8240
8241 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8242 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8243 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8244
8245 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8246 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8247
8248 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8249 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8250
8251 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8252 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8253
8254 /* Server first round Output */
8255 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_KEY_SHARE,
8256 buffer0 + buffer0_off,
8257 512 - buffer0_off, &s_g1_len ) );
8258 s_g1_off = buffer0_off;
8259 buffer0_off += s_g1_len;
8260 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_ZK_PUBLIC,
8261 buffer0 + buffer0_off,
8262 512 - buffer0_off, &s_x1_pk_len ) );
8263 s_x1_pk_off = buffer0_off;
8264 buffer0_off += s_x1_pk_len;
8265 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_ZK_PROOF,
8266 buffer0 + buffer0_off,
8267 512 - buffer0_off, &s_x1_pr_len ) );
8268 s_x1_pr_off = buffer0_off;
8269 buffer0_off += s_x1_pr_len;
8270 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_KEY_SHARE,
8271 buffer0 + buffer0_off,
8272 512 - buffer0_off, &s_g2_len ) );
8273 s_g2_off = buffer0_off;
8274 buffer0_off += s_g2_len;
8275 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_ZK_PUBLIC,
8276 buffer0 + buffer0_off,
8277 512 - buffer0_off, &s_x2_pk_len ) );
8278 s_x2_pk_off = buffer0_off;
8279 buffer0_off += s_x2_pk_len;
8280 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_ZK_PROOF,
8281 buffer0 + buffer0_off,
8282 512 - buffer0_off, &s_x2_pr_len ) );
8283 s_x2_pr_off = buffer0_off;
8284 buffer0_off += s_x2_pr_len;
8285
8286 /* Client first round Output */
8287 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_KEY_SHARE,
8288 buffer1 + buffer1_off,
8289 512 - buffer1_off, &c_g1_len ) );
8290 c_g1_off = buffer1_off;
8291 buffer1_off += c_g1_len;
8292 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_ZK_PUBLIC,
8293 buffer1 + buffer1_off,
8294 512 - buffer1_off, &c_x1_pk_len ) );
8295 c_x1_pk_off = buffer1_off;
8296 buffer1_off += c_x1_pk_len;
8297 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_ZK_PROOF,
8298 buffer1 + buffer1_off,
8299 512 - buffer1_off, &c_x1_pr_len ) );
8300 c_x1_pr_off = buffer1_off;
8301 buffer1_off += c_x1_pr_len;
8302 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_KEY_SHARE,
8303 buffer1 + buffer1_off,
8304 512 - buffer1_off, &c_g2_len ) );
8305 c_g2_off = buffer1_off;
8306 buffer1_off += c_g2_len;
8307 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_ZK_PUBLIC,
8308 buffer1 + buffer1_off,
8309 512 - buffer1_off, &c_x2_pk_len ) );
8310 c_x2_pk_off = buffer1_off;
8311 buffer1_off += c_x2_pk_len;
8312 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_ZK_PROOF,
8313 buffer1 + buffer1_off,
8314 512 - buffer1_off, &c_x2_pr_len ) );
8315 c_x2_pr_off = buffer1_off;
8316 buffer1_off += c_x2_pr_len;
8317
8318 /* Client first round Input */
8319 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_KEY_SHARE,
8320 buffer0 + s_g1_off, s_g1_len ) );
8321 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_ZK_PUBLIC,
8322 buffer0 + s_x1_pk_off, s_x1_pk_len ) );
8323 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_ZK_PROOF,
8324 buffer0 + s_x1_pr_off, s_x1_pr_len ) );
8325 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_KEY_SHARE,
8326 buffer0 + s_g2_off, s_g2_len ) );
8327 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_ZK_PUBLIC,
8328 buffer0 + s_x2_pk_off, s_x2_pk_len ) );
8329 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_ZK_PROOF,
8330 buffer0 + s_x2_pr_off, s_x2_pr_len ) );
8331
8332 /* Server first round Input */
8333 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_KEY_SHARE,
8334 buffer1 + c_g1_off, c_g1_len ) );
8335 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_ZK_PUBLIC,
8336 buffer1 + c_x1_pk_off, c_x1_pk_len ) );
8337 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_ZK_PROOF,
8338 buffer1 + c_x1_pr_off, c_x1_pr_len ) );
8339 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_KEY_SHARE,
8340 buffer1 + c_g2_off, c_g2_len ) );
8341 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_ZK_PUBLIC,
8342 buffer1 + c_x2_pk_off, c_x2_pk_len ) );
8343 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_ZK_PROOF,
8344 buffer1 + c_x2_pr_off, c_x2_pr_len ) );
8345
8346 /* Server second round Output */
8347 buffer0_off = 0;
8348
8349 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_KEY_SHARE,
8350 buffer0 + buffer0_off,
8351 512 - buffer0_off, &s_a_len ) );
8352 s_a_off = buffer0_off;
8353 buffer0_off += s_a_len;
8354 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_ZK_PUBLIC,
8355 buffer0 + buffer0_off,
8356 512 - buffer0_off, &s_x2s_pk_len ) );
8357 s_x2s_pk_off = buffer0_off;
8358 buffer0_off += s_x2s_pk_len;
8359 PSA_ASSERT( psa_pake_output( &server, PSA_PAKE_STEP_ZK_PROOF,
8360 buffer0 + buffer0_off,
8361 512 - buffer0_off, &s_x2s_pr_len ) );
8362 s_x2s_pr_off = buffer0_off;
8363 buffer0_off += s_x2s_pr_len;
8364
8365 /* Client second round Output */
8366 buffer1_off = 0;
8367
8368 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_KEY_SHARE,
8369 buffer1 + buffer1_off,
8370 512 - buffer1_off, &c_a_len ) );
8371 c_a_off = buffer1_off;
8372 buffer1_off += c_a_len;
8373 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_ZK_PUBLIC,
8374 buffer1 + buffer1_off,
8375 512 - buffer1_off, &c_x2s_pk_len ) );
8376 c_x2s_pk_off = buffer1_off;
8377 buffer1_off += c_x2s_pk_len;
8378 PSA_ASSERT( psa_pake_output( &client, PSA_PAKE_STEP_ZK_PROOF,
8379 buffer1 + buffer1_off,
8380 512 - buffer1_off, &c_x2s_pr_len ) );
8381 c_x2s_pr_off = buffer1_off;
8382 buffer1_off += c_x2s_pr_len;
8383
8384 /* Client second round Input */
8385 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_KEY_SHARE,
8386 buffer0 + s_a_off, s_a_len ) );
8387 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_ZK_PUBLIC,
8388 buffer0 + s_x2s_pk_off, s_x2s_pk_len ) );
8389 PSA_ASSERT( psa_pake_input( &client, PSA_PAKE_STEP_ZK_PROOF,
8390 buffer0 + s_x2s_pr_off, s_x2s_pr_len ) );
8391
8392 /* Server second round Input */
8393 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_KEY_SHARE,
8394 buffer1 + c_a_off, c_a_len ) );
8395 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_ZK_PUBLIC,
8396 buffer1 + c_x2s_pk_off, c_x2s_pk_len ) );
8397 PSA_ASSERT( psa_pake_input( &server, PSA_PAKE_STEP_ZK_PROOF,
8398 buffer1 + c_x2s_pr_off, c_x2s_pr_len ) );
8399
8400
8401 /* Get shared key */
8402 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
8403 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
8404
8405 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
8406 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
8407 {
8408 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
8409 PSA_KEY_DERIVATION_INPUT_SEED,
8410 (const uint8_t*) "", 0) );
8411 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
8412 PSA_KEY_DERIVATION_INPUT_SEED,
8413 (const uint8_t*) "", 0) );
8414 }
8415
8416 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
8417 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
8418
8419exit:
8420 psa_key_derivation_abort( &server_derive );
8421 psa_key_derivation_abort( &client_derive );
8422 psa_destroy_key( key );
8423 psa_pake_abort( &server );
8424 psa_pake_abort( &client );
8425 mbedtls_free( buffer0 );
8426 mbedtls_free( buffer1 );
8427 PSA_DONE( );
8428}
8429/* END_CASE */