blob: fa237d3667e7718cd4809d97b185fa95ab0aed8c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Archana4d7ae1d2021-07-07 02:50:22 +053018#if defined(PSA_CRYPTO_DRIVER_TEST)
19#include "test/drivers/test_driver.h"
Archana8a180362021-07-05 02:18:48 +053020#define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION
21#else
22#define TEST_DRIVER_LOCATION 0x7fffff
Archana4d7ae1d2021-07-07 02:50:22 +053023#endif
Ronald Cron28a45ed2021-02-09 20:35:42 +010024
Gilles Peskine4023c012021-05-27 13:21:20 +020025/* If this comes up, it's a bug in the test code or in the test data. */
26#define UNUSED 0xdeadbeef
27
Dave Rodgman647791d2021-06-23 12:49:59 +010028/* Assert that an operation is (not) active.
29 * This serves as a proxy for checking if the operation is aborted. */
30#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
31#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
Gilles Peskinef426e0f2019-02-25 17:42:03 +010032
33/** An invalid export length that will never be set by psa_export_key(). */
34static const size_t INVALID_EXPORT_LENGTH = ~0U;
35
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036/** Test if a buffer contains a constant byte value.
37 *
38 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039 *
40 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042 * \param size Size of the buffer in bytes.
43 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 * \return 1 if the buffer is all-bits-zero.
45 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020046 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020047static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048{
49 size_t i;
50 for( i = 0; i < size; i++ )
51 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020052 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020053 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020054 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020055 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020056}
Andrzej Kurek77b8e092022-01-17 15:29:38 +010057#if defined(MBEDTLS_ASN1_WRITE_C)
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
59static int asn1_write_10x( unsigned char **p,
60 unsigned char *start,
61 size_t bits,
62 unsigned char x )
63{
64 int ret;
65 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020066 if( bits == 0 )
67 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
68 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020069 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030070 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020071 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
72 *p -= len;
73 ( *p )[len-1] = x;
74 if( bits % 8 == 0 )
75 ( *p )[1] |= 1;
76 else
77 ( *p )[0] |= 1 << ( bits % 8 );
78 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
79 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
80 MBEDTLS_ASN1_INTEGER ) );
81 return( len );
82}
83
84static int construct_fake_rsa_key( unsigned char *buffer,
85 size_t buffer_size,
86 unsigned char **p,
87 size_t bits,
88 int keypair )
89{
90 size_t half_bits = ( bits + 1 ) / 2;
91 int ret;
92 int len = 0;
93 /* Construct something that looks like a DER encoding of
94 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
95 * RSAPrivateKey ::= SEQUENCE {
96 * version Version,
97 * modulus INTEGER, -- n
98 * publicExponent INTEGER, -- e
99 * privateExponent INTEGER, -- d
100 * prime1 INTEGER, -- p
101 * prime2 INTEGER, -- q
102 * exponent1 INTEGER, -- d mod (p-1)
103 * exponent2 INTEGER, -- d mod (q-1)
104 * coefficient INTEGER, -- (inverse of q) mod p
105 * otherPrimeInfos OtherPrimeInfos OPTIONAL
106 * }
107 * Or, for a public key, the same structure with only
108 * version, modulus and publicExponent.
109 */
110 *p = buffer + buffer_size;
111 if( keypair )
112 {
113 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
116 asn1_write_10x( p, buffer, half_bits, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
118 asn1_write_10x( p, buffer, half_bits, 1 ) );
119 MBEDTLS_ASN1_CHK_ADD( len, /* q */
120 asn1_write_10x( p, buffer, half_bits, 1 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
122 asn1_write_10x( p, buffer, half_bits, 3 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, /* d */
124 asn1_write_10x( p, buffer, bits, 1 ) );
125 }
126 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
127 asn1_write_10x( p, buffer, 17, 1 ) );
128 MBEDTLS_ASN1_CHK_ADD( len, /* n */
129 asn1_write_10x( p, buffer, bits, 1 ) );
130 if( keypair )
131 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
132 mbedtls_asn1_write_int( p, buffer, 0 ) );
133 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
134 {
135 const unsigned char tag =
136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
137 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
138 }
139 return( len );
140}
Andrzej Kurek77b8e092022-01-17 15:29:38 +0100141#endif /* MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100143int exercise_mac_setup( psa_key_type_t key_type,
144 const unsigned char *key_bytes,
145 size_t key_length,
146 psa_algorithm_t alg,
147 psa_mac_operation_t *operation,
148 psa_status_t *status )
149{
Ronald Cron5425a212020-08-04 14:58:35 +0200150 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100152
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100153 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200154 psa_set_key_algorithm( &attributes, alg );
155 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200156 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157
Ronald Cron5425a212020-08-04 14:58:35 +0200158 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100159 /* Whether setup succeeded or failed, abort must succeed. */
160 PSA_ASSERT( psa_mac_abort( operation ) );
161 /* If setup failed, reproduce the failure, so that the caller can
162 * test the resulting state of the operation object. */
163 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100164 {
Ronald Cron5425a212020-08-04 14:58:35 +0200165 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 }
167
Ronald Cron5425a212020-08-04 14:58:35 +0200168 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100169 return( 1 );
170
171exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200172 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100173 return( 0 );
174}
175
176int exercise_cipher_setup( psa_key_type_t key_type,
177 const unsigned char *key_bytes,
178 size_t key_length,
179 psa_algorithm_t alg,
180 psa_cipher_operation_t *operation,
181 psa_status_t *status )
182{
Ronald Cron5425a212020-08-04 14:58:35 +0200183 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200184 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100185
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
187 psa_set_key_algorithm( &attributes, alg );
188 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200189 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190
Ronald Cron5425a212020-08-04 14:58:35 +0200191 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 /* Whether setup succeeded or failed, abort must succeed. */
193 PSA_ASSERT( psa_cipher_abort( operation ) );
194 /* If setup failed, reproduce the failure, so that the caller can
195 * test the resulting state of the operation object. */
196 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100197 {
Ronald Cron5425a212020-08-04 14:58:35 +0200198 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100199 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 }
201
Ronald Cron5425a212020-08-04 14:58:35 +0200202 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100203 return( 1 );
204
205exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200206 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100207 return( 0 );
208}
209
Ronald Cron5425a212020-08-04 14:58:35 +0200210static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200211{
212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200213 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200214 uint8_t buffer[1];
215 size_t length;
216 int ok = 0;
217
Ronald Cronecfb2372020-07-23 17:13:42 +0200218 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
220 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
221 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200222 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000223 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200224 TEST_EQUAL(
225 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
226 TEST_EQUAL(
227 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200228 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200229 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
230 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
231 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
232 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
233
Ronald Cron5425a212020-08-04 14:58:35 +0200234 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000235 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200236 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200237 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000238 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200239
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 ok = 1;
241
242exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100243 /*
244 * Key attributes may have been returned by psa_get_key_attributes()
245 * thus reset them as required.
246 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200247 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100248
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200249 return( ok );
250}
251
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200252/* Assert that a key isn't reported as having a slot number. */
253#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
254#define ASSERT_NO_SLOT_NUMBER( attributes ) \
255 do \
256 { \
257 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
258 TEST_EQUAL( psa_get_key_slot_number( \
259 attributes, \
260 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
261 PSA_ERROR_INVALID_ARGUMENT ); \
262 } \
263 while( 0 )
264#else /* MBEDTLS_PSA_CRYPTO_SE_C */
265#define ASSERT_NO_SLOT_NUMBER( attributes ) \
266 ( (void) 0 )
267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
268
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100269/* An overapproximation of the amount of storage needed for a key of the
270 * given type and with the given content. The API doesn't make it easy
271 * to find a good value for the size. The current implementation doesn't
272 * care about the value anyway. */
273#define KEY_BITS_FROM_DATA( type, data ) \
274 ( data )->len
275
Darryl Green0c6575a2018-11-07 16:05:30 +0000276typedef enum {
277 IMPORT_KEY = 0,
278 GENERATE_KEY = 1,
279 DERIVE_KEY = 2
280} generate_method;
281
Paul Elliott33746aa2021-09-15 16:40:40 +0100282typedef enum
283{
284 DO_NOT_SET_LENGTHS = 0,
285 SET_LENGTHS_BEFORE_NONCE = 1,
286 SET_LENGTHS_AFTER_NONCE = 2
Paul Elliottbb979e72021-09-22 12:54:42 +0100287} set_lengths_method_t;
Paul Elliott33746aa2021-09-15 16:40:40 +0100288
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100289typedef enum
290{
291 USE_NULL_TAG = 0,
292 USE_GIVEN_TAG = 1,
Paul Elliottbb979e72021-09-22 12:54:42 +0100293} tag_usage_method_t;
Paul Elliott1c67e0b2021-09-19 13:11:50 +0100294
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100295/*!
296 * \brief Internal Function for AEAD multipart tests.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100297 * \param key_type_arg Type of key passed in
298 * \param key_data The encryption / decryption key data
299 * \param alg_arg The type of algorithm used
300 * \param nonce Nonce data
301 * \param additional_data Additional data
Paul Elliott8eec8d42021-09-19 22:38:27 +0100302 * \param ad_part_len_arg If not -1, the length of chunks to
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100303 * feed additional data in to be encrypted /
304 * decrypted. If -1, no chunking.
305 * \param input_data Data to encrypt / decrypt
Paul Elliott8eec8d42021-09-19 22:38:27 +0100306 * \param data_part_len_arg If not -1, the length of chunks to feed
307 * the data in to be encrypted / decrypted. If
308 * -1, no chunking
Paul Elliottbb979e72021-09-22 12:54:42 +0100309 * \param set_lengths_method A member of the set_lengths_method_t enum is
Paul Elliott8eec8d42021-09-19 22:38:27 +0100310 * expected here, this controls whether or not
311 * to set lengths, and in what order with
312 * respect to set nonce.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100313 * \param expected_output Expected output
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100314 * \param is_encrypt If non-zero this is an encryption operation.
Paul Elliott41ffae12021-07-22 21:52:01 +0100315 * \param do_zero_parts If non-zero, interleave zero length chunks
Paul Elliott8eec8d42021-09-19 22:38:27 +0100316 * with normal length chunks.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100317 * \return int Zero on failure, non-zero on success.
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100318 */
319static int aead_multipart_internal_func( int key_type_arg, data_t *key_data,
320 int alg_arg,
321 data_t *nonce,
322 data_t *additional_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100323 int ad_part_len_arg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100324 data_t *input_data,
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100325 int data_part_len_arg,
Paul Elliottbb979e72021-09-22 12:54:42 +0100326 set_lengths_method_t set_lengths_method,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100327 data_t *expected_output,
Paul Elliott329d5382021-07-22 17:10:45 +0100328 int is_encrypt,
Paul Elliott33746aa2021-09-15 16:40:40 +0100329 int do_zero_parts )
Paul Elliottd3f82412021-06-16 16:52:21 +0100330{
331 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
332 psa_key_type_t key_type = key_type_arg;
333 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +0100334 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottd3f82412021-06-16 16:52:21 +0100335 unsigned char *output_data = NULL;
336 unsigned char *part_data = NULL;
337 unsigned char *final_data = NULL;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100338 size_t data_true_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100339 size_t part_data_size = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100340 size_t output_size = 0;
341 size_t final_output_size = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100342 size_t output_length = 0;
343 size_t key_bits = 0;
344 size_t tag_length = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100345 size_t part_offset = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100346 size_t part_length = 0;
347 size_t output_part_length = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100348 size_t tag_size = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100349 size_t ad_part_len = 0;
350 size_t data_part_len = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100351 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliottd3f82412021-06-16 16:52:21 +0100352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
354
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100355 int test_ok = 0;
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100356 size_t part_count = 0;
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100357
Paul Elliottd3f82412021-06-16 16:52:21 +0100358 PSA_ASSERT( psa_crypto_init( ) );
359
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100360 if( is_encrypt )
361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
362 else
363 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
364
Paul Elliottd3f82412021-06-16 16:52:21 +0100365 psa_set_key_algorithm( &attributes, alg );
366 psa_set_key_type( &attributes, key_type );
367
368 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
369 &key ) );
370
371 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
372 key_bits = psa_get_key_bits( &attributes );
373
374 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
375
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100376 if( is_encrypt )
377 {
378 /* Tag gets written at end of buffer. */
379 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
380 ( input_data->len +
381 tag_length ) );
382 data_true_size = input_data->len;
383 }
384 else
385 {
386 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
387 ( input_data->len -
388 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100389
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100390 /* Do not want to attempt to decrypt tag. */
391 data_true_size = input_data->len - tag_length;
392 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100393
394 ASSERT_ALLOC( output_data, output_size );
395
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100396 if( is_encrypt )
397 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100398 final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200399 TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100400 }
401 else
402 {
Paul Elliott5a9642f2021-09-13 19:13:22 +0100403 final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200404 TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100405 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100406
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100407 ASSERT_ALLOC( final_data, final_output_size );
Paul Elliottd3f82412021-06-16 16:52:21 +0100408
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100409 if( is_encrypt )
410 status = psa_aead_encrypt_setup( &operation, key, alg );
411 else
412 status = psa_aead_decrypt_setup( &operation, key, alg );
Paul Elliottd3f82412021-06-16 16:52:21 +0100413
414 /* If the operation is not supported, just skip and not fail in case the
415 * encryption involves a common limitation of cryptography hardwares and
416 * an alternative implementation. */
417 if( status == PSA_ERROR_NOT_SUPPORTED )
418 {
419 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
420 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
421 }
422
423 PSA_ASSERT( status );
424
Paul Elliott33746aa2021-09-15 16:40:40 +0100425 if( set_lengths_method == DO_NOT_SET_LENGTHS )
426 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
427 else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
Paul Elliottd3f82412021-06-16 16:52:21 +0100428 {
Paul Elliott33746aa2021-09-15 16:40:40 +0100429 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
430 data_true_size ) );
Paul Elliottebf91632021-07-22 17:54:42 +0100431 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
432 }
Paul Elliott33746aa2021-09-15 16:40:40 +0100433 else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
Paul Elliottebf91632021-07-22 17:54:42 +0100434 {
435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
436
Paul Elliott33746aa2021-09-15 16:40:40 +0100437 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
438 data_true_size ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100439 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100440
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100441 if( ad_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100442 {
443 /* Pass additional data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100444 ad_part_len = (size_t) ad_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100445
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100446 for( part_offset = 0, part_count = 0;
447 part_offset < additional_data->len;
448 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100449 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100450 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100451 {
Paul Elliott329d5382021-07-22 17:10:45 +0100452 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100453 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100454 else if( additional_data->len - part_offset < ad_part_len )
455 {
456 part_length = additional_data->len - part_offset;
457 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100458 else
459 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100460 part_length = ad_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100461 }
462
463 PSA_ASSERT( psa_aead_update_ad( &operation,
464 additional_data->x + part_offset,
465 part_length ) );
466
Paul Elliottd3f82412021-06-16 16:52:21 +0100467 }
468 }
469 else
470 {
471 /* Pass additional data in one go. */
472 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
473 additional_data->len ) );
474 }
475
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100476 if( data_part_len_arg != -1 )
Paul Elliottd3f82412021-06-16 16:52:21 +0100477 {
478 /* Pass data in parts */
Paul Elliott6bfd0fb2021-09-15 14:15:55 +0100479 data_part_len = ( size_t ) data_part_len_arg;
Paul Elliottd3f82412021-06-16 16:52:21 +0100480 part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100481 ( size_t ) data_part_len );
Paul Elliottd3f82412021-06-16 16:52:21 +0100482
483 ASSERT_ALLOC( part_data, part_data_size );
484
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100485 for( part_offset = 0, part_count = 0;
486 part_offset < data_true_size;
487 part_offset += part_length, part_count++ )
Paul Elliottd3f82412021-06-16 16:52:21 +0100488 {
Paul Elliott4e4d71a2021-09-15 16:50:01 +0100489 if( do_zero_parts && ( part_count & 0x01 ) )
Paul Elliottd3f82412021-06-16 16:52:21 +0100490 {
Paul Elliott329d5382021-07-22 17:10:45 +0100491 part_length = 0;
Paul Elliottd3f82412021-06-16 16:52:21 +0100492 }
Paul Elliotte49fe452021-09-15 16:52:11 +0100493 else if( ( data_true_size - part_offset ) < data_part_len )
494 {
495 part_length = ( data_true_size - part_offset );
496 }
Paul Elliottd3f82412021-06-16 16:52:21 +0100497 else
498 {
Paul Elliotte49fe452021-09-15 16:52:11 +0100499 part_length = data_part_len;
Paul Elliottd3f82412021-06-16 16:52:21 +0100500 }
501
502 PSA_ASSERT( psa_aead_update( &operation,
503 ( input_data->x + part_offset ),
504 part_length, part_data,
505 part_data_size,
506 &output_part_length ) );
507
508 if( output_data && output_part_length )
509 {
Mircea Udrea657ff4f2022-01-31 13:51:56 +0100510 memcpy( ( output_data + output_length ), part_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100511 output_part_length );
512 }
513
Paul Elliottd3f82412021-06-16 16:52:21 +0100514 output_length += output_part_length;
515 }
516 }
517 else
518 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100519 /* Pass all data in one go. */
Paul Elliottd3f82412021-06-16 16:52:21 +0100520 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100521 data_true_size, output_data,
Paul Elliottd3f82412021-06-16 16:52:21 +0100522 output_size, &output_length ) );
523 }
524
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100525 if( is_encrypt )
526 PSA_ASSERT( psa_aead_finish( &operation, final_data,
527 final_output_size,
528 &output_part_length,
529 tag_buffer, tag_length,
530 &tag_size ) );
531 else
Paul Elliottd3f82412021-06-16 16:52:21 +0100532 {
Paul Elliott9961a662021-09-17 19:19:02 +0100533 PSA_ASSERT( psa_aead_verify( &operation, final_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100534 final_output_size,
535 &output_part_length,
536 ( input_data->x + data_true_size ),
Paul Elliott9961a662021-09-17 19:19:02 +0100537 tag_length ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100538 }
539
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100540 if( output_data && output_part_length )
541 memcpy( ( output_data + output_length ), final_data,
542 output_part_length );
Paul Elliottd3f82412021-06-16 16:52:21 +0100543
544 output_length += output_part_length;
545
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100546
547 /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE
548 * should be exact.*/
549 if( is_encrypt )
Paul Elliottd3f82412021-06-16 16:52:21 +0100550 {
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100551 TEST_EQUAL( tag_length, tag_size );
552
553 if( output_data && tag_length )
554 memcpy( ( output_data + output_length ), tag_buffer,
555 tag_length );
556
557 output_length += tag_length;
558
559 TEST_EQUAL( output_length,
Gilles Peskine7be11a72022-04-14 00:12:57 +0200560 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg,
561 input_data->len ) );
562 TEST_LE_U( output_length,
563 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100564 }
565 else
566 {
567 TEST_EQUAL( output_length,
568 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg,
569 input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +0200570 TEST_LE_U( output_length,
571 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Paul Elliottd3f82412021-06-16 16:52:21 +0100572 }
573
Paul Elliottd3f82412021-06-16 16:52:21 +0100574
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100575 ASSERT_COMPARE( expected_output->x, expected_output->len,
Paul Elliottd3f82412021-06-16 16:52:21 +0100576 output_data, output_length );
577
Paul Elliottd3f82412021-06-16 16:52:21 +0100578
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100579 test_ok = 1;
Paul Elliottd3f82412021-06-16 16:52:21 +0100580
581exit:
582 psa_destroy_key( key );
583 psa_aead_abort( &operation );
584 mbedtls_free( output_data );
585 mbedtls_free( part_data );
586 mbedtls_free( final_data );
587 PSA_DONE( );
Paul Elliott97fd1ba2021-07-21 18:46:06 +0100588
589 return( test_ok );
Paul Elliottd3f82412021-06-16 16:52:21 +0100590}
591
Neil Armstrong4766f992022-02-28 16:23:59 +0100592/*!
593 * \brief Internal Function for MAC multipart tests.
594 * \param key_type_arg Type of key passed in
595 * \param key_data The encryption / decryption key data
596 * \param alg_arg The type of algorithm used
597 * \param input_data Data to encrypt / decrypt
598 * \param data_part_len_arg If not -1, the length of chunks to feed
599 * the data in to be encrypted / decrypted. If
600 * -1, no chunking
601 * \param expected_output Expected output
602 * \param is_verify If non-zero this is an verify operation.
603 * \param do_zero_parts If non-zero, interleave zero length chunks
604 * with normal length chunks.
605 * \return int Zero on failure, non-zero on success.
606 */
607static int mac_multipart_internal_func( int key_type_arg, data_t *key_data,
608 int alg_arg,
609 data_t *input_data,
610 int data_part_len_arg,
611 data_t *expected_output,
612 int is_verify,
613 int do_zero_parts )
614{
615 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
616 psa_key_type_t key_type = key_type_arg;
617 psa_algorithm_t alg = alg_arg;
618 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
619 unsigned char mac[PSA_MAC_MAX_SIZE];
620 size_t part_offset = 0;
621 size_t part_length = 0;
622 size_t data_part_len = 0;
623 size_t mac_len = 0;
624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
625 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
626
627 int test_ok = 0;
628 size_t part_count = 0;
629
Neil Armstrongfd4c2592022-03-07 10:11:11 +0100630 PSA_INIT( );
Neil Armstrong4766f992022-02-28 16:23:59 +0100631
632 if( is_verify )
633 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
634 else
635 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
636
637 psa_set_key_algorithm( &attributes, alg );
638 psa_set_key_type( &attributes, key_type );
639
640 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
641 &key ) );
642
643 if( is_verify )
644 status = psa_mac_verify_setup( &operation, key, alg );
645 else
646 status = psa_mac_sign_setup( &operation, key, alg );
647
648 PSA_ASSERT( status );
649
650 if( data_part_len_arg != -1 )
651 {
652 /* Pass data in parts */
653 data_part_len = ( size_t ) data_part_len_arg;
654
655 for( part_offset = 0, part_count = 0;
656 part_offset < input_data->len;
657 part_offset += part_length, part_count++ )
658 {
659 if( do_zero_parts && ( part_count & 0x01 ) )
660 {
661 part_length = 0;
662 }
663 else if( ( input_data->len - part_offset ) < data_part_len )
664 {
665 part_length = ( input_data->len - part_offset );
666 }
667 else
668 {
669 part_length = data_part_len;
670 }
671
672 PSA_ASSERT( psa_mac_update( &operation,
673 ( input_data->x + part_offset ),
674 part_length ) );
675 }
676 }
677 else
678 {
679 /* Pass all data in one go. */
680 PSA_ASSERT( psa_mac_update( &operation, input_data->x,
681 input_data->len ) );
682 }
683
684 if( is_verify )
685 {
686 PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x,
687 expected_output->len ) );
688 }
689 else
690 {
691 PSA_ASSERT( psa_mac_sign_finish( &operation, mac,
692 PSA_MAC_MAX_SIZE, &mac_len ) );
693
694 ASSERT_COMPARE( expected_output->x, expected_output->len,
695 mac, mac_len );
696 }
697
698 test_ok = 1;
699
700exit:
701 psa_destroy_key( key );
702 psa_mac_abort( &operation );
703 PSA_DONE( );
704
705 return( test_ok );
706}
707
Neil Armstrong75673ab2022-06-15 17:39:01 +0200708#if defined(PSA_WANT_ALG_JPAKE)
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200709static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive,
710 psa_pake_operation_t *server,
711 psa_pake_operation_t *client,
712 int client_input_first,
713 int round, int inject_error )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200714{
715 unsigned char *buffer0 = NULL, *buffer1 = NULL;
716 size_t buffer_length = (
717 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) +
718 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) +
719 PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2;
720 size_t buffer0_off = 0;
721 size_t buffer1_off = 0;
722 size_t s_g1_len, s_g2_len, s_a_len;
723 size_t s_g1_off, s_g2_off, s_a_off;
724 size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len;
725 size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off;
726 size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len;
727 size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off;
728 size_t c_g1_len, c_g2_len, c_a_len;
729 size_t c_g1_off, c_g2_off, c_a_off;
730 size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len;
731 size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off;
732 size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len;
733 size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off;
734 psa_status_t expected_status = PSA_SUCCESS;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200735 psa_status_t status;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200736
737 ASSERT_ALLOC( buffer0, buffer_length );
738 ASSERT_ALLOC( buffer1, buffer_length );
739
740 switch( round )
741 {
742 case 1:
743 /* Server first round Output */
744 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
745 buffer0 + buffer0_off,
746 512 - buffer0_off, &s_g1_len ) );
747 s_g1_off = buffer0_off;
748 buffer0_off += s_g1_len;
749 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
750 buffer0 + buffer0_off,
751 512 - buffer0_off, &s_x1_pk_len ) );
752 s_x1_pk_off = buffer0_off;
753 buffer0_off += s_x1_pk_len;
754 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
755 buffer0 + buffer0_off,
756 512 - buffer0_off, &s_x1_pr_len ) );
757 s_x1_pr_off = buffer0_off;
758 buffer0_off += s_x1_pr_len;
759 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
760 buffer0 + buffer0_off,
761 512 - buffer0_off, &s_g2_len ) );
762 s_g2_off = buffer0_off;
763 buffer0_off += s_g2_len;
764 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
765 buffer0 + buffer0_off,
766 512 - buffer0_off, &s_x2_pk_len ) );
767 s_x2_pk_off = buffer0_off;
768 buffer0_off += s_x2_pk_len;
769 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
770 buffer0 + buffer0_off,
771 512 - buffer0_off, &s_x2_pr_len ) );
772 s_x2_pr_off = buffer0_off;
773 buffer0_off += s_x2_pr_len;
774
775 if( inject_error == 1 )
776 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +0200777 buffer0[s_x1_pk_off + 8] >>= 4;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200778 buffer0[s_x2_pk_off + 7] <<= 4;
779 expected_status = PSA_ERROR_DATA_INVALID;
780 }
781
Neil Armstrong51009d72022-09-05 17:59:54 +0200782 /*
783 * When injecting errors in inputs, the implementation is
784 * free to detect it right away of with a delay.
785 * This permits delaying the error until the end of the input
786 * sequence, if no error appears then, this will be treated
787 * as an error.
788 */
789
Neil Armstrongf983caf2022-06-15 15:27:48 +0200790 if( client_input_first == 1 )
791 {
792 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200793 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
794 buffer0 + s_g1_off, s_g1_len );
795 if( inject_error == 1 && status != PSA_SUCCESS )
Neil Armstrongf983caf2022-06-15 15:27:48 +0200796 {
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200797 TEST_EQUAL( status, expected_status );
798 break;
Neil Armstrongf983caf2022-06-15 15:27:48 +0200799 }
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200800 else
801 {
802 TEST_EQUAL( status, PSA_SUCCESS );
803 }
804
805 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
806 buffer0 + s_x1_pk_off,
807 s_x1_pk_len );
808 if( inject_error == 1 && status != PSA_SUCCESS )
809 {
810 TEST_EQUAL( status, expected_status );
811 break;
812 }
813 else
814 {
815 TEST_EQUAL( status, PSA_SUCCESS );
816 }
817
818 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
819 buffer0 + s_x1_pr_off,
820 s_x1_pr_len );
821 if( inject_error == 1 && status != PSA_SUCCESS )
822 {
823 TEST_EQUAL( status, expected_status );
824 break;
825 }
826 else
827 {
828 TEST_EQUAL( status, PSA_SUCCESS );
829 }
830
831 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
832 buffer0 + s_g2_off,
833 s_g2_len );
834 if( inject_error == 1 && status != PSA_SUCCESS )
835 {
836 TEST_EQUAL( status, expected_status );
837 break;
838 }
839 else
840 {
841 TEST_EQUAL( status, PSA_SUCCESS );
842 }
843
844 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
845 buffer0 + s_x2_pk_off,
846 s_x2_pk_len );
847 if( inject_error == 1 && status != PSA_SUCCESS )
848 {
849 TEST_EQUAL( status, expected_status );
850 break;
851 }
852 else
853 {
854 TEST_EQUAL( status, PSA_SUCCESS );
855 }
856
857 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
858 buffer0 + s_x2_pr_off,
859 s_x2_pr_len );
860 if( inject_error == 1 && status != PSA_SUCCESS )
861 {
862 TEST_EQUAL( status, expected_status );
863 break;
864 }
865 else
866 {
867 TEST_EQUAL( status, PSA_SUCCESS );
868 }
869
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200870 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200871 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200872 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200873 }
874
875 /* Client first round Output */
876 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
877 buffer1 + buffer1_off,
878 512 - buffer1_off, &c_g1_len ) );
879 c_g1_off = buffer1_off;
880 buffer1_off += c_g1_len;
881 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
882 buffer1 + buffer1_off,
883 512 - buffer1_off, &c_x1_pk_len ) );
884 c_x1_pk_off = buffer1_off;
885 buffer1_off += c_x1_pk_len;
886 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
887 buffer1 + buffer1_off,
888 512 - buffer1_off, &c_x1_pr_len ) );
889 c_x1_pr_off = buffer1_off;
890 buffer1_off += c_x1_pr_len;
891 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
892 buffer1 + buffer1_off,
893 512 - buffer1_off, &c_g2_len ) );
894 c_g2_off = buffer1_off;
895 buffer1_off += c_g2_len;
896 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
897 buffer1 + buffer1_off,
898 512 - buffer1_off, &c_x2_pk_len ) );
899 c_x2_pk_off = buffer1_off;
900 buffer1_off += c_x2_pk_len;
901 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
902 buffer1 + buffer1_off,
903 512 - buffer1_off, &c_x2_pr_len ) );
904 c_x2_pr_off = buffer1_off;
905 buffer1_off += c_x2_pr_len;
906
907 if( client_input_first == 0 )
908 {
909 /* Client first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200910 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
911 buffer0 + s_g1_off, s_g1_len );
912 if( inject_error == 1 && status != PSA_SUCCESS )
913 {
914 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200915 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200916 }
917 else
918 {
919 TEST_EQUAL( status, PSA_SUCCESS );
920 }
921
922 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
923 buffer0 + s_x1_pk_off,
924 s_x1_pk_len );
925 if( inject_error == 1 && status != PSA_SUCCESS )
926 {
927 TEST_EQUAL( status, expected_status );
928 break;
929 }
930 else
931 {
932 TEST_EQUAL( status, PSA_SUCCESS );
933 }
934
935 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
936 buffer0 + s_x1_pr_off,
937 s_x1_pr_len );
938 if( inject_error == 1 && status != PSA_SUCCESS )
939 {
940 TEST_EQUAL( status, expected_status );
941 break;
942 }
943 else
944 {
945 TEST_EQUAL( status, PSA_SUCCESS );
946 }
947
948 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
949 buffer0 + s_g2_off,
950 s_g2_len );
951 if( inject_error == 1 && status != PSA_SUCCESS )
952 {
953 TEST_EQUAL( status, expected_status );
954 break;
955 }
956 else
957 {
958 TEST_EQUAL( status, PSA_SUCCESS );
959 }
960
961 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
962 buffer0 + s_x2_pk_off,
963 s_x2_pk_len );
964 if( inject_error == 1 && status != PSA_SUCCESS )
965 {
966 TEST_EQUAL( status, expected_status );
967 break;
968 }
969 else
970 {
971 TEST_EQUAL( status, PSA_SUCCESS );
972 }
973
974 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
975 buffer0 + s_x2_pr_off,
976 s_x2_pr_len );
977 if( inject_error == 1 && status != PSA_SUCCESS )
978 {
979 TEST_EQUAL( status, expected_status );
980 break;
981 }
982 else
983 {
984 TEST_EQUAL( status, PSA_SUCCESS );
985 }
986
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200987 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +0200988 if( inject_error == 1 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +0200989 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +0200990 }
991
992 if( inject_error == 2 )
993 {
994 buffer1[c_x1_pk_off + 12] >>= 4;
995 buffer1[c_x2_pk_off + 7] <<= 4;
996 expected_status = PSA_ERROR_DATA_INVALID;
997 }
998
999 /* Server first round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001000 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1001 buffer1 + c_g1_off, c_g1_len );
1002 if( inject_error == 2 && status != PSA_SUCCESS )
1003 {
1004 TEST_EQUAL( status, expected_status );
1005 break;
1006 }
1007 else
1008 {
1009 TEST_EQUAL( status, PSA_SUCCESS );
1010 }
1011
1012 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1013 buffer1 + c_x1_pk_off, c_x1_pk_len );
1014 if( inject_error == 2 && status != PSA_SUCCESS )
1015 {
1016 TEST_EQUAL( status, expected_status );
1017 break;
1018 }
1019 else
1020 {
1021 TEST_EQUAL( status, PSA_SUCCESS );
1022 }
1023
1024 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1025 buffer1 + c_x1_pr_off, c_x1_pr_len );
1026 if( inject_error == 2 && status != PSA_SUCCESS )
1027 {
1028 TEST_EQUAL( status, expected_status );
1029 break;
1030 }
1031 else
1032 {
1033 TEST_EQUAL( status, PSA_SUCCESS );
1034 }
1035
1036 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1037 buffer1 + c_g2_off, c_g2_len );
1038 if( inject_error == 2 && status != PSA_SUCCESS )
1039 {
1040 TEST_EQUAL( status, expected_status );
1041 break;
1042 }
1043 else
1044 {
1045 TEST_EQUAL( status, PSA_SUCCESS );
1046 }
1047
1048 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1049 buffer1 + c_x2_pk_off, c_x2_pk_len );
1050 if( inject_error == 2 && status != PSA_SUCCESS )
1051 {
1052 TEST_EQUAL( status, expected_status );
1053 break;
1054 }
1055 else
1056 {
1057 TEST_EQUAL( status, PSA_SUCCESS );
1058 }
1059
1060 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1061 buffer1 + c_x2_pr_off, c_x2_pr_len );
1062 if( inject_error == 2 && status != PSA_SUCCESS )
1063 {
1064 TEST_EQUAL( status, expected_status );
1065 break;
1066 }
1067 else
1068 {
1069 TEST_EQUAL( status, PSA_SUCCESS );
1070 }
1071
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001072 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001073 if( inject_error == 2 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001074 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001075
1076 break;
1077
1078 case 2:
1079 /* Server second round Output */
1080 buffer0_off = 0;
1081
1082 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE,
1083 buffer0 + buffer0_off,
1084 512 - buffer0_off, &s_a_len ) );
1085 s_a_off = buffer0_off;
1086 buffer0_off += s_a_len;
1087 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC,
1088 buffer0 + buffer0_off,
1089 512 - buffer0_off, &s_x2s_pk_len ) );
1090 s_x2s_pk_off = buffer0_off;
1091 buffer0_off += s_x2s_pk_len;
1092 PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF,
1093 buffer0 + buffer0_off,
1094 512 - buffer0_off, &s_x2s_pr_len ) );
1095 s_x2s_pr_off = buffer0_off;
1096 buffer0_off += s_x2s_pr_len;
1097
1098 if( inject_error == 3 )
1099 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001100 buffer0[s_x2s_pk_off + 12] += 0x33;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001101 expected_status = PSA_ERROR_DATA_INVALID;
1102 }
1103
1104 if( client_input_first == 1 )
1105 {
1106 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001107 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1108 buffer0 + s_a_off, s_a_len );
1109 if( inject_error == 3 && status != PSA_SUCCESS )
1110 {
1111 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001112 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001113 }
1114 else
1115 {
1116 TEST_EQUAL( status, PSA_SUCCESS );
1117 }
1118
1119 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1120 buffer0 + s_x2s_pk_off,
1121 s_x2s_pk_len );
1122 if( inject_error == 3 && status != PSA_SUCCESS )
1123 {
1124 TEST_EQUAL( status, expected_status );
1125 break;
1126 }
1127 else
1128 {
1129 TEST_EQUAL( status, PSA_SUCCESS );
1130 }
1131
1132 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1133 buffer0 + s_x2s_pr_off,
1134 s_x2s_pr_len );
1135 if( inject_error == 3 && status != PSA_SUCCESS )
1136 {
1137 TEST_EQUAL( status, expected_status );
1138 break;
1139 }
1140 else
1141 {
1142 TEST_EQUAL( status, PSA_SUCCESS );
1143 }
1144
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001145 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001146 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001147 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001148 }
1149
1150 /* Client second round Output */
1151 buffer1_off = 0;
1152
1153 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE,
1154 buffer1 + buffer1_off,
1155 512 - buffer1_off, &c_a_len ) );
1156 c_a_off = buffer1_off;
1157 buffer1_off += c_a_len;
1158 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC,
1159 buffer1 + buffer1_off,
1160 512 - buffer1_off, &c_x2s_pk_len ) );
1161 c_x2s_pk_off = buffer1_off;
1162 buffer1_off += c_x2s_pk_len;
1163 PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF,
1164 buffer1 + buffer1_off,
1165 512 - buffer1_off, &c_x2s_pr_len ) );
1166 c_x2s_pr_off = buffer1_off;
1167 buffer1_off += c_x2s_pr_len;
1168
1169 if( client_input_first == 0 )
1170 {
1171 /* Client second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001172 status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE,
1173 buffer0 + s_a_off, s_a_len );
1174 if( inject_error == 3 && status != PSA_SUCCESS )
1175 {
1176 TEST_EQUAL( status, expected_status );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001177 break;
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001178 }
1179 else
1180 {
1181 TEST_EQUAL( status, PSA_SUCCESS );
1182 }
1183
1184 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC,
1185 buffer0 + s_x2s_pk_off,
1186 s_x2s_pk_len );
1187 if( inject_error == 3 && status != PSA_SUCCESS )
1188 {
1189 TEST_EQUAL( status, expected_status );
1190 break;
1191 }
1192 else
1193 {
1194 TEST_EQUAL( status, PSA_SUCCESS );
1195 }
1196
1197 status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF,
1198 buffer0 + s_x2s_pr_off,
1199 s_x2s_pr_len );
1200 if( inject_error == 3 && status != PSA_SUCCESS )
1201 {
1202 TEST_EQUAL( status, expected_status );
1203 break;
1204 }
1205 else
1206 {
1207 TEST_EQUAL( status, PSA_SUCCESS );
1208 }
1209
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001210 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001211 if( inject_error == 3 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001212 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001213 }
1214
1215 if( inject_error == 4 )
1216 {
Neil Armstrongeae1dfc2022-06-21 13:37:06 +02001217 buffer1[c_x2s_pk_off + 7] += 0x28;
Neil Armstrongf983caf2022-06-15 15:27:48 +02001218 expected_status = PSA_ERROR_DATA_INVALID;
1219 }
1220
1221 /* Server second round Input */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001222 status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE,
1223 buffer1 + c_a_off, c_a_len );
1224 if( inject_error == 4 && status != PSA_SUCCESS )
1225 {
1226 TEST_EQUAL( status, expected_status );
1227 break;
1228 }
1229 else
1230 {
1231 TEST_EQUAL( status, PSA_SUCCESS );
1232 }
1233
1234 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC,
1235 buffer1 + c_x2s_pk_off, c_x2s_pk_len );
1236 if( inject_error == 4 && status != PSA_SUCCESS )
1237 {
1238 TEST_EQUAL( status, expected_status );
1239 break;
1240 }
1241 else
1242 {
1243 TEST_EQUAL( status, PSA_SUCCESS );
1244 }
1245
1246 status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF,
1247 buffer1 + c_x2s_pr_off, c_x2s_pr_len );
1248 if( inject_error == 4 && status != PSA_SUCCESS )
1249 {
1250 TEST_EQUAL( status, expected_status );
1251 break;
1252 }
1253 else
1254 {
1255 TEST_EQUAL( status, PSA_SUCCESS );
1256 }
1257
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001258 /* Error didn't trigger, make test fail */
Neil Armstrongdb5b9602022-06-20 14:56:50 +02001259 if( inject_error == 4 )
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02001260 TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001261
1262 break;
1263
1264 }
1265
Neil Armstrongf983caf2022-06-15 15:27:48 +02001266exit:
1267 mbedtls_free( buffer0 );
1268 mbedtls_free( buffer1 );
Neil Armstrongf983caf2022-06-15 15:27:48 +02001269}
Neil Armstrong75673ab2022-06-15 17:39:01 +02001270#endif /* PSA_WANT_ALG_JPAKE */
Neil Armstrongf983caf2022-06-15 15:27:48 +02001271
Gilles Peskinee59236f2018-01-27 23:32:46 +01001272/* END_HEADER */
1273
1274/* BEGIN_DEPENDENCIES
1275 * depends_on:MBEDTLS_PSA_CRYPTO_C
1276 * END_DEPENDENCIES
1277 */
1278
1279/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001280void static_checks( )
1281{
1282 size_t max_truncated_mac_size =
1283 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1284
1285 /* Check that the length for a truncated MAC always fits in the algorithm
1286 * encoding. The shifted mask is the maximum truncated value. The
1287 * untruncated algorithm may be one byte larger. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02001288 TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size );
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001289}
1290/* END_CASE */
1291
1292/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001293void import_with_policy( int type_arg,
1294 int usage_arg, int alg_arg,
1295 int expected_status_arg )
1296{
1297 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1298 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001299 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +02001300 psa_key_type_t type = type_arg;
1301 psa_key_usage_t usage = usage_arg;
1302 psa_algorithm_t alg = alg_arg;
1303 psa_status_t expected_status = expected_status_arg;
1304 const uint8_t key_material[16] = {0};
1305 psa_status_t status;
1306
1307 PSA_ASSERT( psa_crypto_init( ) );
1308
1309 psa_set_key_type( &attributes, type );
1310 psa_set_key_usage_flags( &attributes, usage );
1311 psa_set_key_algorithm( &attributes, alg );
1312
1313 status = psa_import_key( &attributes,
1314 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +02001315 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001316 TEST_EQUAL( status, expected_status );
1317 if( status != PSA_SUCCESS )
1318 goto exit;
1319
Ronald Cron5425a212020-08-04 14:58:35 +02001320 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001321 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001322 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001323 mbedtls_test_update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001324 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001325 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001326
Ronald Cron5425a212020-08-04 14:58:35 +02001327 PSA_ASSERT( psa_destroy_key( key ) );
1328 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001329
1330exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001331 /*
1332 * Key attributes may have been returned by psa_get_key_attributes()
1333 * thus reset them as required.
1334 */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001335 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001336
1337 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001338 PSA_DONE( );
1339}
1340/* END_CASE */
1341
1342/* BEGIN_CASE */
1343void import_with_data( data_t *data, int type_arg,
1344 int attr_bits_arg,
1345 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001346{
1347 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1348 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001349 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001350 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001351 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001352 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001353 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354
Gilles Peskine8817f612018-12-18 00:18:46 +01001355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001356
Gilles Peskine4747d192019-04-17 15:05:45 +02001357 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001358 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001359
Ronald Cron5425a212020-08-04 14:58:35 +02001360 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001361 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001362 if( status != PSA_SUCCESS )
1363 goto exit;
1364
Ronald Cron5425a212020-08-04 14:58:35 +02001365 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001366 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001367 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001368 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001369 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001370
Ronald Cron5425a212020-08-04 14:58:35 +02001371 PSA_ASSERT( psa_destroy_key( key ) );
1372 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001373
1374exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001375 /*
1376 * Key attributes may have been returned by psa_get_key_attributes()
1377 * thus reset them as required.
1378 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001379 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001380
1381 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001382 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001383}
1384/* END_CASE */
1385
1386/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001387void import_large_key( int type_arg, int byte_size_arg,
1388 int expected_status_arg )
1389{
1390 psa_key_type_t type = type_arg;
1391 size_t byte_size = byte_size_arg;
1392 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1393 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001394 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02001395 psa_status_t status;
1396 uint8_t *buffer = NULL;
1397 size_t buffer_size = byte_size + 1;
1398 size_t n;
1399
Steven Cooreman69967ce2021-01-18 18:01:08 +01001400 /* Skip the test case if the target running the test cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001401 * accommodate large keys due to heap size constraints */
Steven Cooreman69967ce2021-01-18 18:01:08 +01001402 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +02001403 memset( buffer, 'K', byte_size );
1404
1405 PSA_ASSERT( psa_crypto_init( ) );
1406
1407 /* Try importing the key */
1408 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1409 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001410 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +01001411 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +02001412 TEST_EQUAL( status, expected_status );
1413
1414 if( status == PSA_SUCCESS )
1415 {
Ronald Cron5425a212020-08-04 14:58:35 +02001416 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001417 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1418 TEST_EQUAL( psa_get_key_bits( &attributes ),
1419 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001420 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001421 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +02001422 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02001423 for( n = 0; n < byte_size; n++ )
1424 TEST_EQUAL( buffer[n], 'K' );
1425 for( n = byte_size; n < buffer_size; n++ )
1426 TEST_EQUAL( buffer[n], 0 );
1427 }
1428
1429exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001430 /*
1431 * Key attributes may have been returned by psa_get_key_attributes()
1432 * thus reset them as required.
1433 */
1434 psa_reset_key_attributes( &attributes );
1435
Ronald Cron5425a212020-08-04 14:58:35 +02001436 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +02001437 PSA_DONE( );
1438 mbedtls_free( buffer );
1439}
1440/* END_CASE */
1441
Andrzej Kurek77b8e092022-01-17 15:29:38 +01001442/* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001443void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1444{
Ronald Cron5425a212020-08-04 14:58:35 +02001445 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001446 size_t bits = bits_arg;
1447 psa_status_t expected_status = expected_status_arg;
1448 psa_status_t status;
1449 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001450 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001451 size_t buffer_size = /* Slight overapproximations */
1452 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001453 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001454 unsigned char *p;
1455 int ret;
1456 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001458
Gilles Peskine8817f612018-12-18 00:18:46 +01001459 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001460 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001461
1462 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1463 bits, keypair ) ) >= 0 );
1464 length = ret;
1465
1466 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001467 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +02001468 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001469 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001470
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001471 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +02001472 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001473
1474exit:
1475 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001476 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001477}
1478/* END_CASE */
1479
1480/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001481void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001482 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001483 int usage_arg, int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301484 int lifetime_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001485 int expected_bits,
1486 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001487 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001488 int canonical_input )
1489{
Ronald Cron5425a212020-08-04 14:58:35 +02001490 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001491 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001492 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001493 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001494 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301495 psa_key_lifetime_t lifetime = lifetime_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001496 unsigned char *exported = NULL;
1497 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001498 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001499 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001502 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503
Moran Pekercb088e72018-07-17 17:36:59 +03001504 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001505 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001507 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001508 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001509
Archana4d7ae1d2021-07-07 02:50:22 +05301510 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001511 psa_set_key_usage_flags( &attributes, usage_arg );
1512 psa_set_key_algorithm( &attributes, alg );
1513 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001514
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001515 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001516 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001517
1518 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001519 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001520 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1521 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001522 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001523
1524 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001525 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001526 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001527
1528 /* The exported length must be set by psa_export_key() to a value between 0
1529 * and export_size. On errors, the exported length must be 0. */
1530 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1531 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001532 TEST_LE_U( exported_length, export_size );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001533
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001534 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001535 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001536 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001537 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001538 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001539 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001540 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001541
Gilles Peskineea38a922021-02-13 00:05:16 +01001542 /* Run sanity checks on the exported key. For non-canonical inputs,
1543 * this validates the canonical representations. For canonical inputs,
1544 * this doesn't directly validate the implementation, but it still helps
1545 * by cross-validating the test data with the sanity check code. */
Archana4d7ae1d2021-07-07 02:50:22 +05301546 if( !psa_key_lifetime_is_external( lifetime ) )
1547 {
1548 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
1549 goto exit;
1550 }
Gilles Peskine8f609232018-08-11 01:24:55 +02001551
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001552 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001553 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001554 else
1555 {
Ronald Cron5425a212020-08-04 14:58:35 +02001556 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +02001557 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +02001558 &key2 ) );
1559 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 reexported,
1561 export_size,
1562 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001563 ASSERT_COMPARE( exported, exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301564 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +02001565 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001566 }
Gilles Peskine7be11a72022-04-14 00:12:57 +02001567 TEST_LE_U( exported_length,
Archana4d7ae1d2021-07-07 02:50:22 +05301568 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
Archana9d17bf42021-09-10 06:22:44 +05301569 psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001570 TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001571
1572destroy:
1573 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001574 PSA_ASSERT( psa_destroy_key( key ) );
1575 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001576
1577exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001578 /*
1579 * Key attributes may have been returned by psa_get_key_attributes()
1580 * thus reset them as required.
1581 */
1582 psa_reset_key_attributes( &got_attributes );
Archana4d7ae1d2021-07-07 02:50:22 +05301583 psa_destroy_key( key ) ;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001584 mbedtls_free( exported );
1585 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001586 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587}
1588/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001589
Moran Pekerf709f4a2018-06-06 17:26:04 +03001590/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001591void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001592 int type_arg,
1593 int alg_arg,
Archana4d7ae1d2021-07-07 02:50:22 +05301594 int lifetime_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001595 int export_size_delta,
1596 int expected_export_status_arg,
1597 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001598{
Ronald Cron5425a212020-08-04 14:58:35 +02001599 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001600 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001601 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001602 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001603 psa_status_t status;
Archana4d7ae1d2021-07-07 02:50:22 +05301604 psa_key_lifetime_t lifetime = lifetime_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001605 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001606 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001607 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001609
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001611
Archana4d7ae1d2021-07-07 02:50:22 +05301612 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine4747d192019-04-17 15:05:45 +02001613 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1614 psa_set_key_algorithm( &attributes, alg );
1615 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001616
1617 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001618 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001619
Gilles Peskine49c25912018-10-29 15:15:31 +01001620 /* Export the public key */
1621 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +02001622 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001623 exported, export_size,
1624 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001625 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001626 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001627 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001628 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001629 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +02001630 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001631 bits = psa_get_key_bits( &attributes );
Gilles Peskine7be11a72022-04-14 00:12:57 +02001632 TEST_LE_U( expected_public_key->len,
1633 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
1634 TEST_LE_U( expected_public_key->len,
1635 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
1636 TEST_LE_U( expected_public_key->len,
1637 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +01001638 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1639 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001640 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001641exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001642 /*
1643 * Key attributes may have been returned by psa_get_key_attributes()
1644 * thus reset them as required.
1645 */
1646 psa_reset_key_attributes( &attributes );
1647
itayzafrir3e02b3b2018-06-12 17:06:52 +03001648 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +02001649 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001650 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001651}
1652/* END_CASE */
1653
Gilles Peskine20035e32018-02-03 22:44:14 +01001654/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001655void import_and_exercise_key( data_t *data,
1656 int type_arg,
1657 int bits_arg,
1658 int alg_arg )
1659{
Ronald Cron5425a212020-08-04 14:58:35 +02001660 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001661 psa_key_type_t type = type_arg;
1662 size_t bits = bits_arg;
1663 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001664 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001666 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001667
Gilles Peskine8817f612018-12-18 00:18:46 +01001668 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001669
Gilles Peskine4747d192019-04-17 15:05:45 +02001670 psa_set_key_usage_flags( &attributes, usage );
1671 psa_set_key_algorithm( &attributes, alg );
1672 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001673
1674 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +02001675 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001676
1677 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02001678 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001679 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1680 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001681
1682 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001683 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001684 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001685
Ronald Cron5425a212020-08-04 14:58:35 +02001686 PSA_ASSERT( psa_destroy_key( key ) );
1687 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001688
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001689exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001690 /*
1691 * Key attributes may have been returned by psa_get_key_attributes()
1692 * thus reset them as required.
1693 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001694 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001695
1696 psa_reset_key_attributes( &attributes );
1697 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001698 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001699}
1700/* END_CASE */
1701
1702/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001703void effective_key_attributes( int type_arg, int expected_type_arg,
1704 int bits_arg, int expected_bits_arg,
1705 int usage_arg, int expected_usage_arg,
1706 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001707{
Ronald Cron5425a212020-08-04 14:58:35 +02001708 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +01001709 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001710 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001711 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001712 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001713 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001714 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001715 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001716 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001718
Gilles Peskine8817f612018-12-18 00:18:46 +01001719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001720
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001721 psa_set_key_usage_flags( &attributes, usage );
1722 psa_set_key_algorithm( &attributes, alg );
1723 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001724 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001725
Ronald Cron5425a212020-08-04 14:58:35 +02001726 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +01001727 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001728
Ronald Cron5425a212020-08-04 14:58:35 +02001729 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001730 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1731 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1732 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1733 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001734
1735exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001736 /*
1737 * Key attributes may have been returned by psa_get_key_attributes()
1738 * thus reset them as required.
1739 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001740 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001741
1742 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001743 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001744}
1745/* END_CASE */
1746
1747/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001748void check_key_policy( int type_arg, int bits_arg,
1749 int usage_arg, int alg_arg )
1750{
1751 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
gabor-mezei-armff8264c2021-06-28 14:36:03 +02001752 usage_arg,
1753 mbedtls_test_update_key_usage_flags( usage_arg ),
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001754 alg_arg, alg_arg );
Gilles Peskine06c28892019-11-26 18:07:46 +01001755 goto exit;
1756}
1757/* END_CASE */
1758
1759/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001760void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001761{
1762 /* Test each valid way of initializing the object, except for `= {0}`, as
1763 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1764 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001765 * to suppress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001766 psa_key_attributes_t func = psa_key_attributes_init( );
1767 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1768 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001769
1770 memset( &zero, 0, sizeof( zero ) );
1771
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001772 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1773 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1774 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001775
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001776 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1777 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1778 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1779
1780 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1781 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1782 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1783
1784 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1785 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1786 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1787
1788 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1789 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1790 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001791}
1792/* END_CASE */
1793
1794/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001795void mac_key_policy( int policy_usage_arg,
1796 int policy_alg_arg,
1797 int key_type_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001798 data_t *key_data,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001799 int exercise_alg_arg,
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001800 int expected_status_sign_arg,
1801 int expected_status_verify_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001802{
Ronald Cron5425a212020-08-04 14:58:35 +02001803 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001804 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001805 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001806 psa_key_type_t key_type = key_type_arg;
1807 psa_algorithm_t policy_alg = policy_alg_arg;
1808 psa_algorithm_t exercise_alg = exercise_alg_arg;
1809 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001810 psa_status_t status;
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001811 psa_status_t expected_status_sign = expected_status_sign_arg;
1812 psa_status_t expected_status_verify = expected_status_verify_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001813 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001814
Gilles Peskine8817f612018-12-18 00:18:46 +01001815 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001817 psa_set_key_usage_flags( &attributes, policy_usage );
1818 psa_set_key_algorithm( &attributes, policy_alg );
1819 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001820
Gilles Peskine049c7532019-05-15 20:22:09 +02001821 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001822 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001823
gabor-mezei-arm40d5cd82021-06-29 11:06:16 +02001824 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
1825 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001826
Ronald Cron5425a212020-08-04 14:58:35 +02001827 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001828 TEST_EQUAL( status, expected_status_sign );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001829
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001830 /* Calculate the MAC, one-shot case. */
1831 uint8_t input[128] = {0};
1832 size_t mac_len;
1833 TEST_EQUAL( psa_mac_compute( key, exercise_alg,
1834 input, 128,
1835 mac, PSA_MAC_MAX_SIZE, &mac_len ),
1836 expected_status_sign );
1837
Neil Armstrong3af9b972022-02-07 12:20:21 +01001838 /* Calculate the MAC, multi-part case. */
1839 PSA_ASSERT( psa_mac_abort( &operation ) );
1840 status = psa_mac_sign_setup( &operation, key, exercise_alg );
1841 if( status == PSA_SUCCESS )
1842 {
1843 status = psa_mac_update( &operation, input, 128 );
1844 if( status == PSA_SUCCESS )
1845 TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE,
1846 &mac_len ),
1847 expected_status_sign );
1848 else
1849 TEST_EQUAL( status, expected_status_sign );
1850 }
1851 else
1852 {
1853 TEST_EQUAL( status, expected_status_sign );
1854 }
1855 PSA_ASSERT( psa_mac_abort( &operation ) );
1856
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001857 /* Verify correct MAC, one-shot case. */
1858 status = psa_mac_verify( key, exercise_alg, input, 128,
1859 mac, mac_len );
1860
1861 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1862 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +01001863 else
Mateusz Starzyk1ebcd552021-08-30 17:09:03 +02001864 TEST_EQUAL( status, expected_status_verify );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001865
Neil Armstrong3af9b972022-02-07 12:20:21 +01001866 /* Verify correct MAC, multi-part case. */
1867 status = psa_mac_verify_setup( &operation, key, exercise_alg );
1868 if( status == PSA_SUCCESS )
1869 {
1870 status = psa_mac_update( &operation, input, 128 );
1871 if( status == PSA_SUCCESS )
1872 {
1873 status = psa_mac_verify_finish( &operation, mac, mac_len );
1874 if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS )
1875 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
1876 else
1877 TEST_EQUAL( status, expected_status_verify );
1878 }
1879 else
1880 {
1881 TEST_EQUAL( status, expected_status_verify );
1882 }
1883 }
1884 else
1885 {
1886 TEST_EQUAL( status, expected_status_verify );
1887 }
1888
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001890
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001891 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001892 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Mateusz Starzykd07f4fc2021-08-24 11:01:23 +02001893 TEST_EQUAL( status, expected_status_verify );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001894
1895exit:
1896 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001897 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001898 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001899}
1900/* END_CASE */
1901
1902/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001903void cipher_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001904 int policy_alg,
1905 int key_type,
1906 data_t *key_data,
1907 int exercise_alg )
1908{
Ronald Cron5425a212020-08-04 14:58:35 +02001909 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001910 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001911 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001912 psa_key_usage_t policy_usage = policy_usage_arg;
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001913 size_t output_buffer_size = 0;
1914 size_t input_buffer_size = 0;
1915 size_t output_length = 0;
1916 uint8_t *output = NULL;
1917 uint8_t *input = NULL;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 psa_status_t status;
1919
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001920 input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg );
1921 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg,
1922 input_buffer_size );
1923
1924 ASSERT_ALLOC( input, input_buffer_size );
1925 ASSERT_ALLOC( output, output_buffer_size );
1926
Gilles Peskine8817f612018-12-18 00:18:46 +01001927 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001928
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001929 psa_set_key_usage_flags( &attributes, policy_usage );
1930 psa_set_key_algorithm( &attributes, policy_alg );
1931 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001932
Gilles Peskine049c7532019-05-15 20:22:09 +02001933 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001934 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001935
gabor-mezei-arm98a34352021-06-28 14:05:00 +02001936 /* Check if no key usage flag implication is done */
1937 TEST_EQUAL( policy_usage,
1938 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001939
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001940 /* Encrypt check, one-shot */
1941 status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size,
1942 output, output_buffer_size,
1943 &output_length);
1944 if( policy_alg == exercise_alg &&
1945 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1946 PSA_ASSERT( status );
1947 else
1948 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1949
1950 /* Encrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001951 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001952 if( policy_alg == exercise_alg &&
1953 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001954 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957 psa_cipher_abort( &operation );
1958
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001959 /* Decrypt check, one-shot */
1960 status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size,
1961 input, input_buffer_size,
1962 &output_length);
1963 if( policy_alg == exercise_alg &&
1964 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1965 PSA_ASSERT( status );
1966 else
1967 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1968
1969 /* Decrypt check, multi-part */
Ronald Cron5425a212020-08-04 14:58:35 +02001970 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001971 if( policy_alg == exercise_alg &&
1972 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001973 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001975 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001976
1977exit:
1978 psa_cipher_abort( &operation );
Neil Armstrong78aeaf82022-02-07 14:50:35 +01001979 mbedtls_free( input );
1980 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02001981 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001982 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001983}
1984/* END_CASE */
1985
1986/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001987void aead_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001988 int policy_alg,
1989 int key_type,
1990 data_t *key_data,
1991 int nonce_length_arg,
1992 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001993 int exercise_alg,
1994 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995{
Ronald Cron5425a212020-08-04 14:58:35 +02001996 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong752d8112022-02-07 14:51:11 +01001998 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02001999 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002000 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002001 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002 unsigned char nonce[16] = {0};
2003 size_t nonce_length = nonce_length_arg;
2004 unsigned char tag[16];
2005 size_t tag_length = tag_length_arg;
2006 size_t output_length;
2007
Gilles Peskine7be11a72022-04-14 00:12:57 +02002008 TEST_LE_U( nonce_length, sizeof( nonce ) );
2009 TEST_LE_U( tag_length, sizeof( tag ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002010
Gilles Peskine8817f612018-12-18 00:18:46 +01002011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002012
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002013 psa_set_key_usage_flags( &attributes, policy_usage );
2014 psa_set_key_algorithm( &attributes, policy_alg );
2015 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016
Gilles Peskine049c7532019-05-15 20:22:09 +02002017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002018 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002020 /* Check if no key usage implication is done */
2021 TEST_EQUAL( policy_usage,
2022 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002023
Neil Armstrong752d8112022-02-07 14:51:11 +01002024 /* Encrypt check, one-shot */
Ronald Cron5425a212020-08-04 14:58:35 +02002025 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002026 nonce, nonce_length,
2027 NULL, 0,
2028 NULL, 0,
2029 tag, tag_length,
2030 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002031 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2032 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002034 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002035
Neil Armstrong752d8112022-02-07 14:51:11 +01002036 /* Encrypt check, multi-part */
2037 status = psa_aead_encrypt_setup( &operation, key, exercise_alg );
2038 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
2039 TEST_EQUAL( status, expected_status );
2040 else
2041 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2042
2043 /* Decrypt check, one-shot */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002045 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002046 nonce, nonce_length,
2047 NULL, 0,
2048 tag, tag_length,
2049 NULL, 0,
2050 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002051 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2052 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2053 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002054 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002056 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057
Neil Armstrong752d8112022-02-07 14:51:11 +01002058 /* Decrypt check, multi-part */
2059 PSA_ASSERT( psa_aead_abort( &operation ) );
2060 status = psa_aead_decrypt_setup( &operation, key, exercise_alg );
2061 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
2062 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2063 else
2064 TEST_EQUAL( status, expected_status );
2065
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002066exit:
Neil Armstrong752d8112022-02-07 14:51:11 +01002067 PSA_ASSERT( psa_aead_abort( &operation ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002068 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002069 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070}
2071/* END_CASE */
2072
2073/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002074void asymmetric_encryption_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075 int policy_alg,
2076 int key_type,
2077 data_t *key_data,
2078 int exercise_alg )
2079{
Ronald Cron5425a212020-08-04 14:58:35 +02002080 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002081 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002082 psa_key_usage_t policy_usage = policy_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083 psa_status_t status;
2084 size_t key_bits;
2085 size_t buffer_length;
2086 unsigned char *buffer = NULL;
2087 size_t output_length;
2088
Gilles Peskine8817f612018-12-18 00:18:46 +01002089 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002090
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002091 psa_set_key_usage_flags( &attributes, policy_usage );
2092 psa_set_key_algorithm( &attributes, policy_alg );
2093 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
Gilles Peskine049c7532019-05-15 20:22:09 +02002095 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002096 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002098 /* Check if no key usage implication is done */
2099 TEST_EQUAL( policy_usage,
2100 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002101
Ronald Cron5425a212020-08-04 14:58:35 +02002102 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002103 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002104 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2105 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002106 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002107
Ronald Cron5425a212020-08-04 14:58:35 +02002108 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109 NULL, 0,
2110 NULL, 0,
2111 buffer, buffer_length,
2112 &output_length );
2113 if( policy_alg == exercise_alg &&
2114 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002115 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002117 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002118
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002119 if( buffer_length != 0 )
2120 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +02002121 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122 buffer, buffer_length,
2123 NULL, 0,
2124 buffer, buffer_length,
2125 &output_length );
2126 if( policy_alg == exercise_alg &&
2127 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002128 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002129 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002130 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002131
2132exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002133 /*
2134 * Key attributes may have been returned by psa_get_key_attributes()
2135 * thus reset them as required.
2136 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002137 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002138
2139 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002140 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141 mbedtls_free( buffer );
2142}
2143/* END_CASE */
2144
2145/* BEGIN_CASE */
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002146void asymmetric_signature_key_policy( int policy_usage_arg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002147 int policy_alg,
2148 int key_type,
2149 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002150 int exercise_alg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002151 int payload_length_arg,
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002152 int expected_usage_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002153{
Ronald Cron5425a212020-08-04 14:58:35 +02002154 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002155 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002156 psa_key_usage_t policy_usage = policy_usage_arg;
2157 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002158 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002159 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2160 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2161 * compatible with the policy and `payload_length_arg` is supposed to be
2162 * a valid input length to sign. If `payload_length_arg <= 0`,
2163 * `exercise_alg` is supposed to be forbidden by the policy. */
2164 int compatible_alg = payload_length_arg > 0;
2165 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002166 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002167 size_t signature_length;
2168
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002169 /* Check if all implicit usage flags are deployed
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002170 in the expected usage flags. */
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002171 TEST_EQUAL( expected_usage,
2172 mbedtls_test_update_key_usage_flags( policy_usage ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002173
Gilles Peskine8817f612018-12-18 00:18:46 +01002174 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002175
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002176 psa_set_key_usage_flags( &attributes, policy_usage );
2177 psa_set_key_algorithm( &attributes, policy_alg );
2178 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002179
Gilles Peskine049c7532019-05-15 20:22:09 +02002180 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002181 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002182
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002183 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
2184
Ronald Cron5425a212020-08-04 14:58:35 +02002185 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002186 payload, payload_length,
2187 signature, sizeof( signature ),
2188 &signature_length );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002189 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002190 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002191 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002192 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002193
2194 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02002195 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002196 payload, payload_length,
2197 signature, sizeof( signature ) );
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002198 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002199 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002200 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002201 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002202
Gilles Peskinef7b41372021-09-22 16:15:05 +02002203 if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) &&
gabor-mezei-armd851d682021-06-28 14:53:49 +02002204 PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) )
gabor-mezei-armedf2df82021-05-13 16:17:16 +02002205 {
2206 status = psa_sign_message( key, exercise_alg,
2207 payload, payload_length,
2208 signature, sizeof( signature ),
2209 &signature_length );
2210 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 )
2211 PSA_ASSERT( status );
2212 else
2213 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2214
2215 memset( signature, 0, sizeof( signature ) );
2216 status = psa_verify_message( key, exercise_alg,
2217 payload, payload_length,
2218 signature, sizeof( signature ) );
2219 if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 )
2220 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
2221 else
2222 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2223 }
2224
Gilles Peskined5b33222018-06-18 22:20:03 +02002225exit:
Ronald Cron5425a212020-08-04 14:58:35 +02002226 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002227 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002228}
2229/* END_CASE */
2230
Janos Follathba3fab92019-06-11 14:50:16 +01002231/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002232void derive_key_policy( int policy_usage,
2233 int policy_alg,
2234 int key_type,
2235 data_t *key_data,
2236 int exercise_alg )
2237{
Ronald Cron5425a212020-08-04 14:58:35 +02002238 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002240 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002241 psa_status_t status;
2242
Gilles Peskine8817f612018-12-18 00:18:46 +01002243 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002244
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002245 psa_set_key_usage_flags( &attributes, policy_usage );
2246 psa_set_key_algorithm( &attributes, policy_alg );
2247 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002248
Gilles Peskine049c7532019-05-15 20:22:09 +02002249 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002250 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002251
Janos Follathba3fab92019-06-11 14:50:16 +01002252 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2253
2254 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2255 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002256 {
Janos Follathba3fab92019-06-11 14:50:16 +01002257 PSA_ASSERT( psa_key_derivation_input_bytes(
2258 &operation,
2259 PSA_KEY_DERIVATION_INPUT_SEED,
2260 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002261 }
Janos Follathba3fab92019-06-11 14:50:16 +01002262
2263 status = psa_key_derivation_input_key( &operation,
2264 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02002265 key );
Janos Follathba3fab92019-06-11 14:50:16 +01002266
Gilles Peskineea0fb492018-07-12 17:17:20 +02002267 if( policy_alg == exercise_alg &&
2268 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002269 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002270 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002271 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002272
2273exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002274 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002275 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002276 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002277}
2278/* END_CASE */
2279
2280/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002281void agreement_key_policy( int policy_usage,
2282 int policy_alg,
2283 int key_type_arg,
2284 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002285 int exercise_alg,
2286 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02002287{
Ronald Cron5425a212020-08-04 14:58:35 +02002288 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002290 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002291 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002292 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002293 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002294
Gilles Peskine8817f612018-12-18 00:18:46 +01002295 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002296
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002297 psa_set_key_usage_flags( &attributes, policy_usage );
2298 psa_set_key_algorithm( &attributes, policy_alg );
2299 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002300
Gilles Peskine049c7532019-05-15 20:22:09 +02002301 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002302 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002303
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002304 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002305 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002306
Steven Cooremance48e852020-10-05 16:02:45 +02002307 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002308
2309exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002310 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002311 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002312 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002313}
2314/* END_CASE */
2315
2316/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002317void key_policy_alg2( int key_type_arg, data_t *key_data,
2318 int usage_arg, int alg_arg, int alg2_arg )
2319{
Ronald Cron5425a212020-08-04 14:58:35 +02002320 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002321 psa_key_type_t key_type = key_type_arg;
2322 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2323 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2324 psa_key_usage_t usage = usage_arg;
2325 psa_algorithm_t alg = alg_arg;
2326 psa_algorithm_t alg2 = alg2_arg;
2327
2328 PSA_ASSERT( psa_crypto_init( ) );
2329
2330 psa_set_key_usage_flags( &attributes, usage );
2331 psa_set_key_algorithm( &attributes, alg );
2332 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2333 psa_set_key_type( &attributes, key_type );
2334 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002335 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002336
gabor-mezei-arm98a34352021-06-28 14:05:00 +02002337 /* Update the usage flags to obtain implicit usage flags */
2338 usage = mbedtls_test_update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02002339 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002340 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2341 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2342 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2343
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002344 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002345 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002346 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002347 goto exit;
2348
2349exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002350 /*
2351 * Key attributes may have been returned by psa_get_key_attributes()
2352 * thus reset them as required.
2353 */
2354 psa_reset_key_attributes( &got_attributes );
2355
Ronald Cron5425a212020-08-04 14:58:35 +02002356 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002357 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002358}
2359/* END_CASE */
2360
2361/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002362void raw_agreement_key_policy( int policy_usage,
2363 int policy_alg,
2364 int key_type_arg,
2365 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02002366 int exercise_alg,
2367 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002368{
Ronald Cron5425a212020-08-04 14:58:35 +02002369 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002371 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002372 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002373 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02002374 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002375
2376 PSA_ASSERT( psa_crypto_init( ) );
2377
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002378 psa_set_key_usage_flags( &attributes, policy_usage );
2379 psa_set_key_algorithm( &attributes, policy_alg );
2380 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002381
Gilles Peskine049c7532019-05-15 20:22:09 +02002382 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002383 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002384
Gilles Peskinec18e25f2021-02-12 23:48:20 +01002385 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002386
Steven Cooremance48e852020-10-05 16:02:45 +02002387 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002388
2389exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002390 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002391 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002392 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002393}
2394/* END_CASE */
2395
2396/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002397void copy_success( int source_usage_arg,
2398 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302399 unsigned int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002400 int type_arg, data_t *material,
2401 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002402 int target_usage_arg,
2403 int target_alg_arg, int target_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302404 unsigned int target_lifetime_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002405 int expected_usage_arg,
2406 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002407{
Gilles Peskineca25db92019-04-19 11:43:08 +02002408 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2409 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002410 psa_key_usage_t expected_usage = expected_usage_arg;
2411 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002412 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Archana8a180362021-07-05 02:18:48 +05302413 psa_key_lifetime_t source_lifetime = source_lifetime_arg;
2414 psa_key_lifetime_t target_lifetime = target_lifetime_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02002415 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2416 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002417 uint8_t *export_buffer = NULL;
2418
Gilles Peskine57ab7212019-01-28 13:03:09 +01002419 PSA_ASSERT( psa_crypto_init( ) );
2420
Gilles Peskineca25db92019-04-19 11:43:08 +02002421 /* Prepare the source key. */
2422 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2423 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002424 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002425 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302426 psa_set_key_lifetime( &source_attributes, source_lifetime);
Gilles Peskine049c7532019-05-15 20:22:09 +02002427 PSA_ASSERT( psa_import_key( &source_attributes,
2428 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002429 &source_key ) );
2430 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002431
Gilles Peskineca25db92019-04-19 11:43:08 +02002432 /* Prepare the target attributes. */
2433 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02002434 {
Gilles Peskineca25db92019-04-19 11:43:08 +02002435 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02002436 }
Archana8a180362021-07-05 02:18:48 +05302437 psa_set_key_lifetime( &target_attributes, target_lifetime);
Ronald Cron65f38a32020-10-23 17:11:13 +02002438
Gilles Peskineca25db92019-04-19 11:43:08 +02002439 if( target_usage_arg != -1 )
2440 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2441 if( target_alg_arg != -1 )
2442 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002443 if( target_alg2_arg != -1 )
2444 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002445
Archana8a180362021-07-05 02:18:48 +05302446
Gilles Peskine57ab7212019-01-28 13:03:09 +01002447 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002448 PSA_ASSERT( psa_copy_key( source_key,
2449 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002450
2451 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02002452 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002453
2454 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02002455 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002456 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2457 psa_get_key_type( &target_attributes ) );
2458 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2459 psa_get_key_bits( &target_attributes ) );
2460 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2461 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002462 TEST_EQUAL( expected_alg2,
2463 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002464 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2465 {
2466 size_t length;
2467 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02002468 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002469 material->len, &length ) );
2470 ASSERT_COMPARE( material->x, material->len,
2471 export_buffer, length );
2472 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01002473
Archana8a180362021-07-05 02:18:48 +05302474 if( !psa_key_lifetime_is_external( target_lifetime ) )
2475 {
2476 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
2477 goto exit;
2478 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
2479 goto exit;
2480 }
Gilles Peskine57ab7212019-01-28 13:03:09 +01002481
Ronald Cron5425a212020-08-04 14:58:35 +02002482 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002483
2484exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002485 /*
2486 * Source and target key attributes may have been returned by
2487 * psa_get_key_attributes() thus reset them as required.
2488 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002489 psa_reset_key_attributes( &source_attributes );
2490 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01002491
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002492 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002493 mbedtls_free( export_buffer );
2494}
2495/* END_CASE */
2496
2497/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002498void copy_fail( int source_usage_arg,
2499 int source_alg_arg, int source_alg2_arg,
Archana8a180362021-07-05 02:18:48 +05302500 int source_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002501 int type_arg, data_t *material,
2502 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002503 int target_usage_arg,
2504 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02002505 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002506 int expected_status_arg )
2507{
2508 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2509 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02002510 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
2511 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02002512 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002513
2514 PSA_ASSERT( psa_crypto_init( ) );
2515
2516 /* Prepare the source key. */
2517 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2518 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002519 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002520 psa_set_key_type( &source_attributes, type_arg );
Archana8a180362021-07-05 02:18:48 +05302521 psa_set_key_lifetime( &source_attributes, source_lifetime_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002522 PSA_ASSERT( psa_import_key( &source_attributes,
2523 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02002524 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002525
2526 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02002527 psa_set_key_id( &target_attributes, key_id );
2528 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002529 psa_set_key_type( &target_attributes, target_type_arg );
2530 psa_set_key_bits( &target_attributes, target_bits_arg );
2531 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2532 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002533 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002534
2535 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02002536 TEST_EQUAL( psa_copy_key( source_key,
2537 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02002538 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002539
Ronald Cron5425a212020-08-04 14:58:35 +02002540 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002541
Gilles Peskine4a644642019-05-03 17:14:08 +02002542exit:
2543 psa_reset_key_attributes( &source_attributes );
2544 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002545 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002546}
2547/* END_CASE */
2548
2549/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002550void hash_operation_init( )
2551{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002552 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002553 /* Test each valid way of initializing the object, except for `= {0}`, as
2554 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2555 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002556 * to suppress the Clang warning for the test. */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002557 psa_hash_operation_t func = psa_hash_operation_init( );
2558 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2559 psa_hash_operation_t zero;
2560
2561 memset( &zero, 0, sizeof( zero ) );
2562
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002563 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002564 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2565 PSA_ERROR_BAD_STATE );
2566 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2567 PSA_ERROR_BAD_STATE );
2568 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2569 PSA_ERROR_BAD_STATE );
2570
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002571 /* A default hash operation should be abortable without error. */
2572 PSA_ASSERT( psa_hash_abort( &func ) );
2573 PSA_ASSERT( psa_hash_abort( &init ) );
2574 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002575}
2576/* END_CASE */
2577
2578/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002579void hash_setup( int alg_arg,
2580 int expected_status_arg )
2581{
2582 psa_algorithm_t alg = alg_arg;
Neil Armstrongedb20862022-02-07 15:47:44 +01002583 uint8_t *output = NULL;
2584 size_t output_size = 0;
2585 size_t output_length = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002586 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002587 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002588 psa_status_t status;
2589
Gilles Peskine8817f612018-12-18 00:18:46 +01002590 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002591
Neil Armstrongedb20862022-02-07 15:47:44 +01002592 /* Hash Setup, one-shot */
Neil Armstrongee9686b2022-02-25 15:47:34 +01002593 output_size = PSA_HASH_LENGTH( alg );
Neil Armstrongedb20862022-02-07 15:47:44 +01002594 ASSERT_ALLOC( output, output_size );
2595
2596 status = psa_hash_compute( alg, NULL, 0,
2597 output, output_size, &output_length );
2598 TEST_EQUAL( status, expected_status );
2599
2600 /* Hash Setup, multi-part */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002601 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002602 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002603
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002604 /* Whether setup succeeded or failed, abort must succeed. */
2605 PSA_ASSERT( psa_hash_abort( &operation ) );
2606
2607 /* If setup failed, reproduce the failure, so as to
2608 * test the resulting state of the operation object. */
2609 if( status != PSA_SUCCESS )
2610 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2611
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002612 /* Now the operation object should be reusable. */
2613#if defined(KNOWN_SUPPORTED_HASH_ALG)
2614 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2615 PSA_ASSERT( psa_hash_abort( &operation ) );
2616#endif
2617
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002618exit:
Neil Armstrongedb20862022-02-07 15:47:44 +01002619 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002620 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002621}
2622/* END_CASE */
2623
2624/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002625void hash_compute_fail( int alg_arg, data_t *input,
2626 int output_size_arg, int expected_status_arg )
2627{
2628 psa_algorithm_t alg = alg_arg;
2629 uint8_t *output = NULL;
2630 size_t output_size = output_size_arg;
2631 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002632 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002633 psa_status_t expected_status = expected_status_arg;
2634 psa_status_t status;
2635
2636 ASSERT_ALLOC( output, output_size );
2637
2638 PSA_ASSERT( psa_crypto_init( ) );
2639
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002640 /* Hash Compute, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002641 status = psa_hash_compute( alg, input->x, input->len,
2642 output, output_size, &output_length );
2643 TEST_EQUAL( status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02002644 TEST_LE_U( output_length, output_size );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002645
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002646 /* Hash Compute, multi-part */
2647 status = psa_hash_setup( &operation, alg );
2648 if( status == PSA_SUCCESS )
2649 {
2650 status = psa_hash_update( &operation, input->x, input->len );
2651 if( status == PSA_SUCCESS )
2652 {
2653 status = psa_hash_finish( &operation, output, output_size,
2654 &output_length );
2655 if( status == PSA_SUCCESS )
Gilles Peskine7be11a72022-04-14 00:12:57 +02002656 TEST_LE_U( output_length, output_size );
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002657 else
2658 TEST_EQUAL( status, expected_status );
2659 }
2660 else
2661 {
2662 TEST_EQUAL( status, expected_status );
2663 }
2664 }
2665 else
2666 {
2667 TEST_EQUAL( status, expected_status );
2668 }
2669
Gilles Peskine0a749c82019-11-28 19:33:58 +01002670exit:
Neil Armstrong161ec5c2022-02-07 11:18:45 +01002671 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002672 mbedtls_free( output );
2673 PSA_DONE( );
2674}
2675/* END_CASE */
2676
2677/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002678void hash_compare_fail( int alg_arg, data_t *input,
2679 data_t *reference_hash,
2680 int expected_status_arg )
2681{
2682 psa_algorithm_t alg = alg_arg;
2683 psa_status_t expected_status = expected_status_arg;
Neil Armstrong55a1be12022-02-07 11:23:20 +01002684 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine88e08462020-01-28 20:43:00 +01002685 psa_status_t status;
2686
2687 PSA_ASSERT( psa_crypto_init( ) );
2688
Neil Armstrong55a1be12022-02-07 11:23:20 +01002689 /* Hash Compare, one-shot */
Gilles Peskine88e08462020-01-28 20:43:00 +01002690 status = psa_hash_compare( alg, input->x, input->len,
2691 reference_hash->x, reference_hash->len );
2692 TEST_EQUAL( status, expected_status );
2693
Neil Armstrong55a1be12022-02-07 11:23:20 +01002694 /* Hash Compare, multi-part */
2695 status = psa_hash_setup( &operation, alg );
2696 if( status == PSA_SUCCESS )
2697 {
2698 status = psa_hash_update( &operation, input->x, input->len );
2699 if( status == PSA_SUCCESS )
2700 {
2701 status = psa_hash_verify( &operation, reference_hash->x,
2702 reference_hash->len );
2703 TEST_EQUAL( status, expected_status );
2704 }
2705 else
2706 {
2707 TEST_EQUAL( status, expected_status );
2708 }
2709 }
2710 else
2711 {
2712 TEST_EQUAL( status, expected_status );
2713 }
2714
Gilles Peskine88e08462020-01-28 20:43:00 +01002715exit:
Neil Armstrong55a1be12022-02-07 11:23:20 +01002716 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine88e08462020-01-28 20:43:00 +01002717 PSA_DONE( );
2718}
2719/* END_CASE */
2720
2721/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002722void hash_compute_compare( int alg_arg, data_t *input,
2723 data_t *expected_output )
2724{
2725 psa_algorithm_t alg = alg_arg;
2726 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2727 size_t output_length = INVALID_EXPORT_LENGTH;
Neil Armstrongca30a002022-02-07 11:40:23 +01002728 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine0a749c82019-11-28 19:33:58 +01002729 size_t i;
2730
2731 PSA_ASSERT( psa_crypto_init( ) );
2732
Neil Armstrongca30a002022-02-07 11:40:23 +01002733 /* Compute with tight buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002734 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002735 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01002736 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002737 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002738 ASSERT_COMPARE( output, output_length,
2739 expected_output->x, expected_output->len );
2740
Neil Armstrongca30a002022-02-07 11:40:23 +01002741 /* Compute with tight buffer, multi-part */
2742 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2743 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2744 PSA_ASSERT( psa_hash_finish( &operation, output,
2745 PSA_HASH_LENGTH( alg ),
2746 &output_length ) );
2747 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2748 ASSERT_COMPARE( output, output_length,
2749 expected_output->x, expected_output->len );
2750
2751 /* Compute with larger buffer, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002752 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2753 output, sizeof( output ),
2754 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002755 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002756 ASSERT_COMPARE( output, output_length,
2757 expected_output->x, expected_output->len );
2758
Neil Armstrongca30a002022-02-07 11:40:23 +01002759 /* Compute with larger buffer, multi-part */
2760 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2761 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2762 PSA_ASSERT( psa_hash_finish( &operation, output,
2763 sizeof( output ), &output_length ) );
2764 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
2765 ASSERT_COMPARE( output, output_length,
2766 expected_output->x, expected_output->len );
2767
2768 /* Compare with correct hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002769 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2770 output, output_length ) );
2771
Neil Armstrongca30a002022-02-07 11:40:23 +01002772 /* Compare with correct hash, multi-part */
2773 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2774 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2775 PSA_ASSERT( psa_hash_verify( &operation, output,
2776 output_length ) );
2777
2778 /* Compare with trailing garbage, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002779 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2780 output, output_length + 1 ),
2781 PSA_ERROR_INVALID_SIGNATURE );
2782
Neil Armstrongca30a002022-02-07 11:40:23 +01002783 /* Compare with trailing garbage, multi-part */
2784 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2785 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2786 TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ),
2787 PSA_ERROR_INVALID_SIGNATURE );
2788
2789 /* Compare with truncated hash, one-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002790 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2791 output, output_length - 1 ),
2792 PSA_ERROR_INVALID_SIGNATURE );
2793
Neil Armstrongca30a002022-02-07 11:40:23 +01002794 /* Compare with truncated hash, multi-part */
2795 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2796 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2797 TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ),
2798 PSA_ERROR_INVALID_SIGNATURE );
2799
Gilles Peskine0a749c82019-11-28 19:33:58 +01002800 /* Compare with corrupted value */
2801 for( i = 0; i < output_length; i++ )
2802 {
Chris Jones9634bb12021-01-20 15:56:42 +00002803 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002804 output[i] ^= 1;
Neil Armstrongca30a002022-02-07 11:40:23 +01002805
2806 /* One-shot */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002807 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2808 output, output_length ),
2809 PSA_ERROR_INVALID_SIGNATURE );
Neil Armstrongca30a002022-02-07 11:40:23 +01002810
2811 /* Multi-Part */
2812 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2813 PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) );
2814 TEST_EQUAL( psa_hash_verify( &operation, output, output_length ),
2815 PSA_ERROR_INVALID_SIGNATURE );
2816
Gilles Peskine0a749c82019-11-28 19:33:58 +01002817 output[i] ^= 1;
2818 }
2819
2820exit:
Neil Armstrongca30a002022-02-07 11:40:23 +01002821 PSA_ASSERT( psa_hash_abort( &operation ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002822 PSA_DONE( );
2823}
2824/* END_CASE */
2825
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002826/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02002827void hash_bad_order( )
2828{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002829 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002830 unsigned char input[] = "";
2831 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002832 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002833 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2834 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2835 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002836 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002837 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002838 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002839
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002841
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002842 /* Call setup twice in a row. */
2843 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002844 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002845 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2846 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002847 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002848 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002849 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002850
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002851 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002852 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002853 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002854 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002855
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002856 /* Check that update calls abort on error. */
2857 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01002858 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002859 ASSERT_OPERATION_IS_ACTIVE( operation );
2860 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
2861 PSA_ERROR_BAD_STATE );
2862 ASSERT_OPERATION_IS_INACTIVE( operation );
2863 PSA_ASSERT( psa_hash_abort( &operation ) );
2864 ASSERT_OPERATION_IS_INACTIVE( operation );
2865
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002866 /* Call update after finish. */
2867 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2868 PSA_ASSERT( psa_hash_finish( &operation,
2869 hash, sizeof( hash ), &hash_len ) );
2870 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002871 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002872 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002873
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002874 /* Call verify without calling setup beforehand. */
2875 TEST_EQUAL( psa_hash_verify( &operation,
2876 valid_hash, sizeof( valid_hash ) ),
2877 PSA_ERROR_BAD_STATE );
2878 PSA_ASSERT( psa_hash_abort( &operation ) );
2879
2880 /* Call verify after finish. */
2881 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2882 PSA_ASSERT( psa_hash_finish( &operation,
2883 hash, sizeof( hash ), &hash_len ) );
2884 TEST_EQUAL( psa_hash_verify( &operation,
2885 valid_hash, sizeof( valid_hash ) ),
2886 PSA_ERROR_BAD_STATE );
2887 PSA_ASSERT( psa_hash_abort( &operation ) );
2888
2889 /* Call verify twice in a row. */
2890 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002891 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002892 PSA_ASSERT( psa_hash_verify( &operation,
2893 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002894 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002895 TEST_EQUAL( psa_hash_verify( &operation,
2896 valid_hash, sizeof( valid_hash ) ),
2897 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002898 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002899 PSA_ASSERT( psa_hash_abort( &operation ) );
2900
2901 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002902 TEST_EQUAL( psa_hash_finish( &operation,
2903 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002904 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002905 PSA_ASSERT( psa_hash_abort( &operation ) );
2906
2907 /* Call finish twice in a row. */
2908 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2909 PSA_ASSERT( psa_hash_finish( &operation,
2910 hash, sizeof( hash ), &hash_len ) );
2911 TEST_EQUAL( psa_hash_finish( &operation,
2912 hash, sizeof( hash ), &hash_len ),
2913 PSA_ERROR_BAD_STATE );
2914 PSA_ASSERT( psa_hash_abort( &operation ) );
2915
2916 /* Call finish after calling verify. */
2917 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2918 PSA_ASSERT( psa_hash_verify( &operation,
2919 valid_hash, sizeof( valid_hash ) ) );
2920 TEST_EQUAL( psa_hash_finish( &operation,
2921 hash, sizeof( hash ), &hash_len ),
2922 PSA_ERROR_BAD_STATE );
2923 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002924
2925exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002926 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002927}
2928/* END_CASE */
2929
Gilles Peskined6dc40c2021-01-12 12:55:31 +01002930/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02002931void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002932{
2933 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002934 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2935 * appended to it */
2936 unsigned char hash[] = {
2937 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2938 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2939 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002940 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002941 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002942
Gilles Peskine8817f612018-12-18 00:18:46 +01002943 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002944
itayzafrir27e69452018-11-01 14:26:34 +02002945 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002946 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002947 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002948 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002949 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002950 ASSERT_OPERATION_IS_INACTIVE( operation );
2951 PSA_ASSERT( psa_hash_abort( &operation ) );
2952 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03002953
itayzafrir27e69452018-11-01 14:26:34 +02002954 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002955 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002956 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002957 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002958
itayzafrir27e69452018-11-01 14:26:34 +02002959 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002960 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002961 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002962 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002963
itayzafrirec93d302018-10-18 18:01:10 +03002964exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002965 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002966}
2967/* END_CASE */
2968
Ronald Cronee414c72021-03-18 18:50:08 +01002969/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002970void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002971{
2972 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002973 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002974 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002975 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002976 size_t hash_len;
2977
Gilles Peskine8817f612018-12-18 00:18:46 +01002978 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002979
itayzafrir58028322018-10-25 10:22:01 +03002980 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002981 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002982 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002983 hash, expected_size - 1, &hash_len ),
2984 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002985
2986exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002987 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002988}
2989/* END_CASE */
2990
Ronald Cronee414c72021-03-18 18:50:08 +01002991/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002992void hash_clone_source_state( )
2993{
2994 psa_algorithm_t alg = PSA_ALG_SHA_256;
2995 unsigned char hash[PSA_HASH_MAX_SIZE];
2996 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2997 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2998 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2999 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3000 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3001 size_t hash_len;
3002
3003 PSA_ASSERT( psa_crypto_init( ) );
3004 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
3005
3006 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3007 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3008 PSA_ASSERT( psa_hash_finish( &op_finished,
3009 hash, sizeof( hash ), &hash_len ) );
3010 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3011 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3012
3013 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
3014 PSA_ERROR_BAD_STATE );
3015
3016 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
3017 PSA_ASSERT( psa_hash_finish( &op_init,
3018 hash, sizeof( hash ), &hash_len ) );
3019 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
3020 PSA_ASSERT( psa_hash_finish( &op_finished,
3021 hash, sizeof( hash ), &hash_len ) );
3022 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
3023 PSA_ASSERT( psa_hash_finish( &op_aborted,
3024 hash, sizeof( hash ), &hash_len ) );
3025
3026exit:
3027 psa_hash_abort( &op_source );
3028 psa_hash_abort( &op_init );
3029 psa_hash_abort( &op_setup );
3030 psa_hash_abort( &op_finished );
3031 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003032 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003033}
3034/* END_CASE */
3035
Ronald Cronee414c72021-03-18 18:50:08 +01003036/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003037void hash_clone_target_state( )
3038{
3039 psa_algorithm_t alg = PSA_ALG_SHA_256;
3040 unsigned char hash[PSA_HASH_MAX_SIZE];
3041 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
3042 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
3043 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
3044 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
3045 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
3046 size_t hash_len;
3047
3048 PSA_ASSERT( psa_crypto_init( ) );
3049
3050 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
3051 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
3052 PSA_ASSERT( psa_hash_finish( &op_finished,
3053 hash, sizeof( hash ), &hash_len ) );
3054 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
3055 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
3056
3057 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
3058 PSA_ASSERT( psa_hash_finish( &op_target,
3059 hash, sizeof( hash ), &hash_len ) );
3060
3061 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
3062 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
3063 PSA_ERROR_BAD_STATE );
3064 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
3065 PSA_ERROR_BAD_STATE );
3066
3067exit:
3068 psa_hash_abort( &op_target );
3069 psa_hash_abort( &op_init );
3070 psa_hash_abort( &op_setup );
3071 psa_hash_abort( &op_finished );
3072 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003073 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01003074}
3075/* END_CASE */
3076
itayzafrir58028322018-10-25 10:22:01 +03003077/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00003078void mac_operation_init( )
3079{
Jaeden Amero252ef282019-02-15 14:05:35 +00003080 const uint8_t input[1] = { 0 };
3081
Jaeden Amero769ce272019-01-04 11:48:03 +00003082 /* Test each valid way of initializing the object, except for `= {0}`, as
3083 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3084 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003085 * to suppress the Clang warning for the test. */
Jaeden Amero769ce272019-01-04 11:48:03 +00003086 psa_mac_operation_t func = psa_mac_operation_init( );
3087 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
3088 psa_mac_operation_t zero;
3089
3090 memset( &zero, 0, sizeof( zero ) );
3091
Jaeden Amero252ef282019-02-15 14:05:35 +00003092 /* A freshly-initialized MAC operation should not be usable. */
3093 TEST_EQUAL( psa_mac_update( &func,
3094 input, sizeof( input ) ),
3095 PSA_ERROR_BAD_STATE );
3096 TEST_EQUAL( psa_mac_update( &init,
3097 input, sizeof( input ) ),
3098 PSA_ERROR_BAD_STATE );
3099 TEST_EQUAL( psa_mac_update( &zero,
3100 input, sizeof( input ) ),
3101 PSA_ERROR_BAD_STATE );
3102
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003103 /* A default MAC operation should be abortable without error. */
3104 PSA_ASSERT( psa_mac_abort( &func ) );
3105 PSA_ASSERT( psa_mac_abort( &init ) );
3106 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00003107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003111void mac_setup( int key_type_arg,
3112 data_t *key,
3113 int alg_arg,
3114 int expected_status_arg )
3115{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003116 psa_key_type_t key_type = key_type_arg;
3117 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003118 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003119 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003120 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
3121#if defined(KNOWN_SUPPORTED_MAC_ALG)
3122 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3123#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003124
Gilles Peskine8817f612018-12-18 00:18:46 +01003125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003126
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003127 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
3128 &operation, &status ) )
3129 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003130 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003131
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003132 /* The operation object should be reusable. */
3133#if defined(KNOWN_SUPPORTED_MAC_ALG)
3134 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
3135 smoke_test_key_data,
3136 sizeof( smoke_test_key_data ),
3137 KNOWN_SUPPORTED_MAC_ALG,
3138 &operation, &status ) )
3139 goto exit;
3140 TEST_EQUAL( status, PSA_SUCCESS );
3141#endif
3142
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003143exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003144 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003145}
3146/* END_CASE */
3147
Gilles Peskined6dc40c2021-01-12 12:55:31 +01003148/* 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 +00003149void mac_bad_order( )
3150{
Ronald Cron5425a212020-08-04 14:58:35 +02003151 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003152 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
3153 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02003154 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00003155 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3156 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3157 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00003159 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3160 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
3161 size_t sign_mac_length = 0;
3162 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
3163 const uint8_t verify_mac[] = {
3164 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
3165 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
3166 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
3167
3168 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003169 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003170 psa_set_key_algorithm( &attributes, alg );
3171 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003172
Ronald Cron5425a212020-08-04 14:58:35 +02003173 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3174 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003175
Jaeden Amero252ef282019-02-15 14:05:35 +00003176 /* Call update without calling setup beforehand. */
3177 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3178 PSA_ERROR_BAD_STATE );
3179 PSA_ASSERT( psa_mac_abort( &operation ) );
3180
3181 /* Call sign finish without calling setup beforehand. */
3182 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
3183 &sign_mac_length),
3184 PSA_ERROR_BAD_STATE );
3185 PSA_ASSERT( psa_mac_abort( &operation ) );
3186
3187 /* Call verify finish without calling setup beforehand. */
3188 TEST_EQUAL( psa_mac_verify_finish( &operation,
3189 verify_mac, sizeof( verify_mac ) ),
3190 PSA_ERROR_BAD_STATE );
3191 PSA_ASSERT( psa_mac_abort( &operation ) );
3192
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003193 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003194 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003195 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003196 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003197 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003198 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003199 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003200 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003201
Jaeden Amero252ef282019-02-15 14:05:35 +00003202 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003203 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003204 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3205 PSA_ASSERT( psa_mac_sign_finish( &operation,
3206 sign_mac, sizeof( sign_mac ),
3207 &sign_mac_length ) );
3208 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3209 PSA_ERROR_BAD_STATE );
3210 PSA_ASSERT( psa_mac_abort( &operation ) );
3211
3212 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003213 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003214 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3215 PSA_ASSERT( psa_mac_verify_finish( &operation,
3216 verify_mac, sizeof( verify_mac ) ) );
3217 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
3218 PSA_ERROR_BAD_STATE );
3219 PSA_ASSERT( psa_mac_abort( &operation ) );
3220
3221 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003222 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003223 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3224 PSA_ASSERT( psa_mac_sign_finish( &operation,
3225 sign_mac, sizeof( sign_mac ),
3226 &sign_mac_length ) );
3227 TEST_EQUAL( psa_mac_sign_finish( &operation,
3228 sign_mac, sizeof( sign_mac ),
3229 &sign_mac_length ),
3230 PSA_ERROR_BAD_STATE );
3231 PSA_ASSERT( psa_mac_abort( &operation ) );
3232
3233 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003234 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003235 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3236 PSA_ASSERT( psa_mac_verify_finish( &operation,
3237 verify_mac, sizeof( verify_mac ) ) );
3238 TEST_EQUAL( psa_mac_verify_finish( &operation,
3239 verify_mac, sizeof( verify_mac ) ),
3240 PSA_ERROR_BAD_STATE );
3241 PSA_ASSERT( psa_mac_abort( &operation ) );
3242
3243 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02003244 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003245 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003246 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003247 TEST_EQUAL( psa_mac_verify_finish( &operation,
3248 verify_mac, sizeof( verify_mac ) ),
3249 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003250 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003251 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003252 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003253
3254 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02003255 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003256 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003257 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003258 TEST_EQUAL( psa_mac_sign_finish( &operation,
3259 sign_mac, sizeof( sign_mac ),
3260 &sign_mac_length ),
3261 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003262 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00003263 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003264 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003265
Ronald Cron5425a212020-08-04 14:58:35 +02003266 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003267
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003268exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003269 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003270}
3271/* END_CASE */
3272
3273/* BEGIN_CASE */
Neil Armstrong4766f992022-02-28 16:23:59 +01003274void mac_sign_verify_multi( int key_type_arg,
3275 data_t *key_data,
3276 int alg_arg,
3277 data_t *input,
3278 int is_verify,
3279 data_t *expected_mac )
3280{
3281 size_t data_part_len = 0;
3282
3283 for( data_part_len = 1; data_part_len <= input->len; data_part_len++ )
3284 {
3285 /* Split data into length(data_part_len) parts. */
3286 mbedtls_test_set_step( 2000 + data_part_len );
3287
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003288 if( mac_multipart_internal_func( key_type_arg, key_data,
3289 alg_arg,
3290 input, data_part_len,
3291 expected_mac,
3292 is_verify, 0 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003293 break;
3294
3295 /* length(0) part, length(data_part_len) part, length(0) part... */
3296 mbedtls_test_set_step( 3000 + data_part_len );
3297
Neil Armstrongfe6da1c2022-03-03 16:29:14 +01003298 if( mac_multipart_internal_func( key_type_arg, key_data,
3299 alg_arg,
3300 input, data_part_len,
3301 expected_mac,
3302 is_verify, 1 ) == 0 )
Neil Armstrong4766f992022-02-28 16:23:59 +01003303 break;
3304 }
3305
3306 /* Goto is required to silence warnings about unused labels, as we
3307 * don't actually do any test assertions in this function. */
3308 goto exit;
3309}
3310/* END_CASE */
3311
3312/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003313void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003314 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003315 int alg_arg,
3316 data_t *input,
3317 data_t *expected_mac )
3318{
Ronald Cron5425a212020-08-04 14:58:35 +02003319 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003320 psa_key_type_t key_type = key_type_arg;
3321 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003322 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003323 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003324 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003325 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003326 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003327 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003328 const size_t output_sizes_to_test[] = {
3329 0,
3330 1,
3331 expected_mac->len - 1,
3332 expected_mac->len,
3333 expected_mac->len + 1,
3334 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003335
Gilles Peskine7be11a72022-04-14 00:12:57 +02003336 TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003337 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02003338 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003339
Gilles Peskine8817f612018-12-18 00:18:46 +01003340 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003341
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003343 psa_set_key_algorithm( &attributes, alg );
3344 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003345
Ronald Cron5425a212020-08-04 14:58:35 +02003346 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3347 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003348
Gilles Peskine8b356b52020-08-25 23:44:59 +02003349 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3350 {
3351 const size_t output_size = output_sizes_to_test[i];
3352 psa_status_t expected_status =
3353 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3354 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003355
Chris Jones9634bb12021-01-20 15:56:42 +00003356 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003357 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003358
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003359 /* Calculate the MAC, one-shot case. */
3360 TEST_EQUAL( psa_mac_compute( key, alg,
3361 input->x, input->len,
3362 actual_mac, output_size, &mac_length ),
3363 expected_status );
3364 if( expected_status == PSA_SUCCESS )
3365 {
3366 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3367 actual_mac, mac_length );
3368 }
3369
3370 if( output_size > 0 )
3371 memset( actual_mac, 0, output_size );
3372
3373 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003374 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02003375 PSA_ASSERT( psa_mac_update( &operation,
3376 input->x, input->len ) );
3377 TEST_EQUAL( psa_mac_sign_finish( &operation,
3378 actual_mac, output_size,
3379 &mac_length ),
3380 expected_status );
3381 PSA_ASSERT( psa_mac_abort( &operation ) );
3382
3383 if( expected_status == PSA_SUCCESS )
3384 {
3385 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3386 actual_mac, mac_length );
3387 }
3388 mbedtls_free( actual_mac );
3389 actual_mac = NULL;
3390 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003391
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003392exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003393 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003394 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003395 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003396 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003397}
3398/* END_CASE */
3399
3400/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003401void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02003402 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003403 int alg_arg,
3404 data_t *input,
3405 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003406{
Ronald Cron5425a212020-08-04 14:58:35 +02003407 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003408 psa_key_type_t key_type = key_type_arg;
3409 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003410 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003412 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003413
Gilles Peskine7be11a72022-04-14 00:12:57 +02003414 TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003415
Gilles Peskine8817f612018-12-18 00:18:46 +01003416 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003417
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003418 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003419 psa_set_key_algorithm( &attributes, alg );
3420 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003421
Ronald Cron5425a212020-08-04 14:58:35 +02003422 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3423 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003424
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003425 /* Verify correct MAC, one-shot case. */
3426 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
3427 expected_mac->x, expected_mac->len ) );
3428
3429 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003430 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003431 PSA_ASSERT( psa_mac_update( &operation,
3432 input->x, input->len ) );
3433 PSA_ASSERT( psa_mac_verify_finish( &operation,
3434 expected_mac->x,
3435 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003436
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003437 /* Test a MAC that's too short, one-shot case. */
3438 TEST_EQUAL( psa_mac_verify( key, alg,
3439 input->x, input->len,
3440 expected_mac->x,
3441 expected_mac->len - 1 ),
3442 PSA_ERROR_INVALID_SIGNATURE );
3443
3444 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003445 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003446 PSA_ASSERT( psa_mac_update( &operation,
3447 input->x, input->len ) );
3448 TEST_EQUAL( psa_mac_verify_finish( &operation,
3449 expected_mac->x,
3450 expected_mac->len - 1 ),
3451 PSA_ERROR_INVALID_SIGNATURE );
3452
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003453 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003454 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3455 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003456 TEST_EQUAL( psa_mac_verify( key, alg,
3457 input->x, input->len,
3458 perturbed_mac, expected_mac->len + 1 ),
3459 PSA_ERROR_INVALID_SIGNATURE );
3460
3461 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02003462 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003463 PSA_ASSERT( psa_mac_update( &operation,
3464 input->x, input->len ) );
3465 TEST_EQUAL( psa_mac_verify_finish( &operation,
3466 perturbed_mac,
3467 expected_mac->len + 1 ),
3468 PSA_ERROR_INVALID_SIGNATURE );
3469
3470 /* Test changing one byte. */
3471 for( size_t i = 0; i < expected_mac->len; i++ )
3472 {
Chris Jones9634bb12021-01-20 15:56:42 +00003473 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003474 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01003475
3476 TEST_EQUAL( psa_mac_verify( key, alg,
3477 input->x, input->len,
3478 perturbed_mac, expected_mac->len ),
3479 PSA_ERROR_INVALID_SIGNATURE );
3480
Ronald Cron5425a212020-08-04 14:58:35 +02003481 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003482 PSA_ASSERT( psa_mac_update( &operation,
3483 input->x, input->len ) );
3484 TEST_EQUAL( psa_mac_verify_finish( &operation,
3485 perturbed_mac,
3486 expected_mac->len ),
3487 PSA_ERROR_INVALID_SIGNATURE );
3488 perturbed_mac[i] ^= 1;
3489 }
3490
Gilles Peskine8c9def32018-02-08 10:02:12 +01003491exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003492 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003493 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003494 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003495 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003496}
3497/* END_CASE */
3498
3499/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003500void cipher_operation_init( )
3501{
Jaeden Ameroab439972019-02-15 14:12:05 +00003502 const uint8_t input[1] = { 0 };
3503 unsigned char output[1] = { 0 };
3504 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003505 /* Test each valid way of initializing the object, except for `= {0}`, as
3506 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3507 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08003508 * to suppress the Clang warning for the test. */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003509 psa_cipher_operation_t func = psa_cipher_operation_init( );
3510 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3511 psa_cipher_operation_t zero;
3512
3513 memset( &zero, 0, sizeof( zero ) );
3514
Jaeden Ameroab439972019-02-15 14:12:05 +00003515 /* A freshly-initialized cipher operation should not be usable. */
3516 TEST_EQUAL( psa_cipher_update( &func,
3517 input, sizeof( input ),
3518 output, sizeof( output ),
3519 &output_length ),
3520 PSA_ERROR_BAD_STATE );
3521 TEST_EQUAL( psa_cipher_update( &init,
3522 input, sizeof( input ),
3523 output, sizeof( output ),
3524 &output_length ),
3525 PSA_ERROR_BAD_STATE );
3526 TEST_EQUAL( psa_cipher_update( &zero,
3527 input, sizeof( input ),
3528 output, sizeof( output ),
3529 &output_length ),
3530 PSA_ERROR_BAD_STATE );
3531
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003532 /* A default cipher operation should be abortable without error. */
3533 PSA_ASSERT( psa_cipher_abort( &func ) );
3534 PSA_ASSERT( psa_cipher_abort( &init ) );
3535 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003536}
3537/* END_CASE */
3538
3539/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003540void cipher_setup( int key_type_arg,
3541 data_t *key,
3542 int alg_arg,
3543 int expected_status_arg )
3544{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003545 psa_key_type_t key_type = key_type_arg;
3546 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003547 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003548 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003549 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01003550#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003551 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3552#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003553
Gilles Peskine8817f612018-12-18 00:18:46 +01003554 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003555
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003556 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3557 &operation, &status ) )
3558 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003559 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003560
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003561 /* The operation object should be reusable. */
3562#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3563 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3564 smoke_test_key_data,
3565 sizeof( smoke_test_key_data ),
3566 KNOWN_SUPPORTED_CIPHER_ALG,
3567 &operation, &status ) )
3568 goto exit;
3569 TEST_EQUAL( status, PSA_SUCCESS );
3570#endif
3571
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003572exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003573 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003574 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003575}
3576/* END_CASE */
3577
Ronald Cronee414c72021-03-18 18:50:08 +01003578/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00003579void cipher_bad_order( )
3580{
Ronald Cron5425a212020-08-04 14:58:35 +02003581 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003582 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3583 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003585 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003586 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02003587 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00003588 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3589 0xaa, 0xaa, 0xaa, 0xaa };
3590 const uint8_t text[] = {
3591 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3592 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003593 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00003594 size_t length = 0;
3595
3596 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003597 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3598 psa_set_key_algorithm( &attributes, alg );
3599 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02003600 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
3601 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003602
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003603 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003604 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003605 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003606 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003607 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003608 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003609 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003610 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003611
3612 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003613 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003614 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02003615 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003616 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003617 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003618 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01003619 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003620
Jaeden Ameroab439972019-02-15 14:12:05 +00003621 /* Generate an IV without calling setup beforehand. */
3622 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3623 buffer, sizeof( buffer ),
3624 &length ),
3625 PSA_ERROR_BAD_STATE );
3626 PSA_ASSERT( psa_cipher_abort( &operation ) );
3627
3628 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003629 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003630 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3631 buffer, sizeof( buffer ),
3632 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003633 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003634 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3635 buffer, sizeof( buffer ),
3636 &length ),
3637 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003638 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003639 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003640 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003641
3642 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003643 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003644 PSA_ASSERT( psa_cipher_set_iv( &operation,
3645 iv, sizeof( iv ) ) );
3646 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3647 buffer, sizeof( buffer ),
3648 &length ),
3649 PSA_ERROR_BAD_STATE );
3650 PSA_ASSERT( psa_cipher_abort( &operation ) );
3651
3652 /* Set an IV without calling setup beforehand. */
3653 TEST_EQUAL( psa_cipher_set_iv( &operation,
3654 iv, sizeof( iv ) ),
3655 PSA_ERROR_BAD_STATE );
3656 PSA_ASSERT( psa_cipher_abort( &operation ) );
3657
3658 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02003659 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003660 PSA_ASSERT( psa_cipher_set_iv( &operation,
3661 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003662 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003663 TEST_EQUAL( psa_cipher_set_iv( &operation,
3664 iv, sizeof( iv ) ),
3665 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003666 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003667 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003668 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003669
3670 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02003671 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003672 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3673 buffer, sizeof( buffer ),
3674 &length ) );
3675 TEST_EQUAL( psa_cipher_set_iv( &operation,
3676 iv, sizeof( iv ) ),
3677 PSA_ERROR_BAD_STATE );
3678 PSA_ASSERT( psa_cipher_abort( &operation ) );
3679
3680 /* Call update without calling setup beforehand. */
3681 TEST_EQUAL( psa_cipher_update( &operation,
3682 text, sizeof( text ),
3683 buffer, sizeof( buffer ),
3684 &length ),
3685 PSA_ERROR_BAD_STATE );
3686 PSA_ASSERT( psa_cipher_abort( &operation ) );
3687
3688 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01003689 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003690 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003691 TEST_EQUAL( psa_cipher_update( &operation,
3692 text, sizeof( text ),
3693 buffer, sizeof( buffer ),
3694 &length ),
3695 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003696 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003697 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003698 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003699
3700 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02003701 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003702 PSA_ASSERT( psa_cipher_set_iv( &operation,
3703 iv, sizeof( iv ) ) );
3704 PSA_ASSERT( psa_cipher_finish( &operation,
3705 buffer, sizeof( buffer ), &length ) );
3706 TEST_EQUAL( psa_cipher_update( &operation,
3707 text, sizeof( text ),
3708 buffer, sizeof( buffer ),
3709 &length ),
3710 PSA_ERROR_BAD_STATE );
3711 PSA_ASSERT( psa_cipher_abort( &operation ) );
3712
3713 /* Call finish without calling setup beforehand. */
3714 TEST_EQUAL( psa_cipher_finish( &operation,
3715 buffer, sizeof( buffer ), &length ),
3716 PSA_ERROR_BAD_STATE );
3717 PSA_ASSERT( psa_cipher_abort( &operation ) );
3718
3719 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02003720 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003721 /* Not calling update means we are encrypting an empty buffer, which is OK
3722 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01003723 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003724 TEST_EQUAL( psa_cipher_finish( &operation,
3725 buffer, sizeof( buffer ), &length ),
3726 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01003727 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003728 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01003729 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00003730
3731 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02003732 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003733 PSA_ASSERT( psa_cipher_set_iv( &operation,
3734 iv, sizeof( iv ) ) );
3735 PSA_ASSERT( psa_cipher_finish( &operation,
3736 buffer, sizeof( buffer ), &length ) );
3737 TEST_EQUAL( psa_cipher_finish( &operation,
3738 buffer, sizeof( buffer ), &length ),
3739 PSA_ERROR_BAD_STATE );
3740 PSA_ASSERT( psa_cipher_abort( &operation ) );
3741
Ronald Cron5425a212020-08-04 14:58:35 +02003742 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02003743
Jaeden Ameroab439972019-02-15 14:12:05 +00003744exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003745 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003746 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003747}
3748/* END_CASE */
3749
3750/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02003751void cipher_encrypt_fail( int alg_arg,
3752 int key_type_arg,
3753 data_t *key_data,
3754 data_t *input,
3755 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003756{
Ronald Cron5425a212020-08-04 14:58:35 +02003757 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003758 psa_status_t status;
3759 psa_key_type_t key_type = key_type_arg;
3760 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003761 psa_status_t expected_status = expected_status_arg;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003762 unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0};
3763 size_t iv_size = PSA_CIPHER_IV_MAX_SIZE;
3764 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003765 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003766 size_t output_buffer_size = 0;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003767 size_t output_length = 0;
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003768 size_t function_output_length;
3769 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3771
3772 if ( PSA_ERROR_BAD_STATE != expected_status )
3773 {
3774 PSA_ASSERT( psa_crypto_init( ) );
3775
3776 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3777 psa_set_key_algorithm( &attributes, alg );
3778 psa_set_key_type( &attributes, key_type );
3779
3780 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3781 input->len );
3782 ASSERT_ALLOC( output, output_buffer_size );
3783
3784 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3785 &key ) );
3786 }
3787
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003788 /* Encrypt, one-shot */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003789 status = psa_cipher_encrypt( key, alg, input->x, input->len, output,
3790 output_buffer_size, &output_length );
3791
3792 TEST_EQUAL( status, expected_status );
3793
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003794 /* Encrypt, multi-part */
3795 status = psa_cipher_encrypt_setup( &operation, key, alg );
3796 if( status == PSA_SUCCESS )
3797 {
3798 if( alg != PSA_ALG_ECB_NO_PADDING )
3799 {
3800 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3801 iv, iv_size,
3802 &iv_length ) );
3803 }
3804
3805 status = psa_cipher_update( &operation, input->x, input->len,
3806 output, output_buffer_size,
3807 &function_output_length );
3808 if( status == PSA_SUCCESS )
3809 {
3810 output_length += function_output_length;
3811
3812 status = psa_cipher_finish( &operation, output + output_length,
3813 output_buffer_size - output_length,
3814 &function_output_length );
3815
3816 TEST_EQUAL( status, expected_status );
3817 }
3818 else
3819 {
3820 TEST_EQUAL( status, expected_status );
3821 }
3822 }
3823 else
3824 {
3825 TEST_EQUAL( status, expected_status );
3826 }
3827
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003828exit:
Neil Armstrongd8dba4e2022-02-07 15:19:29 +01003829 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003830 mbedtls_free( output );
3831 psa_destroy_key( key );
3832 PSA_DONE( );
3833}
3834/* END_CASE */
3835
3836/* BEGIN_CASE */
Mateusz Starzyked71e922021-10-21 10:04:57 +02003837void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data,
3838 data_t *input, int iv_length,
3839 int expected_result )
3840{
3841 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3842 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3843 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3844 size_t output_buffer_size = 0;
3845 unsigned char *output = NULL;
3846
3847 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
3848 ASSERT_ALLOC( output, output_buffer_size );
3849
3850 PSA_ASSERT( psa_crypto_init( ) );
3851
3852 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3853 psa_set_key_algorithm( &attributes, alg );
3854 psa_set_key_type( &attributes, key_type );
3855
3856 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3857 &key ) );
3858 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3859 TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output,
3860 iv_length ) );
3861
3862exit:
3863 psa_cipher_abort( &operation );
3864 mbedtls_free( output );
3865 psa_destroy_key( key );
3866 PSA_DONE( );
3867}
3868/* END_CASE */
3869
3870/* BEGIN_CASE */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003871void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data,
3872 data_t *plaintext, data_t *ciphertext )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003873{
3874 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3875 psa_key_type_t key_type = key_type_arg;
3876 psa_algorithm_t alg = alg_arg;
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003877 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3878 uint8_t iv[1] = { 0x5a };
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003879 unsigned char *output = NULL;
3880 size_t output_buffer_size = 0;
Gilles Peskine286c3142022-04-20 17:09:38 +02003881 size_t output_length, length;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003882 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3883
3884 PSA_ASSERT( psa_crypto_init( ) );
3885
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003886 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02003887 TEST_LE_U( ciphertext->len,
3888 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) );
3889 TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003890 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003891 TEST_LE_U( plaintext->len,
3892 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) );
3893 TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ),
3894 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003895
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003896
3897 /* Set up key and output buffer */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003898 psa_set_key_usage_flags( &attributes,
3899 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003900 psa_set_key_algorithm( &attributes, alg );
3901 psa_set_key_type( &attributes, key_type );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003902 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3903 &key ) );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003904 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg,
3905 plaintext->len );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003906 ASSERT_ALLOC( output, output_buffer_size );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003907
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003908 /* set_iv() is not allowed */
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003909 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3910 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
3911 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003912 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3913 TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ),
Ronald Cron6c9bb0f2021-07-15 09:38:11 +02003914 PSA_ERROR_BAD_STATE );
3915
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003916 /* generate_iv() is not allowed */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003917 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3918 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003919 &length ),
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003920 PSA_ERROR_BAD_STATE );
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003921 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3922 TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ),
Gilles Peskine286c3142022-04-20 17:09:38 +02003923 &length ),
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003924 PSA_ERROR_BAD_STATE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003925
Gilles Peskine286c3142022-04-20 17:09:38 +02003926 /* Multipart encryption */
3927 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
3928 output_length = 0;
3929 length = ~0;
3930 PSA_ASSERT( psa_cipher_update( &operation,
3931 plaintext->x, plaintext->len,
3932 output, output_buffer_size,
3933 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003934 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003935 output_length += length;
3936 PSA_ASSERT( psa_cipher_finish( &operation,
3937 output + output_length,
3938 output_buffer_size - output_length,
3939 &length ) );
3940 output_length += length;
3941 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003942 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003943
Gilles Peskine286c3142022-04-20 17:09:38 +02003944 /* Multipart encryption */
3945 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
3946 output_length = 0;
3947 length = ~0;
3948 PSA_ASSERT( psa_cipher_update( &operation,
3949 ciphertext->x, ciphertext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003950 output, output_buffer_size,
Gilles Peskine286c3142022-04-20 17:09:38 +02003951 &length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02003952 TEST_LE_U( length, output_buffer_size );
Gilles Peskine286c3142022-04-20 17:09:38 +02003953 output_length += length;
3954 PSA_ASSERT( psa_cipher_finish( &operation,
3955 output + output_length,
3956 output_buffer_size - output_length,
3957 &length ) );
3958 output_length += length;
3959 ASSERT_COMPARE( plaintext->x, plaintext->len,
3960 output, output_length );
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003961
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003962 /* One-shot encryption */
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003963 output_length = ~0;
3964 PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len,
3965 output, output_buffer_size,
3966 &output_length ) );
3967 ASSERT_COMPARE( ciphertext->x, ciphertext->len,
3968 output, output_length );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003969
Gilles Peskine9e38f2c2022-04-20 17:07:52 +02003970 /* One-shot decryption */
3971 output_length = ~0;
3972 PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len,
3973 output, output_buffer_size,
3974 &output_length ) );
3975 ASSERT_COMPARE( plaintext->x, plaintext->len,
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003976 output, output_length );
3977
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003978exit:
Neil Armstrong3ee335d2022-02-07 14:51:37 +01003979 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003980 mbedtls_free( output );
Gilles Peskine9b9b6142022-04-20 16:55:03 +02003981 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01003982 psa_destroy_key( key );
3983 PSA_DONE( );
3984}
3985/* END_CASE */
3986
3987/* BEGIN_CASE */
Paul Elliotta417f562021-07-14 12:31:21 +01003988void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data )
3989{
3990 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3991 psa_algorithm_t alg = alg_arg;
3992 psa_key_type_t key_type = key_type_arg;
3993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3994 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3995 psa_status_t status;
3996
3997 PSA_ASSERT( psa_crypto_init( ) );
3998
3999 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4000 psa_set_key_algorithm( &attributes, alg );
4001 psa_set_key_type( &attributes, key_type );
4002
4003 /* Usage of either of these two size macros would cause divide by zero
4004 * with incorrect key types previously. Input length should be irrelevant
4005 * here. */
4006 TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ),
4007 0 );
4008 TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 );
4009
4010
4011 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4012 &key ) );
4013
4014 /* Should fail due to invalid alg type (to support invalid key type).
4015 * Encrypt or decrypt will end up in the same place. */
4016 status = psa_cipher_encrypt_setup( &operation, key, alg );
4017
4018 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
4019
4020exit:
4021 psa_cipher_abort( &operation );
4022 psa_destroy_key( key );
4023 PSA_DONE( );
4024}
4025/* END_CASE */
4026
4027/* BEGIN_CASE */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004028void cipher_encrypt_validation( int alg_arg,
4029 int key_type_arg,
4030 data_t *key_data,
4031 data_t *input )
4032{
4033 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4034 psa_key_type_t key_type = key_type_arg;
4035 psa_algorithm_t alg = alg_arg;
4036 size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg );
4037 unsigned char *output1 = NULL;
4038 size_t output1_buffer_size = 0;
4039 size_t output1_length = 0;
4040 unsigned char *output2 = NULL;
4041 size_t output2_buffer_size = 0;
4042 size_t output2_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004043 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004044 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004045 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004046
Gilles Peskine8817f612018-12-18 00:18:46 +01004047 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004048
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004049 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4050 psa_set_key_algorithm( &attributes, alg );
4051 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004052
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004053 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
4054 output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4055 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4056 ASSERT_ALLOC( output1, output1_buffer_size );
4057 ASSERT_ALLOC( output2, output2_buffer_size );
4058
Ronald Cron5425a212020-08-04 14:58:35 +02004059 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4060 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004061
gabor-mezei-arm50c86cf2021-06-25 15:47:50 +02004062 /* The one-shot cipher encryption uses generated iv so validating
4063 the output is not possible. Validating with multipart encryption. */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004064 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1,
4065 output1_buffer_size, &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004066 TEST_LE_U( output1_length,
4067 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4068 TEST_LE_U( output1_length,
4069 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004070
4071 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
4072 PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004073
Gilles Peskine8817f612018-12-18 00:18:46 +01004074 PSA_ASSERT( psa_cipher_update( &operation,
4075 input->x, input->len,
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004076 output2, output2_buffer_size,
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004078 TEST_LE_U( function_output_length,
4079 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
4080 TEST_LE_U( function_output_length,
4081 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004082 output2_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004083
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004084 PSA_ASSERT( psa_cipher_finish( &operation,
4085 output2 + output2_length,
4086 output2_buffer_size - output2_length,
4087 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004088 TEST_LE_U( function_output_length,
4089 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4090 TEST_LE_U( function_output_length,
4091 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004092 output2_length += function_output_length;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004093
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004094 PSA_ASSERT( psa_cipher_abort( &operation ) );
4095 ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size,
4096 output2, output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004097
Gilles Peskine50e586b2018-06-08 14:28:46 +02004098exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004099 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004100 mbedtls_free( output1 );
4101 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004102 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004103 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004104}
4105/* END_CASE */
4106
4107/* BEGIN_CASE */
4108void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004109 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004110 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004111 int first_part_size_arg,
4112 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004113 data_t *expected_output,
4114 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004115{
Ronald Cron5425a212020-08-04 14:58:35 +02004116 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004117 psa_key_type_t key_type = key_type_arg;
4118 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004119 psa_status_t status;
4120 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004121 size_t first_part_size = first_part_size_arg;
4122 size_t output1_length = output1_length_arg;
4123 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004124 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004125 size_t output_buffer_size = 0;
4126 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004127 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004128 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004129 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004130
Gilles Peskine8817f612018-12-18 00:18:46 +01004131 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004132
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004133 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4134 psa_set_key_algorithm( &attributes, alg );
4135 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004136
Ronald Cron5425a212020-08-04 14:58:35 +02004137 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4138 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004139
Ronald Cron5425a212020-08-04 14:58:35 +02004140 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004141
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004142 if( iv->len > 0 )
4143 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004144 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004145 }
4146
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004147 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4148 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004149 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004150
Gilles Peskine7be11a72022-04-14 00:12:57 +02004151 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004152 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
4153 output, output_buffer_size,
4154 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004155 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004156 TEST_LE_U( function_output_length,
4157 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4158 TEST_LE_U( function_output_length,
4159 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004160 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004161
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004162 if( first_part_size < input->len )
4163 {
4164 PSA_ASSERT( psa_cipher_update( &operation,
4165 input->x + first_part_size,
4166 input->len - first_part_size,
4167 ( output_buffer_size == 0 ? NULL :
4168 output + total_output_length ),
4169 output_buffer_size - total_output_length,
4170 &function_output_length ) );
4171 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004172 TEST_LE_U( function_output_length,
4173 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4174 alg,
4175 input->len - first_part_size ) );
4176 TEST_LE_U( function_output_length,
4177 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004178 total_output_length += function_output_length;
4179 }
gabor-mezei-armceface22021-01-21 12:26:17 +01004180
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004181 status = psa_cipher_finish( &operation,
4182 ( output_buffer_size == 0 ? NULL :
4183 output + total_output_length ),
4184 output_buffer_size - total_output_length,
4185 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004186 TEST_LE_U( function_output_length,
4187 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4188 TEST_LE_U( function_output_length,
4189 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004190 total_output_length += function_output_length;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004191 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004192
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004193 if( expected_status == PSA_SUCCESS )
4194 {
4195 PSA_ASSERT( psa_cipher_abort( &operation ) );
4196
4197 ASSERT_COMPARE( expected_output->x, expected_output->len,
4198 output, total_output_length );
4199 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004200
4201exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004202 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004203 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004204 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004205 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004206}
4207/* END_CASE */
4208
4209/* BEGIN_CASE */
4210void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004211 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004212 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004213 int first_part_size_arg,
4214 int output1_length_arg, int output2_length_arg,
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004215 data_t *expected_output,
4216 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02004217{
Ronald Cron5425a212020-08-04 14:58:35 +02004218 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004219 psa_key_type_t key_type = key_type_arg;
4220 psa_algorithm_t alg = alg_arg;
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004221 psa_status_t status;
4222 psa_status_t expected_status = expected_status_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004223 size_t first_part_size = first_part_size_arg;
4224 size_t output1_length = output1_length_arg;
4225 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004226 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004227 size_t output_buffer_size = 0;
4228 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004229 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004230 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02004232
Gilles Peskine8817f612018-12-18 00:18:46 +01004233 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004234
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004235 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4236 psa_set_key_algorithm( &attributes, alg );
4237 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004238
Ronald Cron5425a212020-08-04 14:58:35 +02004239 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4240 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004241
Ronald Cron5425a212020-08-04 14:58:35 +02004242 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004243
Steven Cooreman177deba2020-09-07 17:14:14 +02004244 if( iv->len > 0 )
4245 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004246 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004247 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004248
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004249 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) +
4250 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004251 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004252
Gilles Peskine7be11a72022-04-14 00:12:57 +02004253 TEST_LE_U( first_part_size, input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01004254 PSA_ASSERT( psa_cipher_update( &operation,
4255 input->x, first_part_size,
4256 output, output_buffer_size,
4257 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01004258 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004259 TEST_LE_U( function_output_length,
4260 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4261 TEST_LE_U( function_output_length,
4262 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004263 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01004264
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004265 if( first_part_size < input->len )
Steven Cooreman177deba2020-09-07 17:14:14 +02004266 {
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004267 PSA_ASSERT( psa_cipher_update( &operation,
4268 input->x + first_part_size,
4269 input->len - first_part_size,
4270 ( output_buffer_size == 0 ? NULL :
4271 output + total_output_length ),
4272 output_buffer_size - total_output_length,
4273 &function_output_length ) );
4274 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004275 TEST_LE_U( function_output_length,
4276 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4277 alg,
4278 input->len - first_part_size ) );
4279 TEST_LE_U( function_output_length,
4280 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004281 total_output_length += function_output_length;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004282 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02004283
Gilles Peskine50e586b2018-06-08 14:28:46 +02004284 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01004285 ( output_buffer_size == 0 ? NULL :
4286 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01004287 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02004288 &function_output_length );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004289 TEST_LE_U( function_output_length,
4290 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4291 TEST_LE_U( function_output_length,
4292 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02004293 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004294 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004295
4296 if( expected_status == PSA_SUCCESS )
4297 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004298 PSA_ASSERT( psa_cipher_abort( &operation ) );
gabor-mezei-arm95aad832021-06-25 18:21:33 +02004299
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004300 ASSERT_COMPARE( expected_output->x, expected_output->len,
4301 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004302 }
4303
Gilles Peskine50e586b2018-06-08 14:28:46 +02004304exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004305 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004306 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02004307 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004308 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02004309}
4310/* END_CASE */
4311
Gilles Peskine50e586b2018-06-08 14:28:46 +02004312/* BEGIN_CASE */
gabor-mezei-arma56756e2021-06-25 15:49:14 +02004313void cipher_decrypt_fail( int alg_arg,
4314 int key_type_arg,
4315 data_t *key_data,
4316 data_t *iv,
4317 data_t *input_arg,
4318 int expected_status_arg )
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004319{
4320 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4321 psa_status_t status;
4322 psa_key_type_t key_type = key_type_arg;
4323 psa_algorithm_t alg = alg_arg;
4324 psa_status_t expected_status = expected_status_arg;
4325 unsigned char *input = NULL;
4326 size_t input_buffer_size = 0;
4327 unsigned char *output = NULL;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004328 unsigned char *output_multi = NULL;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004329 size_t output_buffer_size = 0;
4330 size_t output_length = 0;
Neil Armstrong66a479f2022-02-07 15:41:19 +01004331 size_t function_output_length;
4332 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4334
4335 if ( PSA_ERROR_BAD_STATE != expected_status )
4336 {
4337 PSA_ASSERT( psa_crypto_init( ) );
4338
4339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4340 psa_set_key_algorithm( &attributes, alg );
4341 psa_set_key_type( &attributes, key_type );
4342
4343 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4344 &key ) );
4345 }
4346
4347 /* Allocate input buffer and copy the iv and the plaintext */
4348 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4349 if ( input_buffer_size > 0 )
4350 {
4351 ASSERT_ALLOC( input, input_buffer_size );
4352 memcpy( input, iv->x, iv->len );
4353 memcpy( input + iv->len, input_arg->x, input_arg->len );
4354 }
4355
4356 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4357 ASSERT_ALLOC( output, output_buffer_size );
4358
Neil Armstrong66a479f2022-02-07 15:41:19 +01004359 /* Decrypt, one-short */
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004360 status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4361 output_buffer_size, &output_length );
4362 TEST_EQUAL( status, expected_status );
4363
Neil Armstrong66a479f2022-02-07 15:41:19 +01004364 /* Decrypt, multi-part */
4365 status = psa_cipher_decrypt_setup( &operation, key, alg );
4366 if( status == PSA_SUCCESS )
4367 {
4368 output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg,
4369 input_arg->len ) +
4370 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg );
4371 ASSERT_ALLOC( output_multi, output_buffer_size );
4372
4373 if( iv->len > 0 )
4374 {
4375 status = psa_cipher_set_iv( &operation, iv->x, iv->len );
4376
4377 if( status != PSA_SUCCESS )
4378 TEST_EQUAL( status, expected_status );
4379 }
4380
4381 if( status == PSA_SUCCESS )
4382 {
4383 status = psa_cipher_update( &operation,
4384 input_arg->x, input_arg->len,
4385 output_multi, output_buffer_size,
4386 &function_output_length );
4387 if( status == PSA_SUCCESS )
4388 {
4389 output_length = function_output_length;
4390
4391 status = psa_cipher_finish( &operation,
4392 output_multi + output_length,
4393 output_buffer_size - output_length,
4394 &function_output_length );
4395
4396 TEST_EQUAL( status, expected_status );
4397 }
4398 else
4399 {
4400 TEST_EQUAL( status, expected_status );
4401 }
4402 }
4403 else
4404 {
4405 TEST_EQUAL( status, expected_status );
4406 }
4407 }
4408 else
4409 {
4410 TEST_EQUAL( status, expected_status );
4411 }
4412
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004413exit:
Neil Armstrong66a479f2022-02-07 15:41:19 +01004414 psa_cipher_abort( &operation );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004415 mbedtls_free( input );
4416 mbedtls_free( output );
Neil Armstrong66a479f2022-02-07 15:41:19 +01004417 mbedtls_free( output_multi );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004418 psa_destroy_key( key );
4419 PSA_DONE( );
4420}
4421/* END_CASE */
4422
4423/* BEGIN_CASE */
4424void cipher_decrypt( int alg_arg,
4425 int key_type_arg,
4426 data_t *key_data,
4427 data_t *iv,
4428 data_t *input_arg,
4429 data_t *expected_output )
4430{
4431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
4432 psa_key_type_t key_type = key_type_arg;
4433 psa_algorithm_t alg = alg_arg;
4434 unsigned char *input = NULL;
4435 size_t input_buffer_size = 0;
4436 unsigned char *output = NULL;
4437 size_t output_buffer_size = 0;
4438 size_t output_length = 0;
4439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4440
4441 PSA_ASSERT( psa_crypto_init( ) );
4442
4443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4444 psa_set_key_algorithm( &attributes, alg );
4445 psa_set_key_type( &attributes, key_type );
4446
4447 /* Allocate input buffer and copy the iv and the plaintext */
4448 input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len );
4449 if ( input_buffer_size > 0 )
4450 {
4451 ASSERT_ALLOC( input, input_buffer_size );
4452 memcpy( input, iv->x, iv->len );
4453 memcpy( input + iv->len, input_arg->x, input_arg->len );
4454 }
4455
4456 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size );
4457 ASSERT_ALLOC( output, output_buffer_size );
4458
4459 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4460 &key ) );
4461
4462 PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output,
4463 output_buffer_size, &output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004464 TEST_LE_U( output_length,
4465 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) );
4466 TEST_LE_U( output_length,
4467 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) );
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004468
4469 ASSERT_COMPARE( expected_output->x, expected_output->len,
4470 output, output_length );
4471exit:
4472 mbedtls_free( input );
4473 mbedtls_free( output );
4474 psa_destroy_key( key );
4475 PSA_DONE( );
4476}
4477/* END_CASE */
4478
4479/* BEGIN_CASE */
4480void cipher_verify_output( int alg_arg,
4481 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004482 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004483 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02004484{
Ronald Cron5425a212020-08-04 14:58:35 +02004485 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004486 psa_key_type_t key_type = key_type_arg;
4487 psa_algorithm_t alg = alg_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004488 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004489 size_t output1_size = 0;
4490 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004491 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004492 size_t output2_size = 0;
4493 size_t output2_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004495
Gilles Peskine8817f612018-12-18 00:18:46 +01004496 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004497
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004498 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4499 psa_set_key_algorithm( &attributes, alg );
4500 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004501
Ronald Cron5425a212020-08-04 14:58:35 +02004502 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4503 &key ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004504 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004505 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03004506
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004507 PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len,
4508 output1, output1_size,
4509 &output1_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004510 TEST_LE_U( output1_length,
4511 PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) );
4512 TEST_LE_U( output1_length,
4513 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03004514
4515 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004516 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03004517
gabor-mezei-armf494bcd2021-03-01 15:11:46 +01004518 PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length,
4519 output2, output2_size,
4520 &output2_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004521 TEST_LE_U( output2_length,
4522 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4523 TEST_LE_U( output2_length,
4524 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03004525
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004526 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03004527
4528exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03004529 mbedtls_free( output1 );
4530 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004531 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004532 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03004533}
4534/* END_CASE */
4535
4536/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02004537void cipher_verify_output_multipart( int alg_arg,
4538 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02004539 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03004540 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01004541 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03004542{
Ronald Cron5425a212020-08-04 14:58:35 +02004543 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004544 psa_key_type_t key_type = key_type_arg;
4545 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01004546 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03004547 unsigned char iv[16] = {0};
4548 size_t iv_size = 16;
4549 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004550 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004551 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004552 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03004553 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004554 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03004555 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02004556 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00004557 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
4558 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03004560
Gilles Peskine8817f612018-12-18 00:18:46 +01004561 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03004562
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004563 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4564 psa_set_key_algorithm( &attributes, alg );
4565 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03004566
Ronald Cron5425a212020-08-04 14:58:35 +02004567 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4568 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03004569
Ronald Cron5425a212020-08-04 14:58:35 +02004570 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
4571 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03004572
Steven Cooreman177deba2020-09-07 17:14:14 +02004573 if( alg != PSA_ALG_ECB_NO_PADDING )
4574 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004575 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
4576 iv, iv_size,
4577 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004578 }
4579
gabor-mezei-armceface22021-01-21 12:26:17 +01004580 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004581 TEST_LE_U( output1_buffer_size,
4582 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004583 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03004584
Gilles Peskine7be11a72022-04-14 00:12:57 +02004585 TEST_LE_U( first_part_size, input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004586
Gilles Peskine8817f612018-12-18 00:18:46 +01004587 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
4588 output1, output1_buffer_size,
4589 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004590 TEST_LE_U( function_output_length,
4591 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4592 TEST_LE_U( function_output_length,
4593 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004594 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004595
Gilles Peskine8817f612018-12-18 00:18:46 +01004596 PSA_ASSERT( psa_cipher_update( &operation1,
4597 input->x + first_part_size,
4598 input->len - first_part_size,
4599 output1, output1_buffer_size,
4600 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004601 TEST_LE_U( function_output_length,
4602 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4603 alg,
4604 input->len - first_part_size ) );
4605 TEST_LE_U( function_output_length,
4606 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004607 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004608
Gilles Peskine8817f612018-12-18 00:18:46 +01004609 PSA_ASSERT( psa_cipher_finish( &operation1,
4610 output1 + output1_length,
4611 output1_buffer_size - output1_length,
4612 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004613 TEST_LE_U( function_output_length,
4614 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4615 TEST_LE_U( function_output_length,
4616 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004617 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02004618
Gilles Peskine8817f612018-12-18 00:18:46 +01004619 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004620
Gilles Peskine048b7f02018-06-08 14:20:49 +02004621 output2_buffer_size = output1_length;
Gilles Peskine7be11a72022-04-14 00:12:57 +02004622 TEST_LE_U( output2_buffer_size,
4623 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
4624 TEST_LE_U( output2_buffer_size,
4625 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004626 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004627
Steven Cooreman177deba2020-09-07 17:14:14 +02004628 if( iv_length > 0 )
4629 {
Steven Cooremana6033e92020-08-25 11:47:50 +02004630 PSA_ASSERT( psa_cipher_set_iv( &operation2,
4631 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004632 }
Moran Pekerded84402018-06-06 16:36:50 +03004633
Gilles Peskine8817f612018-12-18 00:18:46 +01004634 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
4635 output2, output2_buffer_size,
4636 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004637 TEST_LE_U( function_output_length,
4638 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
4639 TEST_LE_U( function_output_length,
4640 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004641 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004642
Gilles Peskine8817f612018-12-18 00:18:46 +01004643 PSA_ASSERT( psa_cipher_update( &operation2,
4644 output1 + first_part_size,
4645 output1_length - first_part_size,
4646 output2, output2_buffer_size,
4647 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004648 TEST_LE_U( function_output_length,
4649 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
4650 alg,
4651 output1_length - first_part_size ) );
4652 TEST_LE_U( function_output_length,
4653 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004654 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03004655
Gilles Peskine8817f612018-12-18 00:18:46 +01004656 PSA_ASSERT( psa_cipher_finish( &operation2,
4657 output2 + output2_length,
4658 output2_buffer_size - output2_length,
4659 &function_output_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004660 TEST_LE_U( function_output_length,
4661 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
4662 TEST_LE_U( function_output_length,
4663 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02004664 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02004665
Gilles Peskine8817f612018-12-18 00:18:46 +01004666 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004667
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004668 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004669
4670exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02004671 psa_cipher_abort( &operation1 );
4672 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004673 mbedtls_free( output1 );
4674 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02004675 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004676 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02004677}
4678/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02004679
Gilles Peskine20035e32018-02-03 22:44:14 +01004680/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004681void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004682 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02004683 data_t *nonce,
4684 data_t *additional_data,
4685 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004686 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004687{
Ronald Cron5425a212020-08-04 14:58:35 +02004688 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004689 psa_key_type_t key_type = key_type_arg;
4690 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004691 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004692 unsigned char *output_data = NULL;
4693 size_t output_size = 0;
4694 size_t output_length = 0;
4695 unsigned char *output_data2 = NULL;
4696 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01004697 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004698 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004699 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004700
Gilles Peskine8817f612018-12-18 00:18:46 +01004701 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004702
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004703 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4704 psa_set_key_algorithm( &attributes, alg );
4705 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004706
Gilles Peskine049c7532019-05-15 20:22:09 +02004707 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004708 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004709 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4710 key_bits = psa_get_key_bits( &attributes );
4711
4712 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4713 alg );
4714 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4715 * should be exact. */
4716 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4717 expected_result != PSA_ERROR_NOT_SUPPORTED )
4718 {
4719 TEST_EQUAL( output_size,
4720 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004721 TEST_LE_U( output_size,
4722 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004723 }
4724 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004725
Steven Cooremanf49478b2021-02-15 15:19:25 +01004726 status = psa_aead_encrypt( key, alg,
4727 nonce->x, nonce->len,
4728 additional_data->x,
4729 additional_data->len,
4730 input_data->x, input_data->len,
4731 output_data, output_size,
4732 &output_length );
4733
4734 /* If the operation is not supported, just skip and not fail in case the
4735 * encryption involves a common limitation of cryptography hardwares and
4736 * an alternative implementation. */
4737 if( status == PSA_ERROR_NOT_SUPPORTED )
4738 {
4739 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4740 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
4741 }
4742
4743 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004744
4745 if( PSA_SUCCESS == expected_result )
4746 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004747 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004748
Gilles Peskine003a4a92019-05-14 16:09:40 +02004749 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4750 * should be exact. */
4751 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01004752 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02004753
Gilles Peskine7be11a72022-04-14 00:12:57 +02004754 TEST_LE_U( input_data->len,
4755 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004756
Ronald Cron5425a212020-08-04 14:58:35 +02004757 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004758 nonce->x, nonce->len,
4759 additional_data->x,
4760 additional_data->len,
4761 output_data, output_length,
4762 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004763 &output_length2 ),
4764 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02004765
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004766 ASSERT_COMPARE( input_data->x, input_data->len,
4767 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004768 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004769
Gilles Peskinea1cac842018-06-11 19:33:02 +02004770exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004771 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004772 mbedtls_free( output_data );
4773 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004774 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004775}
4776/* END_CASE */
4777
4778/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004779void aead_encrypt( int key_type_arg, data_t *key_data,
4780 int alg_arg,
4781 data_t *nonce,
4782 data_t *additional_data,
4783 data_t *input_data,
4784 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004785{
Ronald Cron5425a212020-08-04 14:58:35 +02004786 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004787 psa_key_type_t key_type = key_type_arg;
4788 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004789 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004790 unsigned char *output_data = NULL;
4791 size_t output_size = 0;
4792 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004793 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01004794 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004795
Gilles Peskine8817f612018-12-18 00:18:46 +01004796 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004797
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004798 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 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004801
Gilles Peskine049c7532019-05-15 20:22:09 +02004802 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004803 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004804 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4805 key_bits = psa_get_key_bits( &attributes );
4806
4807 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4808 alg );
4809 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
4810 * should be exact. */
4811 TEST_EQUAL( output_size,
4812 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004813 TEST_LE_U( output_size,
4814 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004815 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004816
Steven Cooremand588ea12021-01-11 19:36:04 +01004817 status = psa_aead_encrypt( key, alg,
4818 nonce->x, nonce->len,
4819 additional_data->x, additional_data->len,
4820 input_data->x, input_data->len,
4821 output_data, output_size,
4822 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004823
Ronald Cron28a45ed2021-02-09 20:35:42 +01004824 /* If the operation is not supported, just skip and not fail in case the
4825 * encryption involves a common limitation of cryptography hardwares and
4826 * an alternative implementation. */
4827 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004828 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004829 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4830 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004831 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004832
4833 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004834 ASSERT_COMPARE( expected_result->x, expected_result->len,
4835 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004836
Gilles Peskinea1cac842018-06-11 19:33:02 +02004837exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004838 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004839 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004840 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004841}
4842/* END_CASE */
4843
4844/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004845void aead_decrypt( int key_type_arg, data_t *key_data,
4846 int alg_arg,
4847 data_t *nonce,
4848 data_t *additional_data,
4849 data_t *input_data,
4850 data_t *expected_data,
4851 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004852{
Ronald Cron5425a212020-08-04 14:58:35 +02004853 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004854 psa_key_type_t key_type = key_type_arg;
4855 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01004856 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004857 unsigned char *output_data = NULL;
4858 size_t output_size = 0;
4859 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004860 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004861 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01004862 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004863
Gilles Peskine8817f612018-12-18 00:18:46 +01004864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004865
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4867 psa_set_key_algorithm( &attributes, alg );
4868 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004869
Gilles Peskine049c7532019-05-15 20:22:09 +02004870 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004871 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004872 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
4873 key_bits = psa_get_key_bits( &attributes );
4874
4875 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
4876 alg );
4877 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
4878 expected_result != PSA_ERROR_NOT_SUPPORTED )
4879 {
4880 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4881 * should be exact. */
4882 TEST_EQUAL( output_size,
4883 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02004884 TEST_LE_U( output_size,
4885 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01004886 }
4887 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004888
Steven Cooremand588ea12021-01-11 19:36:04 +01004889 status = psa_aead_decrypt( key, alg,
4890 nonce->x, nonce->len,
4891 additional_data->x,
4892 additional_data->len,
4893 input_data->x, input_data->len,
4894 output_data, output_size,
4895 &output_length );
4896
Ronald Cron28a45ed2021-02-09 20:35:42 +01004897 /* If the operation is not supported, just skip and not fail in case the
4898 * decryption involves a common limitation of cryptography hardwares and
4899 * an alternative implementation. */
4900 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01004901 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01004902 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
4903 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01004904 }
Steven Cooremand588ea12021-01-11 19:36:04 +01004905
4906 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004907
Gilles Peskine2d277862018-06-18 15:41:12 +02004908 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004909 ASSERT_COMPARE( expected_data->x, expected_data->len,
4910 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004911
Gilles Peskinea1cac842018-06-11 19:33:02 +02004912exit:
Ronald Cron5425a212020-08-04 14:58:35 +02004913 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004914 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004915 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004916}
4917/* END_CASE */
4918
4919/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01004920void aead_multipart_encrypt( int key_type_arg, data_t *key_data,
4921 int alg_arg,
4922 data_t *nonce,
4923 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01004924 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01004925 int do_set_lengths,
4926 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004927{
Paul Elliottd3f82412021-06-16 16:52:21 +01004928 size_t ad_part_len = 0;
4929 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01004930 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004931
Paul Elliott32f46ba2021-09-23 18:24:36 +01004932 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004933 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004934 mbedtls_test_set_step( ad_part_len );
4935
4936 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004937 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004938 if( ad_part_len & 0x01 )
4939 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4940 else
4941 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004942 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01004943
4944 /* Split ad into length(ad_part_len) parts. */
4945 if( !aead_multipart_internal_func( key_type_arg, key_data,
4946 alg_arg, nonce,
4947 additional_data,
4948 ad_part_len,
4949 input_data, -1,
4950 set_lengths_method,
4951 expected_output,
4952 1, 0 ) )
4953 break;
4954
4955 /* length(0) part, length(ad_part_len) part, length(0) part... */
4956 mbedtls_test_set_step( 1000 + ad_part_len );
4957
4958 if( !aead_multipart_internal_func( key_type_arg, key_data,
4959 alg_arg, nonce,
4960 additional_data,
4961 ad_part_len,
4962 input_data, -1,
4963 set_lengths_method,
4964 expected_output,
4965 1, 1 ) )
4966 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004967 }
Paul Elliottd3f82412021-06-16 16:52:21 +01004968
Paul Elliott32f46ba2021-09-23 18:24:36 +01004969 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004970 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004971 /* Split data into length(data_part_len) parts. */
4972 mbedtls_test_set_step( 2000 + data_part_len );
4973
4974 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01004975 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01004976 if( data_part_len & 0x01 )
4977 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
4978 else
4979 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01004980 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01004981
Paul Elliott32f46ba2021-09-23 18:24:36 +01004982 if( !aead_multipart_internal_func( key_type_arg, key_data,
4983 alg_arg, nonce,
4984 additional_data, -1,
4985 input_data, data_part_len,
4986 set_lengths_method,
4987 expected_output,
4988 1, 0 ) )
4989 break;
4990
4991 /* length(0) part, length(data_part_len) part, length(0) part... */
4992 mbedtls_test_set_step( 3000 + data_part_len );
4993
4994 if( !aead_multipart_internal_func( key_type_arg, key_data,
4995 alg_arg, nonce,
4996 additional_data, -1,
4997 input_data, data_part_len,
4998 set_lengths_method,
4999 expected_output,
5000 1, 1 ) )
5001 break;
5002 }
Paul Elliott0023e0a2021-04-27 10:06:22 +01005003
Paul Elliott8fc45162021-06-23 16:06:01 +01005004 /* Goto is required to silence warnings about unused labels, as we
5005 * don't actually do any test assertions in this function. */
5006 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005007}
5008/* END_CASE */
5009
5010/* BEGIN_CASE */
Paul Elliott0023e0a2021-04-27 10:06:22 +01005011void aead_multipart_decrypt( int key_type_arg, data_t *key_data,
5012 int alg_arg,
5013 data_t *nonce,
5014 data_t *additional_data,
Paul Elliott0023e0a2021-04-27 10:06:22 +01005015 data_t *input_data,
Paul Elliott97fd1ba2021-07-21 18:46:06 +01005016 int do_set_lengths,
Paul Elliott9961a662021-09-17 19:19:02 +01005017 data_t *expected_output )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005018{
Paul Elliottd3f82412021-06-16 16:52:21 +01005019 size_t ad_part_len = 0;
5020 size_t data_part_len = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005021 set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005022
Paul Elliott32f46ba2021-09-23 18:24:36 +01005023 for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005024 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005025 /* Split ad into length(ad_part_len) parts. */
5026 mbedtls_test_set_step( ad_part_len );
5027
5028 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005029 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005030 if( ad_part_len & 0x01 )
5031 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5032 else
5033 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005034 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005035
5036 if( !aead_multipart_internal_func( key_type_arg, key_data,
5037 alg_arg, nonce,
5038 additional_data,
5039 ad_part_len,
5040 input_data, -1,
5041 set_lengths_method,
5042 expected_output,
5043 0, 0 ) )
5044 break;
5045
5046 /* length(0) part, length(ad_part_len) part, length(0) part... */
5047 mbedtls_test_set_step( 1000 + ad_part_len );
5048
5049 if( !aead_multipart_internal_func( key_type_arg, key_data,
5050 alg_arg, nonce,
5051 additional_data,
5052 ad_part_len,
5053 input_data, -1,
5054 set_lengths_method,
5055 expected_output,
5056 0, 1 ) )
5057 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005058 }
5059
Paul Elliott32f46ba2021-09-23 18:24:36 +01005060 for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005061 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005062 /* Split data into length(data_part_len) parts. */
5063 mbedtls_test_set_step( 2000 + data_part_len );
5064
5065 if( do_set_lengths )
Paul Elliott0023e0a2021-04-27 10:06:22 +01005066 {
Paul Elliott32f46ba2021-09-23 18:24:36 +01005067 if( data_part_len & 0x01 )
5068 set_lengths_method = SET_LENGTHS_AFTER_NONCE;
5069 else
5070 set_lengths_method = SET_LENGTHS_BEFORE_NONCE;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005071 }
Paul Elliott32f46ba2021-09-23 18:24:36 +01005072
5073 if( !aead_multipart_internal_func( key_type_arg, key_data,
5074 alg_arg, nonce,
5075 additional_data, -1,
5076 input_data, data_part_len,
5077 set_lengths_method,
5078 expected_output,
5079 0, 0 ) )
5080 break;
5081
5082 /* length(0) part, length(data_part_len) part, length(0) part... */
5083 mbedtls_test_set_step( 3000 + data_part_len );
5084
5085 if( !aead_multipart_internal_func( key_type_arg, key_data,
5086 alg_arg, nonce,
5087 additional_data, -1,
5088 input_data, data_part_len,
5089 set_lengths_method,
5090 expected_output,
5091 0, 1 ) )
5092 break;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005093 }
5094
Paul Elliott8fc45162021-06-23 16:06:01 +01005095 /* Goto is required to silence warnings about unused labels, as we
5096 * don't actually do any test assertions in this function. */
Paul Elliottd3f82412021-06-16 16:52:21 +01005097 goto exit;
Paul Elliott0023e0a2021-04-27 10:06:22 +01005098}
5099/* END_CASE */
5100
5101/* BEGIN_CASE */
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005102void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data,
5103 int alg_arg,
Paul Elliottf1277632021-08-24 18:11:37 +01005104 int nonce_length,
5105 int expected_nonce_length_arg,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005106 data_t *additional_data,
5107 data_t *input_data,
5108 int expected_status_arg )
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005109{
5110
5111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5112 psa_key_type_t key_type = key_type_arg;
5113 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005114 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005115 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5117 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Paul Elliott693bf312021-07-23 17:40:41 +01005118 psa_status_t expected_status = expected_status_arg;
Paul Elliottf1277632021-08-24 18:11:37 +01005119 size_t actual_nonce_length = 0;
5120 size_t expected_nonce_length = expected_nonce_length_arg;
5121 unsigned char *output = NULL;
5122 unsigned char *ciphertext = NULL;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005123 size_t output_size = 0;
Paul Elliottf1277632021-08-24 18:11:37 +01005124 size_t ciphertext_size = 0;
5125 size_t ciphertext_length = 0;
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005126 size_t tag_length = 0;
5127 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005128
5129 PSA_ASSERT( psa_crypto_init( ) );
5130
5131 psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT );
5132 psa_set_key_algorithm( & attributes, alg );
5133 psa_set_key_type( & attributes, key_type );
5134
5135 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5136 &key ) );
5137
5138 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5139
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005140 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5141
Paul Elliottf1277632021-08-24 18:11:37 +01005142 ASSERT_ALLOC( output, output_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005143
Paul Elliottf1277632021-08-24 18:11:37 +01005144 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005145
Gilles Peskine7be11a72022-04-14 00:12:57 +02005146 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005147
Paul Elliottf1277632021-08-24 18:11:37 +01005148 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005149
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005150 status = psa_aead_encrypt_setup( &operation, key, alg );
5151
5152 /* If the operation is not supported, just skip and not fail in case the
5153 * encryption involves a common limitation of cryptography hardwares and
5154 * an alternative implementation. */
5155 if( status == PSA_ERROR_NOT_SUPPORTED )
5156 {
5157 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliottf1277632021-08-24 18:11:37 +01005158 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005159 }
5160
5161 PSA_ASSERT( status );
5162
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005163 status = psa_aead_generate_nonce( &operation, nonce_buffer,
Paul Elliottf1277632021-08-24 18:11:37 +01005164 nonce_length,
5165 &actual_nonce_length );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005166
Paul Elliott693bf312021-07-23 17:40:41 +01005167 TEST_EQUAL( status, expected_status );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005168
Paul Elliottf1277632021-08-24 18:11:37 +01005169 TEST_EQUAL( actual_nonce_length, expected_nonce_length );
Paul Elliottd85f5472021-07-16 18:20:16 +01005170
Paul Elliott88ecbe12021-09-22 17:23:03 +01005171 if( expected_status == PSA_SUCCESS )
5172 TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type,
5173 alg ) );
5174
Gilles Peskine7be11a72022-04-14 00:12:57 +02005175 TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE );
Paul Elliotte0fcb3b2021-07-16 18:52:03 +01005176
Paul Elliott693bf312021-07-23 17:40:41 +01005177 if( expected_status == PSA_SUCCESS )
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005178 {
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005179 /* Ensure we can still complete operation. */
Paul Elliottd79c5c52021-10-06 21:49:41 +01005180 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5181 input_data->len ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005182
5183 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5184 additional_data->len ) );
5185
5186 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottf1277632021-08-24 18:11:37 +01005187 output, output_size,
5188 &ciphertext_length ) );
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005189
Paul Elliottf1277632021-08-24 18:11:37 +01005190 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5191 &ciphertext_length, tag_buffer,
Paul Elliott3bd5dba2021-06-23 17:14:40 +01005192 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5193 }
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005194
5195exit:
5196 psa_destroy_key( key );
Paul Elliottf1277632021-08-24 18:11:37 +01005197 mbedtls_free( output );
5198 mbedtls_free( ciphertext );
Paul Elliott8eb9daf2021-06-04 16:42:21 +01005199 psa_aead_abort( &operation );
5200 PSA_DONE( );
5201}
5202/* END_CASE */
5203
5204/* BEGIN_CASE */
Paul Elliott863864a2021-07-23 17:28:31 +01005205void aead_multipart_set_nonce( int key_type_arg, data_t *key_data,
5206 int alg_arg,
Paul Elliott4023ffd2021-09-10 16:21:22 +01005207 int nonce_length_arg,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005208 int set_lengths_method_arg,
Paul Elliott863864a2021-07-23 17:28:31 +01005209 data_t *additional_data,
5210 data_t *input_data,
5211 int expected_status_arg )
5212{
5213
5214 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5215 psa_key_type_t key_type = key_type_arg;
5216 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005217 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott863864a2021-07-23 17:28:31 +01005218 uint8_t *nonce_buffer = NULL;
5219 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5220 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5221 psa_status_t expected_status = expected_status_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005222 unsigned char *output = NULL;
5223 unsigned char *ciphertext = NULL;
Paul Elliott4023ffd2021-09-10 16:21:22 +01005224 size_t nonce_length;
Paul Elliott863864a2021-07-23 17:28:31 +01005225 size_t output_size = 0;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005226 size_t ciphertext_size = 0;
5227 size_t ciphertext_length = 0;
Paul Elliott863864a2021-07-23 17:28:31 +01005228 size_t tag_length = 0;
5229 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
Paul Elliott4023ffd2021-09-10 16:21:22 +01005230 size_t index = 0;
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005231 set_lengths_method_t set_lengths_method = set_lengths_method_arg;
Paul Elliott863864a2021-07-23 17:28:31 +01005232
5233 PSA_ASSERT( psa_crypto_init( ) );
5234
5235 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5236 psa_set_key_algorithm( &attributes, alg );
5237 psa_set_key_type( &attributes, key_type );
5238
5239 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5240 &key ) );
5241
5242 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5243
5244 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5245
Paul Elliott6f0e7202021-08-25 12:57:18 +01005246 ASSERT_ALLOC( output, output_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005247
Paul Elliott6f0e7202021-08-25 12:57:18 +01005248 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott863864a2021-07-23 17:28:31 +01005249
Gilles Peskine7be11a72022-04-14 00:12:57 +02005250 TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliott863864a2021-07-23 17:28:31 +01005251
Paul Elliott6f0e7202021-08-25 12:57:18 +01005252 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott863864a2021-07-23 17:28:31 +01005253
Paul Elliott863864a2021-07-23 17:28:31 +01005254 status = psa_aead_encrypt_setup( &operation, key, alg );
5255
5256 /* If the operation is not supported, just skip and not fail in case the
5257 * encryption involves a common limitation of cryptography hardwares and
5258 * an alternative implementation. */
5259 if( status == PSA_ERROR_NOT_SUPPORTED )
5260 {
5261 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005262 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg );
Paul Elliott863864a2021-07-23 17:28:31 +01005263 }
5264
5265 PSA_ASSERT( status );
5266
Paul Elliott4023ffd2021-09-10 16:21:22 +01005267 /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */
5268 if( nonce_length_arg == -1 )
Paul Elliott863864a2021-07-23 17:28:31 +01005269 {
Paul Elliott5e69aa52021-08-25 17:24:37 +01005270 /* Arbitrary size buffer, to test zero length valid buffer. */
5271 ASSERT_ALLOC( nonce_buffer, 4 );
Paul Elliott4023ffd2021-09-10 16:21:22 +01005272 nonce_length = 0;
Paul Elliott66696b52021-08-16 18:42:41 +01005273 }
5274 else
5275 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005276 /* If length is zero, then this will return NULL. */
5277 nonce_length = ( size_t ) nonce_length_arg;
Paul Elliott6f0e7202021-08-25 12:57:18 +01005278 ASSERT_ALLOC( nonce_buffer, nonce_length );
Paul Elliott66696b52021-08-16 18:42:41 +01005279
Paul Elliott4023ffd2021-09-10 16:21:22 +01005280 if( nonce_buffer )
Paul Elliott66696b52021-08-16 18:42:41 +01005281 {
Paul Elliott4023ffd2021-09-10 16:21:22 +01005282 for( index = 0; index < nonce_length - 1; ++index )
5283 {
5284 nonce_buffer[index] = 'a' + index;
5285 }
Paul Elliott66696b52021-08-16 18:42:41 +01005286 }
Paul Elliott863864a2021-07-23 17:28:31 +01005287 }
5288
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005289 if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE )
5290 {
5291 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5292 input_data->len ) );
5293 }
5294
Paul Elliott6f0e7202021-08-25 12:57:18 +01005295 status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length );
Paul Elliott863864a2021-07-23 17:28:31 +01005296
Paul Elliott693bf312021-07-23 17:40:41 +01005297 TEST_EQUAL( status, expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005298
5299 if( expected_status == PSA_SUCCESS )
5300 {
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005301 if( set_lengths_method == SET_LENGTHS_AFTER_NONCE )
5302 {
5303 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5304 input_data->len ) );
5305 }
5306 if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS )
5307 expected_status = PSA_ERROR_BAD_STATE;
Paul Elliott863864a2021-07-23 17:28:31 +01005308
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005309 /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */
5310 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5311 additional_data->len ),
5312 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005313
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005314 TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005315 output, output_size,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005316 &ciphertext_length ),
5317 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005318
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005319 TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size,
Paul Elliott6f0e7202021-08-25 12:57:18 +01005320 &ciphertext_length, tag_buffer,
Andrzej Kureka2ce72e2021-12-25 17:21:47 +01005321 PSA_AEAD_TAG_MAX_SIZE, &tag_length ),
5322 expected_status );
Paul Elliott863864a2021-07-23 17:28:31 +01005323 }
5324
5325exit:
5326 psa_destroy_key( key );
Paul Elliott6f0e7202021-08-25 12:57:18 +01005327 mbedtls_free( output );
5328 mbedtls_free( ciphertext );
Paul Elliott863864a2021-07-23 17:28:31 +01005329 mbedtls_free( nonce_buffer );
5330 psa_aead_abort( &operation );
5331 PSA_DONE( );
5332}
5333/* END_CASE */
5334
5335/* BEGIN_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005336void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data,
5337 int alg_arg,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005338 int output_size_arg,
Paul Elliott43fbda62021-07-23 18:30:59 +01005339 data_t *nonce,
5340 data_t *additional_data,
5341 data_t *input_data,
5342 int expected_status_arg )
5343{
5344
5345 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5346 psa_key_type_t key_type = key_type_arg;
5347 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005348 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott43fbda62021-07-23 18:30:59 +01005349 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5350 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5351 psa_status_t expected_status = expected_status_arg;
Paul Elliottc6d11d02021-09-01 12:04:23 +01005352 unsigned char *output = NULL;
5353 unsigned char *ciphertext = NULL;
5354 size_t output_size = output_size_arg;
5355 size_t ciphertext_size = 0;
5356 size_t ciphertext_length = 0;
Paul Elliott43fbda62021-07-23 18:30:59 +01005357 size_t tag_length = 0;
5358 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5359
5360 PSA_ASSERT( psa_crypto_init( ) );
5361
5362 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5363 psa_set_key_algorithm( &attributes, alg );
5364 psa_set_key_type( &attributes, key_type );
5365
5366 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5367 &key ) );
5368
5369 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5370
Paul Elliottc6d11d02021-09-01 12:04:23 +01005371 ASSERT_ALLOC( output, output_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005372
Paul Elliottc6d11d02021-09-01 12:04:23 +01005373 ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
Paul Elliott43fbda62021-07-23 18:30:59 +01005374
Paul Elliottc6d11d02021-09-01 12:04:23 +01005375 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott43fbda62021-07-23 18:30:59 +01005376
Paul Elliott43fbda62021-07-23 18:30:59 +01005377 status = psa_aead_encrypt_setup( &operation, key, alg );
5378
5379 /* If the operation is not supported, just skip and not fail in case the
5380 * encryption involves a common limitation of cryptography hardwares and
5381 * an alternative implementation. */
5382 if( status == PSA_ERROR_NOT_SUPPORTED )
5383 {
5384 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5385 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5386 }
5387
5388 PSA_ASSERT( status );
5389
Paul Elliott47b9a142021-10-07 15:04:57 +01005390 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5391 input_data->len ) );
5392
Paul Elliott43fbda62021-07-23 18:30:59 +01005393 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5394
5395 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5396 additional_data->len ) );
5397
5398 status = psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliottc6d11d02021-09-01 12:04:23 +01005399 output, output_size, &ciphertext_length );
Paul Elliott43fbda62021-07-23 18:30:59 +01005400
5401 TEST_EQUAL( status, expected_status );
5402
5403 if( expected_status == PSA_SUCCESS )
5404 {
5405 /* Ensure we can still complete operation. */
Paul Elliottc6d11d02021-09-01 12:04:23 +01005406 PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size,
5407 &ciphertext_length, tag_buffer,
Paul Elliott43fbda62021-07-23 18:30:59 +01005408 PSA_AEAD_TAG_MAX_SIZE, &tag_length ) );
5409 }
5410
5411exit:
5412 psa_destroy_key( key );
Paul Elliottc6d11d02021-09-01 12:04:23 +01005413 mbedtls_free( output );
5414 mbedtls_free( ciphertext );
Paul Elliott43fbda62021-07-23 18:30:59 +01005415 psa_aead_abort( &operation );
5416 PSA_DONE( );
5417}
5418/* END_CASE */
5419
Paul Elliott91b021e2021-07-23 18:52:31 +01005420/* BEGIN_CASE */
5421void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data,
5422 int alg_arg,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005423 int finish_ciphertext_size_arg,
Paul Elliott719c1322021-09-13 18:27:22 +01005424 int tag_size_arg,
Paul Elliott91b021e2021-07-23 18:52:31 +01005425 data_t *nonce,
5426 data_t *additional_data,
5427 data_t *input_data,
5428 int expected_status_arg )
5429{
5430
5431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5432 psa_key_type_t key_type = key_type_arg;
5433 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005434 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott91b021e2021-07-23 18:52:31 +01005435 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5436 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5437 psa_status_t expected_status = expected_status_arg;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005438 unsigned char *ciphertext = NULL;
5439 unsigned char *finish_ciphertext = NULL;
Paul Elliott719c1322021-09-13 18:27:22 +01005440 unsigned char *tag_buffer = NULL;
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005441 size_t ciphertext_size = 0;
5442 size_t ciphertext_length = 0;
5443 size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg;
Paul Elliott719c1322021-09-13 18:27:22 +01005444 size_t tag_size = ( size_t ) tag_size_arg;
Paul Elliott91b021e2021-07-23 18:52:31 +01005445 size_t tag_length = 0;
Paul Elliott91b021e2021-07-23 18:52:31 +01005446
5447 PSA_ASSERT( psa_crypto_init( ) );
5448
5449 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
5450 psa_set_key_algorithm( &attributes, alg );
5451 psa_set_key_type( &attributes, key_type );
5452
5453 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5454 &key ) );
5455
5456 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5457
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005458 ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
Paul Elliott91b021e2021-07-23 18:52:31 +01005459
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005460 ASSERT_ALLOC( ciphertext, ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005461
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005462 ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size );
Paul Elliott91b021e2021-07-23 18:52:31 +01005463
Paul Elliott719c1322021-09-13 18:27:22 +01005464 ASSERT_ALLOC( tag_buffer, tag_size );
5465
Paul Elliott91b021e2021-07-23 18:52:31 +01005466 status = psa_aead_encrypt_setup( &operation, key, alg );
5467
5468 /* If the operation is not supported, just skip and not fail in case the
5469 * encryption involves a common limitation of cryptography hardwares and
5470 * an alternative implementation. */
5471 if( status == PSA_ERROR_NOT_SUPPORTED )
5472 {
5473 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5474 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5475 }
5476
5477 PSA_ASSERT( status );
5478
5479 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5480
Paul Elliott76bda482021-10-07 17:07:23 +01005481 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5482 input_data->len ) );
5483
Paul Elliott91b021e2021-07-23 18:52:31 +01005484 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5485 additional_data->len ) );
5486
5487 PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len,
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005488 ciphertext, ciphertext_size, &ciphertext_length ) );
Paul Elliott91b021e2021-07-23 18:52:31 +01005489
5490 /* Ensure we can still complete operation. */
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005491 status = psa_aead_finish( &operation, finish_ciphertext,
5492 finish_ciphertext_size,
5493 &ciphertext_length, tag_buffer,
Paul Elliott719c1322021-09-13 18:27:22 +01005494 tag_size, &tag_length );
Paul Elliott91b021e2021-07-23 18:52:31 +01005495
5496 TEST_EQUAL( status, expected_status );
5497
5498exit:
5499 psa_destroy_key( key );
Paul Elliotte58cb1e2021-09-10 18:36:00 +01005500 mbedtls_free( ciphertext );
5501 mbedtls_free( finish_ciphertext );
Paul Elliott4a760882021-09-20 09:42:21 +01005502 mbedtls_free( tag_buffer );
Paul Elliott91b021e2021-07-23 18:52:31 +01005503 psa_aead_abort( &operation );
5504 PSA_DONE( );
5505}
5506/* END_CASE */
Paul Elliott43fbda62021-07-23 18:30:59 +01005507
5508/* BEGIN_CASE */
Paul Elliott9961a662021-09-17 19:19:02 +01005509void aead_multipart_verify( int key_type_arg, data_t *key_data,
5510 int alg_arg,
5511 data_t *nonce,
5512 data_t *additional_data,
5513 data_t *input_data,
5514 data_t *tag,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005515 int tag_usage_arg,
Andrzej Kurekf8816012021-12-19 17:00:12 +01005516 int expected_setup_status_arg,
Paul Elliott9961a662021-09-17 19:19:02 +01005517 int expected_status_arg )
5518{
5519 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5520 psa_key_type_t key_type = key_type_arg;
5521 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005522 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott9961a662021-09-17 19:19:02 +01005523 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5524 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5525 psa_status_t expected_status = expected_status_arg;
Andrzej Kurekf8816012021-12-19 17:00:12 +01005526 psa_status_t expected_setup_status = expected_setup_status_arg;
Paul Elliott9961a662021-09-17 19:19:02 +01005527 unsigned char *plaintext = NULL;
5528 unsigned char *finish_plaintext = NULL;
5529 size_t plaintext_size = 0;
5530 size_t plaintext_length = 0;
5531 size_t verify_plaintext_size = 0;
Paul Elliottbb979e72021-09-22 12:54:42 +01005532 tag_usage_method_t tag_usage = tag_usage_arg;
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005533 unsigned char *tag_buffer = NULL;
5534 size_t tag_size = 0;
Paul Elliott9961a662021-09-17 19:19:02 +01005535
5536 PSA_ASSERT( psa_crypto_init( ) );
5537
5538 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
5539 psa_set_key_algorithm( &attributes, alg );
5540 psa_set_key_type( &attributes, key_type );
5541
5542 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5543 &key ) );
5544
5545 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5546
5547 plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg,
5548 input_data->len );
5549
5550 ASSERT_ALLOC( plaintext, plaintext_size );
5551
5552 verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg );
5553
5554 ASSERT_ALLOC( finish_plaintext, verify_plaintext_size );
5555
Paul Elliott9961a662021-09-17 19:19:02 +01005556 status = psa_aead_decrypt_setup( &operation, key, alg );
5557
5558 /* If the operation is not supported, just skip and not fail in case the
5559 * encryption involves a common limitation of cryptography hardwares and
5560 * an alternative implementation. */
5561 if( status == PSA_ERROR_NOT_SUPPORTED )
5562 {
5563 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
5564 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
5565 }
Andrzej Kurekf8816012021-12-19 17:00:12 +01005566 TEST_EQUAL( status, expected_setup_status );
5567
5568 if( status != PSA_SUCCESS )
5569 goto exit;
Paul Elliott9961a662021-09-17 19:19:02 +01005570
5571 PSA_ASSERT( status );
5572
5573 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5574
Paul Elliottfec6f372021-10-06 17:15:02 +01005575 status = psa_aead_set_lengths( &operation, additional_data->len,
5576 input_data->len );
Andrzej Kurekf8816012021-12-19 17:00:12 +01005577 PSA_ASSERT( status );
Paul Elliottfec6f372021-10-06 17:15:02 +01005578
Paul Elliott9961a662021-09-17 19:19:02 +01005579 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
5580 additional_data->len ) );
5581
5582 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
5583 input_data->len,
5584 plaintext, plaintext_size,
5585 &plaintext_length ) );
5586
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005587 if( tag_usage == USE_GIVEN_TAG )
5588 {
5589 tag_buffer = tag->x;
5590 tag_size = tag->len;
5591 }
5592
Paul Elliott9961a662021-09-17 19:19:02 +01005593 status = psa_aead_verify( &operation, finish_plaintext,
5594 verify_plaintext_size,
5595 &plaintext_length,
Paul Elliott1c67e0b2021-09-19 13:11:50 +01005596 tag_buffer, tag_size );
Paul Elliott9961a662021-09-17 19:19:02 +01005597
5598 TEST_EQUAL( status, expected_status );
5599
5600exit:
5601 psa_destroy_key( key );
5602 mbedtls_free( plaintext );
5603 mbedtls_free( finish_plaintext );
5604 psa_aead_abort( &operation );
5605 PSA_DONE( );
5606}
5607/* END_CASE */
5608
Paul Elliott9961a662021-09-17 19:19:02 +01005609/* BEGIN_CASE */
Paul Elliott5221ef62021-09-19 17:33:03 +01005610void aead_multipart_setup( int key_type_arg, data_t *key_data,
5611 int alg_arg, int expected_status_arg )
5612{
5613 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5614 psa_key_type_t key_type = key_type_arg;
5615 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005616 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliott5221ef62021-09-19 17:33:03 +01005617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5618 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
5619 psa_status_t expected_status = expected_status_arg;
5620
5621 PSA_ASSERT( psa_crypto_init( ) );
5622
5623 psa_set_key_usage_flags( &attributes,
5624 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5625 psa_set_key_algorithm( &attributes, alg );
5626 psa_set_key_type( &attributes, key_type );
5627
5628 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5629 &key ) );
5630
Paul Elliott5221ef62021-09-19 17:33:03 +01005631 status = psa_aead_encrypt_setup( &operation, key, alg );
5632
5633 TEST_EQUAL( status, expected_status );
5634
5635 psa_aead_abort( &operation );
5636
Paul Elliott5221ef62021-09-19 17:33:03 +01005637 status = psa_aead_decrypt_setup( &operation, key, alg );
5638
5639 TEST_EQUAL(status, expected_status );
5640
5641exit:
5642 psa_destroy_key( key );
5643 psa_aead_abort( &operation );
5644 PSA_DONE( );
5645}
5646/* END_CASE */
5647
5648/* BEGIN_CASE */
Paul Elliottc23a9a02021-06-21 18:32:46 +01005649void aead_multipart_state_test( int key_type_arg, data_t *key_data,
5650 int alg_arg,
5651 data_t *nonce,
5652 data_t *additional_data,
5653 data_t *input_data )
5654{
5655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5656 psa_key_type_t key_type = key_type_arg;
5657 psa_algorithm_t alg = alg_arg;
Paul Elliottfbb4c6d2021-09-22 16:44:21 +01005658 psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
Paul Elliottc23a9a02021-06-21 18:32:46 +01005659 unsigned char *output_data = NULL;
5660 unsigned char *final_data = NULL;
5661 size_t output_size = 0;
5662 size_t finish_output_size = 0;
5663 size_t output_length = 0;
5664 size_t key_bits = 0;
5665 size_t tag_length = 0;
5666 size_t tag_size = 0;
5667 size_t nonce_length = 0;
5668 uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE];
5669 uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE];
5670 size_t output_part_length = 0;
5671 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5672
5673 PSA_ASSERT( psa_crypto_init( ) );
5674
5675 psa_set_key_usage_flags( & attributes,
5676 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
5677 psa_set_key_algorithm( & attributes, alg );
5678 psa_set_key_type( & attributes, key_type );
5679
5680 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5681 &key ) );
5682
5683 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
5684 key_bits = psa_get_key_bits( &attributes );
5685
5686 tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg );
5687
Gilles Peskine7be11a72022-04-14 00:12:57 +02005688 TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005689
5690 output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len );
5691
5692 ASSERT_ALLOC( output_data, output_size );
5693
5694 finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg );
5695
Gilles Peskine7be11a72022-04-14 00:12:57 +02005696 TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01005697
5698 ASSERT_ALLOC( final_data, finish_output_size );
5699
5700 /* Test all operations error without calling setup first. */
5701
Paul Elliottc23a9a02021-06-21 18:32:46 +01005702 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5703 PSA_ERROR_BAD_STATE );
5704
5705 psa_aead_abort( &operation );
5706
Paul Elliottc23a9a02021-06-21 18:32:46 +01005707 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5708 PSA_AEAD_NONCE_MAX_SIZE,
5709 &nonce_length ),
5710 PSA_ERROR_BAD_STATE );
5711
5712 psa_aead_abort( &operation );
5713
Paul Elliott481be342021-07-16 17:38:47 +01005714 /* ------------------------------------------------------- */
5715
Paul Elliottc23a9a02021-06-21 18:32:46 +01005716 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
5717 input_data->len ),
5718 PSA_ERROR_BAD_STATE );
5719
5720 psa_aead_abort( &operation );
5721
Paul Elliott481be342021-07-16 17:38:47 +01005722 /* ------------------------------------------------------- */
5723
Paul Elliottc23a9a02021-06-21 18:32:46 +01005724 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5725 additional_data->len ),
5726 PSA_ERROR_BAD_STATE );
5727
5728 psa_aead_abort( &operation );
5729
Paul Elliott481be342021-07-16 17:38:47 +01005730 /* ------------------------------------------------------- */
5731
Paul Elliottc23a9a02021-06-21 18:32:46 +01005732 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5733 input_data->len, output_data,
5734 output_size, &output_length ),
5735 PSA_ERROR_BAD_STATE );
5736
5737 psa_aead_abort( &operation );
5738
Paul Elliott481be342021-07-16 17:38:47 +01005739 /* ------------------------------------------------------- */
5740
Paul Elliottc23a9a02021-06-21 18:32:46 +01005741 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5742 finish_output_size,
5743 &output_part_length,
5744 tag_buffer, tag_length,
5745 &tag_size ),
5746 PSA_ERROR_BAD_STATE );
5747
5748 psa_aead_abort( &operation );
5749
Paul Elliott481be342021-07-16 17:38:47 +01005750 /* ------------------------------------------------------- */
5751
Paul Elliottc23a9a02021-06-21 18:32:46 +01005752 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5753 finish_output_size,
5754 &output_part_length,
5755 tag_buffer,
5756 tag_length ),
5757 PSA_ERROR_BAD_STATE );
5758
5759 psa_aead_abort( &operation );
5760
5761 /* Test for double setups. */
5762
Paul Elliottc23a9a02021-06-21 18:32:46 +01005763 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5764
5765 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5766 PSA_ERROR_BAD_STATE );
5767
5768 psa_aead_abort( &operation );
5769
Paul Elliott481be342021-07-16 17:38:47 +01005770 /* ------------------------------------------------------- */
5771
Paul Elliottc23a9a02021-06-21 18:32:46 +01005772 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5773
5774 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5775 PSA_ERROR_BAD_STATE );
5776
5777 psa_aead_abort( &operation );
5778
Paul Elliott374a2be2021-07-16 17:53:40 +01005779 /* ------------------------------------------------------- */
5780
Paul Elliott374a2be2021-07-16 17:53:40 +01005781 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5782
5783 TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ),
5784 PSA_ERROR_BAD_STATE );
5785
5786 psa_aead_abort( &operation );
5787
5788 /* ------------------------------------------------------- */
5789
Paul Elliott374a2be2021-07-16 17:53:40 +01005790 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5791
5792 TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ),
5793 PSA_ERROR_BAD_STATE );
5794
5795 psa_aead_abort( &operation );
5796
Paul Elliottc23a9a02021-06-21 18:32:46 +01005797 /* Test for not setting a nonce. */
5798
Paul Elliottc23a9a02021-06-21 18:32:46 +01005799 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5800
5801 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
5802 additional_data->len ),
5803 PSA_ERROR_BAD_STATE );
5804
5805 psa_aead_abort( &operation );
5806
Paul Elliott7f628422021-09-01 12:08:29 +01005807 /* ------------------------------------------------------- */
5808
Paul Elliott7f628422021-09-01 12:08:29 +01005809 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5810
5811 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
5812 input_data->len, output_data,
5813 output_size, &output_length ),
5814 PSA_ERROR_BAD_STATE );
5815
5816 psa_aead_abort( &operation );
5817
Paul Elliottbdc2c682021-09-21 18:37:10 +01005818 /* ------------------------------------------------------- */
5819
Paul Elliottbdc2c682021-09-21 18:37:10 +01005820 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5821
5822 TEST_EQUAL( psa_aead_finish( &operation, final_data,
5823 finish_output_size,
5824 &output_part_length,
5825 tag_buffer, tag_length,
5826 &tag_size ),
5827 PSA_ERROR_BAD_STATE );
5828
5829 psa_aead_abort( &operation );
5830
5831 /* ------------------------------------------------------- */
5832
Paul Elliottbdc2c682021-09-21 18:37:10 +01005833 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
5834
5835 TEST_EQUAL( psa_aead_verify( &operation, final_data,
5836 finish_output_size,
5837 &output_part_length,
5838 tag_buffer,
5839 tag_length ),
5840 PSA_ERROR_BAD_STATE );
5841
5842 psa_aead_abort( &operation );
5843
Paul Elliottc23a9a02021-06-21 18:32:46 +01005844 /* Test for double setting nonce. */
5845
Paul Elliottc23a9a02021-06-21 18:32:46 +01005846 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5847
5848 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5849
5850 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5851 PSA_ERROR_BAD_STATE );
5852
5853 psa_aead_abort( &operation );
5854
Paul Elliott374a2be2021-07-16 17:53:40 +01005855 /* Test for double generating nonce. */
5856
Paul Elliott374a2be2021-07-16 17:53:40 +01005857 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5858
5859 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5860 PSA_AEAD_NONCE_MAX_SIZE,
5861 &nonce_length ) );
5862
5863 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5864 PSA_AEAD_NONCE_MAX_SIZE,
5865 &nonce_length ),
5866 PSA_ERROR_BAD_STATE );
5867
5868
5869 psa_aead_abort( &operation );
5870
5871 /* Test for generate nonce then set and vice versa */
5872
Paul Elliott374a2be2021-07-16 17:53:40 +01005873 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5874
5875 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5876 PSA_AEAD_NONCE_MAX_SIZE,
5877 &nonce_length ) );
5878
5879 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5880 PSA_ERROR_BAD_STATE );
5881
5882 psa_aead_abort( &operation );
5883
Andrzej Kurekad837522021-12-15 15:28:49 +01005884 /* Test for generating nonce after calling set lengths */
5885
5886 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5887
5888 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5889 input_data->len ) );
5890
5891 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5892 PSA_AEAD_NONCE_MAX_SIZE,
5893 &nonce_length ) );
5894
5895 psa_aead_abort( &operation );
5896
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005897 /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005898
5899 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5900
5901 if( operation.alg == PSA_ALG_CCM )
5902 {
5903 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5904 input_data->len ),
5905 PSA_ERROR_INVALID_ARGUMENT );
5906 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5907 PSA_AEAD_NONCE_MAX_SIZE,
5908 &nonce_length ),
5909 PSA_ERROR_BAD_STATE );
5910 }
5911 else
5912 {
5913 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5914 input_data->len ) );
5915 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5916 PSA_AEAD_NONCE_MAX_SIZE,
5917 &nonce_length ) );
5918 }
5919
5920 psa_aead_abort( &operation );
5921
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005922 /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurekad837522021-12-15 15:28:49 +01005923#if SIZE_MAX > UINT32_MAX
5924 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5925
5926 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
5927 {
5928 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
5929 input_data->len ),
5930 PSA_ERROR_INVALID_ARGUMENT );
5931 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
5932 PSA_AEAD_NONCE_MAX_SIZE,
5933 &nonce_length ),
5934 PSA_ERROR_BAD_STATE );
5935 }
5936 else
5937 {
5938 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
5939 input_data->len ) );
5940 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5941 PSA_AEAD_NONCE_MAX_SIZE,
5942 &nonce_length ) );
5943 }
5944
5945 psa_aead_abort( &operation );
5946#endif
5947
Andrzej Kurek031df4a2022-01-19 12:44:49 -05005948 /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */
Andrzej Kurekad837522021-12-15 15:28:49 +01005949
5950 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5951
5952 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
5953 PSA_AEAD_NONCE_MAX_SIZE,
5954 &nonce_length ) );
5955
5956 if( operation.alg == PSA_ALG_CCM )
5957 {
5958 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5959 input_data->len ),
5960 PSA_ERROR_INVALID_ARGUMENT );
5961 }
5962 else
5963 {
5964 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5965 input_data->len ) );
5966 }
5967
5968 psa_aead_abort( &operation );
5969
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005970 /* ------------------------------------------------------- */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005971 /* Test for setting nonce after calling set lengths */
5972
5973 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5974
5975 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
5976 input_data->len ) );
5977
5978 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5979
5980 psa_aead_abort( &operation );
5981
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01005982 /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01005983
5984 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
5985
5986 if( operation.alg == PSA_ALG_CCM )
5987 {
5988 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
5989 input_data->len ),
5990 PSA_ERROR_INVALID_ARGUMENT );
5991 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
5992 PSA_ERROR_BAD_STATE );
5993 }
5994 else
5995 {
5996 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
5997 input_data->len ) );
5998 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
5999 }
6000
6001 psa_aead_abort( &operation );
6002
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006003 /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006004#if SIZE_MAX > UINT32_MAX
6005 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6006
6007 if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM )
6008 {
6009 TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX,
6010 input_data->len ),
6011 PSA_ERROR_INVALID_ARGUMENT );
6012 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6013 PSA_ERROR_BAD_STATE );
6014 }
6015 else
6016 {
6017 PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX,
6018 input_data->len ) );
6019 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6020 }
6021
6022 psa_aead_abort( &operation );
6023#endif
6024
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006025 /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */
Andrzej Kurek1e8e1742021-12-25 23:50:53 +01006026
6027 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6028
6029 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6030
6031 if( operation.alg == PSA_ALG_CCM )
6032 {
6033 TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX,
6034 input_data->len ),
6035 PSA_ERROR_INVALID_ARGUMENT );
6036 }
6037 else
6038 {
6039 PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX,
6040 input_data->len ) );
6041 }
6042
6043 psa_aead_abort( &operation );
Andrzej Kurekad837522021-12-15 15:28:49 +01006044
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006045 /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006046#if SIZE_MAX > UINT32_MAX
6047 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6048
6049 if( operation.alg == PSA_ALG_GCM )
6050 {
6051 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6052 SIZE_MAX ),
6053 PSA_ERROR_INVALID_ARGUMENT );
6054 TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ),
6055 PSA_ERROR_BAD_STATE );
6056 }
6057 else if ( operation.alg != PSA_ALG_CCM )
6058 {
6059 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6060 SIZE_MAX ) );
6061 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6062 }
6063
6064 psa_aead_abort( &operation );
6065
Andrzej Kurek031df4a2022-01-19 12:44:49 -05006066 /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */
Andrzej Kureke5f94fb2021-12-26 01:00:20 +01006067 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6068
6069 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6070
6071 if( operation.alg == PSA_ALG_GCM )
6072 {
6073 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6074 SIZE_MAX ),
6075 PSA_ERROR_INVALID_ARGUMENT );
6076 }
6077 else if ( operation.alg != PSA_ALG_CCM )
6078 {
6079 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6080 SIZE_MAX ) );
6081 }
6082
6083 psa_aead_abort( &operation );
6084#endif
Paul Elliott374a2be2021-07-16 17:53:40 +01006085
6086 /* ------------------------------------------------------- */
6087
Paul Elliott374a2be2021-07-16 17:53:40 +01006088 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6089
6090 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6091
6092 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6093 PSA_AEAD_NONCE_MAX_SIZE,
6094 &nonce_length ),
6095 PSA_ERROR_BAD_STATE );
6096
6097 psa_aead_abort( &operation );
6098
Paul Elliott7220cae2021-06-22 17:25:57 +01006099 /* Test for generating nonce in decrypt setup. */
6100
Paul Elliott7220cae2021-06-22 17:25:57 +01006101 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6102
6103 TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer,
6104 PSA_AEAD_NONCE_MAX_SIZE,
6105 &nonce_length ),
6106 PSA_ERROR_BAD_STATE );
6107
6108 psa_aead_abort( &operation );
6109
Paul Elliottc23a9a02021-06-21 18:32:46 +01006110 /* Test for setting lengths twice. */
6111
Paul Elliottc23a9a02021-06-21 18:32:46 +01006112 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6113
6114 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6115
6116 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6117 input_data->len ) );
6118
6119 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6120 input_data->len ),
6121 PSA_ERROR_BAD_STATE );
6122
6123 psa_aead_abort( &operation );
6124
Andrzej Kurekad837522021-12-15 15:28:49 +01006125 /* Test for setting lengths after setting nonce + already starting data. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006126
Paul Elliottc23a9a02021-06-21 18:32:46 +01006127 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6128
6129 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6130
Andrzej Kurekad837522021-12-15 15:28:49 +01006131 if( operation.alg == PSA_ALG_CCM )
6132 {
Paul Elliottf94bd992021-09-19 18:15:59 +01006133
Andrzej Kurekad837522021-12-15 15:28:49 +01006134 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6135 additional_data->len ),
6136 PSA_ERROR_BAD_STATE );
6137 }
6138 else
6139 {
6140 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6141 additional_data->len ) );
Paul Elliottf94bd992021-09-19 18:15:59 +01006142
Andrzej Kurekad837522021-12-15 15:28:49 +01006143 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6144 input_data->len ),
6145 PSA_ERROR_BAD_STATE );
6146 }
Paul Elliottf94bd992021-09-19 18:15:59 +01006147 psa_aead_abort( &operation );
6148
6149 /* ------------------------------------------------------- */
6150
Paul Elliottf94bd992021-09-19 18:15:59 +01006151 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6152
6153 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6154
Andrzej Kurekad837522021-12-15 15:28:49 +01006155 if( operation.alg == PSA_ALG_CCM )
6156 {
6157 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6158 input_data->len, output_data,
6159 output_size, &output_length ),
6160 PSA_ERROR_BAD_STATE );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006161
Andrzej Kurekad837522021-12-15 15:28:49 +01006162 }
6163 else
6164 {
6165 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6166 input_data->len, output_data,
6167 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006168
Andrzej Kurekad837522021-12-15 15:28:49 +01006169 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6170 input_data->len ),
6171 PSA_ERROR_BAD_STATE );
6172 }
6173 psa_aead_abort( &operation );
6174
6175 /* ------------------------------------------------------- */
6176
6177 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6178
6179 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6180
6181 if( operation.alg == PSA_ALG_CCM )
6182 {
6183 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6184 finish_output_size,
6185 &output_part_length,
6186 tag_buffer, tag_length,
6187 &tag_size ) );
6188 }
6189 else
6190 {
6191 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6192 finish_output_size,
6193 &output_part_length,
6194 tag_buffer, tag_length,
6195 &tag_size ) );
6196
6197 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6198 input_data->len ),
6199 PSA_ERROR_BAD_STATE );
6200 }
6201 psa_aead_abort( &operation );
6202
6203 /* Test for setting lengths after generating nonce + already starting data. */
6204
6205 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6206
6207 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6208 PSA_AEAD_NONCE_MAX_SIZE,
6209 &nonce_length ) );
6210 if( operation.alg == PSA_ALG_CCM )
6211 {
6212
6213 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6214 additional_data->len ),
6215 PSA_ERROR_BAD_STATE );
6216 }
6217 else
6218 {
6219 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6220 additional_data->len ) );
6221
6222 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6223 input_data->len ),
6224 PSA_ERROR_BAD_STATE );
6225 }
6226 psa_aead_abort( &operation );
6227
6228 /* ------------------------------------------------------- */
6229
6230 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6231
6232 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6233 PSA_AEAD_NONCE_MAX_SIZE,
6234 &nonce_length ) );
6235 if( operation.alg == PSA_ALG_CCM )
6236 {
6237 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6238 input_data->len, output_data,
6239 output_size, &output_length ),
6240 PSA_ERROR_BAD_STATE );
6241
6242 }
6243 else
6244 {
6245 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6246 input_data->len, output_data,
6247 output_size, &output_length ) );
6248
6249 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6250 input_data->len ),
6251 PSA_ERROR_BAD_STATE );
6252 }
6253 psa_aead_abort( &operation );
6254
6255 /* ------------------------------------------------------- */
6256
6257 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6258
6259 PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer,
6260 PSA_AEAD_NONCE_MAX_SIZE,
6261 &nonce_length ) );
6262 if( operation.alg == PSA_ALG_CCM )
6263 {
6264 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6265 finish_output_size,
6266 &output_part_length,
6267 tag_buffer, tag_length,
6268 &tag_size ) );
6269 }
6270 else
6271 {
6272 PSA_ASSERT( psa_aead_finish( &operation, final_data,
6273 finish_output_size,
6274 &output_part_length,
6275 tag_buffer, tag_length,
6276 &tag_size ) );
6277
6278 TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len,
6279 input_data->len ),
6280 PSA_ERROR_BAD_STATE );
6281 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006282 psa_aead_abort( &operation );
6283
Paul Elliott243080c2021-07-21 19:01:17 +01006284 /* Test for not sending any additional data or data after setting non zero
6285 * lengths for them. (encrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006286
Paul Elliottc23a9a02021-06-21 18:32:46 +01006287 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6288
6289 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6290
6291 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6292 input_data->len ) );
6293
6294 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6295 finish_output_size,
6296 &output_part_length,
6297 tag_buffer, tag_length,
6298 &tag_size ),
6299 PSA_ERROR_INVALID_ARGUMENT );
6300
6301 psa_aead_abort( &operation );
6302
Paul Elliott243080c2021-07-21 19:01:17 +01006303 /* Test for not sending any additional data or data after setting non-zero
6304 * lengths for them. (decrypt) */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006305
Paul Elliottc23a9a02021-06-21 18:32:46 +01006306 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6307
6308 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6309
6310 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6311 input_data->len ) );
6312
6313 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6314 finish_output_size,
6315 &output_part_length,
6316 tag_buffer,
6317 tag_length ),
6318 PSA_ERROR_INVALID_ARGUMENT );
6319
6320 psa_aead_abort( &operation );
6321
Paul Elliott243080c2021-07-21 19:01:17 +01006322 /* Test for not sending any additional data after setting a non-zero length
6323 * for it. */
Paul Elliottc23a9a02021-06-21 18:32:46 +01006324
Paul Elliottc23a9a02021-06-21 18:32:46 +01006325 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6326
6327 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6328
6329 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6330 input_data->len ) );
6331
6332 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6333 input_data->len, output_data,
6334 output_size, &output_length ),
6335 PSA_ERROR_INVALID_ARGUMENT );
6336
6337 psa_aead_abort( &operation );
6338
Paul Elliottf94bd992021-09-19 18:15:59 +01006339 /* Test for not sending any data after setting a non-zero length for it.*/
6340
Paul Elliottf94bd992021-09-19 18:15:59 +01006341 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6342
6343 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6344
6345 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6346 input_data->len ) );
6347
6348 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6349 additional_data->len ) );
6350
6351 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6352 finish_output_size,
6353 &output_part_length,
6354 tag_buffer, tag_length,
6355 &tag_size ),
6356 PSA_ERROR_INVALID_ARGUMENT );
6357
6358 psa_aead_abort( &operation );
6359
Paul Elliottb0450fe2021-09-01 15:06:26 +01006360 /* Test for sending too much additional data after setting lengths. */
6361
Paul Elliottb0450fe2021-09-01 15:06:26 +01006362 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6363
6364 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6365
6366 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6367
6368
6369 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6370 additional_data->len ),
6371 PSA_ERROR_INVALID_ARGUMENT );
6372
6373 psa_aead_abort( &operation );
6374
Paul Elliotta2a09b02021-09-22 14:56:40 +01006375 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006376
6377 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6378
6379 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6380
6381 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6382 input_data->len ) );
6383
6384 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6385 additional_data->len ) );
6386
6387 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6388 1 ),
6389 PSA_ERROR_INVALID_ARGUMENT );
6390
6391 psa_aead_abort( &operation );
6392
Paul Elliottb0450fe2021-09-01 15:06:26 +01006393 /* Test for sending too much data after setting lengths. */
6394
Paul Elliottb0450fe2021-09-01 15:06:26 +01006395 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6396
6397 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6398
6399 PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) );
6400
6401 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6402 input_data->len, output_data,
6403 output_size, &output_length ),
6404 PSA_ERROR_INVALID_ARGUMENT );
6405
6406 psa_aead_abort( &operation );
6407
Paul Elliotta2a09b02021-09-22 14:56:40 +01006408 /* ------------------------------------------------------- */
Paul Elliottfd0c154c2021-09-17 18:03:52 +01006409
6410 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6411
6412 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6413
6414 PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len,
6415 input_data->len ) );
6416
6417 PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x,
6418 additional_data->len ) );
6419
6420 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6421 input_data->len, output_data,
6422 output_size, &output_length ) );
6423
6424 TEST_EQUAL( psa_aead_update( &operation, input_data->x,
6425 1, output_data,
6426 output_size, &output_length ),
6427 PSA_ERROR_INVALID_ARGUMENT );
6428
6429 psa_aead_abort( &operation );
6430
Paul Elliottc23a9a02021-06-21 18:32:46 +01006431 /* Test sending additional data after data. */
6432
Paul Elliottc23a9a02021-06-21 18:32:46 +01006433 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6434
6435 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6436
Andrzej Kurekad837522021-12-15 15:28:49 +01006437 if( operation.alg != PSA_ALG_CCM )
6438 {
6439 PSA_ASSERT( psa_aead_update( &operation, input_data->x,
6440 input_data->len, output_data,
6441 output_size, &output_length ) );
Paul Elliottc23a9a02021-06-21 18:32:46 +01006442
Andrzej Kurekad837522021-12-15 15:28:49 +01006443 TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x,
6444 additional_data->len ),
6445 PSA_ERROR_BAD_STATE );
6446 }
Paul Elliottc23a9a02021-06-21 18:32:46 +01006447 psa_aead_abort( &operation );
6448
Paul Elliott534d0b42021-06-22 19:15:20 +01006449 /* Test calling finish on decryption. */
6450
Paul Elliott534d0b42021-06-22 19:15:20 +01006451 PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) );
6452
6453 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6454
6455 TEST_EQUAL( psa_aead_finish( &operation, final_data,
6456 finish_output_size,
6457 &output_part_length,
6458 tag_buffer, tag_length,
6459 &tag_size ),
6460 PSA_ERROR_BAD_STATE );
6461
6462 psa_aead_abort( &operation );
6463
6464 /* Test calling verify on encryption. */
6465
Paul Elliott534d0b42021-06-22 19:15:20 +01006466 PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) );
6467
6468 PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) );
6469
6470 TEST_EQUAL( psa_aead_verify( &operation, final_data,
6471 finish_output_size,
6472 &output_part_length,
6473 tag_buffer,
6474 tag_length ),
Paul Elliott5b065cb2021-06-23 08:33:22 +01006475 PSA_ERROR_BAD_STATE );
Paul Elliott534d0b42021-06-22 19:15:20 +01006476
6477 psa_aead_abort( &operation );
6478
6479
Paul Elliottc23a9a02021-06-21 18:32:46 +01006480exit:
6481 psa_destroy_key( key );
6482 psa_aead_abort( &operation );
6483 mbedtls_free( output_data );
6484 mbedtls_free( final_data );
6485 PSA_DONE( );
6486}
6487/* END_CASE */
6488
6489/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006490void signature_size( int type_arg,
6491 int bits,
6492 int alg_arg,
6493 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006494{
6495 psa_key_type_t type = type_arg;
6496 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006497 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006498
Gilles Peskinefe11b722018-12-18 00:24:04 +01006499 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01006500
Gilles Peskinee59236f2018-01-27 23:32:46 +01006501exit:
6502 ;
6503}
6504/* END_CASE */
6505
6506/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006507void sign_hash_deterministic( int key_type_arg, data_t *key_data,
6508 int alg_arg, data_t *input_data,
6509 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006510{
Ronald Cron5425a212020-08-04 14:58:35 +02006511 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006512 psa_key_type_t key_type = key_type_arg;
6513 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006514 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01006515 unsigned char *signature = NULL;
6516 size_t signature_size;
6517 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006518 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006519
Gilles Peskine8817f612018-12-18 00:18:46 +01006520 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006521
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006522 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006523 psa_set_key_algorithm( &attributes, alg );
6524 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006525
Gilles Peskine049c7532019-05-15 20:22:09 +02006526 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006527 &key ) );
6528 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006529 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01006530
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006531 /* Allocate a buffer which has the size advertised by the
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006532 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006533 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02006534 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01006535 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006536 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006537 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006538
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006539 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006540 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006541 input_data->x, input_data->len,
6542 signature, signature_size,
6543 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006544 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02006545 ASSERT_COMPARE( output_data->x, output_data->len,
6546 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01006547
6548exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006549 /*
6550 * Key attributes may have been returned by psa_get_key_attributes()
6551 * thus reset them as required.
6552 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006553 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006554
Ronald Cron5425a212020-08-04 14:58:35 +02006555 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01006556 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006557 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006558}
6559/* END_CASE */
6560
6561/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006562void sign_hash_fail( int key_type_arg, data_t *key_data,
6563 int alg_arg, data_t *input_data,
6564 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01006565{
Ronald Cron5425a212020-08-04 14:58:35 +02006566 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006567 psa_key_type_t key_type = key_type_arg;
6568 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006569 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01006570 psa_status_t actual_status;
6571 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01006572 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01006573 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01006575
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006576 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006577
Gilles Peskine8817f612018-12-18 00:18:46 +01006578 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006579
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006580 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006581 psa_set_key_algorithm( &attributes, alg );
6582 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07006583
Gilles Peskine049c7532019-05-15 20:22:09 +02006584 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006585 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01006586
Ronald Cron5425a212020-08-04 14:58:35 +02006587 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006588 input_data->x, input_data->len,
6589 signature, signature_size,
6590 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006591 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02006592 /* The value of *signature_length is unspecified on error, but
6593 * whatever it is, it should be less than signature_size, so that
6594 * if the caller tries to read *signature_length bytes without
6595 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006596 TEST_LE_U( signature_length, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01006597
6598exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006599 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006600 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01006601 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006602 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01006603}
6604/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03006605
6606/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006607void sign_verify_hash( int key_type_arg, data_t *key_data,
6608 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02006609{
Ronald Cron5425a212020-08-04 14:58:35 +02006610 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006611 psa_key_type_t key_type = key_type_arg;
6612 psa_algorithm_t alg = alg_arg;
6613 size_t key_bits;
6614 unsigned char *signature = NULL;
6615 size_t signature_size;
6616 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006617 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02006618
Gilles Peskine8817f612018-12-18 00:18:46 +01006619 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006620
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006621 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006622 psa_set_key_algorithm( &attributes, alg );
6623 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02006624
Gilles Peskine049c7532019-05-15 20:22:09 +02006625 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006626 &key ) );
6627 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006628 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02006629
Shaun Case8b0ecbc2021-12-20 21:14:10 -08006630 /* Allocate a buffer which has the size advertised by the
Gilles Peskine9911b022018-06-29 17:30:48 +02006631 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006632 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02006633 key_bits, alg );
6634 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006635 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02006636 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006637
6638 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02006639 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006640 input_data->x, input_data->len,
6641 signature, signature_size,
6642 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006643 /* Check that the signature length looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006644 TEST_LE_U( signature_length, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02006645 TEST_ASSERT( signature_length > 0 );
6646
6647 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02006648 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006649 input_data->x, input_data->len,
6650 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02006651
6652 if( input_data->len != 0 )
6653 {
6654 /* Flip a bit in the input and verify that the signature is now
6655 * detected as invalid. Flip a bit at the beginning, not at the end,
6656 * because ECDSA may ignore the last few bits of the input. */
6657 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02006658 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006659 input_data->x, input_data->len,
6660 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01006661 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02006662 }
6663
6664exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006665 /*
6666 * Key attributes may have been returned by psa_get_key_attributes()
6667 * thus reset them as required.
6668 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006669 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01006670
Ronald Cron5425a212020-08-04 14:58:35 +02006671 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02006672 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006673 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02006674}
6675/* END_CASE */
6676
6677/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006678void verify_hash( int key_type_arg, data_t *key_data,
6679 int alg_arg, data_t *hash_data,
6680 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03006681{
Ronald Cron5425a212020-08-04 14:58:35 +02006682 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006683 psa_key_type_t key_type = key_type_arg;
6684 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03006686
Gilles Peskine7be11a72022-04-14 00:12:57 +02006687 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02006688
Gilles Peskine8817f612018-12-18 00:18:46 +01006689 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03006690
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006691 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006692 psa_set_key_algorithm( &attributes, alg );
6693 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03006694
Gilles Peskine049c7532019-05-15 20:22:09 +02006695 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006696 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03006697
Ronald Cron5425a212020-08-04 14:58:35 +02006698 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006699 hash_data->x, hash_data->len,
6700 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01006701
itayzafrir5c753392018-05-08 11:18:38 +03006702exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006703 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006704 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006705 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03006706}
6707/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006708
6709/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02006710void verify_hash_fail( int key_type_arg, data_t *key_data,
6711 int alg_arg, data_t *hash_data,
6712 data_t *signature_data,
6713 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006714{
Ronald Cron5425a212020-08-04 14:58:35 +02006715 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006716 psa_key_type_t key_type = key_type_arg;
6717 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006718 psa_status_t actual_status;
6719 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006721
Gilles Peskine8817f612018-12-18 00:18:46 +01006722 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006723
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02006725 psa_set_key_algorithm( &attributes, alg );
6726 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03006727
Gilles Peskine049c7532019-05-15 20:22:09 +02006728 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02006729 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006730
Ronald Cron5425a212020-08-04 14:58:35 +02006731 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01006732 hash_data->x, hash_data->len,
6733 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01006734 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006735
6736exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02006737 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02006738 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02006739 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006740}
6741/* END_CASE */
6742
Nir Sonnenschein39e59142018-05-02 23:16:26 +03006743/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02006744void sign_message_deterministic( int key_type_arg,
6745 data_t *key_data,
6746 int alg_arg,
6747 data_t *input_data,
6748 data_t *output_data )
6749{
6750 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6751 psa_key_type_t key_type = key_type_arg;
6752 psa_algorithm_t alg = alg_arg;
6753 size_t key_bits;
6754 unsigned char *signature = NULL;
6755 size_t signature_size;
6756 size_t signature_length = 0xdeadbeef;
6757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6758
6759 PSA_ASSERT( psa_crypto_init( ) );
6760
6761 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6762 psa_set_key_algorithm( &attributes, alg );
6763 psa_set_key_type( &attributes, key_type );
6764
6765 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6766 &key ) );
6767 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6768 key_bits = psa_get_key_bits( &attributes );
6769
6770 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6771 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006772 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006773 ASSERT_ALLOC( signature, signature_size );
6774
6775 PSA_ASSERT( psa_sign_message( key, alg,
6776 input_data->x, input_data->len,
6777 signature, signature_size,
6778 &signature_length ) );
6779
6780 ASSERT_COMPARE( output_data->x, output_data->len,
6781 signature, signature_length );
6782
6783exit:
6784 psa_reset_key_attributes( &attributes );
6785
6786 psa_destroy_key( key );
6787 mbedtls_free( signature );
6788 PSA_DONE( );
6789
6790}
6791/* END_CASE */
6792
6793/* BEGIN_CASE */
6794void sign_message_fail( int key_type_arg,
6795 data_t *key_data,
6796 int alg_arg,
6797 data_t *input_data,
6798 int signature_size_arg,
6799 int expected_status_arg )
6800{
6801 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6802 psa_key_type_t key_type = key_type_arg;
6803 psa_algorithm_t alg = alg_arg;
6804 size_t signature_size = signature_size_arg;
6805 psa_status_t actual_status;
6806 psa_status_t expected_status = expected_status_arg;
6807 unsigned char *signature = NULL;
6808 size_t signature_length = 0xdeadbeef;
6809 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6810
6811 ASSERT_ALLOC( signature, signature_size );
6812
6813 PSA_ASSERT( psa_crypto_init( ) );
6814
6815 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
6816 psa_set_key_algorithm( &attributes, alg );
6817 psa_set_key_type( &attributes, key_type );
6818
6819 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6820 &key ) );
6821
6822 actual_status = psa_sign_message( key, alg,
6823 input_data->x, input_data->len,
6824 signature, signature_size,
6825 &signature_length );
6826 TEST_EQUAL( actual_status, expected_status );
6827 /* The value of *signature_length is unspecified on error, but
6828 * whatever it is, it should be less than signature_size, so that
6829 * if the caller tries to read *signature_length bytes without
6830 * checking the error code then they don't overflow a buffer. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02006831 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006832
6833exit:
6834 psa_reset_key_attributes( &attributes );
6835 psa_destroy_key( key );
6836 mbedtls_free( signature );
6837 PSA_DONE( );
6838}
6839/* END_CASE */
6840
6841/* BEGIN_CASE */
6842void sign_verify_message( int key_type_arg,
6843 data_t *key_data,
6844 int alg_arg,
6845 data_t *input_data )
6846{
6847 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6848 psa_key_type_t key_type = key_type_arg;
6849 psa_algorithm_t alg = alg_arg;
6850 size_t key_bits;
6851 unsigned char *signature = NULL;
6852 size_t signature_size;
6853 size_t signature_length = 0xdeadbeef;
6854 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6855
6856 PSA_ASSERT( psa_crypto_init( ) );
6857
6858 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
6859 PSA_KEY_USAGE_VERIFY_MESSAGE );
6860 psa_set_key_algorithm( &attributes, alg );
6861 psa_set_key_type( &attributes, key_type );
6862
6863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6864 &key ) );
6865 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
6866 key_bits = psa_get_key_bits( &attributes );
6867
6868 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
6869 TEST_ASSERT( signature_size != 0 );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006870 TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006871 ASSERT_ALLOC( signature, signature_size );
6872
6873 PSA_ASSERT( psa_sign_message( key, alg,
6874 input_data->x, input_data->len,
6875 signature, signature_size,
6876 &signature_length ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02006877 TEST_LE_U( signature_length, signature_size );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006878 TEST_ASSERT( signature_length > 0 );
6879
6880 PSA_ASSERT( psa_verify_message( key, alg,
6881 input_data->x, input_data->len,
6882 signature, signature_length ) );
6883
6884 if( input_data->len != 0 )
6885 {
6886 /* Flip a bit in the input and verify that the signature is now
6887 * detected as invalid. Flip a bit at the beginning, not at the end,
6888 * because ECDSA may ignore the last few bits of the input. */
6889 input_data->x[0] ^= 1;
6890 TEST_EQUAL( psa_verify_message( key, alg,
6891 input_data->x, input_data->len,
6892 signature, signature_length ),
6893 PSA_ERROR_INVALID_SIGNATURE );
6894 }
6895
6896exit:
6897 psa_reset_key_attributes( &attributes );
6898
6899 psa_destroy_key( key );
6900 mbedtls_free( signature );
6901 PSA_DONE( );
6902}
6903/* END_CASE */
6904
6905/* BEGIN_CASE */
6906void verify_message( int key_type_arg,
6907 data_t *key_data,
6908 int alg_arg,
6909 data_t *input_data,
6910 data_t *signature_data )
6911{
6912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6913 psa_key_type_t key_type = key_type_arg;
6914 psa_algorithm_t alg = alg_arg;
6915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6916
Gilles Peskine7be11a72022-04-14 00:12:57 +02006917 TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE );
gabor-mezei-arm53028482021-04-15 18:19:50 +02006918
6919 PSA_ASSERT( psa_crypto_init( ) );
6920
6921 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6922 psa_set_key_algorithm( &attributes, alg );
6923 psa_set_key_type( &attributes, key_type );
6924
6925 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6926 &key ) );
6927
6928 PSA_ASSERT( psa_verify_message( key, alg,
6929 input_data->x, input_data->len,
6930 signature_data->x, signature_data->len ) );
6931
6932exit:
6933 psa_reset_key_attributes( &attributes );
6934 psa_destroy_key( key );
6935 PSA_DONE( );
6936}
6937/* END_CASE */
6938
6939/* BEGIN_CASE */
6940void verify_message_fail( int key_type_arg,
6941 data_t *key_data,
6942 int alg_arg,
6943 data_t *hash_data,
6944 data_t *signature_data,
6945 int expected_status_arg )
6946{
6947 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
6948 psa_key_type_t key_type = key_type_arg;
6949 psa_algorithm_t alg = alg_arg;
6950 psa_status_t actual_status;
6951 psa_status_t expected_status = expected_status_arg;
6952 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
6953
6954 PSA_ASSERT( psa_crypto_init( ) );
6955
6956 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
6957 psa_set_key_algorithm( &attributes, alg );
6958 psa_set_key_type( &attributes, key_type );
6959
6960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
6961 &key ) );
6962
6963 actual_status = psa_verify_message( key, alg,
6964 hash_data->x, hash_data->len,
6965 signature_data->x,
6966 signature_data->len );
6967 TEST_EQUAL( actual_status, expected_status );
6968
6969exit:
6970 psa_reset_key_attributes( &attributes );
6971 psa_destroy_key( key );
6972 PSA_DONE( );
6973}
6974/* END_CASE */
6975
6976/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02006977void asymmetric_encrypt( int key_type_arg,
6978 data_t *key_data,
6979 int alg_arg,
6980 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02006981 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02006982 int expected_output_length_arg,
6983 int expected_status_arg )
6984{
Ronald Cron5425a212020-08-04 14:58:35 +02006985 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006986 psa_key_type_t key_type = key_type_arg;
6987 psa_algorithm_t alg = alg_arg;
6988 size_t expected_output_length = expected_output_length_arg;
6989 size_t key_bits;
6990 unsigned char *output = NULL;
6991 size_t output_size;
6992 size_t output_length = ~0;
6993 psa_status_t actual_status;
6994 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02006995 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02006996
Gilles Peskine8817f612018-12-18 00:18:46 +01006997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01006998
Gilles Peskine656896e2018-06-29 19:12:28 +02006999 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007000 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
7001 psa_set_key_algorithm( &attributes, alg );
7002 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02007003 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007004 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02007005
7006 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02007007 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007008 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007009
Gilles Peskine656896e2018-06-29 19:12:28 +02007010 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007011 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007012 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02007013
7014 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02007015 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02007016 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007017 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02007018 output, output_size,
7019 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007020 TEST_EQUAL( actual_status, expected_status );
7021 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02007022
Gilles Peskine68428122018-06-30 18:42:41 +02007023 /* If the label is empty, the test framework puts a non-null pointer
7024 * in label->x. Test that a null pointer works as well. */
7025 if( label->len == 0 )
7026 {
7027 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007028 if( output_size != 0 )
7029 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007030 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007031 input_data->x, input_data->len,
7032 NULL, label->len,
7033 output, output_size,
7034 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007035 TEST_EQUAL( actual_status, expected_status );
7036 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007037 }
7038
Gilles Peskine656896e2018-06-29 19:12:28 +02007039exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007040 /*
7041 * Key attributes may have been returned by psa_get_key_attributes()
7042 * thus reset them as required.
7043 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007044 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007045
Ronald Cron5425a212020-08-04 14:58:35 +02007046 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02007047 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007048 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02007049}
7050/* END_CASE */
7051
7052/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007053void asymmetric_encrypt_decrypt( int key_type_arg,
7054 data_t *key_data,
7055 int alg_arg,
7056 data_t *input_data,
7057 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007058{
Ronald Cron5425a212020-08-04 14:58:35 +02007059 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007060 psa_key_type_t key_type = key_type_arg;
7061 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007062 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007063 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007064 size_t output_size;
7065 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007066 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007067 size_t output2_size;
7068 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007070
Gilles Peskine8817f612018-12-18 00:18:46 +01007071 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007072
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007073 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
7074 psa_set_key_algorithm( &attributes, alg );
7075 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007076
Gilles Peskine049c7532019-05-15 20:22:09 +02007077 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007078 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007079
7080 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02007081 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007082 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01007083
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007084 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007085 TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007086 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01007087
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007088 output2_size = input_data->len;
Gilles Peskine7be11a72022-04-14 00:12:57 +02007089 TEST_LE_U( output2_size,
7090 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
7091 TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007092 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007093
Gilles Peskineeebd7382018-06-08 18:11:54 +02007094 /* We test encryption by checking that encrypt-then-decrypt gives back
7095 * the original plaintext because of the non-optional random
7096 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02007097 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007098 input_data->x, input_data->len,
7099 label->x, label->len,
7100 output, output_size,
7101 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007102 /* We don't know what ciphertext length to expect, but check that
7103 * it looks sensible. */
Gilles Peskine7be11a72022-04-14 00:12:57 +02007104 TEST_LE_U( output_length, output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03007105
Ronald Cron5425a212020-08-04 14:58:35 +02007106 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007107 output, output_length,
7108 label->x, label->len,
7109 output2, output2_size,
7110 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007111 ASSERT_COMPARE( input_data->x, input_data->len,
7112 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007113
7114exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007115 /*
7116 * Key attributes may have been returned by psa_get_key_attributes()
7117 * thus reset them as required.
7118 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007119 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007120
Ronald Cron5425a212020-08-04 14:58:35 +02007121 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007122 mbedtls_free( output );
7123 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007124 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007125}
7126/* END_CASE */
7127
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007128/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007129void asymmetric_decrypt( int key_type_arg,
7130 data_t *key_data,
7131 int alg_arg,
7132 data_t *input_data,
7133 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02007134 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007135{
Ronald Cron5425a212020-08-04 14:58:35 +02007136 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007137 psa_key_type_t key_type = key_type_arg;
7138 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01007139 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007140 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03007141 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007142 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007143 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007144
Gilles Peskine8817f612018-12-18 00:18:46 +01007145 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007146
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007147 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7148 psa_set_key_algorithm( &attributes, alg );
7149 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007150
Gilles Peskine049c7532019-05-15 20:22:09 +02007151 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007152 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007153
gabor-mezei-armceface22021-01-21 12:26:17 +01007154 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
7155 key_bits = psa_get_key_bits( &attributes );
7156
7157 /* Determine the maximum ciphertext length */
7158 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007159 TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
gabor-mezei-armceface22021-01-21 12:26:17 +01007160 ASSERT_ALLOC( output, output_size );
7161
Ronald Cron5425a212020-08-04 14:58:35 +02007162 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007163 input_data->x, input_data->len,
7164 label->x, label->len,
7165 output,
7166 output_size,
7167 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007168 ASSERT_COMPARE( expected_data->x, expected_data->len,
7169 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007170
Gilles Peskine68428122018-06-30 18:42:41 +02007171 /* If the label is empty, the test framework puts a non-null pointer
7172 * in label->x. Test that a null pointer works as well. */
7173 if( label->len == 0 )
7174 {
7175 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007176 if( output_size != 0 )
7177 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007178 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01007179 input_data->x, input_data->len,
7180 NULL, label->len,
7181 output,
7182 output_size,
7183 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02007184 ASSERT_COMPARE( expected_data->x, expected_data->len,
7185 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02007186 }
7187
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007188exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007189 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007190 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03007191 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007192 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007193}
7194/* END_CASE */
7195
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007196/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02007197void asymmetric_decrypt_fail( int key_type_arg,
7198 data_t *key_data,
7199 int alg_arg,
7200 data_t *input_data,
7201 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00007202 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02007203 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007204{
Ronald Cron5425a212020-08-04 14:58:35 +02007205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007206 psa_key_type_t key_type = key_type_arg;
7207 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007208 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00007209 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02007210 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007211 psa_status_t actual_status;
7212 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007214
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007215 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007216
Gilles Peskine8817f612018-12-18 00:18:46 +01007217 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007218
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
7220 psa_set_key_algorithm( &attributes, alg );
7221 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03007222
Gilles Peskine049c7532019-05-15 20:22:09 +02007223 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007224 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007225
Ronald Cron5425a212020-08-04 14:58:35 +02007226 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007227 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02007228 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02007229 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02007230 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007231 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007232 TEST_LE_U( output_length, output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007233
Gilles Peskine68428122018-06-30 18:42:41 +02007234 /* If the label is empty, the test framework puts a non-null pointer
7235 * in label->x. Test that a null pointer works as well. */
7236 if( label->len == 0 )
7237 {
7238 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02007239 if( output_size != 0 )
7240 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02007241 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02007242 input_data->x, input_data->len,
7243 NULL, label->len,
7244 output, output_size,
7245 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007246 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007247 TEST_LE_U( output_length, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02007248 }
7249
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007250exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007251 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02007252 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02007253 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007254 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03007255}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02007256/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02007257
7258/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02007259void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00007260{
7261 /* Test each valid way of initializing the object, except for `= {0}`, as
7262 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
7263 * though it's OK by the C standard. We could test for this, but we'd need
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007264 * to suppress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007265 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007266 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
7267 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
7268 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00007269
7270 memset( &zero, 0, sizeof( zero ) );
7271
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007272 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007273 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007274 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007275 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007276 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007277 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007278 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00007279
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007280 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02007281 PSA_ASSERT( psa_key_derivation_abort(&func) );
7282 PSA_ASSERT( psa_key_derivation_abort(&init) );
7283 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00007284}
7285/* END_CASE */
7286
Janos Follath16de4a42019-06-13 16:32:24 +01007287/* BEGIN_CASE */
7288void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02007289{
Gilles Peskineea0fb492018-07-12 17:17:20 +02007290 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007291 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007292 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02007293
Gilles Peskine8817f612018-12-18 00:18:46 +01007294 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007295
Janos Follath16de4a42019-06-13 16:32:24 +01007296 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007297 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007298
7299exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007300 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007301 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02007302}
7303/* END_CASE */
7304
Janos Follathaf3c2a02019-06-12 12:34:34 +01007305/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01007306void derive_set_capacity( int alg_arg, int capacity_arg,
7307 int expected_status_arg )
7308{
7309 psa_algorithm_t alg = alg_arg;
7310 size_t capacity = capacity_arg;
7311 psa_status_t expected_status = expected_status_arg;
7312 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7313
7314 PSA_ASSERT( psa_crypto_init( ) );
7315
7316 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7317
7318 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
7319 expected_status );
7320
7321exit:
7322 psa_key_derivation_abort( &operation );
7323 PSA_DONE( );
7324}
7325/* END_CASE */
7326
7327/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01007328void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02007329 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007330 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02007331 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01007332 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02007333 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007334 int expected_status_arg3,
7335 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007336{
7337 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02007338 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
7339 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01007340 psa_status_t expected_statuses[] = {expected_status_arg1,
7341 expected_status_arg2,
7342 expected_status_arg3};
7343 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02007344 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
7345 MBEDTLS_SVC_KEY_ID_INIT,
7346 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01007347 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
7348 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
7349 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007350 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007351 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007352 psa_status_t expected_output_status = expected_output_status_arg;
7353 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01007354
7355 PSA_ASSERT( psa_crypto_init( ) );
7356
7357 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7358 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007359
7360 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7361
7362 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
7363 {
Gilles Peskine4023c012021-05-27 13:21:20 +02007364 mbedtls_test_set_step( i );
7365 if( steps[i] == 0 )
7366 {
7367 /* Skip this step */
7368 }
7369 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01007370 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02007371 psa_set_key_type( &attributes, key_types[i] );
7372 PSA_ASSERT( psa_import_key( &attributes,
7373 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007374 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007375 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
7376 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
7377 {
7378 // When taking a private key as secret input, use key agreement
7379 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007380 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
7381 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007382 expected_statuses[i] );
7383 }
7384 else
7385 {
7386 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02007387 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02007388 expected_statuses[i] );
7389 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02007390 }
7391 else
7392 {
7393 TEST_EQUAL( psa_key_derivation_input_bytes(
7394 &operation, steps[i],
7395 inputs[i]->x, inputs[i]->len ),
7396 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007397 }
7398 }
7399
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007400 if( output_key_type != PSA_KEY_TYPE_NONE )
7401 {
7402 psa_reset_key_attributes( &attributes );
Dave Rodgman491d8492021-11-16 12:12:49 +00007403 psa_set_key_type( &attributes, output_key_type );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007404 psa_set_key_bits( &attributes, 8 );
7405 actual_output_status =
7406 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007407 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02007408 }
7409 else
7410 {
7411 uint8_t buffer[1];
7412 actual_output_status =
7413 psa_key_derivation_output_bytes( &operation,
7414 buffer, sizeof( buffer ) );
7415 }
7416 TEST_EQUAL( actual_output_status, expected_output_status );
7417
Janos Follathaf3c2a02019-06-12 12:34:34 +01007418exit:
7419 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007420 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7421 psa_destroy_key( keys[i] );
7422 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01007423 PSA_DONE( );
7424}
7425/* END_CASE */
7426
Janos Follathd958bb72019-07-03 15:02:16 +01007427/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007428void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007429{
Janos Follathd958bb72019-07-03 15:02:16 +01007430 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02007431 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02007432 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007433 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01007434 unsigned char input1[] = "Input 1";
7435 size_t input1_length = sizeof( input1 );
7436 unsigned char input2[] = "Input 2";
7437 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007438 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02007439 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02007440 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7441 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
7442 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007443 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007444
Gilles Peskine8817f612018-12-18 00:18:46 +01007445 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007446
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02007447 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7448 psa_set_key_algorithm( &attributes, alg );
7449 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007450
Gilles Peskine73676cb2019-05-15 20:15:10 +02007451 PSA_ASSERT( psa_import_key( &attributes,
7452 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02007453 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007454
7455 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007456 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7457 input1, input1_length,
7458 input2, input2_length,
7459 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01007460 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007461
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007462 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01007463 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01007464 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007465
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007466 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007467
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007468 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02007469 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007470
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007471exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007472 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007473 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007474 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007475}
7476/* END_CASE */
7477
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007478/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02007479void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007480{
7481 uint8_t output_buffer[16];
7482 size_t buffer_size = 16;
7483 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007484 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007485
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007486 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7487 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007488 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007489
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007490 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007491 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007492
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007493 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007494
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007495 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
7496 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007497 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007498
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007499 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00007500 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03007501
7502exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007503 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03007504}
7505/* END_CASE */
7506
7507/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007508void derive_output( int alg_arg,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007509 int step1_arg, data_t *input1, int expected_status_arg1,
7510 int step2_arg, data_t *input2, int expected_status_arg2,
7511 int step3_arg, data_t *input3, int expected_status_arg3,
7512 int step4_arg, data_t *input4, int expected_status_arg4,
7513 data_t *key_agreement_peer_key,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007514 int requested_capacity_arg,
7515 data_t *expected_output1,
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007516 data_t *expected_output2,
7517 int other_key_input_type,
7518 int key_input_type,
7519 int derive_type )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007520{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007521 psa_algorithm_t alg = alg_arg;
Przemek Stekielffbb7d32022-03-31 11:13:47 +02007522 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg};
7523 data_t *inputs[] = {input1, input2, input3, input4};
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007524 mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT,
7525 MBEDTLS_SVC_KEY_ID_INIT,
7526 MBEDTLS_SVC_KEY_ID_INIT,
7527 MBEDTLS_SVC_KEY_ID_INIT};
7528 psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2,
7529 expected_status_arg3, expected_status_arg4};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007530 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007531 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007532 uint8_t *expected_outputs[2] =
7533 {expected_output1->x, expected_output2->x};
7534 size_t output_sizes[2] =
7535 {expected_output1->len, expected_output2->len};
7536 size_t output_buffer_size = 0;
7537 uint8_t *output_buffer = NULL;
7538 size_t expected_capacity;
7539 size_t current_capacity;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007540 psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
7541 psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
7542 psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT;
7543 psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT;
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007544 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007545 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02007546 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007547
7548 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
7549 {
7550 if( output_sizes[i] > output_buffer_size )
7551 output_buffer_size = output_sizes[i];
7552 if( output_sizes[i] == 0 )
7553 expected_outputs[i] = NULL;
7554 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007555 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01007556 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007557
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007558 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02007559 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
7560 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
7561 requested_capacity ) );
7562 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007563 {
Gilles Peskine1468da72019-05-29 17:35:49 +02007564 switch( steps[i] )
7565 {
7566 case 0:
7567 break;
7568 case PSA_KEY_DERIVATION_INPUT_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007569 switch( key_input_type )
gabor-mezei-armceface22021-01-21 12:26:17 +01007570 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007571 case 0: // input bytes
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007572 TEST_EQUAL( psa_key_derivation_input_bytes(
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007573 &operation, steps[i],
Przemek Stekielfcdd0232022-05-19 10:28:58 +02007574 inputs[i]->x, inputs[i]->len ),
7575 statuses[i] );
7576
7577 if( statuses[i] != PSA_SUCCESS )
7578 goto exit;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007579 break;
7580 case 1: // input key
7581 psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE );
7582 psa_set_key_algorithm( &attributes1, alg );
7583 psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE );
7584
7585 PSA_ASSERT( psa_import_key( &attributes1,
7586 inputs[i]->x, inputs[i]->len,
7587 &keys[i] ) );
7588
Przemek Stekiel38647de2022-04-19 13:27:47 +02007589 if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007590 {
7591 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) );
Gilles Peskine7be11a72022-04-14 00:12:57 +02007592 TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ),
7593 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007594 }
7595
Przemek Stekiel38647de2022-04-19 13:27:47 +02007596 PSA_ASSERT( psa_key_derivation_input_key( &operation,
7597 steps[i],
7598 keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007599 break;
7600 default:
7601 TEST_ASSERT( ! "default case not supported" );
7602 break;
7603 }
7604 break;
7605 case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET:
Przemek Stekiel38647de2022-04-19 13:27:47 +02007606 switch( other_key_input_type )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007607 {
7608 case 0: // input bytes
Przemek Stekiel38647de2022-04-19 13:27:47 +02007609 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
7610 steps[i],
7611 inputs[i]->x,
7612 inputs[i]->len ),
7613 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007614 break;
Przemek Stekiele6654662022-04-20 09:14:51 +02007615 case 1: // input key, type DERIVE
7616 case 11: // input key, type RAW
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007617 psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE );
7618 psa_set_key_algorithm( &attributes2, alg );
7619 psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE );
7620
7621 // other secret of type RAW_DATA passed with input_key
Przemek Stekiele6654662022-04-20 09:14:51 +02007622 if( other_key_input_type == 11 )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007623 psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA );
7624
7625 PSA_ASSERT( psa_import_key( &attributes2,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007626 inputs[i]->x, inputs[i]->len,
7627 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007628
Przemek Stekiel38647de2022-04-19 13:27:47 +02007629 TEST_EQUAL( psa_key_derivation_input_key( &operation,
7630 steps[i],
7631 keys[i] ),
7632 statuses[i] );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007633 break;
7634 case 2: // key agreement
7635 psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE );
7636 psa_set_key_algorithm( &attributes3, alg );
7637 psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) );
7638
7639 PSA_ASSERT( psa_import_key( &attributes3,
Przemek Stekiel38647de2022-04-19 13:27:47 +02007640 inputs[i]->x, inputs[i]->len,
7641 &keys[i] ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007642
7643 TEST_EQUAL( psa_key_derivation_key_agreement(
7644 &operation,
7645 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
7646 keys[i], key_agreement_peer_key->x,
7647 key_agreement_peer_key->len ), statuses[i] );
7648 break;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007649 default:
7650 TEST_ASSERT( ! "default case not supported" );
7651 break;
gabor-mezei-armceface22021-01-21 12:26:17 +01007652 }
7653
Przemek Stekiel38647de2022-04-19 13:27:47 +02007654 if( statuses[i] != PSA_SUCCESS )
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007655 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007656 break;
7657 default:
Przemek Stekielead1bb92022-05-11 12:22:57 +02007658 TEST_EQUAL( psa_key_derivation_input_bytes(
Gilles Peskine1468da72019-05-29 17:35:49 +02007659 &operation, steps[i],
Przemek Stekielead1bb92022-05-11 12:22:57 +02007660 inputs[i]->x, inputs[i]->len ), statuses[i] );
7661
7662 if( statuses[i] != PSA_SUCCESS )
7663 goto exit;
Gilles Peskine1468da72019-05-29 17:35:49 +02007664 break;
7665 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01007666 }
Gilles Peskine1468da72019-05-29 17:35:49 +02007667
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007668 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007669 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007670 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007671 expected_capacity = requested_capacity;
7672
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007673 if( derive_type == 1 ) // output key
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007674 {
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007675 psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED;
7676
7677 /* For output key derivation secret must be provided using
7678 input key, otherwise operation is not permitted. */
Przemek Stekiel4e47a912022-04-21 11:40:18 +02007679 if( key_input_type == 1 )
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007680 expected_status = PSA_SUCCESS;
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007681
7682 psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT );
7683 psa_set_key_algorithm( &attributes4, alg );
7684 psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE );
Przemek Stekiel0e993912022-06-03 15:01:14 +02007685 psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007686
7687 TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation,
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007688 &derived_key ), expected_status );
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007689 }
7690 else // output bytes
7691 {
7692 /* Expansion phase. */
7693 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007694 {
Przemek Stekielcd00d7f2022-04-01 13:40:48 +02007695 /* Read some bytes. */
7696 status = psa_key_derivation_output_bytes( &operation,
7697 output_buffer, output_sizes[i] );
7698 if( expected_capacity == 0 && output_sizes[i] == 0 )
7699 {
7700 /* Reading 0 bytes when 0 bytes are available can go either way. */
7701 TEST_ASSERT( status == PSA_SUCCESS ||
7702 status == PSA_ERROR_INSUFFICIENT_DATA );
7703 continue;
7704 }
7705 else if( expected_capacity == 0 ||
7706 output_sizes[i] > expected_capacity )
7707 {
7708 /* Capacity exceeded. */
7709 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
7710 expected_capacity = 0;
7711 continue;
7712 }
7713 /* Success. Check the read data. */
7714 PSA_ASSERT( status );
7715 if( output_sizes[i] != 0 )
7716 ASSERT_COMPARE( output_buffer, output_sizes[i],
7717 expected_outputs[i], output_sizes[i] );
7718 /* Check the operation status. */
7719 expected_capacity -= output_sizes[i];
7720 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
7721 &current_capacity ) );
7722 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007723 }
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007724 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007725 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007726
7727exit:
7728 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007729 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007730 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
7731 psa_destroy_key( keys[i] );
Przemek Stekiel4daaa2b2022-04-20 10:06:38 +02007732 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007733 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02007734}
7735/* END_CASE */
7736
7737/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02007738void derive_full( int alg_arg,
7739 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01007740 data_t *input1,
7741 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02007742 int requested_capacity_arg )
7743{
Ronald Cron5425a212020-08-04 14:58:35 +02007744 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007745 psa_algorithm_t alg = alg_arg;
7746 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007747 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007748 unsigned char output_buffer[16];
7749 size_t expected_capacity = requested_capacity;
7750 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007751 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02007752
Gilles Peskine8817f612018-12-18 00:18:46 +01007753 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007754
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007755 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7756 psa_set_key_algorithm( &attributes, alg );
7757 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02007758
Gilles Peskine049c7532019-05-15 20:22:09 +02007759 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007760 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007761
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007762 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
7763 input1->x, input1->len,
7764 input2->x, input2->len,
7765 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01007766 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01007767
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007768 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007769 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007770 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007771
7772 /* Expansion phase. */
7773 while( current_capacity > 0 )
7774 {
7775 size_t read_size = sizeof( output_buffer );
7776 if( read_size > current_capacity )
7777 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007778 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007779 output_buffer,
7780 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007781 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007782 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007783 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007784 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02007785 }
7786
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007787 /* Check that the operation refuses to go over capacity. */
7788 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02007789 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02007790
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007791 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02007792
7793exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007794 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007795 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007796 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02007797}
7798/* END_CASE */
7799
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007800/* BEGIN_CASE depends_on:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS:MBEDTLS_SHA256_C */
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007801void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007802 int derivation_step,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007803 int capacity, int expected_capacity_status_arg,
Andrzej Kurek2be16892022-09-16 07:14:04 -04007804 data_t *expected_output,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007805 int expected_output_status_arg )
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007806{
7807 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
7808 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekd3785042022-09-16 06:45:44 -04007809 psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007810 uint8_t *output_buffer = NULL;
7811 psa_status_t status;
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007812 psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg;
7813 psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg;
7814 psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg;
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007815
7816 ASSERT_ALLOC( output_buffer, expected_output->len );
7817 PSA_ASSERT( psa_crypto_init() );
7818
7819 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Andrzej Kurek2be16892022-09-16 07:14:04 -04007820 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007821 expected_capacity_status);
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007822
7823 TEST_EQUAL( psa_key_derivation_input_bytes( &operation,
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007824 step, input->x, input->len ),
7825 expected_input_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007826
7827 if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS )
7828 goto exit;
7829
7830 status = psa_key_derivation_output_bytes( &operation, output_buffer,
7831 expected_output->len );
7832
Andrzej Kurek3539f2c2022-09-26 10:56:02 -04007833 TEST_EQUAL( status, expected_output_status );
Andrzej Kurekd8705bc2022-07-29 10:02:05 -04007834 if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS )
7835 ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x,
7836 expected_output->len );
7837
7838exit:
7839 mbedtls_free( output_buffer );
7840 psa_key_derivation_abort( &operation );
7841 PSA_DONE();
7842}
7843/* END_CASE */
7844
Janos Follathe60c9052019-07-03 13:51:30 +01007845/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007846void derive_key_exercise( int alg_arg,
7847 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01007848 data_t *input1,
7849 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007850 int derived_type_arg,
7851 int derived_bits_arg,
7852 int derived_usage_arg,
7853 int derived_alg_arg )
7854{
Ronald Cron5425a212020-08-04 14:58:35 +02007855 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7856 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007857 psa_algorithm_t alg = alg_arg;
7858 psa_key_type_t derived_type = derived_type_arg;
7859 size_t derived_bits = derived_bits_arg;
7860 psa_key_usage_t derived_usage = derived_usage_arg;
7861 psa_algorithm_t derived_alg = derived_alg_arg;
7862 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007863 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007865 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007866
Gilles Peskine8817f612018-12-18 00:18:46 +01007867 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007868
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007869 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
7870 psa_set_key_algorithm( &attributes, alg );
7871 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007872 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007873 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007874
7875 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007876 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7877 input1->x, input1->len,
7878 input2->x, input2->len,
7879 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01007880 goto exit;
7881
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007882 psa_set_key_usage_flags( &attributes, derived_usage );
7883 psa_set_key_algorithm( &attributes, derived_alg );
7884 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007885 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007886 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007887 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007888
7889 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02007890 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02007891 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
7892 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007893
7894 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007895 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02007896 goto exit;
7897
7898exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007899 /*
7900 * Key attributes may have been returned by psa_get_key_attributes()
7901 * thus reset them as required.
7902 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02007903 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01007904
7905 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007906 psa_destroy_key( base_key );
7907 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007908 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007909}
7910/* END_CASE */
7911
Janos Follath42fd8882019-07-03 14:17:09 +01007912/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02007913void derive_key_export( int alg_arg,
7914 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01007915 data_t *input1,
7916 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02007917 int bytes1_arg,
7918 int bytes2_arg )
7919{
Ronald Cron5425a212020-08-04 14:58:35 +02007920 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
7921 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007922 psa_algorithm_t alg = alg_arg;
7923 size_t bytes1 = bytes1_arg;
7924 size_t bytes2 = bytes2_arg;
7925 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007926 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007927 uint8_t *output_buffer = NULL;
7928 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007929 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
7930 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02007931 size_t length;
7932
Gilles Peskine8cebbba2018-09-27 13:54:18 +02007933 ASSERT_ALLOC( output_buffer, capacity );
7934 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01007935 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007936
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007937 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
7938 psa_set_key_algorithm( &base_attributes, alg );
7939 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02007940 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02007941 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007942
7943 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007944 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7945 input1->x, input1->len,
7946 input2->x, input2->len,
7947 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007948 goto exit;
7949
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007950 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02007951 output_buffer,
7952 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007953 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007954
7955 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01007956 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
7957 input1->x, input1->len,
7958 input2->x, input2->len,
7959 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01007960 goto exit;
7961
Gilles Peskineff5f0e72019-04-18 12:53:30 +02007962 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
7963 psa_set_key_algorithm( &derived_attributes, 0 );
7964 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007965 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007966 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007967 &derived_key ) );
7968 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007969 export_buffer, bytes1,
7970 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007971 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02007972 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02007973 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007974 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02007975 &derived_key ) );
7976 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01007977 export_buffer + bytes1, bytes2,
7978 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01007979 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007980
7981 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01007982 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
7983 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007984
7985exit:
7986 mbedtls_free( output_buffer );
7987 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02007988 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02007989 psa_destroy_key( base_key );
7990 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02007991 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02007992}
7993/* END_CASE */
7994
7995/* BEGIN_CASE */
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01007996void derive_key_type( int alg_arg,
7997 data_t *key_data,
7998 data_t *input1,
7999 data_t *input2,
8000 int key_type_arg, int bits_arg,
8001 data_t *expected_export )
8002{
8003 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8004 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
8005 const psa_algorithm_t alg = alg_arg;
8006 const psa_key_type_t key_type = key_type_arg;
8007 const size_t bits = bits_arg;
8008 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8009 const size_t export_buffer_size =
8010 PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits );
8011 uint8_t *export_buffer = NULL;
8012 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8013 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8014 size_t export_length;
8015
8016 ASSERT_ALLOC( export_buffer, export_buffer_size );
8017 PSA_ASSERT( psa_crypto_init( ) );
8018
8019 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8020 psa_set_key_algorithm( &base_attributes, alg );
8021 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8022 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
8023 &base_key ) );
8024
Przemek Stekielc85f0912022-03-08 11:37:54 +01008025 if( mbedtls_test_psa_setup_key_derivation_wrap(
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008026 &operation, base_key, alg,
8027 input1->x, input1->len,
8028 input2->x, input2->len,
Przemek Stekielc85f0912022-03-08 11:37:54 +01008029 PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 )
Przemyslaw Stekiel696b1202021-11-24 16:29:10 +01008030 goto exit;
8031
8032 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8033 psa_set_key_algorithm( &derived_attributes, 0 );
8034 psa_set_key_type( &derived_attributes, key_type );
8035 psa_set_key_bits( &derived_attributes, bits );
8036 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
8037 &derived_key ) );
8038
8039 PSA_ASSERT( psa_export_key( derived_key,
8040 export_buffer, export_buffer_size,
8041 &export_length ) );
8042 ASSERT_COMPARE( export_buffer, export_length,
8043 expected_export->x, expected_export->len );
8044
8045exit:
8046 mbedtls_free( export_buffer );
8047 psa_key_derivation_abort( &operation );
8048 psa_destroy_key( base_key );
8049 psa_destroy_key( derived_key );
8050 PSA_DONE( );
8051}
8052/* END_CASE */
8053
8054/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008055void derive_key( int alg_arg,
8056 data_t *key_data, data_t *input1, data_t *input2,
8057 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008058 int expected_status_arg,
8059 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02008060{
Ronald Cron5425a212020-08-04 14:58:35 +02008061 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
8062 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02008063 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008064 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02008065 size_t bits = bits_arg;
8066 psa_status_t expected_status = expected_status_arg;
8067 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
8068 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8069 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
8070
8071 PSA_ASSERT( psa_crypto_init( ) );
8072
8073 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
8074 psa_set_key_algorithm( &base_attributes, alg );
8075 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
8076 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008077 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02008078
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008079 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
8080 input1->x, input1->len,
8081 input2->x, input2->len,
8082 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02008083 goto exit;
8084
8085 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
8086 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02008087 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02008088 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01008089
8090 psa_status_t status =
8091 psa_key_derivation_output_key( &derived_attributes,
8092 &operation,
8093 &derived_key );
8094 if( is_large_output > 0 )
8095 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8096 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02008097
8098exit:
8099 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02008100 psa_destroy_key( base_key );
8101 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02008102 PSA_DONE( );
8103}
8104/* END_CASE */
8105
8106/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02008107void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02008108 int our_key_type_arg, int our_key_alg_arg,
8109 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02008110 int expected_status_arg )
8111{
Ronald Cron5425a212020-08-04 14:58:35 +02008112 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008113 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008114 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008115 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008116 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02008118 psa_status_t expected_status = expected_status_arg;
8119 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02008120
Gilles Peskine8817f612018-12-18 00:18:46 +01008121 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008122
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008123 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02008124 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008125 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008126 PSA_ASSERT( psa_import_key( &attributes,
8127 our_key_data->x, our_key_data->len,
8128 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008129
Gilles Peskine77f40d82019-04-11 21:27:06 +02008130 /* The tests currently include inputs that should fail at either step.
8131 * Test cases that fail at the setup step should be changed to call
8132 * key_derivation_setup instead, and this function should be renamed
8133 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008134 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02008135 if( status == PSA_SUCCESS )
8136 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008137 TEST_EQUAL( psa_key_derivation_key_agreement(
8138 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
8139 our_key,
8140 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02008141 expected_status );
8142 }
8143 else
8144 {
8145 TEST_ASSERT( status == expected_status );
8146 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02008147
8148exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008149 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008150 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008151 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02008152}
8153/* END_CASE */
8154
8155/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02008156void raw_key_agreement( int alg_arg,
8157 int our_key_type_arg, data_t *our_key_data,
8158 data_t *peer_key_data,
8159 data_t *expected_output )
8160{
Ronald Cron5425a212020-08-04 14:58:35 +02008161 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008162 psa_algorithm_t alg = alg_arg;
8163 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008165 unsigned char *output = NULL;
8166 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01008167 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008168
Gilles Peskinef0cba732019-04-11 22:12:38 +02008169 PSA_ASSERT( psa_crypto_init( ) );
8170
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008171 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8172 psa_set_key_algorithm( &attributes, alg );
8173 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008174 PSA_ASSERT( psa_import_key( &attributes,
8175 our_key_data->x, our_key_data->len,
8176 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008177
gabor-mezei-armceface22021-01-21 12:26:17 +01008178 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
8179 key_bits = psa_get_key_bits( &attributes );
8180
Gilles Peskine992bee82022-04-13 23:25:52 +02008181 /* Validate size macros */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008182 TEST_LE_U( expected_output->len,
8183 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
8184 TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ),
8185 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskine992bee82022-04-13 23:25:52 +02008186
8187 /* Good case with exact output size */
8188 ASSERT_ALLOC( output, expected_output->len );
Gilles Peskinebe697d82019-05-16 18:00:41 +02008189 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8190 peer_key_data->x, peer_key_data->len,
8191 output, expected_output->len,
8192 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008193 ASSERT_COMPARE( output, output_length,
8194 expected_output->x, expected_output->len );
Gilles Peskine992bee82022-04-13 23:25:52 +02008195 mbedtls_free( output );
8196 output = NULL;
8197 output_length = ~0;
8198
8199 /* Larger buffer */
8200 ASSERT_ALLOC( output, expected_output->len + 1 );
8201 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
8202 peer_key_data->x, peer_key_data->len,
8203 output, expected_output->len + 1,
8204 &output_length ) );
8205 ASSERT_COMPARE( output, output_length,
8206 expected_output->x, expected_output->len );
8207 mbedtls_free( output );
8208 output = NULL;
8209 output_length = ~0;
8210
8211 /* Buffer too small */
8212 ASSERT_ALLOC( output, expected_output->len - 1 );
8213 TEST_EQUAL( psa_raw_key_agreement( alg, our_key,
8214 peer_key_data->x, peer_key_data->len,
8215 output, expected_output->len - 1,
8216 &output_length ),
8217 PSA_ERROR_BUFFER_TOO_SMALL );
8218 /* Not required by the spec, but good robustness */
Gilles Peskine7be11a72022-04-14 00:12:57 +02008219 TEST_LE_U( output_length, expected_output->len - 1 );
Gilles Peskine992bee82022-04-13 23:25:52 +02008220 mbedtls_free( output );
8221 output = NULL;
Gilles Peskinef0cba732019-04-11 22:12:38 +02008222
8223exit:
8224 mbedtls_free( output );
8225 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008226 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02008227}
8228/* END_CASE */
8229
8230/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02008231void key_agreement_capacity( int alg_arg,
8232 int our_key_type_arg, data_t *our_key_data,
8233 data_t *peer_key_data,
8234 int expected_capacity_arg )
8235{
Ronald Cron5425a212020-08-04 14:58:35 +02008236 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008237 psa_algorithm_t alg = alg_arg;
8238 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008239 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008241 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02008242 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02008243
Gilles Peskine8817f612018-12-18 00:18:46 +01008244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008245
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008246 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8247 psa_set_key_algorithm( &attributes, alg );
8248 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008249 PSA_ASSERT( psa_import_key( &attributes,
8250 our_key_data->x, our_key_data->len,
8251 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008252
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008253 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008254 PSA_ASSERT( psa_key_derivation_key_agreement(
8255 &operation,
8256 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8257 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008258 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8259 {
8260 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008261 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008262 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008263 NULL, 0 ) );
8264 }
Gilles Peskine59685592018-09-18 12:11:34 +02008265
Shaun Case8b0ecbc2021-12-20 21:14:10 -08008266 /* Test the advertised capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02008267 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008268 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01008269 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02008270
Gilles Peskinebf491972018-10-25 22:36:12 +02008271 /* Test the actual capacity by reading the output. */
8272 while( actual_capacity > sizeof( output ) )
8273 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008274 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008275 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02008276 actual_capacity -= sizeof( output );
8277 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008278 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008279 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008280 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02008281 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02008282
Gilles Peskine59685592018-09-18 12:11:34 +02008283exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008284 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008285 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008286 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008287}
8288/* END_CASE */
8289
8290/* BEGIN_CASE */
8291void key_agreement_output( int alg_arg,
8292 int our_key_type_arg, data_t *our_key_data,
8293 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008294 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02008295{
Ronald Cron5425a212020-08-04 14:58:35 +02008296 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02008297 psa_algorithm_t alg = alg_arg;
8298 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008299 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008301 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02008302
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008303 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
8304 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008305
Gilles Peskine8817f612018-12-18 00:18:46 +01008306 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008307
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008308 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8309 psa_set_key_algorithm( &attributes, alg );
8310 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02008311 PSA_ASSERT( psa_import_key( &attributes,
8312 our_key_data->x, our_key_data->len,
8313 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02008314
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008315 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008316 PSA_ASSERT( psa_key_derivation_key_agreement(
8317 &operation,
8318 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
8319 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008320 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
8321 {
8322 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008323 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02008324 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02008325 NULL, 0 ) );
8326 }
Gilles Peskine59685592018-09-18 12:11:34 +02008327
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008328 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008329 actual_output,
8330 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008331 ASSERT_COMPARE( actual_output, expected_output1->len,
8332 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008333 if( expected_output2->len != 0 )
8334 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008335 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008336 actual_output,
8337 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01008338 ASSERT_COMPARE( actual_output, expected_output2->len,
8339 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02008340 }
Gilles Peskine59685592018-09-18 12:11:34 +02008341
8342exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008343 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02008344 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008345 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02008346 mbedtls_free( actual_output );
8347}
8348/* END_CASE */
8349
8350/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02008351void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02008352{
Gilles Peskinea50d7392018-06-21 10:22:13 +02008353 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008354 unsigned char *output = NULL;
8355 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02008356 size_t i;
8357 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02008358
Simon Butcher49f8e312020-03-03 15:51:50 +00008359 TEST_ASSERT( bytes_arg >= 0 );
8360
Gilles Peskine91892022021-02-08 19:50:26 +01008361 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02008362 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02008363
Gilles Peskine8817f612018-12-18 00:18:46 +01008364 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02008365
Gilles Peskinea50d7392018-06-21 10:22:13 +02008366 /* Run several times, to ensure that every output byte will be
8367 * nonzero at least once with overwhelming probability
8368 * (2^(-8*number_of_runs)). */
8369 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02008370 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02008371 if( bytes != 0 )
8372 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01008373 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008374
Gilles Peskinea50d7392018-06-21 10:22:13 +02008375 for( i = 0; i < bytes; i++ )
8376 {
8377 if( output[i] != 0 )
8378 ++changed[i];
8379 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008380 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02008381
8382 /* Check that every byte was changed to nonzero at least once. This
8383 * validates that psa_generate_random is overwriting every byte of
8384 * the output buffer. */
8385 for( i = 0; i < bytes; i++ )
8386 {
8387 TEST_ASSERT( changed[i] != 0 );
8388 }
Gilles Peskine05d69892018-06-19 22:00:52 +02008389
8390exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008391 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02008392 mbedtls_free( output );
8393 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02008394}
8395/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02008396
8397/* BEGIN_CASE */
8398void generate_key( int type_arg,
8399 int bits_arg,
8400 int usage_arg,
8401 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01008402 int expected_status_arg,
8403 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02008404{
Ronald Cron5425a212020-08-04 14:58:35 +02008405 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008406 psa_key_type_t type = type_arg;
8407 psa_key_usage_t usage = usage_arg;
8408 size_t bits = bits_arg;
8409 psa_algorithm_t alg = alg_arg;
8410 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008411 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008412 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008413
Gilles Peskine8817f612018-12-18 00:18:46 +01008414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008415
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008416 psa_set_key_usage_flags( &attributes, usage );
8417 psa_set_key_algorithm( &attributes, alg );
8418 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008419 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008420
8421 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01008422 psa_status_t status = psa_generate_key( &attributes, &key );
8423
8424 if( is_large_key > 0 )
8425 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
8426 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008427 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02008428 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008429
8430 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008431 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02008432 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
8433 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008434
Gilles Peskine818ca122018-06-20 18:16:48 +02008435 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008436 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02008437 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02008438
8439exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008440 /*
8441 * Key attributes may have been returned by psa_get_key_attributes()
8442 * thus reset them as required.
8443 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008444 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008445
Ronald Cron5425a212020-08-04 14:58:35 +02008446 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008447 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02008448}
8449/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03008450
Ronald Cronee414c72021-03-18 18:50:08 +01008451/* 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 +02008452void generate_key_rsa( int bits_arg,
8453 data_t *e_arg,
8454 int expected_status_arg )
8455{
Ronald Cron5425a212020-08-04 14:58:35 +02008456 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02008457 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02008458 size_t bits = bits_arg;
8459 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
8460 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
8461 psa_status_t expected_status = expected_status_arg;
8462 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8463 uint8_t *exported = NULL;
8464 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008465 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008466 size_t exported_length = SIZE_MAX;
8467 uint8_t *e_read_buffer = NULL;
8468 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02008469 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008470 size_t e_read_length = SIZE_MAX;
8471
8472 if( e_arg->len == 0 ||
8473 ( e_arg->len == 3 &&
8474 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
8475 {
8476 is_default_public_exponent = 1;
8477 e_read_size = 0;
8478 }
8479 ASSERT_ALLOC( e_read_buffer, e_read_size );
8480 ASSERT_ALLOC( exported, exported_size );
8481
8482 PSA_ASSERT( psa_crypto_init( ) );
8483
8484 psa_set_key_usage_flags( &attributes, usage );
8485 psa_set_key_algorithm( &attributes, alg );
8486 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
8487 e_arg->x, e_arg->len ) );
8488 psa_set_key_bits( &attributes, bits );
8489
8490 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008491 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008492 if( expected_status != PSA_SUCCESS )
8493 goto exit;
8494
8495 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02008496 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008497 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8498 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
8499 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
8500 e_read_buffer, e_read_size,
8501 &e_read_length ) );
8502 if( is_default_public_exponent )
8503 TEST_EQUAL( e_read_length, 0 );
8504 else
8505 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
8506
8507 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008508 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02008509 goto exit;
8510
8511 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02008512 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02008513 exported, exported_size,
8514 &exported_length ) );
8515 {
8516 uint8_t *p = exported;
8517 uint8_t *end = exported + exported_length;
8518 size_t len;
8519 /* RSAPublicKey ::= SEQUENCE {
8520 * modulus INTEGER, -- n
8521 * publicExponent INTEGER } -- e
8522 */
8523 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008524 MBEDTLS_ASN1_SEQUENCE |
8525 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01008526 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008527 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
8528 MBEDTLS_ASN1_INTEGER ) );
8529 if( len >= 1 && p[0] == 0 )
8530 {
8531 ++p;
8532 --len;
8533 }
8534 if( e_arg->len == 0 )
8535 {
8536 TEST_EQUAL( len, 3 );
8537 TEST_EQUAL( p[0], 1 );
8538 TEST_EQUAL( p[1], 0 );
8539 TEST_EQUAL( p[2], 1 );
8540 }
8541 else
8542 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
8543 }
8544
8545exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008546 /*
8547 * Key attributes may have been returned by psa_get_key_attributes() or
8548 * set by psa_set_key_domain_parameters() thus reset them as required.
8549 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02008550 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008551
Ronald Cron5425a212020-08-04 14:58:35 +02008552 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008553 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02008554 mbedtls_free( e_read_buffer );
8555 mbedtls_free( exported );
8556}
8557/* END_CASE */
8558
Darryl Greend49a4992018-06-18 17:27:26 +01008559/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008560void persistent_key_load_key_from_storage( data_t *data,
8561 int type_arg, int bits_arg,
8562 int usage_flags_arg, int alg_arg,
8563 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01008564{
Ronald Cron71016a92020-08-28 19:01:50 +02008565 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02008567 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8568 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008569 psa_key_type_t type = type_arg;
8570 size_t bits = bits_arg;
8571 psa_key_usage_t usage_flags = usage_flags_arg;
8572 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008573 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01008574 unsigned char *first_export = NULL;
8575 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01008576 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008577 size_t first_exported_length;
8578 size_t second_exported_length;
8579
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008580 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8581 {
8582 ASSERT_ALLOC( first_export, export_size );
8583 ASSERT_ALLOC( second_export, export_size );
8584 }
Darryl Greend49a4992018-06-18 17:27:26 +01008585
Gilles Peskine8817f612018-12-18 00:18:46 +01008586 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008587
Gilles Peskinec87af662019-05-15 16:12:22 +02008588 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008589 psa_set_key_usage_flags( &attributes, usage_flags );
8590 psa_set_key_algorithm( &attributes, alg );
8591 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02008592 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01008593
Darryl Green0c6575a2018-11-07 16:05:30 +00008594 switch( generation_method )
8595 {
8596 case IMPORT_KEY:
8597 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02008598 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02008599 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008600 break;
Darryl Greend49a4992018-06-18 17:27:26 +01008601
Darryl Green0c6575a2018-11-07 16:05:30 +00008602 case GENERATE_KEY:
8603 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02008604 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008605 break;
8606
8607 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01008608#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008609 {
8610 /* Create base key */
8611 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
8612 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
8613 psa_set_key_usage_flags( &base_attributes,
8614 PSA_KEY_USAGE_DERIVE );
8615 psa_set_key_algorithm( &base_attributes, derive_alg );
8616 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02008617 PSA_ASSERT( psa_import_key( &base_attributes,
8618 data->x, data->len,
8619 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008620 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008621 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008622 PSA_ASSERT( psa_key_derivation_input_key(
8623 &operation,
8624 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008625 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008626 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008627 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02008628 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
8629 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02008630 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008631 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008632 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02008633 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008634 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01008635#else
8636 TEST_ASSUME( ! "KDF not supported in this configuration" );
8637#endif
8638 break;
8639
8640 default:
8641 TEST_ASSERT( ! "generation_method not implemented in test" );
8642 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00008643 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008644 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01008645
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008646 /* Export the key if permitted by the key policy. */
8647 if( usage_flags & PSA_KEY_USAGE_EXPORT )
8648 {
Ronald Cron5425a212020-08-04 14:58:35 +02008649 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008650 first_export, export_size,
8651 &first_exported_length ) );
8652 if( generation_method == IMPORT_KEY )
8653 ASSERT_COMPARE( data->x, data->len,
8654 first_export, first_exported_length );
8655 }
Darryl Greend49a4992018-06-18 17:27:26 +01008656
8657 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02008658 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008659 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01008660 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01008661
Darryl Greend49a4992018-06-18 17:27:26 +01008662 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02008663 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02008664 TEST_ASSERT( mbedtls_svc_key_id_equal(
8665 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008666 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
8667 PSA_KEY_LIFETIME_PERSISTENT );
8668 TEST_EQUAL( psa_get_key_type( &attributes ), type );
8669 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02008670 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
gabor-mezei-arm98a34352021-06-28 14:05:00 +02008671 mbedtls_test_update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008672 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01008673
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008674 /* Export the key again if permitted by the key policy. */
8675 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00008676 {
Ronald Cron5425a212020-08-04 14:58:35 +02008677 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008678 second_export, export_size,
8679 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00008680 ASSERT_COMPARE( first_export, first_exported_length,
8681 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00008682 }
8683
8684 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01008685 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00008686 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01008687
8688exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008689 /*
8690 * Key attributes may have been returned by psa_get_key_attributes()
8691 * thus reset them as required.
8692 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02008693 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01008694
Darryl Greend49a4992018-06-18 17:27:26 +01008695 mbedtls_free( first_export );
8696 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02008697 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02008698 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02008699 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02008700 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01008701}
8702/* END_CASE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008703
Neil Armstronga557cb82022-06-10 08:58:32 +02008704/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong2a73f212022-09-06 11:34:54 +02008705void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg,
8706 int primitive_arg, int hash_arg, int role_arg,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008707 int input_first, data_t *pw_data,
Neil Armstrong2a73f212022-09-06 11:34:54 +02008708 int expected_status_setup_arg,
8709 int expected_status_set_role_arg,
8710 int expected_status_set_password_key_arg,
8711 int expected_status_input_output_arg)
Neil Armstrongd597bc72022-05-25 11:28:39 +02008712{
8713 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8714 psa_pake_operation_t operation = psa_pake_operation_init();
8715 psa_algorithm_t alg = alg_arg;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008716 psa_key_type_t key_type_pw = key_type_pw_arg;
8717 psa_key_usage_t key_usage_pw = key_usage_pw_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008718 psa_algorithm_t hash_alg = hash_arg;
8719 psa_pake_role_t role = role_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008720 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrong2a73f212022-09-06 11:34:54 +02008722 psa_status_t expected_status_setup = expected_status_setup_arg;
8723 psa_status_t expected_status_set_role = expected_status_set_role_arg;
8724 psa_status_t expected_status_set_password_key =
8725 expected_status_set_password_key_arg;
8726 psa_status_t expected_status_input_output =
8727 expected_status_input_output_arg;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008728 unsigned char *output_buffer = NULL;
8729 size_t output_len = 0;
8730
8731 PSA_INIT( );
8732
8733 ASSERT_ALLOC( output_buffer,
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008734 PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg,
8735 PSA_PAKE_STEP_KEY_SHARE) );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008736
8737 if( pw_data->len > 0 )
8738 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008739 psa_set_key_usage_flags( &attributes, key_usage_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008740 psa_set_key_algorithm( &attributes, alg );
Neil Armstrong2a73f212022-09-06 11:34:54 +02008741 psa_set_key_type( &attributes, key_type_pw );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008742 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8743 &key ) );
8744 }
8745
8746 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8747 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8748 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8749
Neil Armstrong645cccd2022-06-08 17:36:23 +02008750 PSA_ASSERT( psa_pake_abort( &operation ) );
8751
8752 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8753 PSA_ERROR_BAD_STATE );
8754 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8755 PSA_ERROR_BAD_STATE );
8756 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8757 PSA_ERROR_BAD_STATE );
8758 TEST_EQUAL( psa_pake_set_role( &operation, role ),
8759 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008760 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8761 NULL, 0, NULL ),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008762 PSA_ERROR_BAD_STATE );
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008763 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0),
Neil Armstrong645cccd2022-06-08 17:36:23 +02008764 PSA_ERROR_BAD_STATE );
8765
8766 PSA_ASSERT( psa_pake_abort( &operation ) );
8767
Neil Armstrong2a73f212022-09-06 11:34:54 +02008768 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8769 expected_status_setup );
8770 if( expected_status_setup != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008771 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008772
Neil Armstrong50de0ae2022-06-08 17:46:24 +02008773 TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ),
8774 PSA_ERROR_BAD_STATE );
8775
Neil Armstrong2a73f212022-09-06 11:34:54 +02008776 TEST_EQUAL( psa_pake_set_role( &operation, role),
8777 expected_status_set_role );
8778 if( expected_status_set_role != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008779 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008780
8781 if( pw_data->len > 0 )
8782 {
Neil Armstrong2a73f212022-09-06 11:34:54 +02008783 TEST_EQUAL( psa_pake_set_password_key( &operation, key ),
8784 expected_status_set_password_key );
8785 if( expected_status_set_password_key != PSA_SUCCESS )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008786 goto exit;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008787 }
8788
Neil Armstrong707d9572022-06-08 17:31:49 +02008789 TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ),
8790 PSA_ERROR_INVALID_ARGUMENT );
8791 TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ),
8792 PSA_ERROR_INVALID_ARGUMENT );
8793
8794 const uint8_t unsupported_id[] = "abcd";
8795
8796 TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ),
8797 PSA_ERROR_NOT_SUPPORTED );
8798 TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ),
8799 PSA_ERROR_NOT_SUPPORTED );
8800
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008801 /* First round */
8802 if( input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008803 {
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008804 /* Invalid parameters */
8805 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8806 NULL, 0 ),
8807 PSA_ERROR_INVALID_ARGUMENT );
8808 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8809 output_buffer, 66 ),
8810 PSA_ERROR_INVALID_ARGUMENT );
8811 /* Invalid first step */
8812 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF,
8813 output_buffer, 66 ),
8814 PSA_ERROR_BAD_STATE );
8815
8816 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE,
8817 output_buffer, 66 ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008818 expected_status_input_output);
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008819
Neil Armstrong2a73f212022-09-06 11:34:54 +02008820 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008821 {
8822 /* Buffer too large */
8823 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8824 output_buffer, 512 ),
8825 PSA_ERROR_INSUFFICIENT_MEMORY );
8826
8827 /* The operation should be aborted at this point */
8828 TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8829 output_buffer, 66 ),
8830 PSA_ERROR_BAD_STATE );
8831 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008832 }
8833 else
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008834 {
8835 /* Invalid parameters */
8836 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8837 NULL, 0, NULL ),
8838 PSA_ERROR_INVALID_ARGUMENT );
8839 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10,
8840 output_buffer, 512, &output_len ),
8841 PSA_ERROR_INVALID_ARGUMENT );
8842 /* Invalid first step */
8843 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF,
8844 output_buffer, 512, &output_len ),
8845 PSA_ERROR_BAD_STATE );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008846
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008847 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE,
8848 output_buffer, 512, &output_len ),
Neil Armstrong2a73f212022-09-06 11:34:54 +02008849 expected_status_input_output );
Neil Armstrong98506ab2022-06-08 17:43:20 +02008850
Neil Armstrong2a73f212022-09-06 11:34:54 +02008851 if( expected_status_input_output == PSA_SUCCESS )
Neil Armstrong9c8b4922022-06-08 17:59:07 +02008852 {
8853 TEST_ASSERT( output_len > 0 );
8854
8855 /* Buffer too small */
8856 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8857 output_buffer, 5, &output_len ),
8858 PSA_ERROR_BUFFER_TOO_SMALL );
8859
8860 /* The operation should be aborted at this point */
8861 TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC,
8862 output_buffer, 512, &output_len ),
8863 PSA_ERROR_BAD_STATE );
8864 }
8865 }
Neil Armstrongd597bc72022-05-25 11:28:39 +02008866
8867exit:
8868 PSA_ASSERT( psa_destroy_key( key ) );
8869 PSA_ASSERT( psa_pake_abort( &operation ) );
8870 mbedtls_free( output_buffer );
8871 PSA_DONE( );
8872}
8873/* END_CASE */
8874
Neil Armstronga557cb82022-06-10 08:58:32 +02008875/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008876void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg,
8877 int client_input_first, int inject_error,
8878 data_t *pw_data )
8879{
8880 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8881 psa_pake_operation_t server = psa_pake_operation_init();
8882 psa_pake_operation_t client = psa_pake_operation_init();
8883 psa_algorithm_t alg = alg_arg;
8884 psa_algorithm_t hash_alg = hash_arg;
8885 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8887
8888 PSA_INIT( );
8889
8890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8891 psa_set_key_algorithm( &attributes, alg );
8892 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8893 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8894 &key ) );
8895
8896 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8897 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8898 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8899
8900
8901 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8902 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8903
8904 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8905 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8906
8907 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8908 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8909
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008910 ecjpake_do_round( alg, primitive_arg, &server, &client,
8911 client_input_first, 1, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008912
8913 if( inject_error == 1 || inject_error == 2 )
8914 goto exit;
8915
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008916 ecjpake_do_round( alg, primitive_arg, &server, &client,
8917 client_input_first, 2, inject_error );
Neil Armstrong8c2e8a62022-06-15 15:28:32 +02008918
8919exit:
8920 psa_destroy_key( key );
8921 psa_pake_abort( &server );
8922 psa_pake_abort( &client );
8923 PSA_DONE( );
8924}
8925/* END_CASE */
8926
8927/* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */
Neil Armstrongd597bc72022-05-25 11:28:39 +02008928void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg,
Neil Armstrongf983caf2022-06-15 15:27:48 +02008929 int derive_alg_arg, data_t *pw_data,
8930 int client_input_first )
Neil Armstrongd597bc72022-05-25 11:28:39 +02008931{
8932 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
8933 psa_pake_operation_t server = psa_pake_operation_init();
8934 psa_pake_operation_t client = psa_pake_operation_init();
8935 psa_algorithm_t alg = alg_arg;
8936 psa_algorithm_t hash_alg = hash_arg;
8937 psa_algorithm_t derive_alg = derive_alg_arg;
8938 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
8939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8940 psa_key_derivation_operation_t server_derive =
8941 PSA_KEY_DERIVATION_OPERATION_INIT;
8942 psa_key_derivation_operation_t client_derive =
8943 PSA_KEY_DERIVATION_OPERATION_INIT;
Neil Armstrongd597bc72022-05-25 11:28:39 +02008944
8945 PSA_INIT( );
8946
Neil Armstrongd597bc72022-05-25 11:28:39 +02008947 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
8948 psa_set_key_algorithm( &attributes, alg );
8949 psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD );
8950 PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len,
8951 &key ) );
8952
8953 psa_pake_cs_set_algorithm( &cipher_suite, alg );
8954 psa_pake_cs_set_primitive( &cipher_suite, primitive_arg );
8955 psa_pake_cs_set_hash( &cipher_suite, hash_alg );
8956
Neil Armstrong1e855602022-06-15 11:32:11 +02008957 /* Get shared key */
8958 PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) );
8959 PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) );
8960
8961 if( PSA_ALG_IS_TLS12_PRF( derive_alg ) ||
8962 PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) )
8963 {
8964 PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive,
8965 PSA_KEY_DERIVATION_INPUT_SEED,
8966 (const uint8_t*) "", 0) );
8967 PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive,
8968 PSA_KEY_DERIVATION_INPUT_SEED,
8969 (const uint8_t*) "", 0) );
8970 }
8971
Neil Armstrongd597bc72022-05-25 11:28:39 +02008972 PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) );
8973 PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) );
8974
8975 PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) );
8976 PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) );
8977
8978 PSA_ASSERT( psa_pake_set_password_key( &server, key ) );
8979 PSA_ASSERT( psa_pake_set_password_key( &client, key ) );
8980
Neil Armstrong1e855602022-06-15 11:32:11 +02008981 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8982 PSA_ERROR_BAD_STATE );
8983 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8984 PSA_ERROR_BAD_STATE );
8985
Neil Armstrongf983caf2022-06-15 15:27:48 +02008986 /* First round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008987 ecjpake_do_round( alg, primitive_arg, &server, &client,
8988 client_input_first, 1, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008989
Neil Armstrong1e855602022-06-15 11:32:11 +02008990 TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ),
8991 PSA_ERROR_BAD_STATE );
8992 TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ),
8993 PSA_ERROR_BAD_STATE );
8994
Neil Armstrongf983caf2022-06-15 15:27:48 +02008995 /* Second round */
Neil Armstrong78c4e8e2022-09-05 18:08:13 +02008996 ecjpake_do_round( alg, primitive_arg, &server, &client,
8997 client_input_first, 2, 0 );
Neil Armstrongd597bc72022-05-25 11:28:39 +02008998
Neil Armstrongd597bc72022-05-25 11:28:39 +02008999 PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) );
9000 PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) );
9001
9002exit:
9003 psa_key_derivation_abort( &server_derive );
9004 psa_key_derivation_abort( &client_derive );
9005 psa_destroy_key( key );
9006 psa_pake_abort( &server );
9007 psa_pake_abort( &client );
Neil Armstrongd597bc72022-05-25 11:28:39 +02009008 PSA_DONE( );
9009}
9010/* END_CASE */